@intelligo.ai/object-to-schema
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -7,43 +7,51 @@ "use strict"; | ||
return Object | ||
.keys(obj) | ||
.reduce(function (schema, key) { return resolveOperation(schema, obj, key); }, schema); | ||
.entries(obj) | ||
.reduce(function (accSchema, _a) { | ||
var _b; | ||
var key = _a[0], value = _a[1]; | ||
return Object.assign(accSchema, (_b = {}, _b[key] = resolveOperation(schema[key], value), _b)); | ||
}, schema); | ||
} | ||
exports.mapObjectToSchema = mapObjectToSchema; | ||
function handleEmptyValue(schema, key) { | ||
var value = schema[key]; | ||
if (value == null || value == 'unknown') { | ||
return unknownType(schema, key); | ||
} | ||
return schema; | ||
function resolveOperation(schema, obj) { | ||
if (schema === void 0) { schema = {}; } | ||
var schemaArchType = generalUtil_1.resolveValueArchtype(schema); | ||
var objArchType = generalUtil_1.resolveValueArchtype(obj); | ||
var strategy = resolveStrategy(objArchType, schemaArchType); | ||
return strategy(obj, schema); | ||
} | ||
function resolveOperation(schema, obj, key) { | ||
var _a, _b; | ||
var nestedValue = obj[key]; | ||
var archType = generalUtil_1.resolveValueArchtype(nestedValue); | ||
switch (archType) { | ||
case 'EMPTY': | ||
return handleEmptyValue(schema, key); | ||
case 'SIMPLE': | ||
var type = generalUtil_1.resolveType(nestedValue); | ||
return simpleType(type, key, schema); | ||
case 'ARRAY': | ||
return Object.assign(schema, (_a = {}, _a[key] = [arrayOfObjectsToSchema(nestedValue, {})], _a)); | ||
case 'OBJECT': | ||
return Object.assign(schema, (_b = {}, _b[key] = mapObjectToSchema(nestedValue, {}), _b)); | ||
function resolveStrategy(obj, schema) { | ||
if (obj == 'SIMPLE') { | ||
return substitution; | ||
} | ||
else if (obj == 'ARRAY') { | ||
return arrayRecursion; | ||
} | ||
else if (obj == 'EMPTY' && schema == 'EMPTY') { | ||
return unknownType; | ||
} | ||
else if (obj == 'EMPTY') { | ||
return discard; | ||
} | ||
else if (obj == 'OBJECT') { | ||
return mapObjectToSchema; | ||
} | ||
else { | ||
return unknownType; | ||
} | ||
} | ||
function arrayOfObjectsToSchema(array, schema) { | ||
function arrayRecursion(obj, schema) { | ||
if (schema === void 0) { schema = {}; } | ||
return array.reduce(function (acc, obj) { return generalUtil_1.resolveValueArchtype(obj) == 'OBJECT' | ||
? mapObjectToSchema(obj, acc) | ||
: JSON.stringify({ type: generalUtil_1.resolveType(obj).toLowerCase() }); }, schema); | ||
var arraySchema = obj.reduce(function (accSchema, subObj) { return resolveOperation(Array.isArray(accSchema) ? accSchema[0] : accSchema, subObj); }, schema); | ||
return Array.isArray(arraySchema) ? arraySchema : [arraySchema]; | ||
} | ||
function simpleType(type, key, schema) { | ||
var _a; | ||
return Object.assign(schema, (_a = {}, _a[key] = JSON.stringify({ type: type.toLowerCase() }), _a)); | ||
function discard(obj, schema) { | ||
return schema; | ||
} | ||
function unknownType(schema, key) { | ||
var _a; | ||
return Object.assign(schema, (_a = {}, _a[key] = JSON.stringify({ type: 'unknown' }), _a)); | ||
function substitution(obj, schema) { | ||
return JSON.stringify({ type: generalUtil_1.resolveType(obj).toLowerCase() }); | ||
} | ||
function unknownType(obj, schema) { | ||
return JSON.stringify({ type: 'unknown' }); | ||
} | ||
//# sourceMappingURL=schemaCreator.js.map |
{ | ||
"name": "@intelligo.ai/object-to-schema", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8338
408443