UNPKG

@hestia-earth/json-schema

Version:
104 lines (103 loc) 4.94 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.schemaFromKey = exports.isCSVIncluded = exports.isDefaultCSVSelected = exports.keyType = void 0; var schema_1 = require("@hestia-earth/schema"); var propertyRef = function (schema, key) { var property = schema && schema.properties ? schema.properties[key] : {}; return property ? ('items' in property ? property.items.$ref : property.$ref) : null; }; var keyType = function (schema, key) { var ref = propertyRef(schema, key); return ref ? (0, schema_1.refToSchemaType)(ref) : null; }; exports.keyType = keyType; var isNumber = function (key) { return key.match(/^\d+$/g) !== null; }; var recursiveKeyCheck = function (schemas, schema, key, checkFunction) { var _a = __read(key.split('.')), firstKey = _a[0], keys = _a.slice(1); return isNumber(firstKey) ? // for arrays, we can continue the recursion without checking if it is selected recursiveKeyCheck(schemas, schema, keys.join('.'), checkFunction) : // each key needs to be set as selected to continue the recursion schema && checkFunction(schema, firstKey) && // either exit if end of recursion or keep going (keys.length < 1 || recursiveKeyCheck(schemas, schemas[(0, exports.keyType)(schema, firstKey)], keys.join('.'), checkFunction)); }; var isPropertySelected = function (_a, key) { var properties = _a.properties; return (properties[key] || {}).exportDefaultCSV; }; /** * Check whether the field should be selected by default or not while exporting as CSV. * * @param schemas {definitions} List of definitions loaded with `loadSchemas`. * @param key {string} The full key of the field, such as `cycle.inputs.0.value`. * @returns true if it should be selected by default, false otherwise. */ var isDefaultCSVSelected = function (schemas) { return function (key) { var _a = __read(key.split('.')), firstKey = _a[0], keys = _a.slice(1); var type = (0, schema_1.typeToSchemaType)(firstKey); var schema = (0, schema_1.isTypeValid)({ type: type }) ? schemas[type] : null; return (schema ? recursiveKeyCheck(schemas, schema, keys.join('.'), isPropertySelected) : null) || false; }; }; exports.isDefaultCSVSelected = isDefaultCSVSelected; var isPropertyNotInternal = function (_a, key) { var properties = _a.properties; return !(properties[key] || {}).internal; }; /** * Check wether the field should be included while exporting as CSV. * * @param schemas {definitions} List of definitions loaded with `loadSchemas`. * @param key {string} The full key of the field, such as `cycle.inputs.0.value`. * @returns true if it should be included, false otherwise. */ var isCSVIncluded = function (schemas) { return function (key) { var _a = __read(key.split('.')), firstKey = _a[0], keys = _a.slice(1); var type = (0, schema_1.typeToSchemaType)(firstKey); var schema = (0, schema_1.isTypeValid)({ type: type }) ? schemas[type] : null; return (schema ? recursiveKeyCheck(schemas, schema, keys.join('.'), isPropertyNotInternal) : null) || false; }; }; exports.isCSVIncluded = isCSVIncluded; var recursiveSchemaFromKey = function (schemas, schema, key) { var _a = __read(key.split('.')), firstKey = _a[0], keys = _a.slice(1); return isNumber(firstKey) ? // for arrays, we can continue the recursion recursiveSchemaFromKey(schemas, schema, keys.join('.')) : !!schema ? keys.length < 1 ? schema : recursiveSchemaFromKey(schemas, schemas[(0, exports.keyType)(schema, firstKey)], keys.join('.')) // keep going deep : undefined; }; /** * Get the schema definition associated with a certain key. Can be `undefined` if key is unknown. * * @param schemas {definitions} List of definitions loaded with `loadSchemas`. * @param key {string} The full key of the field, such as `cycle.inputs.0.value`. * @returns The definition if found. */ var schemaFromKey = function (schemas, key) { var _a = __read(key.split('.')), firstKey = _a[0], keys = _a.slice(1); var type = (0, schema_1.typeToSchemaType)(firstKey); var schema = (0, schema_1.isTypeValid)({ type: type }) ? schemas[type] : null; return (schema ? recursiveSchemaFromKey(schemas, schema, keys.join('.')) : null) || undefined; }; exports.schemaFromKey = schemaFromKey;