index.cjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. // src/index.js
  20. var index_exports = {};
  21. __export(index_exports, {
  22. DataProjector: () => DataProjector,
  23. ProjectionSchemaRegistry: () => ProjectionSchemaRegistry,
  24. projectData: () => projectData,
  25. validateProjectionSchema: () => validateProjectionSchema,
  26. validateProjectionSchemaDefinition: () => validateProjectionSchemaDefinition
  27. });
  28. module.exports = __toCommonJS(index_exports);
  29. // src/project-data.js
  30. var import_js_format2 = require("@e22m4u/js-format");
  31. // src/validate-projection-schema.js
  32. var import_js_format = require("@e22m4u/js-format");
  33. function validateProjectionSchema(schema, shallowMode = false) {
  34. if (!schema || typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) {
  35. throw new import_js_format.InvalidArgumentError(
  36. "Projection schema must be an Object, a Function or a non-empty String, but %v was given.",
  37. schema
  38. );
  39. }
  40. if (typeof schema !== "object") {
  41. return;
  42. }
  43. Object.keys(schema).forEach((propName) => {
  44. const propOptions = schema[propName];
  45. if (propOptions === void 0) {
  46. return;
  47. }
  48. if (propOptions === null || typeof propOptions !== "object" && typeof propOptions !== "boolean" || Array.isArray(propOptions)) {
  49. throw new import_js_format.InvalidArgumentError(
  50. "Property options must be an Object or a Boolean, but %v was given.",
  51. propOptions
  52. );
  53. }
  54. if (typeof propOptions === "boolean") {
  55. return;
  56. }
  57. if (propOptions.select !== void 0 && typeof propOptions.select !== "boolean") {
  58. throw new import_js_format.InvalidArgumentError(
  59. 'Property option "select" must be a Boolean, but %v was given.',
  60. propOptions.select
  61. );
  62. }
  63. if (propOptions.scopes !== void 0) {
  64. if (!propOptions.scopes || typeof propOptions.scopes !== "object" || Array.isArray(propOptions.scopes)) {
  65. throw new import_js_format.InvalidArgumentError(
  66. 'Property option "scopes" must be an Object, but %v was given.',
  67. propOptions.scopes
  68. );
  69. }
  70. Object.values(propOptions.scopes).forEach((scopeOptions) => {
  71. if (scopeOptions === void 0) {
  72. return;
  73. }
  74. if (scopeOptions === null || typeof scopeOptions !== "object" && typeof scopeOptions !== "boolean" || Array.isArray(scopeOptions)) {
  75. throw new import_js_format.InvalidArgumentError(
  76. "Scope options must be an Object or a Boolean, but %v was given.",
  77. scopeOptions
  78. );
  79. }
  80. if (scopeOptions.select !== void 0 && typeof scopeOptions.select !== "boolean") {
  81. throw new import_js_format.InvalidArgumentError(
  82. 'Scope option "select" must be a Boolean, but %v was given.',
  83. scopeOptions.select
  84. );
  85. }
  86. });
  87. }
  88. if (propOptions.schema !== void 0) {
  89. if (!propOptions.schema || typeof propOptions.schema !== "object" && typeof propOptions.schema !== "function" && typeof propOptions.schema !== "string" || Array.isArray(propOptions.schema)) {
  90. throw new import_js_format.InvalidArgumentError(
  91. 'Property option "schema" must be an Object, a Function or a non-empty String, but %v was given.',
  92. propOptions.schema
  93. );
  94. }
  95. if (!shallowMode && typeof propOptions.schema === "object") {
  96. validateProjectionSchema(propOptions.schema, shallowMode);
  97. }
  98. }
  99. });
  100. }
  101. __name(validateProjectionSchema, "validateProjectionSchema");
  102. // src/project-data.js
  103. function projectData(schema, data, options) {
  104. if (options !== void 0) {
  105. if (!options || typeof options !== "object" || Array.isArray(options)) {
  106. throw new import_js_format2.InvalidArgumentError(
  107. "Projection options must be an Object, but %v was given.",
  108. options
  109. );
  110. }
  111. if (options.strict !== void 0 && typeof options.strict !== "boolean") {
  112. throw new import_js_format2.InvalidArgumentError(
  113. 'Projection option "strict" must be a Boolean, but %v was given.',
  114. options.strict
  115. );
  116. }
  117. if (options.scope !== void 0 && (options.scope === "" || typeof options.scope !== "string")) {
  118. throw new import_js_format2.InvalidArgumentError(
  119. 'Projection option "scope" must be a non-empty String, but %v was given.',
  120. options.scope
  121. );
  122. }
  123. if (options.resolver !== void 0 && typeof options.resolver !== "function") {
  124. throw new import_js_format2.InvalidArgumentError(
  125. 'Projection option "resolver" must be a Function, but %v was given.',
  126. options.resolver
  127. );
  128. }
  129. }
  130. if (typeof schema === "function") {
  131. schema = schema();
  132. if (!schema || typeof schema !== "object" && typeof schema !== "string" || Array.isArray(schema)) {
  133. throw new import_js_format2.InvalidArgumentError(
  134. "Schema factory must return an Object or a non-empty String, but %v was given.",
  135. schema
  136. );
  137. }
  138. }
  139. if (typeof schema === "string") {
  140. if (!options || !options.resolver) {
  141. throw new import_js_format2.InvalidArgumentError(
  142. 'Projection option "resolver" is required to resolve %v schema.',
  143. schema
  144. );
  145. }
  146. schema = options.resolver(schema);
  147. if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
  148. throw new import_js_format2.InvalidArgumentError(
  149. "Schema resolver must return an Object, but %v was given.",
  150. schema
  151. );
  152. }
  153. }
  154. validateProjectionSchema(schema, true);
  155. if (data == null || typeof data !== "object") {
  156. return data;
  157. }
  158. if (Array.isArray(data)) {
  159. return data.map((item) => projectData(schema, item, options));
  160. }
  161. const result = {};
  162. const strict = Boolean(options && options.strict);
  163. const scope = options && options.scope || void 0;
  164. const propNames = Object.keys(strict ? schema : data);
  165. propNames.forEach((propName) => {
  166. if (!(propName in data)) return;
  167. const propOptions = schema[propName];
  168. if (_shouldSelect(propOptions, strict, scope)) {
  169. const value = data[propName];
  170. if (propOptions && typeof propOptions === "object" && propOptions.schema) {
  171. result[propName] = projectData(propOptions.schema, value, options);
  172. } else {
  173. result[propName] = value;
  174. }
  175. }
  176. });
  177. return result;
  178. }
  179. __name(projectData, "projectData");
  180. function _shouldSelect(propOptions, strict, scope) {
  181. if (typeof propOptions === "boolean") {
  182. return propOptions;
  183. }
  184. if (propOptions && typeof propOptions === "object" && !Array.isArray(propOptions)) {
  185. if (scope && typeof scope === "string" && propOptions.scopes && typeof propOptions.scopes === "object" && propOptions.scopes[scope] !== void 0) {
  186. const scopeOptions = propOptions.scopes[scope];
  187. if (typeof scopeOptions === "boolean") {
  188. return scopeOptions;
  189. }
  190. if (scopeOptions && typeof scopeOptions === "object" && !Array.isArray(scopeOptions) && typeof scopeOptions.select === "boolean") {
  191. return scopeOptions.select;
  192. }
  193. }
  194. if (typeof propOptions.select === "boolean") {
  195. return propOptions.select;
  196. }
  197. }
  198. return !strict;
  199. }
  200. __name(_shouldSelect, "_shouldSelect");
  201. // src/data-projector.js
  202. var import_js_service2 = require("@e22m4u/js-service");
  203. // src/projection-schema-registry.js
  204. var import_js_service = require("@e22m4u/js-service");
  205. var import_js_format4 = require("@e22m4u/js-format");
  206. // src/validate-projection-schema-definition.js
  207. var import_js_format3 = require("@e22m4u/js-format");
  208. function validateProjectionSchemaDefinition(schemaDef) {
  209. if (!schemaDef || typeof schemaDef !== "object" || Array.isArray(schemaDef)) {
  210. throw new import_js_format3.InvalidArgumentError(
  211. "Schema definition must be an Object, but %v was given.",
  212. schemaDef
  213. );
  214. }
  215. if (!schemaDef.name || typeof schemaDef.name !== "string") {
  216. throw new import_js_format3.InvalidArgumentError(
  217. 'Definition option "name" must be a non-empty String, but %v was given.',
  218. schemaDef.name
  219. );
  220. }
  221. if (!schemaDef.schema || typeof schemaDef.schema !== "object" || Array.isArray(schemaDef.schema)) {
  222. throw new import_js_format3.InvalidArgumentError(
  223. 'Definition option "schema" must be an Object, but %v was given.',
  224. schemaDef.schema
  225. );
  226. }
  227. validateProjectionSchema(schemaDef.schema);
  228. }
  229. __name(validateProjectionSchemaDefinition, "validateProjectionSchemaDefinition");
  230. // src/projection-schema-registry.js
  231. var _ProjectionSchemaRegistry = class _ProjectionSchemaRegistry extends import_js_service.Service {
  232. /**
  233. * Definitions.
  234. */
  235. _definitions = /* @__PURE__ */ new Map();
  236. /**
  237. * Define schema.
  238. *
  239. * @param {object} schemaDef
  240. * @returns {this}
  241. */
  242. defineSchema(schemaDef) {
  243. validateProjectionSchemaDefinition(schemaDef);
  244. if (this._definitions.has(schemaDef.name)) {
  245. throw new import_js_format4.InvalidArgumentError(
  246. "Projection schema %v is already registered.",
  247. schemaDef.name
  248. );
  249. }
  250. this._definitions.set(schemaDef.name, schemaDef);
  251. return this;
  252. }
  253. /**
  254. * Has schema.
  255. *
  256. * @param {string} schemaName
  257. * @returns {boolean}
  258. */
  259. hasSchema(schemaName) {
  260. return this._definitions.has(schemaName);
  261. }
  262. /**
  263. * Get schema.
  264. *
  265. * @param {string} schemaName
  266. * @returns {object}
  267. */
  268. getSchema(schemaName) {
  269. const schemaDef = this._definitions.get(schemaName);
  270. if (!schemaDef) {
  271. throw new import_js_format4.InvalidArgumentError(
  272. "Projection schema %v is not found.",
  273. schemaName
  274. );
  275. }
  276. return schemaDef.schema;
  277. }
  278. /**
  279. * Get definition.
  280. *
  281. * @param {string} schemaName
  282. * @returns {object}
  283. */
  284. getDefinition(schemaName) {
  285. const schemaDef = this._definitions.get(schemaName);
  286. if (!schemaDef) {
  287. throw new import_js_format4.InvalidArgumentError(
  288. "Schema definition %v is not found.",
  289. schemaName
  290. );
  291. }
  292. return schemaDef;
  293. }
  294. };
  295. __name(_ProjectionSchemaRegistry, "ProjectionSchemaRegistry");
  296. var ProjectionSchemaRegistry = _ProjectionSchemaRegistry;
  297. // src/data-projector.js
  298. var _DataProjector = class _DataProjector extends import_js_service2.Service {
  299. /**
  300. * Define schema.
  301. *
  302. * @param {object} schemaDef
  303. * @returns {this}
  304. */
  305. defineSchema(schemaDef) {
  306. this.getService(ProjectionSchemaRegistry).defineSchema(schemaDef);
  307. return this;
  308. }
  309. /**
  310. * Project.
  311. *
  312. * @param {object|Function|string} schema
  313. * @param {object|object[]|*} data
  314. * @param {object} [options]
  315. * @returns {*}
  316. */
  317. project(schema, data, options) {
  318. const registry = this.getService(ProjectionSchemaRegistry);
  319. const resolver = /* @__PURE__ */ __name((name) => {
  320. return registry.getSchema(name);
  321. }, "resolver");
  322. return projectData(schema, data, { ...options, resolver });
  323. }
  324. };
  325. __name(_DataProjector, "DataProjector");
  326. var DataProjector = _DataProjector;
  327. // Annotate the CommonJS export names for ESM import in node:
  328. 0 && (module.exports = {
  329. DataProjector,
  330. ProjectionSchemaRegistry,
  331. projectData,
  332. validateProjectionSchema,
  333. validateProjectionSchemaDefinition
  334. });