index.cjs 14 KB

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