index.cjs 12 KB

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