index.cjs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. }
  229. return !strict;
  230. }
  231. __name(_shouldSelect, "_shouldSelect");
  232. // src/data-projector.js
  233. var import_js_service2 = require("@e22m4u/js-service");
  234. // src/projection-schema-registry.js
  235. var import_js_service = require("@e22m4u/js-service");
  236. var import_js_format4 = require("@e22m4u/js-format");
  237. // src/validate-projection-schema-definition.js
  238. var import_js_format3 = require("@e22m4u/js-format");
  239. function validateProjectionSchemaDefinition(schemaDef) {
  240. if (!schemaDef || typeof schemaDef !== "object" || Array.isArray(schemaDef)) {
  241. throw new import_js_format3.InvalidArgumentError(
  242. "Schema definition must be an Object, but %v was given.",
  243. schemaDef
  244. );
  245. }
  246. if (!schemaDef.name || typeof schemaDef.name !== "string") {
  247. throw new import_js_format3.InvalidArgumentError(
  248. 'Definition option "name" must be a non-empty String, but %v was given.',
  249. schemaDef.name
  250. );
  251. }
  252. if (!schemaDef.schema || typeof schemaDef.schema !== "object" && typeof schemaDef.schema !== "function" && typeof schemaDef.schema !== "string" || Array.isArray(schemaDef.schema)) {
  253. throw new import_js_format3.InvalidArgumentError(
  254. 'Definition option "schema" must be an Object, a Function or a non-empty String, but %v was given.',
  255. schemaDef.schema
  256. );
  257. }
  258. validateProjectionSchema(schemaDef.schema);
  259. }
  260. __name(validateProjectionSchemaDefinition, "validateProjectionSchemaDefinition");
  261. // src/projection-schema-registry.js
  262. var _ProjectionSchemaRegistry = class _ProjectionSchemaRegistry extends import_js_service.Service {
  263. /**
  264. * Definitions.
  265. */
  266. _definitions = /* @__PURE__ */ new Map();
  267. /**
  268. * Define schema.
  269. *
  270. * @param {object} schemaDef
  271. * @returns {this}
  272. */
  273. defineSchema(schemaDef) {
  274. validateProjectionSchemaDefinition(schemaDef);
  275. if (this._definitions.has(schemaDef.name)) {
  276. throw new import_js_format4.InvalidArgumentError(
  277. "Projection schema %v is already registered.",
  278. schemaDef.name
  279. );
  280. }
  281. this._definitions.set(schemaDef.name, schemaDef);
  282. return this;
  283. }
  284. /**
  285. * Has schema.
  286. *
  287. * @param {string} schemaName
  288. * @returns {boolean}
  289. */
  290. hasSchema(schemaName) {
  291. return this._definitions.has(schemaName);
  292. }
  293. /**
  294. * Get schema.
  295. *
  296. * @param {string} schemaName
  297. * @returns {object}
  298. */
  299. getSchema(schemaName) {
  300. const schemaDef = this._definitions.get(schemaName);
  301. if (!schemaDef) {
  302. throw new import_js_format4.InvalidArgumentError(
  303. "Projection schema %v is not found.",
  304. schemaName
  305. );
  306. }
  307. return schemaDef.schema;
  308. }
  309. /**
  310. * Get definition.
  311. *
  312. * @param {string} schemaName
  313. * @returns {object}
  314. */
  315. getDefinition(schemaName) {
  316. const schemaDef = this._definitions.get(schemaName);
  317. if (!schemaDef) {
  318. throw new import_js_format4.InvalidArgumentError(
  319. "Schema definition %v is not found.",
  320. schemaName
  321. );
  322. }
  323. return schemaDef;
  324. }
  325. };
  326. __name(_ProjectionSchemaRegistry, "ProjectionSchemaRegistry");
  327. var ProjectionSchemaRegistry = _ProjectionSchemaRegistry;
  328. // src/data-projector.js
  329. var _DataProjector = class _DataProjector extends import_js_service2.Service {
  330. /**
  331. * Define schema.
  332. *
  333. * @param {object} schemaDef
  334. * @returns {this}
  335. */
  336. defineSchema(schemaDef) {
  337. this.getService(ProjectionSchemaRegistry).defineSchema(schemaDef);
  338. return this;
  339. }
  340. /**
  341. * Project.
  342. *
  343. * @param {object|object[]|*} data
  344. * @param {object|Function|string} schema
  345. * @param {object} [options]
  346. * @returns {*}
  347. */
  348. project(data, schema, options) {
  349. const registry = this.getService(ProjectionSchemaRegistry);
  350. const defaultNameResolver = /* @__PURE__ */ __name((name) => registry.getSchema(name), "defaultNameResolver");
  351. const nameResolver = options && options.nameResolver || defaultNameResolver;
  352. const factoryArgs = options && options.factoryArgs || [this.container];
  353. return projectData(data, schema, { ...options, nameResolver, factoryArgs });
  354. }
  355. /**
  356. * Project input.
  357. *
  358. * @param {object|object[]|*} data
  359. * @param {object|Function|string} schema
  360. * @param {object} [options]
  361. * @returns {*}
  362. */
  363. projectInput(data, schema, options) {
  364. return this.project(data, schema, { ...options, scope: "input" });
  365. }
  366. /**
  367. * Project output.
  368. *
  369. * @param {object|object[]|*} data
  370. * @param {object|Function|string} schema
  371. * @param {object} [options]
  372. * @returns {*}
  373. */
  374. projectOutput(data, schema, options) {
  375. return this.project(data, schema, { ...options, scope: "output" });
  376. }
  377. };
  378. __name(_DataProjector, "DataProjector");
  379. var DataProjector = _DataProjector;
  380. // Annotate the CommonJS export names for ESM import in node:
  381. 0 && (module.exports = {
  382. DataProjector,
  383. ProjectionSchemaRegistry,
  384. projectData,
  385. validateProjectionSchema,
  386. validateProjectionSchemaDefinition
  387. });