"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, { DataProjector: () => DataProjector, ProjectionSchemaRegistry: () => ProjectionSchemaRegistry, projectData: () => projectData, validateProjectionSchema: () => validateProjectionSchema, validateProjectionSchemaDefinition: () => validateProjectionSchemaDefinition }); 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, validatedSchemas = /* @__PURE__ */ new Set()) { if (!schema && schema !== false || typeof schema !== "boolean" && typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) { throw new import_js_format.InvalidArgumentError( "Projection schema must be a Boolean, an Object, a Function or a non-empty String, but %v was given.", schema ); } if (typeof shallowMode !== "boolean") { throw new import_js_format.InvalidArgumentError( 'Argument "shallowMode" must be a Boolean, but %v was given.', shallowMode ); } if (!(validatedSchemas instanceof Set)) { throw new import_js_format.InvalidArgumentError( 'Argument "validatedSchemas" must be an instance of Set, but %v was given.', validatedSchemas ); } if (validatedSchemas.has(schema)) { return; } if (typeof schema !== "object") { return; } validatedSchemas.add(schema); Object.keys(schema).forEach((propName) => { const propSchema = schema[propName]; if (propSchema === void 0) { return; } if (!propSchema && propSchema !== false || typeof propSchema !== "boolean" && typeof propSchema !== "object" && typeof propSchema !== "function" && typeof propSchema !== "string" || Array.isArray(propSchema)) { throw new import_js_format.InvalidArgumentError( "Projection schema of the property %v must be a Boolean, an Object, a Function or a non-empty String, but %v was given.", propName, propSchema ); } if (!shallowMode && typeof propSchema === "object") { validateProjectionSchema(propSchema, shallowMode, validatedSchemas); } }); } __name(validateProjectionSchema, "validateProjectionSchema"); // src/project-data.js var DATA_SCHEMA_INLINE_OPTIONS = ["$keepUnknown"]; function projectData(data, schema, options) { if (options !== void 0) { if (!options || typeof options !== "object" || Array.isArray(options)) { throw new import_js_format2.InvalidArgumentError( "Projection options must be an Object, but %v was given.", options ); } if (options.keepUnknown !== void 0 && typeof options.keepUnknown !== "boolean") { throw new import_js_format2.InvalidArgumentError( 'Projection option "keepUnknown" must be a Boolean, but %v was given.', options.keepUnknown ); } if (options.nameResolver !== void 0 && typeof options.nameResolver !== "function") { throw new import_js_format2.InvalidArgumentError( 'Projection option "nameResolver" must be a Function, but %v was given.', options.nameResolver ); } if (options.factoryArgs !== void 0 && !Array.isArray(options.factoryArgs)) { throw new import_js_format2.InvalidArgumentError( 'Projection option "factoryArgs" must be an Array, but %v was given.', options.factoryArgs ); } } if (typeof schema === "function") { const factoryArgs = options && options.factoryArgs || []; schema = schema(...factoryArgs); if (!schema && schema !== false || typeof schema !== "boolean" && typeof schema !== "object" && typeof schema !== "string" || Array.isArray(schema)) { throw new import_js_format2.InvalidArgumentError( "Schema factory must return a Boolean, an Object or a non-empty String, but %v was given.", schema ); } } if (typeof schema === "string") { if (!options || !options.nameResolver) { throw new import_js_format2.InvalidArgumentError( 'Failed to resolve the schema name %v because the option "nameResolver" is missing.', schema ); } schema = options.nameResolver(schema); if (!schema && schema !== false || typeof schema !== "boolean" && typeof schema !== "object" && typeof schema !== "function" && typeof schema !== "string" || Array.isArray(schema)) { throw new import_js_format2.InvalidArgumentError( "Name resolver must return a Boolean, an Object, a Function or a non-empty String, but %v was given.", schema ); } if (typeof schema === "function" || typeof schema === "string") { return projectData(data, schema, options); } } if (typeof schema === "boolean") { return schema ? data : void 0; } validateProjectionSchema(schema, true); if (data == null) { return data; } if (typeof data !== "object") { throw new import_js_format2.InvalidArgumentError( "Data source of the object projection must be an Object or an Array, but %v was given.", data ); } if (Array.isArray(data)) { return data.map((item) => projectData(item, schema, options)); } const result = {}; if (schema.$keepUnknown !== void 0 && typeof schema.$keepUnknown !== "boolean") { throw new import_js_format2.InvalidArgumentError( 'Schema property "$keepUnknown" must be a Boolean, but %v was given.', schema.$keepUnknown ); } const keepUnknown = Boolean( typeof schema.$keepUnknown === "boolean" ? schema.$keepUnknown : options && options.keepUnknown ); const propNames = Object.keys(keepUnknown ? data : schema); propNames.forEach((propName) => { if (!(propName in data) || DATA_SCHEMA_INLINE_OPTIONS.includes(propName)) { return; } const propSchema = schema[propName]; const propValue = data[propName]; if (typeof propSchema === "boolean") { if (propSchema) { result[propName] = propValue; } return; } if (propSchema != null) { result[propName] = projectData(propValue, propSchema, options); return; } if (keepUnknown) { result[propName] = propValue; } }); return result; } __name(projectData, "projectData"); // src/data-projector.js var import_js_service2 = require("@e22m4u/js-service"); // src/projection-schema-registry.js var import_js_service = require("@e22m4u/js-service"); var import_js_format4 = require("@e22m4u/js-format"); // src/validate-projection-schema-definition.js var import_js_format3 = require("@e22m4u/js-format"); function validateProjectionSchemaDefinition(schemaDef) { if (!schemaDef || typeof schemaDef !== "object" || Array.isArray(schemaDef)) { throw new import_js_format3.InvalidArgumentError( "Schema definition must be an Object, but %v was given.", schemaDef ); } if (!schemaDef.name || typeof schemaDef.name !== "string") { throw new import_js_format3.InvalidArgumentError( 'Definition option "name" must be a non-empty String, but %v was given.', schemaDef.name ); } if (!schemaDef.schema || typeof schemaDef.schema !== "object" && typeof schemaDef.schema !== "function" && typeof schemaDef.schema !== "string" || Array.isArray(schemaDef.schema)) { throw new import_js_format3.InvalidArgumentError( 'Definition option "schema" must be an Object, a Function or a non-empty String, but %v was given.', schemaDef.schema ); } validateProjectionSchema(schemaDef.schema); } __name(validateProjectionSchemaDefinition, "validateProjectionSchemaDefinition"); // src/projection-schema-registry.js var _ProjectionSchemaRegistry = class _ProjectionSchemaRegistry extends import_js_service.Service { /** * Definitions. */ _definitions = /* @__PURE__ */ new Map(); /** * Define schema. * * @param {object} schemaDef * @returns {this} */ defineSchema(schemaDef) { validateProjectionSchemaDefinition(schemaDef); this._definitions.set(schemaDef.name, schemaDef); return this; } /** * Has schema. * * @param {string} schemaName * @returns {boolean} */ hasSchema(schemaName) { return this._definitions.has(schemaName); } /** * Get schema. * * @param {string} schemaName * @returns {object} */ getSchema(schemaName) { const schemaDef = this._definitions.get(schemaName); if (!schemaDef) { throw new import_js_format4.InvalidArgumentError( "Projection schema %v is not found.", schemaName ); } return schemaDef.schema; } /** * Get definition. * * @param {string} schemaName * @returns {object} */ getDefinition(schemaName) { const schemaDef = this._definitions.get(schemaName); if (!schemaDef) { throw new import_js_format4.InvalidArgumentError( "Schema definition %v is not found.", schemaName ); } return schemaDef; } }; __name(_ProjectionSchemaRegistry, "ProjectionSchemaRegistry"); var ProjectionSchemaRegistry = _ProjectionSchemaRegistry; // src/data-projector.js var _DataProjector = class _DataProjector extends import_js_service2.Service { /** * Define schema. * * @param {object} schemaDef * @returns {this} */ defineSchema(schemaDef) { this.getService(ProjectionSchemaRegistry).defineSchema(schemaDef); return this; } /** * Has schema. * * @param {string} schemaName * @returns {boolean} */ hasSchema(schemaName) { return this.getService(ProjectionSchemaRegistry).hasSchema(schemaName); } /** * Get schema. * * @param {string} schemaName * @returns {object} */ getSchema(schemaName) { return this.getService(ProjectionSchemaRegistry).getSchema(schemaName); } /** * Project. * * @param {object|object[]|*} data * @param {object|Function|string} schema * @param {object} [options] * @returns {*} */ project(data, schema, options) { const registry = this.getService(ProjectionSchemaRegistry); const defaultNameResolver = /* @__PURE__ */ __name((name) => registry.getSchema(name), "defaultNameResolver"); const nameResolver = options && options.nameResolver || defaultNameResolver; const factoryArgs = options && options.factoryArgs || [this.container]; return projectData(data, schema, { ...options, nameResolver, factoryArgs }); } }; __name(_DataProjector, "DataProjector"); var DataProjector = _DataProjector; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DataProjector, ProjectionSchemaRegistry, projectData, validateProjectionSchema, validateProjectionSchemaDefinition });