@intelligo.ai/object-to-schema
Advanced tools
Comparing version 2.0.1 to 2.0.2
{ | ||
"person": { | ||
"name": { "mname": "MNAME" }, | ||
"dateOfBirth": "10/14/88", | ||
"dateOfBirth": "1/1/66", | ||
"dateOfDeath": "10/14/2026", | ||
"positions": { | ||
@@ -6,0 +7,0 @@ "pastPositions": ["a", "b", "c", "d"], |
@@ -112,4 +112,28 @@ { | ||
} | ||
}, | ||
{ | ||
"source": "person.dateOfBirth", | ||
"target": { | ||
"path": "d.Dates[]" | ||
} | ||
}, | ||
{ | ||
"source": "person.dateOfDeath", | ||
"target": { | ||
"path": "d.Dates[]" | ||
} | ||
}, | ||
{ | ||
"source": "person.dateOfBirth", | ||
"target": { | ||
"path": "dates[].dob" | ||
} | ||
}, | ||
{ | ||
"source": "person.dateOfDeath", | ||
"target": { | ||
"path": "dates[].dod" | ||
} | ||
} | ||
] | ||
} |
"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; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -12,3 +28,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var _b; | ||
var key = _a[0], value = _a[1]; | ||
var _c = __read(_a, 2), key = _c[0], value = _c[1]; | ||
return ramda_1.default.mergeDeepLeft(example, (_b = {}, _b[key] = cases(value), _b)); | ||
@@ -15,0 +31,0 @@ }, {}); |
@@ -13,2 +13,18 @@ "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; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -24,2 +40,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var transform_1 = require("./utils/transform"); | ||
var predefinedTransformations_1 = require("./utils/predefinedTransformations"); | ||
function mapObject(originalObj, transformations) { | ||
@@ -37,3 +54,3 @@ if (!transformations || transformations.length == 0) { | ||
var extractionTree = buildTree(transformations); | ||
var _a = ramda_1.default.partition(isTargetValue(), Object.entries(extractionTree)), treeEntries = _a[0], treeLeafs = _a[1]; | ||
var _a = __read(ramda_1.default.partition(isTargetValue(), Object.entries(extractionTree)), 2), treeEntries = _a[0], treeLeafs = _a[1]; | ||
treatLeafsMutation(originalObj, treeLeafs, targetObject); | ||
@@ -45,3 +62,3 @@ treatTreeMutation(originalObj, treeEntries, transformations, targetObject); | ||
treeEntries.forEach(function (_a) { | ||
var source = _a[0], _ = _a[1]; | ||
var _b = __read(_a, 2), source = _b[0], _ = _b[1]; | ||
var arrayValuesToSet = object_path_1.default.get(originalObj, source); | ||
@@ -57,28 +74,15 @@ var nextTargets = getSubTransformations(transformations, source); | ||
} | ||
var toLowerCase = transform_1.applyToOneOrMany(function (str) { return str.toLowerCase(); }); | ||
var toUpperCase = transform_1.applyToOneOrMany(function (str) { return str.toUpperCase(); }); | ||
var titleCase = transform_1.applyToOneOrMany(function (str) { return str | ||
.split(/\s/) | ||
.map(function (word) { return "" + word[0].toUpperCase() + word.slice(1); }) | ||
.join(' '); }); | ||
var toDate = transform_1.applyToOneOrMany(function (str) { return new Date(str).toISOString(); }); | ||
var stringToArray = transform_1.applyToOneOrMany(function (str, separator) { return str.split(separator); }); | ||
var strategies = { | ||
predefinedTransformations: { | ||
toUpperCase: function (str) { return toUpperCase(str); }, | ||
toLowerCase: function (str) { return toLowerCase(str); }, | ||
titleCase: function (str) { return titleCase(str); }, | ||
toDate: function (str) { return toDate(str); }, | ||
arrayToString: function (arr, separator) { return arr.join(separator); }, | ||
stringToArray: function (str, separator) { return stringToArray(str, separator); }, | ||
} | ||
}; | ||
function chooseHighestPriorityTransform(originalObj) { | ||
function chooseHighestPriorityTransforms(originalObj) { | ||
return function withObject(transforms) { | ||
if (transforms.every(function (transform) { return transform.target.path.includes('[]'); })) { | ||
return transforms; | ||
} | ||
var transformGenerator = transform_1.generateTransform(transforms); | ||
var transform = transformGenerator.next(); | ||
while (!transform.done && !object_path_1.default.has(originalObj, stringUtil_1.unUnidotify(transform.value.source)) && !transform.value.target.defaultValue) { | ||
while (!transform.done | ||
&& !object_path_1.default.has(originalObj, stringUtil_1.unUnidotify(transform.value.source)) | ||
&& !transform.value.target.defaultValue) { | ||
transform = transformGenerator.next(); | ||
} | ||
return transform.done ? null : transform.value; | ||
return transform.done ? [] : [transform.value]; | ||
}; | ||
@@ -91,3 +95,3 @@ } | ||
.map(function (_a) { | ||
var path = _a[0], transforms = _a[1]; | ||
var _b = __read(_a, 2), path = _b[0], transforms = _b[1]; | ||
return [path, transforms.map(function (transform) { return transform.target; })]; | ||
@@ -102,15 +106,15 @@ }); | ||
.map(function (_a) { | ||
var path = _a[0], transforms = _a[1]; | ||
var _b = __read(_a, 2), path = _b[0], transforms = _b[1]; | ||
return [ | ||
path, | ||
chooseHighestPriorityTransform(originalObj)(transform_1.sortTransformsByPriority(transforms)) | ||
chooseHighestPriorityTransforms(originalObj)(transform_1.sortTransformsByPriority(transforms)) | ||
]; | ||
}) | ||
.filter(function (_a) { | ||
var _ = _a[0], transforms = _a[1]; | ||
return transforms != null; | ||
var _b = __read(_a, 2), _ = _b[0], transforms = _b[1]; | ||
return transforms.length; | ||
}) | ||
.map(function (_a) { | ||
var _ = _a[0], transform = _a[1]; | ||
return transform; | ||
.flatMap(function (_a) { | ||
var _b = __read(_a, 2), _ = _b[0], transforms = _b[1]; | ||
return transforms.map(generalUtil_1.identity); | ||
}); | ||
@@ -123,4 +127,5 @@ var priorityTreeLeafs = transformsToTreeLeafs(highestPriorityTransforms); | ||
priorityTreeLeafs.forEach(function (_a) { | ||
var source = _a[0], mappingsArray = _a[1]; | ||
var _b = __read(_a, 2), source = _b[0], mappingsArray = _b[1]; | ||
mappingsArray.forEach(function (target) { | ||
var _a; | ||
var valueToSet = object_path_1.default.get(originalObj, stringUtil_1.unUnidotify(source)) || target.defaultValue || null; | ||
@@ -134,7 +139,11 @@ if (target.predefinedTransformations) { | ||
var transformationArgs = (_a = predefinedTransformation === null || predefinedTransformation === void 0 ? void 0 : predefinedTransformation.transformationArgs) !== null && _a !== void 0 ? _a : undefined; | ||
return ((_b = strategies.predefinedTransformations[transformationName]) !== null && _b !== void 0 ? _b : generalUtil_1.identity)(finalValue, transformationArgs); | ||
return ((_b = predefinedTransformations_1.strategies.predefinedTransformations[transformationName]) !== null && _b !== void 0 ? _b : generalUtil_1.identity)(finalValue, transformationArgs); | ||
}, valueToSet); | ||
} | ||
if (target.path.includes('[]')) { | ||
object_path_1.default.push(targetObject, target.path.split('[]')[0], valueToSet); | ||
var splitTargetByArray = target.path.split('[]'); | ||
var valueToPush = ramda_1.default.isEmpty(splitTargetByArray[1]) | ||
? valueToSet | ||
: (_a = {}, _a[splitTargetByArray[1].slice(1)] = valueToSet, _a); | ||
object_path_1.default.push(targetObject, splitTargetByArray[0], valueToPush); | ||
} | ||
@@ -167,3 +176,3 @@ else { | ||
.map(function (_a) { | ||
var superTarget = _a[0], value = _a[1]; | ||
var _b = __read(_a, 2), superTarget = _b[0], value = _b[1]; | ||
return ({ | ||
@@ -177,3 +186,3 @@ superTarget: superTarget, | ||
return function (_a) { | ||
var _ = _a[0], value = _a[1]; | ||
var _b = __read(_a, 2), _ = _b[0], value = _b[1]; | ||
return !Array.isArray(value); | ||
@@ -188,3 +197,3 @@ }; | ||
var _b; | ||
var key = _a[0], value = _a[1]; | ||
var _c = __read(_a, 2), key = _c[0], value = _c[1]; | ||
return (__assign(__assign({}, acc), (_b = {}, | ||
@@ -202,3 +211,3 @@ _b[key] = buildTree(value.map(function (transform) { return ({ | ||
function groupExtractionsByPath(transformations) { | ||
var _a = ramda_1.default.partition(function (transform) { return transform.source.includes('[]'); }, transformations), furtherProcessing = _a[0], noProcessing = _a[1]; | ||
var _a = __read(ramda_1.default.partition(function (transform) { return transform.source.includes('[]'); }, transformations), 2), furtherProcessing = _a[0], noProcessing = _a[1]; | ||
return { | ||
@@ -212,102 +221,2 @@ group: groupBySourcePrefix()(furtherProcessing), | ||
} | ||
var data = { | ||
"Title": "Avengers: Infinity War", | ||
"Year": "2018", | ||
"Rated": "PG-13", | ||
"ReleasedUSA": "23 Apr 2018", | ||
"ReleasedIsrael": "26 Apr 2018", | ||
"Runtime": "149 min", | ||
"Genre": "Action, Adventure, Sci-Fi", | ||
"Director": "Anthony Russo, Joe Russo", | ||
"Writer": "Christopher Markus (screenplay by), Stephen McFeely (screenplay by), Stan Lee (based on the Marvel comics by), Jack Kirby (based on the Marvel comics by), Joe Simon (Captain America created by), Jack Kirby (Captain America created by), Steve Englehart (Star-Lord created by), Steve Gan (Star-Lord created by), Bill Mantlo (Rocket Raccoon created by), Keith Giffen (Rocket Raccoon created by), Jim Starlin (Thanos, Gamora and Drax created by), Stan Lee (Groot created by), Larry Lieber (Groot created by), Jack Kirby (Groot created by), Steve Englehart (Mantis created by), Don Heck (Mantis created by)", | ||
"Actors": "Robert Downey Jr., Chris Hemsworth, Mark Ruffalo, Chris Evans", | ||
"Language": "English", | ||
"Country": "USA", | ||
"Ratings": [ | ||
{ | ||
"Source": "Internet Movie Database", | ||
"Value": "8.5/10" | ||
}, | ||
{ | ||
"Source": "Rotten Tomatoes", | ||
"Value": "85%", | ||
"TotalVotes": 111 | ||
}, | ||
{ | ||
"Source": "Metacritic", | ||
"Value": "68/100" | ||
} | ||
], | ||
"DVD": "14 Aug 2018" | ||
}; | ||
var transforms = [ | ||
{ | ||
source: 'Title', | ||
target: { | ||
path: 'title' | ||
} | ||
}, | ||
{ | ||
source: 'Year', | ||
target: { | ||
path: 'year' | ||
} | ||
}, | ||
{ | ||
source: '', | ||
target: { | ||
path: 'isReleased', | ||
conditionalValue: { | ||
targetPathCondition: 'dates.released', | ||
value: true | ||
} | ||
} | ||
}, | ||
{ | ||
source: 'Actors', | ||
target: { | ||
path: 'actors', | ||
predefinedTransformations: [ | ||
{ | ||
transformation: 'stringToArray', | ||
transformationArgs: ', ' | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
source: 'DVD', | ||
target: { | ||
path: 'dates.dvd', | ||
predefinedTransformations: [{ transformation: 'toDate' }] | ||
} | ||
}, | ||
{ | ||
source: 'ReleasedUSA', | ||
target: { | ||
path: 'dates.released[].usa', | ||
} | ||
}, | ||
{ | ||
source: 'ReleasedIsrael', | ||
target: { | ||
path: 'dates.released[].israel', | ||
} | ||
}, | ||
{ | ||
source: 'Ratings[].Source', | ||
target: { | ||
path: 'ratings[].siteName', | ||
predefinedTransformations: [{ transformation: 'titleCase' }] | ||
} | ||
}, | ||
{ | ||
source: 'Ratings[].Value', | ||
target: { | ||
path: 'ratings[].rating' | ||
} | ||
} | ||
]; | ||
var mapped = mapObject(data, transforms); | ||
console.log(JSON.stringify(mapped, null, 2)); | ||
//# sourceMappingURL=mapper.js.map |
@@ -6,3 +6,3 @@ export declare const strategies: { | ||
titleCase: (str: string | string[]) => string | string[]; | ||
toDate: (str: string | string[]) => Date | Date[]; | ||
toDate: (str: string | string[]) => string | string[]; | ||
arrayToString: (arr: string[], separator: string) => string; | ||
@@ -9,0 +9,0 @@ stringToArray: (str: string | string[], separator: string) => string[] | string[][]; |
@@ -10,3 +10,3 @@ "use strict"; | ||
.join(' '); }); | ||
var toDate = transform_1.applyToOneOrMany(function (str) { return new Date(str); }); | ||
var toDate = transform_1.applyToOneOrMany(function (str) { return new Date(str).toISOString(); }); | ||
var stringToArray = transform_1.applyToOneOrMany(function (str, separator) { return str.split(separator); }); | ||
@@ -13,0 +13,0 @@ exports.strategies = { |
@@ -29,2 +29,29 @@ "use strict"; | ||
}; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
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; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -37,19 +64,32 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
function generateTransform(transforms) { | ||
var _i, transforms_1, transform; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var transforms_1, transforms_1_1, transform, e_1_1; | ||
var e_1, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_i = 0, transforms_1 = transforms; | ||
_a.label = 1; | ||
_b.trys.push([0, 5, 6, 7]); | ||
transforms_1 = __values(transforms), transforms_1_1 = transforms_1.next(); | ||
_b.label = 1; | ||
case 1: | ||
if (!(_i < transforms_1.length)) return [3, 4]; | ||
transform = transforms_1[_i]; | ||
if (!!transforms_1_1.done) return [3, 4]; | ||
transform = transforms_1_1.value; | ||
return [4, transform]; | ||
case 2: | ||
_a.sent(); | ||
_a.label = 3; | ||
_b.sent(); | ||
_b.label = 3; | ||
case 3: | ||
_i++; | ||
transforms_1_1 = transforms_1.next(); | ||
return [3, 1]; | ||
case 4: return [2]; | ||
case 4: return [3, 7]; | ||
case 5: | ||
e_1_1 = _b.sent(); | ||
e_1 = { error: e_1_1 }; | ||
return [3, 7]; | ||
case 6: | ||
try { | ||
if (transforms_1_1 && !transforms_1_1.done && (_a = transforms_1.return)) _a.call(transforms_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
return [7]; | ||
case 7: return [2]; | ||
} | ||
@@ -74,3 +114,3 @@ }); | ||
var transforms = treeLeafs.flatMap(function (_a) { | ||
var path = _a[0], targets = _a[1]; | ||
var _b = __read(_a, 2), path = _b[0], targets = _b[1]; | ||
return targets.map(function (target) { return ({ source: path, target: target }); }); | ||
@@ -77,0 +117,0 @@ }); |
{ | ||
"name": "@intelligo.ai/object-to-schema", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"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
Sorry, the diff of this file is not supported yet
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
69398
939