index.cjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. ProjectionScope: () => ProjectionScope,
  26. projectData: () => projectData,
  27. validateProjectionSchema: () => validateProjectionSchema,
  28. validateProjectionSchemaDefinition: () => validateProjectionSchemaDefinition
  29. });
  30. module.exports = __toCommonJS(index_exports);
  31. // src/project-data.js
  32. var import_js_format2 = require("@e22m4u/js-format");
  33. // src/validate-projection-schema.js
  34. var import_js_format = require("@e22m4u/js-format");
  35. function validateProjectionSchema(schema, shallowMode = false) {
  36. if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
  37. throw new import_js_format.InvalidArgumentError(
  38. "Projection schema must be an Object, but %v was given.",
  39. schema
  40. );
  41. }
  42. if (typeof shallowMode !== "boolean") {
  43. throw new import_js_format.InvalidArgumentError(
  44. 'Parameter "shallowMode" must be a Boolean, but %v was given.',
  45. shallowMode
  46. );
  47. }
  48. Object.keys(schema).forEach((propName) => {
  49. const options = schema[propName];
  50. if (options === void 0) {
  51. return;
  52. }
  53. if (options === null || typeof options !== "boolean" && typeof options !== "object" || Array.isArray(options)) {
  54. throw new import_js_format.InvalidArgumentError(
  55. "Property options must be a Boolean or an Object, but %v was given.",
  56. options
  57. );
  58. }
  59. if (typeof options === "boolean") {
  60. return;
  61. }
  62. if (options.select !== void 0 && typeof options.select !== "boolean") {
  63. throw new import_js_format.InvalidArgumentError(
  64. 'Property option "select" must be a Boolean, but %v was given.',
  65. options.select
  66. );
  67. }
  68. if (options.schema !== void 0) {
  69. if (!options.schema || typeof options.schema !== "object" && typeof options.schema !== "function" && typeof options.schema !== "string" || Array.isArray(options.schema)) {
  70. throw new import_js_format.InvalidArgumentError(
  71. "Embedded schema must be an Object, a Function or a non-empty String, but %v was given.",
  72. options.schema
  73. );
  74. }
  75. if (!shallowMode && typeof options.schema === "object") {
  76. validateProjectionSchema(options.schema, shallowMode);
  77. }
  78. }
  79. if (options.scopes !== void 0) {
  80. if (!options.scopes || typeof options.scopes !== "object" || Array.isArray(options.scopes)) {
  81. throw new import_js_format.InvalidArgumentError(
  82. 'Property option "scopes" must be an Object, but %v was given.',
  83. options.scopes
  84. );
  85. }
  86. Object.keys(options.scopes).forEach((scopeName) => {
  87. const scopeOptions = options.scopes[scopeName];
  88. if (scopeOptions === void 0) {
  89. return;
  90. }
  91. if (scopeOptions === null || typeof scopeOptions !== "boolean" && typeof scopeOptions !== "object" || Array.isArray(scopeOptions)) {
  92. throw new import_js_format.InvalidArgumentError(
  93. "Scope options must be a Boolean or an Object, but %v was given.",
  94. scopeOptions
  95. );
  96. }
  97. if (typeof scopeOptions === "boolean") {
  98. return;
  99. }
  100. if (scopeOptions.select !== void 0) {
  101. if (typeof scopeOptions.select !== "boolean") {
  102. throw new import_js_format.InvalidArgumentError(
  103. 'Scope option "select" must be a Boolean, but %v was given.',
  104. scopeOptions.select
  105. );
  106. }
  107. }
  108. });
  109. }
  110. });
  111. }
  112. __name(validateProjectionSchema, "validateProjectionSchema");
  113. // src/project-data.js
  114. function projectData(schemaOrFactory, data, options) {
  115. if (!schemaOrFactory || typeof schemaOrFactory !== "object" && typeof schemaOrFactory !== "function" && typeof schemaOrFactory !== "string" || Array.isArray(schemaOrFactory)) {
  116. throw new import_js_format2.InvalidArgumentError(
  117. "Projection schema must be an Object, a Function or a non-empty String, but %v was given.",
  118. schemaOrFactory
  119. );
  120. }
  121. if (options !== void 0) {
  122. if (!options || typeof options !== "object" || Array.isArray(options)) {
  123. throw new import_js_format2.InvalidArgumentError(
  124. 'Parameter "options" must be an Object, but %v was given.',
  125. options
  126. );
  127. }
  128. if (options.strict !== void 0 && typeof options.strict !== "boolean") {
  129. throw new import_js_format2.InvalidArgumentError(
  130. 'Option "strict" must be a Boolean, but %v was given.',
  131. options.strict
  132. );
  133. }
  134. if (options.scope !== void 0 && (!options.scope || typeof options.scope !== "string")) {
  135. throw new import_js_format2.InvalidArgumentError(
  136. 'Option "scope" must be a non-empty String, but %v was given.',
  137. options.scope
  138. );
  139. }
  140. if (options.resolver !== void 0 && typeof options.resolver !== "function") {
  141. throw new import_js_format2.InvalidArgumentError(
  142. 'Option "resolver" must be a Function, but %v was given.',
  143. options.resolver
  144. );
  145. }
  146. }
  147. const strict = Boolean(options && options.strict);
  148. const scope = options && options.scope || void 0;
  149. let schemaOrName = schemaOrFactory;
  150. if (typeof schemaOrFactory === "function") {
  151. schemaOrName = schemaOrFactory();
  152. if (!schemaOrName || typeof schemaOrName !== "object" && typeof schemaOrName !== "string" || Array.isArray(schemaOrName)) {
  153. throw new import_js_format2.InvalidArgumentError(
  154. "Projection schema factory must return an Object or a non-empty String, but %v was given.",
  155. schemaOrName
  156. );
  157. }
  158. }
  159. let schema = schemaOrName;
  160. if (schemaOrName && typeof schemaOrName === "string") {
  161. if (!options || !options.resolver) {
  162. throw new import_js_format2.InvalidArgumentError(
  163. "Unable to resolve the projection schema %v without a provided resolver.",
  164. schemaOrName
  165. );
  166. }
  167. schema = options.resolver(schemaOrName);
  168. if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
  169. throw new import_js_format2.InvalidArgumentError(
  170. "Projection schema resolver must return an Object, but %v was given.",
  171. schema
  172. );
  173. }
  174. }
  175. validateProjectionSchema(schema, true);
  176. if (data === null || typeof data !== "object") {
  177. return data;
  178. }
  179. if (Array.isArray(data)) {
  180. return data.map((item) => projectData(schema, item, options));
  181. }
  182. const result = {};
  183. const fields = Object.keys(strict ? schema : data);
  184. for (const field of fields) {
  185. if (!(field in data)) {
  186. continue;
  187. }
  188. if (!Object.prototype.hasOwnProperty.call(data, field)) {
  189. continue;
  190. }
  191. const propOptionsOrBoolean = schema[field];
  192. if (_shouldSelect(propOptionsOrBoolean, strict, scope)) {
  193. const value = data[field];
  194. if (propOptionsOrBoolean && typeof propOptionsOrBoolean === "object" && propOptionsOrBoolean.schema) {
  195. result[field] = projectData(
  196. propOptionsOrBoolean.schema,
  197. value,
  198. options
  199. );
  200. } else {
  201. result[field] = value;
  202. }
  203. }
  204. }
  205. return result;
  206. }
  207. __name(projectData, "projectData");
  208. function _shouldSelect(propOptionsOrBoolean, strict, scope) {
  209. if (typeof propOptionsOrBoolean === "boolean") {
  210. return propOptionsOrBoolean;
  211. }
  212. if (typeof propOptionsOrBoolean === "object") {
  213. const propOptions = propOptionsOrBoolean;
  214. if (scope && propOptions.scopes && typeof propOptions.scopes === "object" && propOptions.scopes[scope] != null) {
  215. const scopeOptionsOrBoolean = propOptions.scopes[scope];
  216. if (typeof scopeOptionsOrBoolean === "boolean") {
  217. return scopeOptionsOrBoolean;
  218. }
  219. if (scopeOptionsOrBoolean && typeof scopeOptionsOrBoolean === "object" && typeof scopeOptionsOrBoolean.select === "boolean") {
  220. return scopeOptionsOrBoolean.select;
  221. }
  222. }
  223. if (typeof propOptionsOrBoolean.select === "boolean") {
  224. return propOptionsOrBoolean.select;
  225. }
  226. }
  227. return !strict;
  228. }
  229. __name(_shouldSelect, "_shouldSelect");
  230. // src/data-projector.js
  231. var import_js_service2 = require("@e22m4u/js-service");
  232. // src/definitions/projection-schema-registry.js
  233. var import_js_service = require("@e22m4u/js-service");
  234. var import_js_format4 = require("@e22m4u/js-format");
  235. // src/definitions/validate-projection-schema-definition.js
  236. var import_js_format3 = require("@e22m4u/js-format");
  237. function validateProjectionSchemaDefinition(schemaDef) {
  238. if (!schemaDef || typeof schemaDef !== "object" || Array.isArray(schemaDef)) {
  239. throw new import_js_format3.InvalidArgumentError(
  240. "Projection schema definition must be an Object, but %v was given.",
  241. schemaDef
  242. );
  243. }
  244. if (!schemaDef.name || typeof schemaDef.name !== "string") {
  245. throw new import_js_format3.InvalidArgumentError(
  246. "Projection schema name must be a non-empty String, but %v was given.",
  247. schemaDef.name
  248. );
  249. }
  250. if (!schemaDef.schema || typeof schemaDef.schema !== "object" || Array.isArray(schemaDef.schema)) {
  251. throw new import_js_format3.InvalidArgumentError(
  252. "Projection schema must be an Object, but %v was given.",
  253. schemaDef.schema
  254. );
  255. }
  256. validateProjectionSchema(schemaDef.schema);
  257. }
  258. __name(validateProjectionSchemaDefinition, "validateProjectionSchemaDefinition");
  259. // src/definitions/projection-schema-registry.js
  260. var _ProjectionSchemaRegistry = class _ProjectionSchemaRegistry extends import_js_service.Service {
  261. /**
  262. * Schema map.
  263. *
  264. * @type {Map<string, object>}
  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. "Projection 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. * Has schema.
  342. *
  343. * @param {string} schemaName
  344. * @returns {boolean}
  345. */
  346. hasSchema(schemaName) {
  347. return this.getService(ProjectionSchemaRegistry).hasSchema(schemaName);
  348. }
  349. /**
  350. * Project.
  351. *
  352. * @param {object|Function|string} schemaOrFactory
  353. * @param {*} data
  354. * @param {object} [options]
  355. * @returns {*}
  356. */
  357. project(schemaOrFactory, data, options) {
  358. const registry = this.getService(ProjectionSchemaRegistry);
  359. const resolver = /* @__PURE__ */ __name((schemaName) => {
  360. return registry.getSchema(schemaName);
  361. }, "resolver");
  362. return projectData(schemaOrFactory, data, { ...options, resolver });
  363. }
  364. };
  365. __name(_DataProjector, "DataProjector");
  366. var DataProjector = _DataProjector;
  367. // src/projection-scope.js
  368. var ProjectionScope = {
  369. INPUT: "input",
  370. OUTPUT: "output"
  371. };
  372. // Annotate the CommonJS export names for ESM import in node:
  373. 0 && (module.exports = {
  374. DataProjector,
  375. ProjectionSchemaRegistry,
  376. ProjectionScope,
  377. projectData,
  378. validateProjectionSchema,
  379. validateProjectionSchemaDefinition
  380. });