{"size": 1485, "ext": "js", "max_stars_repo_path": "src/types/object/index.js", "max_stars_repo_name": "leog/json-transformation-schema", "max_stars_repo_stars_event_min_datetime": "2020-09-23T22:48:49.000Z", "max_stars_repo_stars_event_max_datetime": "2020-09-23T22:48:49.000Z", "max_issues_repo_path": "src/types/object/index.js", "max_issues_repo_name": "leog/json-transformation-schema", "max_issues_repo_issues_event_min_datetime": "2020-09-04T21:24:54.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T19:34:14.000Z", "max_forks_repo_path": "src/types/object/index.js", "max_forks_repo_name": "leog/json-transformation-schema", "max_forks_repo_forks_event_min_datetime": "2020-10-23T22:08:47.000Z", "max_forks_repo_forks_event_max_datetime": "2020-10-23T22:08:47.000Z"}
{"max_stars_count": 1, "max_issues_count": 4, "max_forks_count": 1, "avg_line_length": 26.0526315789, "max_line_length": 111, "alphanum_fraction": 0.6518518519}
/**
* Type - object
*
*/

/* Helpers */
const { assign, collect, keys, merge, select } = require('@laufire/utils').collection;
const { standardizeSchema, transform } = require('../../core');
const { inferType } = require('@laufire/utils').reflection;

/* Config */
const schemaDefaults = {
	preserve: false,
}

/* Exports */
module.exports = {
	standardizeSchema: (schema, options) => {
		const itemsConfig = schema.items;
		const properties = collect(schema.properties || {}, (propSchema, prop) =>
			merge({ prop }, itemsConfig, standardizeSchema(propSchema, options))
		);

		return merge({}, schemaDefaults, schema, { properties });
	},
	transform: (data, schema, options) => {
		const ret = {};
		const { properties = {} } = schema;
		const propList = keys(properties);

		if(schema.preserve === true && data) {
			const allProps = keys(data);
			const untouchedProps = allProps.filter((prop) => !propList.includes(prop));
			assign(ret, select(data, untouchedProps));
		}

		const propCount = propList.length;
		let propIndex = 0;

		while(propIndex < propCount) {
			const prop = propList[propIndex++];
			const propSchema = properties[prop] || {};
			const value = transform(data, propSchema, options);

			if(value !== undefined) //NOTE: To have undefined keys in the result, set the config - default to undefined.
				ret[prop] = value;
		}

		return ret;
	},
	parsers: {
		string: (value) =>
			JSON.parse(value),
	},
	validate: (value) => inferType(value) === 'object',
};
