index.cjs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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" && typeof options.schema !== "string" || Array.isArray(options.schema)) {
  67. throw new import_js_format.InvalidArgumentError(
  68. "Embedded schema must be an Object, a Function or a non-empty String, 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) {
  112. if (!schemaOrFactory || typeof schemaOrFactory !== "object" && typeof schemaOrFactory !== "function" && typeof schemaOrFactory !== "string" || Array.isArray(schemaOrFactory)) {
  113. throw new import_js_format2.InvalidArgumentError(
  114. "Projection schema must be an Object, a Function or a non-empty String, 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. if (options.resolver !== void 0 && typeof options.resolver !== "function") {
  138. throw new import_js_format2.InvalidArgumentError(
  139. 'Option "resolver" must be a Function, but %v was given.',
  140. options.resolver
  141. );
  142. }
  143. }
  144. const strict = Boolean(options && options.strict);
  145. const scope = options && options.scope || void 0;
  146. let schemaOrName = schemaOrFactory;
  147. if (typeof schemaOrFactory === "function") {
  148. schemaOrName = schemaOrFactory();
  149. if (!schemaOrName || typeof schemaOrName !== "object" && typeof schemaOrName !== "string" || Array.isArray(schemaOrName)) {
  150. throw new import_js_format2.InvalidArgumentError(
  151. "Projection schema factory must return an Object or a non-empty String, but %v was given.",
  152. schemaOrName
  153. );
  154. }
  155. }
  156. let schema = schemaOrName;
  157. if (schemaOrName && typeof schemaOrName === "string") {
  158. if (!options || !options.resolver) {
  159. throw new import_js_format2.InvalidArgumentError(
  160. "Unable to resolve the projection schema %v without a provided resolver.",
  161. schemaOrName
  162. );
  163. }
  164. schema = options.resolver(schemaOrName);
  165. if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
  166. throw new import_js_format2.InvalidArgumentError(
  167. "Projection schema resolver must return an Object, but %v was given.",
  168. schema
  169. );
  170. }
  171. }
  172. validateProjectionSchema(schema, true);
  173. if (data === null || typeof data !== "object") {
  174. return data;
  175. }
  176. if (Array.isArray(data)) {
  177. return data.map((item) => projectData(schema, item, options));
  178. }
  179. const result = {};
  180. const fields = Object.keys(strict ? schema : data);
  181. for (const field of fields) {
  182. if (!(field in data)) {
  183. continue;
  184. }
  185. if (!Object.prototype.hasOwnProperty.call(data, field)) {
  186. continue;
  187. }
  188. const propOptionsOrBoolean = schema[field];
  189. if (_shouldSelect(propOptionsOrBoolean, strict, scope)) {
  190. const value = data[field];
  191. if (propOptionsOrBoolean && typeof propOptionsOrBoolean === "object" && propOptionsOrBoolean.schema) {
  192. result[field] = projectData(
  193. propOptionsOrBoolean.schema,
  194. value,
  195. options
  196. );
  197. } else {
  198. result[field] = value;
  199. }
  200. }
  201. }
  202. return result;
  203. }
  204. __name(projectData, "projectData");
  205. function _shouldSelect(propOptionsOrBoolean, strict, scope) {
  206. if (typeof propOptionsOrBoolean === "boolean") {
  207. return propOptionsOrBoolean;
  208. }
  209. if (typeof propOptionsOrBoolean === "object") {
  210. const propOptions = propOptionsOrBoolean;
  211. if (scope && propOptions.scopes && typeof propOptions.scopes === "object" && propOptions.scopes[scope] != null) {
  212. const scopeOptionsOrBoolean = propOptions.scopes[scope];
  213. if (typeof scopeOptionsOrBoolean === "boolean") {
  214. return scopeOptionsOrBoolean;
  215. }
  216. if (scopeOptionsOrBoolean && typeof scopeOptionsOrBoolean === "object" && typeof scopeOptionsOrBoolean.select === "boolean") {
  217. return scopeOptionsOrBoolean.select;
  218. }
  219. }
  220. if (typeof propOptionsOrBoolean.select === "boolean") {
  221. return propOptionsOrBoolean.select;
  222. }
  223. }
  224. return !strict;
  225. }
  226. __name(_shouldSelect, "_shouldSelect");
  227. // src/projection-scope.js
  228. var ProjectionScope = {
  229. INPUT: "input",
  230. OUTPUT: "output"
  231. };
  232. // Annotate the CommonJS export names for ESM import in node:
  233. 0 && (module.exports = {
  234. ProjectionScope,
  235. projectData,
  236. validateProjectionSchema
  237. });