| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- "use strict";
- var __defProp = Object.defineProperty;
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
- var __getOwnPropNames = Object.getOwnPropertyNames;
- var __hasOwnProp = Object.prototype.hasOwnProperty;
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
- var __export = (target, all) => {
- for (var name in all)
- __defProp(target, name, { get: all[name], enumerable: true });
- };
- var __copyProps = (to, from, except, desc) => {
- if (from && typeof from === "object" || typeof from === "function") {
- for (let key of __getOwnPropNames(from))
- if (!__hasOwnProp.call(to, key) && key !== except)
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
- }
- return to;
- };
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
- // src/index.js
- var index_exports = {};
- __export(index_exports, {
- ProjectionScope: () => ProjectionScope,
- projectData: () => projectData,
- validateProjectionSchema: () => validateProjectionSchema
- });
- module.exports = __toCommonJS(index_exports);
- // src/project-data.js
- var import_js_format2 = require("@e22m4u/js-format");
- // src/validate-projection-schema.js
- var import_js_format = require("@e22m4u/js-format");
- function validateProjectionSchema(schema, shallowMode = false) {
- if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
- throw new import_js_format.InvalidArgumentError(
- "Projection schema must be an Object, but %v was given.",
- schema
- );
- }
- if (typeof shallowMode !== "boolean") {
- throw new import_js_format.InvalidArgumentError(
- 'Parameter "shallowMode" must be a Boolean, but %v was given.',
- shallowMode
- );
- }
- Object.keys(schema).forEach((propName) => {
- const options = schema[propName];
- if (options === void 0) {
- return;
- }
- if (options === null || typeof options !== "boolean" && typeof options !== "object" || Array.isArray(options)) {
- throw new import_js_format.InvalidArgumentError(
- "Property options must be a Boolean or an Object, but %v was given.",
- options
- );
- }
- if (typeof options === "boolean") {
- return;
- }
- if (options.select !== void 0 && typeof options.select !== "boolean") {
- throw new import_js_format.InvalidArgumentError(
- 'Property option "select" must be a Boolean, but %v was given.',
- options.select
- );
- }
- if (options.schema !== void 0) {
- if (!options.schema || typeof options.schema !== "object" && typeof options.schema !== "function" || Array.isArray(options.schema)) {
- throw new import_js_format.InvalidArgumentError(
- "Embedded schema must be an Object or a Function that returns a schema, but %v was given.",
- options.schema
- );
- }
- if (!shallowMode && typeof options.schema === "object") {
- validateProjectionSchema(options.schema, shallowMode);
- }
- }
- if (options.scopes !== void 0) {
- if (!options.scopes || typeof options.scopes !== "object" || Array.isArray(options.scopes)) {
- throw new import_js_format.InvalidArgumentError(
- 'Property option "scopes" must be an Object, but %v was given.',
- options.scopes
- );
- }
- Object.keys(options.scopes).forEach((scopeName) => {
- const scopeOptions = options.scopes[scopeName];
- if (scopeOptions === void 0) {
- return;
- }
- if (scopeOptions === null || typeof scopeOptions !== "boolean" && typeof scopeOptions !== "object" || Array.isArray(scopeOptions)) {
- throw new import_js_format.InvalidArgumentError(
- "Scope options must be a Boolean or an Object, but %v was given.",
- scopeOptions
- );
- }
- if (typeof scopeOptions === "boolean") {
- return;
- }
- if (scopeOptions.select !== void 0) {
- if (typeof scopeOptions.select !== "boolean") {
- throw new import_js_format.InvalidArgumentError(
- 'Scope option "select" must be a Boolean, but %v was given.',
- scopeOptions.select
- );
- }
- }
- });
- }
- });
- }
- __name(validateProjectionSchema, "validateProjectionSchema");
- // src/project-data.js
- function projectData(schemaOrFactory, data, options = void 0) {
- if (!schemaOrFactory || typeof schemaOrFactory !== "object" && typeof schemaOrFactory !== "function" || Array.isArray(schemaOrFactory)) {
- throw new import_js_format2.InvalidArgumentError(
- "Projection schema must be an Object or a Function that returns a schema object, but %v was given.",
- schemaOrFactory
- );
- }
- if (options !== void 0) {
- if (!options || typeof options !== "object" || Array.isArray(options)) {
- throw new import_js_format2.InvalidArgumentError(
- 'Parameter "options" must be an Object, but %v was given.',
- options
- );
- }
- if (options.strict !== void 0 && typeof options.strict !== "boolean") {
- throw new import_js_format2.InvalidArgumentError(
- 'Option "strict" must be a Boolean, but %v was given.',
- options.strict
- );
- }
- if (options.scope !== void 0 && (!options.scope || typeof options.scope !== "string")) {
- throw new import_js_format2.InvalidArgumentError(
- 'Option "scope" must be a non-empty String, but %v was given.',
- options.scope
- );
- }
- }
- const strict = Boolean(options && options.strict);
- const scope = options && options.scope || void 0;
- let schema = schemaOrFactory;
- if (typeof schemaOrFactory === "function") {
- schema = schemaOrFactory();
- if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
- throw new import_js_format2.InvalidArgumentError(
- "Projection schema factory must return an Object, but %v was given.",
- schema
- );
- }
- }
- validateProjectionSchema(schema, true);
- if (data === null || typeof data !== "object") {
- return data;
- }
- if (Array.isArray(data)) {
- return data.map((item) => projectData(schema, item, options));
- }
- const result = {};
- const keys = Object.keys(strict ? schema : data);
- for (const key of keys) {
- if (!(key in data)) continue;
- const propOptionsOrBoolean = schema[key];
- if (_shouldSelect(propOptionsOrBoolean, strict, scope)) {
- const value = data[key];
- if (propOptionsOrBoolean && typeof propOptionsOrBoolean === "object" && propOptionsOrBoolean.schema) {
- result[key] = projectData(propOptionsOrBoolean.schema, value, options);
- } else {
- result[key] = value;
- }
- }
- }
- return result;
- }
- __name(projectData, "projectData");
- function _shouldSelect(propOptionsOrBoolean, strict, scope) {
- if (typeof propOptionsOrBoolean === "boolean") {
- return propOptionsOrBoolean;
- }
- if (typeof propOptionsOrBoolean === "object") {
- const propOptions = propOptionsOrBoolean;
- if (scope && propOptions.scopes && typeof propOptions.scopes === "object" && propOptions.scopes[scope] != null) {
- const scopeOptionsOrBoolean = propOptions.scopes[scope];
- if (typeof scopeOptionsOrBoolean === "boolean") {
- return scopeOptionsOrBoolean;
- }
- if (scopeOptionsOrBoolean && typeof scopeOptionsOrBoolean === "object" && typeof scopeOptionsOrBoolean.select === "boolean") {
- return scopeOptionsOrBoolean.select;
- }
- }
- if (typeof propOptionsOrBoolean.select === "boolean") {
- return propOptionsOrBoolean.select;
- }
- }
- return !strict;
- }
- __name(_shouldSelect, "_shouldSelect");
- // src/projection-scope.js
- var ProjectionScope = {
- INPUT: "input",
- OUTPUT: "output"
- };
- // Annotate the CommonJS export names for ESM import in node:
- 0 && (module.exports = {
- ProjectionScope,
- projectData,
- validateProjectionSchema
- });
|