index.cjs 14 KB

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