index.cjs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. ProjectionScope: () => ProjectionScope,
  24. projectData: () => projectData,
  25. validateProjectionSchema: () => validateProjectionSchema
  26. });
  27. module.exports = __toCommonJS(index_exports);
  28. // src/project-data.js
  29. var import_js_format2 = require("@e22m4u/js-format");
  30. // src/validate-projection-schema.js
  31. var import_js_format = require("@e22m4u/js-format");
  32. function validateProjectionSchema(schema, shallowMode = false) {
  33. if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
  34. throw new import_js_format.InvalidArgumentError(
  35. "Projection schema must be an Object, but %v was given.",
  36. schema
  37. );
  38. }
  39. if (typeof shallowMode !== "boolean") {
  40. throw new import_js_format.InvalidArgumentError(
  41. 'Parameter "shallowMode" must be a Boolean, but %v was given.',
  42. shallowMode
  43. );
  44. }
  45. Object.keys(schema).forEach((propName) => {
  46. const options = schema[propName];
  47. if (options === void 0) {
  48. return;
  49. }
  50. if (options === null || typeof options !== "boolean" && typeof options !== "object" || Array.isArray(options)) {
  51. throw new import_js_format.InvalidArgumentError(
  52. "Property options must be a Boolean or an Object, but %v was given.",
  53. options
  54. );
  55. }
  56. if (typeof options === "boolean") {
  57. return;
  58. }
  59. if (options.select !== void 0 && typeof options.select !== "boolean") {
  60. throw new import_js_format.InvalidArgumentError(
  61. 'Property option "select" must be a Boolean, but %v was given.',
  62. options.select
  63. );
  64. }
  65. if (options.schema !== void 0) {
  66. if (!options.schema || typeof options.schema !== "object" && typeof options.schema !== "function" || Array.isArray(options.schema)) {
  67. throw new import_js_format.InvalidArgumentError(
  68. "Embedded schema must be an Object or a Function that returns a schema, but %v was given.",
  69. options.schema
  70. );
  71. }
  72. if (!shallowMode && typeof options.schema === "object") {
  73. validateProjectionSchema(options.schema, shallowMode);
  74. }
  75. }
  76. if (options.scopes !== void 0) {
  77. if (!options.scopes || typeof options.scopes !== "object" || Array.isArray(options.scopes)) {
  78. throw new import_js_format.InvalidArgumentError(
  79. 'Property option "scopes" must be an Object, but %v was given.',
  80. options.scopes
  81. );
  82. }
  83. Object.keys(options.scopes).forEach((scopeName) => {
  84. const scopeOptions = options.scopes[scopeName];
  85. if (scopeOptions === void 0) {
  86. return;
  87. }
  88. if (scopeOptions === null || typeof scopeOptions !== "boolean" && typeof scopeOptions !== "object" || Array.isArray(scopeOptions)) {
  89. throw new import_js_format.InvalidArgumentError(
  90. "Scope options must be a Boolean or an Object, but %v was given.",
  91. scopeOptions
  92. );
  93. }
  94. if (typeof scopeOptions === "boolean") {
  95. return;
  96. }
  97. if (scopeOptions.select !== void 0) {
  98. if (typeof scopeOptions.select !== "boolean") {
  99. throw new import_js_format.InvalidArgumentError(
  100. 'Scope option "select" must be a Boolean, but %v was given.',
  101. scopeOptions.select
  102. );
  103. }
  104. }
  105. });
  106. }
  107. });
  108. }
  109. __name(validateProjectionSchema, "validateProjectionSchema");
  110. // src/project-data.js
  111. function projectData(schemaOrFactory, data, options = void 0) {
  112. if (!schemaOrFactory || typeof schemaOrFactory !== "object" && typeof schemaOrFactory !== "function" || Array.isArray(schemaOrFactory)) {
  113. throw new import_js_format2.InvalidArgumentError(
  114. "Projection schema must be an Object or a Function that returns a schema object, but %v was given.",
  115. schemaOrFactory
  116. );
  117. }
  118. if (options !== void 0) {
  119. if (!options || typeof options !== "object" || Array.isArray(options)) {
  120. throw new import_js_format2.InvalidArgumentError(
  121. 'Parameter "options" must be an Object, but %v was given.',
  122. options
  123. );
  124. }
  125. if (options.strict !== void 0 && typeof options.strict !== "boolean") {
  126. throw new import_js_format2.InvalidArgumentError(
  127. 'Option "strict" must be a Boolean, but %v was given.',
  128. options.strict
  129. );
  130. }
  131. if (options.scope !== void 0 && (!options.scope || typeof options.scope !== "string")) {
  132. throw new import_js_format2.InvalidArgumentError(
  133. 'Option "scope" must be a non-empty String, but %v was given.',
  134. options.scope
  135. );
  136. }
  137. }
  138. const strict = Boolean(options && options.strict);
  139. const scope = options && options.scope || void 0;
  140. let schema = schemaOrFactory;
  141. if (typeof schemaOrFactory === "function") {
  142. schema = schemaOrFactory();
  143. if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
  144. throw new import_js_format2.InvalidArgumentError(
  145. "Projection schema factory must return an Object, but %v was given.",
  146. schema
  147. );
  148. }
  149. }
  150. validateProjectionSchema(schema, true);
  151. if (data === null || typeof data !== "object") {
  152. return data;
  153. }
  154. if (Array.isArray(data)) {
  155. return data.map((item) => projectData(schema, item, options));
  156. }
  157. const result = {};
  158. const keys = Object.keys(strict ? schema : data);
  159. for (const key of keys) {
  160. if (!(key in data)) continue;
  161. const propOptionsOrBoolean = schema[key];
  162. if (_shouldSelect(propOptionsOrBoolean, strict, scope)) {
  163. const value = data[key];
  164. if (propOptionsOrBoolean && typeof propOptionsOrBoolean === "object" && propOptionsOrBoolean.schema) {
  165. result[key] = projectData(propOptionsOrBoolean.schema, value, options);
  166. } else {
  167. result[key] = value;
  168. }
  169. }
  170. }
  171. return result;
  172. }
  173. __name(projectData, "projectData");
  174. function _shouldSelect(propOptionsOrBoolean, strict, scope) {
  175. if (typeof propOptionsOrBoolean === "boolean") {
  176. return propOptionsOrBoolean;
  177. }
  178. if (typeof propOptionsOrBoolean === "object") {
  179. const propOptions = propOptionsOrBoolean;
  180. if (scope && propOptions.scopes && typeof propOptions.scopes === "object" && propOptions.scopes[scope] != null) {
  181. const scopeOptionsOrBoolean = propOptions.scopes[scope];
  182. if (typeof scopeOptionsOrBoolean === "boolean") {
  183. return scopeOptionsOrBoolean;
  184. }
  185. if (scopeOptionsOrBoolean && typeof scopeOptionsOrBoolean === "object" && typeof scopeOptionsOrBoolean.select === "boolean") {
  186. return scopeOptionsOrBoolean.select;
  187. }
  188. }
  189. if (typeof propOptionsOrBoolean.select === "boolean") {
  190. return propOptionsOrBoolean.select;
  191. }
  192. }
  193. return !strict;
  194. }
  195. __name(_shouldSelect, "_shouldSelect");
  196. // src/projection-scope.js
  197. var ProjectionScope = {
  198. INPUT: "input",
  199. OUTPUT: "output"
  200. };
  201. // Annotate the CommonJS export names for ESM import in node:
  202. 0 && (module.exports = {
  203. ProjectionScope,
  204. projectData,
  205. validateProjectionSchema
  206. });