New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

map-factory

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

map-factory - npm Package Compare versions

Comparing version 1.6.2 to 1.6.3

dist/test/nested-properties-test.js

20

CHANGELOG.md

@@ -0,1 +1,19 @@

### 1.6.3
Set experimental flag to eliminate some of the object-mapper code
### 1.6.2
Remove object-mapper dependency
### 1.6.1
Fixed regression in experimental mode
### 1.6.0
Added experimental mode to begin absorbing object-mapper into code base
```js
const mapper = createMapper({ experimental:true });
```
### 1.5.0

@@ -5,3 +23,3 @@ Converted code from TypeScript to ES6 with babel

### 1.4.1
Fixed bug where you could map from the same source field more than once.
Fixed bug where you couldn't map from the same source field more than once.

@@ -8,0 +26,0 @@ ### 1.4.0

59

dist/benchmark/mapper-vs-custom.js

@@ -14,8 +14,15 @@ "use strict";

/* eslint-disable */
const buildObject = (0, _index2.default)(false).map("fieldName").to("field.name").map("fieldId").to("field.id");
const existingMode = (0, _index2.default)(false).map("fieldName").to("field.name").map("fieldId").to("field.id").map(["fieldName", "fieldId"]).to("merged", (name, id) => {
`${name}-${id}`;
});
const arrayFromExistingMode = (0, _index2.default)(false).map(["fieldName"]).to("field.name", value => value).map(["fieldId"]).to("field.id", value => value).map(["fieldName", "fieldId"]).to("merged", (name, id) => {
`${name}-${id}`;
});
const experimentMode = (0, _index2.default)(true).map("fieldName").to("field.name", value => value).map("fieldId").to("field.id", value => value).map(["fieldName", "fieldId"]).to("merged", (name, id) => {
`${name}-${id}`;
});
const arrayFromExperimentMode = (0, _index2.default)(true).map(["fieldName"]).to("field.name", value => value).map(["fieldId"]).to("field.id", value => value).map(["fieldName", "fieldId"]).to("merged", (name, id) => {
`${name}-${id}`;
});
const getSetApproach = (0, _index2.default)(false).map(["fieldName"]).to("field.name", value => value).map(["fieldId"]).to("field.id", value => value);
const experiment = (0, _index2.default)(true).map("fieldName").to("field.name", value => value).map("fieldId").to("field.id", value => value);
const source = {

@@ -26,5 +33,6 @@ fieldName: "my name",

buildObject.execute(source);
experiment.execute(source);
getSetApproach.execute(source);
existingMode.execute(source);
experimentMode.execute(source);
arrayFromExistingMode.execute(source);
arrayFromExperimentMode.execute(source);

@@ -34,15 +42,20 @@ const suite = new _benchmark2.default.Suite();

// add tests
suite.add("using experiment", () => {
experiment.execute(source);
}).add("using get and set KeyValue", () => {
getSetApproach.execute(source);
}).add("just wrapping object-mapper", () => {
buildObject.execute(source);
}).add("map and run", () => {
suite.add("map and run", () => {
const map = (0, _index2.default)();
for (let i = 0; i < 100000; i++) {}
map("fieldName").to("field.name", value => value);
map("fieldId").to("field.id", value => value);
map(["fieldName", "fieldId"]).to("merged", (name, id) => {
`${name}-${id}`;
});
map.execute(source);
}).add("using arrayMaps with old code", () => {
arrayFromExistingMode.execute(source);
}).add("using arrayMaps with new code", () => {
arrayFromExperimentMode.execute(source);
}).add("using experiment", () => {
experimentMode.execute(source);
}).add("just wrapping object-mapper", () => {
existingMode.execute(source);
})

@@ -54,7 +67,11 @@ // add listeners

// console.log("Fastest is", Benchmark.filter(result, "fastest")[0].name);
console.log("re", result);
console.log(suite["0"].name, suite["0"].count, suite["0"].times);
console.log(suite["1"].name, suite["1"].count, suite["1"].times);
console.log(suite["2"].name, suite["2"].count, suite["2"].times);
console.log(suite["3"].name, suite["3"].count, suite["3"].times);
console.log(suite["4"].name, suite["4"].count, suite["4"].times);
for (const item of result) {
console.log(item.name, item.count);
}
// for(const item of result) {
// console.log(item.name, item.count);
// }
// console.log("result", Benchmark.filter(result, "fastest"));

@@ -61,0 +78,0 @@ })

@@ -21,6 +21,5 @@ "use strict";

const opts = options || {};
const om = _objectMapper2.default;
const me = {
mapper: new _mapper2.default(opts.experimental, om)
mapper: new _mapper2.default(opts.experimental, _objectMapper2.default)
};

@@ -27,0 +26,0 @@

@@ -16,10 +16,15 @@ "use strict";

const SINGLE_MODE = 0;
const MULTI_MODE = 1;
const OR_MODE = 2;
class Mapper {
constructor(experiment, om) {
constructor(experimental, om) {
this.om = om;
this.experimental = experimental;
this.assignment = [];
this.mapCache_ = null;
this.experiment = experiment;
}

@@ -43,2 +48,3 @@

if (!sourceArray) {
// This should probably return undefined
return null;

@@ -57,34 +63,184 @@ }

// This should probably return undefined
return null;
}
// execute(source, destination) {
// if (!this.experiment) {
// return this.executeOld(source, destination);
// }
execute(source, destination) {
if (!this.experimental) {
return this.executeOld(source, destination);
}
// return this.executeNew(source, destination);
// }
return this.executeNew(source, destination);
}
// executeNew(source, destination) {
executeNew(source, destination) {
// if (source === null || source === undefined) {
// throw new Error("A source object is required");
// }
if (source === null || source === undefined) {
throw new Error("A source object is required");
}
// if (destination === null || destination === undefined) {
// destination = {};
// }
if (destination === null || destination === undefined) {
destination = {};
}
// if (this.mapCache_ === null) {
// this.mapCache_ = this.createMapData_();
// }
for (const item of this.assignment) {
// const output = this.om(source, destination, this.mapCache_.transform);
// TODO: This method should be done at map declaration not at execution
const descriptor = this.getTransformDescriptor_(item);
// return this.appendMultiSelections_(source, output, this.mapCache_.multiMaps);
// }
// annoyingly, VS Code's auto format is at odds with eslint
// console.log("descriptor", descriptor)
execute(source, destination) {
/* eslint-disable indent */
switch (descriptor.mode) {
case SINGLE_MODE:
destination = this.processSingleItem_(source, destination, descriptor);
break;
case MULTI_MODE:
destination = this.processMultiItem_(source, destination, descriptor);
break;
case OR_MODE:
destination = this.processOrItem_(source, destination, descriptor);
break;
default:
throw new Error(`Invalid Mapping Mode: mode was ${descriptor.mode}`);
}
/* eslint-enable indent */
}
return destination;
}
getTransformDescriptor_(item) {
const sourcePath = item.source;
let targetPath = item.target;
let transform = item.transform;
let isCustomTransform = true;
const mode = this.decideMode_(item);
// TODO: offload to Mapping declaration
if (!transform) {
isCustomTransform = false;
transform = value => value;
}
if (!targetPath) {
targetPath = sourcePath;
}
return { mode: mode, targetPath: targetPath, sourcePath: sourcePath, transform: transform, isCustomTransform: isCustomTransform };
}
decideMode_(item) {
const isArray = Array.isArray(item.source);
if (isArray === false) {
return SINGLE_MODE;
}
if (isArray === true && item.orMode === false) {
return MULTI_MODE;
}
return OR_MODE;
}
processSingleItem_(sourceObject, destinationObject, _ref) {
let targetPath = _ref.targetPath,
sourcePath = _ref.sourcePath,
transform = _ref.transform;
// console.log("single mode", targetPath, sourcePath, transform);
// Get source
let value = this.om.getKeyValue(sourceObject, sourcePath);
// console.log("pre-transform value", value);
// Apply transform - will become optional
value = transform(value);
// console.log("post-transform value", value);
// Set value on destination object
return this.om.setKeyValue(destinationObject, targetPath, value);
}
processMultiItem_(sourceObject, destinationObject, _ref2) {
let sourcePath = _ref2.sourcePath,
targetPath = _ref2.targetPath,
transform = _ref2.transform,
isCustomTransform = _ref2.isCustomTransform;
// console.log("multi mode", targetPath, sourcePath, transform, isCustomTransform);
if (isCustomTransform === false) {
throw new Error("Multiple selections must map to a transform. No transform provided.");
}
const values = [];
// Get source
for (const fromKey of sourcePath) {
values.push(this.om.getKeyValue(sourceObject, fromKey));
}
// console.log("pre-transform value", values);
// Apply transform - will become optional
const value = transform.apply(undefined, values);
// console.log("post-transform value", value);
// Set value on destination object
return this.om.setKeyValue(destinationObject, targetPath, value);
}
processOrItem_(sourceObject, destinationObject, _ref3) {
let sourcePath = _ref3.sourcePath,
targetPath = _ref3.targetPath,
transform = _ref3.transform,
isCustomTransform = _ref3.isCustomTransform;
// console.log("or mode", targetPath, sourcePath, transform, isCustomTransform);
let orValue;
const sourceArray = sourcePath;
for (const sourceKey of sourceArray) {
orValue = this.om.getKeyValue(sourceObject, sourceKey);
if (orValue !== null && orValue !== undefined) {
break;
}
}
// console.log("pre-transform value", orValue);
// no transform
if (isCustomTransform === false) {
return this.om.setKeyValue(destinationObject, targetPath, orValue);
}
// has a transform
const params = [];
params.push(orValue);
const result = transform.apply(undefined, params);
// console.log("post-transform value", result);
return this.om.setKeyValue(destinationObject, targetPath, result);
}
// Old execute logic is below: marked for death
executeOld(source, destination) {
if (source === null || source === undefined) {

@@ -91,0 +247,0 @@ throw new Error("A source object is required");

@@ -67,4 +67,4 @@ "use strict";

if (!target) {
throw new Error("the target field name cannot be null");
if (!target || typeof target !== "string") {
throw new Error("the target field name must be a string");
}

@@ -71,0 +71,0 @@

@@ -120,2 +120,3 @@ 'use strict';

if (Array.isArray(destinationObject[key]) === false) {
// console.log("first one");
destinationObject[key] = [];

@@ -141,2 +142,3 @@ }

if (Array.isArray(destinationObject[key]) === false) {
console.log("second one");
destinationObject[key] = [];

@@ -143,0 +145,0 @@ }

"use strict";
var _code = require("code");
var _lab = require("lab");

@@ -9,6 +7,10 @@

var _labTesting = require("lab-testing");
var _notationSuite = require("./suites/notation-suite");
var _labTesting2 = _interopRequireDefault(_labTesting);
var _notationSuite2 = _interopRequireDefault(_notationSuite);
var _manyMappingsSuite = require("./suites/many-mappings-suite");
var _manyMappingsSuite2 = _interopRequireDefault(_manyMappingsSuite);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -19,42 +21,63 @@

const lab = exports.lab = Lab.script();
const testing = (0, _labTesting2.default)(lab);
const group = testing.createExperiment("map-factory");
const createMapper = require("../lib/index");
_notationSuite2.default.run(lab, {
LABELS: ["arrays", "single mapping array"],
GET_ITEM: "array.[].id",
SET_ITEM: "array.levels.[].id",
SOURCE: {
array: [{ id: 1 }, { id: 2 }, { id: 3 }]
},
EXPECTED: {
array: {
levels: [{ id: 1 }, { id: 2 }, { id: 3 }]
}
},
NO_SOURCE_EXPECTED: {
array: {
levels: []
}
},
MODIFY_VALUE: "modified",
MODIFIED_EXPECTED: {
array: {
levels: [{ id: "modified" }]
}
}
});
group("working with arrays", () => {
// size is missing from source
const labels = ["arrays", "mix of available and missing data"];
const mappings = [{ from: "propertySpaces.[].useType.parentCategory", to: "property.spaces.[].useTypeParentCategory" }, { from: "propertySpaces.[].size", to: "property.spaces.[].size" }];
const expected = {
"property": {
"spaces": [{
"useTypeParentCategory": "Office"
}]
}
};
const source = {
"propertySpaces": [{
"useType": {
"name": "Office",
"sector": "Business (B1a)",
"category": "Office",
"parentCategory": "Office"
}
}]
};
lab.test("a mix of existing and non-existing source data works correctly", done => {
_manyMappingsSuite2.default.run(lab, {
LABELS: labels,
MAPPINGS: mappings,
SOURCE: source,
EXPECTED: expected,
EXPERIMENTAL: true
});
const mapper = createMapper({ "experimental": true });
const expected = {
"property": {
"spaces": [{
"useTypeName": "Office",
"useTypeSector": "Business (B1a)",
"useTypeCategory": "Office",
"useTypeParentCategory": "Office"
}]
}
};
mapper.map("propertySpaces.[].useType.name").to("property.spaces.[].useTypeName").map("propertySpaces.[].useType.sector").to("property.spaces.[].useTypeSector").map("propertySpaces.[].useType.category").to("property.spaces.[].useTypeCategory").map("propertySpaces.[].useType.parentCategory").to("property.spaces.[].useTypeParentCategory").map("propertySpaces.[].size").to("property.spaces.[].size"); // size is missing from source
const result = mapper.execute({
"propertySpaces": [{
"useType": {
"name": "Office",
"sector": "Business (B1a)",
"category": "Office",
"parentCategory": "Office"
}
}]
});
(0, _code.expect)(result).to.equal(expected);
return done();
});
_manyMappingsSuite2.default.run(lab, {
LABELS: labels,
MAPPINGS: mappings,
SOURCE: source,
EXPECTED: expected,
EXPERIMENTAL: false
});
"use strict";
var _code = require("code");
var _lab = require("lab");

@@ -9,988 +7,1232 @@

var _labTesting = require("lab-testing");
var _interfaceSuite = require("./suites/interface-suite");
var _labTesting2 = _interopRequireDefault(_labTesting);
var _interfaceSuite2 = _interopRequireDefault(_interfaceSuite);
var _mapFactory = require("../lib/map-factory");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _mapFactory2 = _interopRequireDefault(_mapFactory);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
var _mapping = require("../lib/mapping");
const lab = exports.lab = Lab.script();
var _mapping2 = _interopRequireDefault(_mapping);
_interfaceSuite2.default.run(lab, { EXPERIMENTAL: true });
_interfaceSuite2.default.run(lab, { EXPERIMENTAL: false });
var _mapper = require("../lib/mapper");
// import { expect } from "code";
// import * as Lab from "lab";
// import getHelper from "lab-testing";
var _mapper2 = _interopRequireDefault(_mapper);
// const lab = exports.lab = Lab.script();
// const testing = getHelper(lab);
// const group = testing.createExperiment("map-factory");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// import createMapper from "../lib/map-factory";
// import Mapping from "../lib/mapping";
// import Mapper from "../lib/mapper";
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
const lab = exports.lab = Lab.script();
const testing = (0, _labTesting2.default)(lab);
const group = testing.createExperiment("map-factory");
// function createSut() {
// return createMapper({ experimental: true });
// }
group("basic functionality", () => {
// group("basic functionality", () => {
lab.test("Can map one field that exists to another", done => {
// lab.test("Can map one field that exists to another", done => {
const source = {
"fieldName": "name1"
};
// const source = {
// "fieldName": "name1"
// };
const expected = {
"field": {
"name": "name1"
}
};
// const expected = {
// "field": {
// "name": "name1"
// }
// };
const map = (0, _mapFactory2.default)();
// const map = createSut();
map("fieldName").to("field.name");
// map("fieldName").to("field.name");
const actual = map.execute(source);
// const actual = map.execute(source);
(0, _code.expect)(actual).to.equal(expected);
// expect(actual).to.equal(expected);
return done();
});
// return done();
// });
lab.test("Throws if a null source is provided", done => {
// lab.test("Throws if a null source is provided", done => {
const map = (0, _mapFactory2.default)();
// const map = createSut();
map("fieldName").to("field.name");
// map("fieldName").to("field.name");
const throws = function throws() {
// const throws = function () {
map.execute(null);
};
// map.execute(null);
(0, _code.expect)(throws).to.throw();
// };
return done();
});
// expect(throws).to.throw();
lab.test("Throws if an undefined source is provided", done => {
// return done();
// });
const map = (0, _mapFactory2.default)();
// lab.test("Throws if an undefined source is provided", done => {
map("fieldName").to("field.name");
// const map = createSut();
const throws = function throws() {
// map("fieldName").to("field.name");
map.execute(undefined);
};
// const throws = function () {
(0, _code.expect)(throws).to.throw();
// map.execute(undefined);
return done();
});
// };
lab.test("Can reuse map for different transform", done => {
// expect(throws).to.throw();
const source = {
"fieldName": "name1"
};
// return done();
// });
const source2 = {
"fieldName": "name2"
};
// lab.test("Can reuse map for different transform", done => {
const expected = {
"field": {
"name": "name1"
}
};
// const source = {
// "fieldName": "name1"
// };
const expected2 = {
"field": {
"name": "name2"
}
};
// const source2 = {
// "fieldName": "name2"
// };
const map = (0, _mapFactory2.default)();
// const expected = {
// "field": {
// "name": "name1"
// }
// };
map("fieldName").to("field.name");
// const expected2 = {
// "field": {
// "name": "name2"
// }
// };
const actual = map.execute(source);
const actual2 = map.execute(source2);
// const map = createSut();
(0, _code.expect)(actual).to.equal(expected);
(0, _code.expect)(actual2).to.equal(expected2);
// map("fieldName").to("field.name");
return done();
});
// const actual = map.execute(source);
// const actual2 = map.execute(source2);
lab.test("Can map from a source where source name is not formatted as a string", done => {
// expect(actual).to.equal(expected);
// expect(actual2).to.equal(expected2);
const source = {
country: "PL"
};
// return done();
// });
const expected = {
"country": "PL"
};
// lab.test("Can map from a source where source name is not formatted as a string", done => {
const map = (0, _mapFactory2.default)();
// const source = {
// country: "PL"
// };
map("country").to("country");
// const expected = {
// "country": "PL"
// };
const actual = map.execute(source);
// const map = createSut();
(0, _code.expect)(actual).to.equal(expected);
// map("country").to("country");
return done();
});
// const actual = map.execute(source);
lab.test("A field that doesn't exists on the source doesn't affect the resulting object", done => {
// expect(actual).to.equal(expected);
const source = {
"fieldName": "name1"
};
// return done();
// });
const expected = {
"field": {
"name": "name1"
}
};
// lab.test("A field that doesn't exists on the source doesn't affect the resulting object", done => {
const map = (0, _mapFactory2.default)();
// const source = {
// "fieldName": "name1"
// };
map("fieldName").to("field.name");
map("fieldId").to("field.name");
// const expected = {
// "field": {
// "name": "name1"
// }
// };
const actual = map.execute(source);
// const map = createSut();
(0, _code.expect)(actual).to.equal(expected);
// map("fieldName").to("field.name");
// map("fieldId").to("field.name");
return done();
});
// const actual = map.execute(source);
lab.test("A field that doesn't exists on the source doesn't affect the resulting object when a pass-through transform is used", done => {
// expect(actual).to.equal(expected);
const source = {
"fieldName": "name1"
};
// return done();
// });
const expected = {
"field": {
"name": "name1"
}
};
// lab.test("A field that doesn't exists on the source doesn't affect the resulting object when a pass-through transform is used", done => {
const map = (0, _mapFactory2.default)();
// const source = {
// "fieldName": "name1"
// };
map("fieldName").to("field.name");
map("fieldId").to("field.name", value => value);
// const expected = {
// "field": {
// "name": "name1"
// }
// };
const actual = map.execute(source);
// const map = createSut();
(0, _code.expect)(actual).to.equal(expected);
// map("fieldName").to("field.name");
// map("fieldId").to("field.name", value => value);
return done();
});
// const actual = map.execute(source);
lab.test.skip("A field that doesn't exists on the source doesn't affect the resulting object when a modifying transform is used", done => {
// expect(actual).to.equal(expected);
const source = {
"fieldName": "name1"
};
// return done();
// });
const expected = {
"field": {
"name": "name1"
}
};
// lab.test("A null source field throws an error", done => {
const map = (0, _mapFactory2.default)();
// const map = createSut();
map("fieldName").to("field.name");
map("fieldId").to("field.name", value => {
return `value is: ${value}`;
});
// const throws = function () {
const actual = map.execute(source);
// map(null).to("field.name");
(0, _code.expect)(actual).to.equal(expected);
// };
return done();
});
// expect(throws).to.throw();
lab.test("A null source field throws an error", done => {
// return done();
const map = (0, _mapFactory2.default)();
// });
const throws = function throws() {
// lab.test("A null target field throws an error", done => {
map(null).to("field.name");
};
// const map = createSut();
(0, _code.expect)(throws).to.throw();
// const throws = function () {
return done();
});
// map("fieldName").to(null);
lab.test("A null target field throws an error", done => {
// };
const map = (0, _mapFactory2.default)();
// expect(throws).to.throw();
const throws = function throws() {
// return done();
map("fieldName").to(null);
};
// });
(0, _code.expect)(throws).to.throw();
// lab.test("The source field is used if no target field is provided", done => {
return done();
});
// const source = {
// "fieldName": "name1"
// };
lab.test("The source field is used if no target field is provided", done => {
// const map = createSut();
const source = {
"fieldName": "name1"
};
// map("fieldName");
const map = (0, _mapFactory2.default)();
// const actual = map.execute(source);
map("fieldName");
// expect(actual).to.equal(source);
const actual = map.execute(source);
// return done();
// });
(0, _code.expect)(actual).to.equal(source);
// lab.test("A source field can be mapped multiple times", done => {
return done();
});
// const source = {
// "fieldName": "name"
// };
lab.test("A source field can be mapped multiple times", done => {
// const expected = {
// "field": "name",
// "name": "name-long"
// };
const source = {
"fieldName": "name"
};
// const map = createSut();
const expected = {
"field": "name",
"name": "name-long"
};
// map("fieldName").to("field");
// map("fieldName").to("name", value => `${value}-long`);
const map = (0, _mapFactory2.default)();
// const actual = map.execute(source);
map("fieldName").to("field");
map("fieldName").to("name", value => `${value}-long`);
// expect(actual).to.equal(expected);
const actual = map.execute(source);
// return done();
// });
// });
(0, _code.expect)(actual).to.equal(expected);
// group("alternate interfaces", () => {
return done();
});
});
// lab.test("default function and map() function are logically equivalent", done => {
group("alternate interfaces", () => {
// const source = {
// "fieldName": "name1"
// };
lab.test("default function and map() function are logically equivalent", done => {
// const expected = {
// "field": {
// "name": "name1"
// }
// };
const source = {
"fieldName": "name1"
};
// const map = createSut();
// const mapper = createSut();
const expected = {
"field": {
"name": "name1"
}
};
// map("fieldName").to("field.name");
// mapper.map("fieldName").to("field.name");
const map = (0, _mapFactory2.default)();
const mapper = (0, _mapFactory2.default)();
// const defaultActual = map.execute(source);
// const functionActual = mapper.execute(source);
map("fieldName").to("field.name");
mapper.map("fieldName").to("field.name");
// expect(defaultActual).to.equal(expected);
// expect(defaultActual).to.equal(functionActual);
const defaultActual = map.execute(source);
const functionActual = mapper.execute(source);
// return done();
(0, _code.expect)(defaultActual).to.equal(expected);
(0, _code.expect)(defaultActual).to.equal(functionActual);
// });
return done();
});
});
// });
group("fluent chaining ", () => {
// group("fluent chaining ", () => {
lab.test("map() returns a chainable object", done => {
// lab.test("map() returns a chainable object", done => {
const mapper = (0, _mapFactory2.default)();
// const mapper = createSut();
const actual = mapper.map("userId");
// const actual = mapper.map("userId");
(0, _code.expect)(actual).to.not.be.null();
(0, _code.expect)(actual).to.be.instanceOf(_mapping2.default);
// expect(actual).to.not.be.null();
// expect(actual).to.be.instanceOf(Mapping);
return done();
});
// return done();
lab.test("to() returns a chainable object", done => {
// });
const mapper = (0, _mapFactory2.default)();
// lab.test("to() returns a chainable object", done => {
const actual = mapper.map("userId").to("user.id");
// const mapper = createSut();
(0, _code.expect)(actual).to.not.be.null();
(0, _code.expect)(actual).to.be.instanceOf(_mapper2.default);
// const actual = mapper.map("userId").to("user.id");
return done();
});
// expect(actual).to.not.be.null();
// expect(actual).to.be.instanceOf(Mapper);
lab.test("to() with a function returns a chainable object", done => {
// return done();
const mapper = (0, _mapFactory2.default)();
// });
const actual = mapper.map("userId").to("user.id", () => {
return "a";
});
// lab.test("to() with a function returns a chainable object", done => {
(0, _code.expect)(actual).to.not.be.null();
(0, _code.expect)(actual).to.be.instanceOf(_mapper2.default);
// const mapper = createSut();
return done();
});
// const actual = mapper.map("userId").to("user.id", () => {
// return "a";
// });
lab.test("mapper can fluently chain call map() after the map() method", done => {
// expect(actual).to.not.be.null();
// expect(actual).to.be.instanceOf(Mapper);
const source = {
"userId": 123,
"userName": "my name"
};
// return done();
const expected = {
"userId": 123,
"name": "my name"
};
// });
const mapper = (0, _mapFactory2.default)();
// lab.test("mapper can fluently chain call map() after the map() method", done => {
mapper.map("userId").map("userName").to("name");
// const source = {
// "userId": 123,
// "userName": "my name"
// };
const actual = mapper.execute(source);
// const expected = {
// "userId": 123,
// "name": "my name"
// };
(0, _code.expect)(actual).to.equal(expected);
// const mapper = createSut();
return done();
});
// mapper
// .map("userId")
// .map("userName").to("name");
lab.test("mapper can fluently chain call map() after the to() method", done => {
// const actual = mapper.execute(source);
const source = {
"userId": 123,
"userName": "my name"
};
// expect(actual).to.equal(expected);
const expected = {
"id": 123,
"name": "my name"
};
// return done();
const mapper = (0, _mapFactory2.default)();
// });
mapper.map("userId").to("id").map("userName").to("name");
// lab.test("mapper can fluently chain call map() after the to() method", done => {
const actual = mapper.execute(source);
// const source = {
// "userId": 123,
// "userName": "my name"
// };
(0, _code.expect)(actual).to.equal(expected);
// const expected = {
// "id": 123,
// "name": "my name"
// };
return done();
});
// const mapper = createSut();
lab.test("mapper can fluently chain call execute() after the to() method", done => {
// mapper
// .map("userId").to("id")
// .map("userName").to("name");
const source = {
"userId": 123,
"userName": "my name"
};
// const actual = mapper.execute(source);
const expected = {
"id": 123,
"name": "my name"
};
// expect(actual).to.equal(expected);
const mapper = (0, _mapFactory2.default)();
// return done();
const actual = mapper.map("userId").to("id").map("userName").to("name").execute(source);
// });
(0, _code.expect)(actual).to.equal(expected);
// lab.test("mapper can fluently chain call execute() after the to() method", done => {
return done();
});
// const source = {
// "userId": 123,
// "userName": "my name"
// };
lab.test("mapper can fluently chain call execute() after the map() method", done => {
// const expected = {
// "id": 123,
// "name": "my name"
// };
const source = {
"userId": 123
};
// const mapper = createSut();
const expected = {
"userId": 123
};
// const actual = mapper
// .map("userId").to("id")
// .map("userName").to("name")
// .execute(source);
const mapper = (0, _mapFactory2.default)();
// expect(actual).to.equal(expected);
const actual = mapper.map("userId").execute(source);
// return done();
(0, _code.expect)(actual).to.equal(expected);
// });
return done();
});
});
// lab.test("mapper can fluently chain call execute() after the map() method", done => {
group("The each() method", () => {
// const source = {
// "userId": 123
// };
lab.test("Can process an array correctly", done => {
const source = [{
"fieldName": "name1"
}, {
"fieldName": "name2"
}];
// const expected = {
// "userId": 123
// };
const expected = [{
"field": {
"name": "name1"
}
}, {
"field": {
"name": "name2"
}
}];
// const mapper = createSut();
const map = (0, _mapFactory2.default)();
// const actual = mapper
// .map("userId")
// .execute(source);
map("fieldName").to("field.name");
// expect(actual).to.equal(expected);
const actual = map.each(source);
// return done();
(0, _code.expect)(actual).to.equal(expected);
// });
return done();
});
// });
lab.test("An empty array does not cause an error", done => {
const source = [];
// group("The each() method", () => {
const expected = null;
// lab.test("Can process an array correctly", done => {
// const source = [{
// "fieldName": "name1"
// }, {
// "fieldName": "name2"
// }];
const map = (0, _mapFactory2.default)();
// const expected = [
// {
// "field": {
// "name": "name1"
// }
// },
// {
// "field": {
// "name": "name2"
// }
// }];
map("fieldName").to("field.name");
// const map = createSut();
const actual = map.each(source);
// map("fieldName").to("field.name");
(0, _code.expect)(actual).to.equal(expected);
// const actual = map.each(source);
return done();
});
// expect(actual).to.equal(expected);
lab.test("Multiple mappers can be used together", done => {
const source = {
one: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }],
two: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }],
three: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }]
};
// return done();
// });
const expected = {
one: [{ newOne: "a" }, { newOne: "b" }, { newOne: "c" }],
two: [{ newOne: "a" }, { newOne: "b" }, { newOne: "c" }],
three: [{ newOne: "a" }, { newOne: "b" }, { newOne: "c" }]
};
// lab.test("An empty array does not cause an error", done => {
// const source = [];
const mainMapper = (0, _mapFactory2.default)();
const childMapper = (0, _mapFactory2.default)();
// const expected = [];
childMapper.map("value").to("newOne");
// const map = createSut();
mainMapper.map("one").to("one", array => childMapper.each(array)).map("two").to("two", array => childMapper.each(array)).map("three").to("three", array => childMapper.each(array));
// map("fieldName").to("field.name");
const actual = mainMapper.execute(source);
// const actual = map.each(source);
(0, _code.expect)(actual).to.equal(expected);
return done();
});
// expect(actual).to.equal(expected);
lab.test("A null parameter should return null", done => {
const map = (0, _mapFactory2.default)();
// return done();
map("fieldName").to("field.name");
// });
(0, _code.expect)(map.each(null)).to.equal(null);
// lab.test("Multiple mappers can be used together", done => {
// const source = {
// one: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }],
// two: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }],
// three: [{ value: "a", drop: "me" }, { value: "b", drop: "me" }, { value: "c", drop: "me" }]
// };
return done();
});
// const expected = {
// one: [{ newOne: "a" }, { newOne: "b" }, { newOne: "c" }],
// two: [{ newOne: "a" }, { newOne: "b" }, { newOne: "c" }],
// three: [{ newOne: "a" }, { newOne: "b" }, { newOne: "c" }]
// };
lab.test("An undefined parameter should return null", done => {
const map = (0, _mapFactory2.default)();
// const mainMapper = createSut();
// const childMapper = createSut();
map("fieldName").to("field.name");
// childMapper
// .map("value").to("newOne");
(0, _code.expect)(map.each(undefined)).to.equal(null);
// mainMapper
// .map("one").to("one", array => childMapper.each(array))
// .map("two").to("two", array => childMapper.each(array))
// .map("three").to("three", array => childMapper.each(array));
return done();
});
// const actual = mainMapper.execute(source);
lab.test("A non-array throws an error", done => {
const map = (0, _mapFactory2.default)();
const source = { "a": "b" };
// expect(actual).to.equal(expected);
map("fieldName").to("field.name");
// return done();
const throws = function throws() {
map.each(source);
};
// });
(0, _code.expect)(throws).to.throw();
// lab.test("A null parameter throws an error", done => {
// const map = createSut();
return done();
});
});
// map("fieldName").to("field.name");
group("source and destination", () => {
// const throws = function () {
// map.each(null);
// };
lab.test("Can map fields from a source onto an existing destination object", done => {
// expect(throws).to.throw();
const source = {
"fieldName": "name1"
};
const destination = {
"existing": "field"
};
// return done();
// });
const expected = {
"field": {
"name": "name1"
},
"existing": "field"
};
// lab.test("A non-array throws an error", done => {
// const map = createSut();
// const source = { "a": "b" };
const map = (0, _mapFactory2.default)();
// map("fieldName").to("field.name");
map("fieldName").to("field.name");
const actual = map.execute(source, destination);
// const throws = function () {
// map.each(source);
// };
(0, _code.expect)(actual).to.equal(expected);
// expect(throws).to.throw();
return done();
});
// return done();
// });
// });
lab.test("Can map a field from source over an existing field on a destination object", done => {
// group("source and destination", () => {
const source = {
"fieldName": "name1"
};
// lab.test("Can map fields from a source onto an existing destination object", done => {
const destination = {
"field": {
"name": "wrong"
}
};
// const source = {
// "fieldName": "name1"
// };
const expected = {
"field": {
"name": "name1"
}
};
// const destination = {
// "existing": "field"
// };
const map = (0, _mapFactory2.default)();
// const expected = {
// "field": {
// "name": "name1"
// },
// "existing": "field"
// };
map("fieldName").to("field.name");
// const map = createSut();
const actual = map.execute(source, destination);
// map("fieldName").to("field.name");
(0, _code.expect)(actual).to.equal(expected);
// const actual = map.execute(source, destination);
return done();
});
});
// expect(actual).to.equal(expected);
group("custom functions", () => {
// return done();
// });
lab.test("Calls a function and alters the resulting object", done => {
// lab.test("Can map a field from source over an existing field on a destination object", done => {
const source = {
"fieldName": "name1"
};
// const source = {
// "fieldName": "name1"
// };
const expected = {
"field": {
"name": "altered"
}
};
// const destination = {
// "field": {
// "name": "wrong"
// }
// };
const map = (0, _mapFactory2.default)();
// const expected = {
// "field": {
// "name": "name1"
// }
// };
map("fieldName").to("field.name", () => "altered");
// const map = createSut();
const actual = map.execute(source);
// map("fieldName").to("field.name");
(0, _code.expect)(actual).to.equal(expected);
// const actual = map.execute(source, destination);
return done();
});
});
// expect(actual).to.equal(expected);
group("multiple selections", () => {
// return done();
// });
// });
lab.test("Can extract multiple selections into a single transform", done => {
// group("custom functions", () => {
const source = {
"group1": {
"name": "A"
},
"group2": {
"name": "B"
}
};
// lab.test("Calls a function and alters the resulting object", done => {
const expected = {
"merged": { "names": ["A", "B"] }
};
// const source = {
// "fieldName": "name1"
// };
const map = (0, _mapFactory2.default)();
// const expected = {
// "field": {
// "name": "altered"
// }
// };
map(["group1", "group2"]).to("merged", (group1, group2) => {
return { "names": [group1.name, group2.name] };
});
// const map = createSut();
const actual = map.execute(source);
// map("fieldName").to("field.name", () => "altered");
(0, _code.expect)(actual).to.equal(expected);
// const actual = map.execute(source);
return done();
});
// expect(actual).to.equal(expected);
lab.test("Can extract multiple selections into a single transform while allowing simpler mappings to work", done => {
// return done();
// });
// });
const source = {
"person": {
"name": "joe"
},
"group1": {
"name": "A"
},
"group2": {
"name": "B"
}
};
// group("multiple selections", () => {
const expected = {
"name": "joe",
"merged": { "groups": ["A", "B"] }
};
// lab.test("Can extract multiple selections into a single transform", done => {
const map = (0, _mapFactory2.default)();
// const source = {
// "group1": {
// "name": "A"
// },
// "group2": {
// "name": "B"
// }
// };
map("person.name").to("name");
map(["group1", "group2"]).to("merged", (group1, group2) => {
return { "groups": [group1.name, group2.name] };
});
// const expected = {
// "merged": { "names": ["A", "B"] }
// };
const actual = map.execute(source);
// const map = createSut();
(0, _code.expect)(actual).to.equal(expected);
// map(["group1", "group2"]).to("merged", (group1, group2) => {
// return { "names": [group1.name, group2.name] };
// });
return done();
});
// const actual = map.execute(source);
lab.test("If multiple selections aren't mapped to a transform and error will occur", done => {
// expect(actual).to.equal(expected);
const source = {
"person": {
"name": "joe"
},
"group1": {
"name": "A"
},
"group2": {
"name": "B"
}
};
// return done();
// });
const map = (0, _mapFactory2.default)();
// lab.test("Can extract multiple selections into a single transform while allowing simpler mappings to work", done => {
map("person.name").to("name");
map(["group1", "group2"]).to("merged");
// const source = {
// "person": {
// "name": "joe"
// },
// "group1": {
// "name": "A"
// },
// "group2": {
// "name": "B"
// }
// };
const throws = function throws() {
// const expected = {
// "name": "joe",
// "merged": { "groups": ["A", "B"] }
// };
map.execute(source);
};
// const map = createSut();
(0, _code.expect)(throws).to.throw();
// map("person.name").to("name");
// map(["group1", "group2"]).to("merged", (group1, group2) => {
// return { "groups": [group1.name, group2.name] };
// });
return done();
});
});
// const actual = map.execute(source);
group("The or() method", () => {
// expect(actual).to.equal(expected);
lab.test("Maps the first item if it is present", done => {
// return done();
// });
const source = {
"fieldName": "name1"
};
// lab.test("If multiple selections aren't mapped to a transform and error will occur", done => {
const expected = {
"field": {
"name": "name1"
}
};
// const source = {
// "person": {
// "name": "joe"
// },
// "group1": {
// "name": "A"
// },
// "group2": {
// "name": "B"
// }
// };
const map = (0, _mapFactory2.default)();
// const map = createSut();
map("fieldName").or("noField").to("field.name");
// map("person.name").to("name");
// map(["group1", "group2"]).to("merged");
const actual = map.execute(source);
// const throws = function () {
(0, _code.expect)(actual).to.equal(expected);
// map.execute(source);
// };
return done();
});
// expect(throws).to.throw();
lab.test("to method can use a transform if provided with first item", done => {
const source = {
"fieldName": "name1"
};
// return done();
// });
// });
const expected = {
"field": {
"name": "altered name1"
}
};
// group("The or() method", () => {
const map = (0, _mapFactory2.default)();
// lab.test("Maps the first item if it is present", done => {
map("fieldName").or("noField").to("field.name", value => `altered ${value}`);
// const source = {
// "fieldName": "name1"
// };
const actual = map.execute(source);
// const expected = {
// "field": {
// "name": "name1"
// }
// };
(0, _code.expect)(actual).to.equal(expected);
// const map = createSut();
return done();
});
// map("fieldName").or("noField").to("field.name");
lab.test("Maps the second item if the first item isn't present", done => {
// const actual = map.execute(source);
const source = {
"fieldName": "name1"
};
// expect(actual).to.equal(expected);
const expected = {
"field": {
"name": "name1"
}
};
// return done();
const map = (0, _mapFactory2.default)();
// });
map("noField").or("fieldName").to("field.name");
// lab.test("to method can use a transform if provided with first item", done => {
// const source = {
// "fieldName": "name1"
// };
const actual = map.execute(source);
// const expected = {
// "field": {
// "name": "altered name1"
// }
// };
(0, _code.expect)(actual).to.equal(expected);
// const map = createSut();
return done();
});
// map("fieldName").or("noField").to("field.name", value => `altered ${value}`);
lab.test("Maps the last item in a very long chain", done => {
// const actual = map.execute(source);
const source = {
"fieldName": "name1"
};
// expect(actual).to.equal(expected);
const expected = {
"field": {
"name": "name1"
}
};
// return done();
// });
const map = (0, _mapFactory2.default)();
// lab.test("Maps the second item if the first item isn't present", done => {
map("a").or("b").or("c").or("d").or("e").or("fieldName").to("field.name");
// const source = {
// "fieldName": "name1"
// };
const actual = map.execute(source);
// const expected = {
// "field": {
// "name": "name1"
// }
// };
(0, _code.expect)(actual).to.equal(expected);
// const map = createSut();
return done();
});
// map("noField").or("fieldName").to("field.name");
lab.test("to method can use a transform if provided with subsequent item", done => {
const source = {
"fieldName": "name1"
};
// const actual = map.execute(source);
const expected = {
"field": {
"name": "altered name1"
}
};
// expect(actual).to.equal(expected);
const map = (0, _mapFactory2.default)();
// return done();
// });
map("noField").or("fieldName").to("field.name", value => `altered ${value}`);
// lab.test("Maps the last item in a very long chain", done => {
const actual = map.execute(source);
// const source = {
// "fieldName": "name1"
// };
(0, _code.expect)(actual).to.equal(expected);
// const expected = {
// "field": {
// "name": "name1"
// }
// };
return done();
});
// const map = createSut();
lab.test("Throws if the initial source field is an array", done => {
// map("a").or("b").or("c").or("d").or("e").or("fieldName").to("field.name");
const map = (0, _mapFactory2.default)();
// const actual = map.execute(source);
const throws = function throws() {
// expect(actual).to.equal(expected);
map(["a", "b"]).or("fieldName").to("field.name");
};
// return done();
// });
(0, _code.expect)(throws).to.throw();
// lab.test("to method can use a transform if provided with subsequent item", done => {
// const source = {
// "fieldName": "name1"
// };
done();
});
// const expected = {
// "field": {
// "name": "altered name1"
// }
// };
lab.test("Throws if and subsequent source field is an array", done => {
// const map = createSut();
const map = (0, _mapFactory2.default)();
// map("noField").or("fieldName").to("field.name", value => `altered ${value}`);
const throws = function throws() {
// const actual = map.execute(source);
map("fieldName").or(["a", "b"]).to("field.name");
};
// expect(actual).to.equal(expected);
(0, _code.expect)(throws).to.throw();
// return done();
// });
done();
});
// lab.test("Throws if the initial source field is an array", done => {
lab.test("Throws if source is null", done => {
const map = (0, _mapFactory2.default)();
// const map = createSut();
const throws = function throws() {
map("fieldName").or(null).to("field.name");
};
// const throws = function () {
(0, _code.expect)(throws).to.throw();
// map(["a", "b"]).or("fieldName").to("field.name");
done();
});
// };
lab.test("Throws if source is undefined", done => {
const map = (0, _mapFactory2.default)();
// expect(throws).to.throw();
const throws = function throws() {
// done();
map("fieldName").or(undefined).to("field.name");
};
// });
(0, _code.expect)(throws).to.throw();
// lab.test("Throws if and subsequent source field is an array", done => {
done();
});
});
// const map = createSut();
// group("array edge case", () => {
// const throws = function () {
// lab.test("A deep array maps properly", done => {
// map("fieldName").or(["a", "b"]).to("field.name");
// const source = {
// array: [{ id: 1 }, { id: 2 }, { id: 3 }]
// };
// const expected = {
// array: {
// levels: [{ id: 1 }, { id: 2 }, { id: 3 }]
// }
// expect(throws).to.throw();
// done();
// });
// lab.test("Throws if source is null", done => {
// const map = createSut();
// const throws = function () {
// map("fieldName").or(null).to("field.name");
// };
// const map = createMapper();
// expect(throws).to.throw();
// map("array.[].id").to("array.levels.[].id");
// done();
// const actual = map.execute(source);
// });
// expect(actual).to.equal(expected);
// lab.test("Throws if source is undefined", done => {
// const map = createSut();
// return done();
// const throws = function () {
// map("fieldName").or(undefined).to("field.name");
// };
// expect(throws).to.throw();
// done();
// });
// lab.test("A null source should not do funky stuff when an array is in the mix", done => {
// });
// const source = {
// array: null
// // PORTED TESTS
// group("ported object-mapper tests", () => {
// lab.test("mapping - map and append full array to existing mapped array", done => {
// const obj = {
// thing: [
// { a: "a1", i: "b1" },
// { a: "a2", i: "b2" },
// { a: "a3", i: "b3" }
// ],
// thingOther: [
// { a: "a4", i: "b4" },
// { a: "a5", i: "b5" },
// { a: "a6", i: "b6" }
// ]
// };
// const expected = {
// "thing2": [
// [
// { a: "a1", i: "b1" },
// { a: "a2", i: "b2" },
// { a: "a3", i: "b3" }
// ],
// [
// { a: "a4", i: "b4" },
// { a: "a5", i: "b5" },
// { a: "a6", i: "b6" }
// ]
// ]
// };
// const map = createMapper();
// const map = createSut();
// map(["array.[].id"]).to("array.levels.[].id", value => value);
// map("thing").to("thing2[]+");
// map("thingOther").to("thing2[]+");
// const actual = map.execute(source);
// const result = map.execute(obj);
// expect(actual).to.equal(expected);
// expect(result).to.equal(expected);
// return done();
// });
// lab.test("map object to another - allow null values", done => {
// const obj = {
// "a": 1234,
// "foo": {
// "bar": null
// }
// };
// const expected = {
// foo: {
// a: 1234
// },
// bar: {
// bar: null
// }
// };
// const map = createSut();
// map("foo.bar").to("bar.bar?");
// map("a").to("foo.a");
// const result = map.execute(obj);
// expect(result).to.equal(expected);
// return done();
// });
// lab.test("A undefined array in source should not do funky stuff when an array is in the mix", done => {
// lab.test("map object to another - with three destinations for same value", done => {
// const baseObject = {
// test: 1
// };
// const source = {
// const obj = {
// "foo": {
// "bar": "baz"
// }
// };
// const expected = {
// test: 1,
// bar: {
// foo: [{
// baz: "baz",
// foo: "baz",
// bar: ["baz"]
// }]
// }
// };
// const map = createMapper();
// const map = createSut();
// map(["array.[].id"]).to("array.levels.[].id", value => value);
// map("foo.bar").to("bar.foo[].baz");
// map("foo.bar").to("bar.foo[].foo");
// map("foo.bar").to("bar.foo[].bar[]");
// const actual = map.execute(source);
// expect(actual).to.equal(expected);
// const result = map.execute(obj, baseObject);
// expect(result).to.equal(expected);
// return done();
// });
// lab.test("A undefined array in source should not do funky stuff when an array is in the mix", done => {
// lab.test.skip("original constious tests", done => {
// // const merge = om.merge;
// const source = {
// "something": {
// "array": undefined,
// "id": "123"
// }
// const obj = {
// "sku": "12345",
// "upc": "99999912345X",
// "title": "Test Item",
// "descriptions": ["Short description", "Long description"],
// "length": 5,
// "width": 2,
// "height": 8,
// "inventory": {
// "onHandQty": 0,
// "replenishQty": null
// },
// "price": 100
// };
// const mapper = createSut();
// mapper
// .map("sku").to("Envelope.Request.Item.SKU")
// .map("upc").to("Envelope.Request.Item.UPC")
// .map("title").to("Envelope.Request.Item.ShortTitle")
// .map("length").to("Envelope.Request.Item.Dimensions.Length")
// .map("width").to("Envelope.Request.Item.Dimensions.Width")
// .map("height").to("Envelope.Request.Item.Dimensions.Height")
// .map("weight").to("Envelope.Request.Item.Weight", () => 10)
// .map("weightUnits").to("Envelope.Request.Item.WeightUnits", () => null)
// .map("inventory.onHandQty").to("Envelope.Request.Item.Inventory?")
// .map("inventory.replenishQty").to("Envelope.Request.Item.RelpenishQuantity?")
// .map("inventory.isInventoryItem").to("Envelope.Request.Item.OnInventory")
// .map("price").to("Envelope.Request.Item.Price[].List")
// .map("price").to("Envelope.Request.Item.Price[].Value")
// .map("price").to("Test[]")
// .map("descriptions[0]").to("Envelope.Request.Item.ShortDescription")
// .map("descriptions[1]").to("Envelope.Request.Item.LongDescription");
// const map = {
// "sku": "Envelope.Request.Item.SKU"
// , "upc": "Envelope.Request.Item.UPC"
// , "title": "Envelope.Request.Item.ShortTitle"
// , "length": "Envelope.Request.Item.Dimensions.Length"
// , "width": "Envelope.Request.Item.Dimensions.Width"
// , "height": "Envelope.Request.Item.Dimensions.Height"
// , "weight": [
// ["Envelope.Request.Item.Weight", null, function () {
// return 10;
// }]
// ]
// , "weightUnits": [["Envelope.Request.Item.WeightUnits", null, function () {
// return null;
// }]]
// , "inventory.onHandQty": "Envelope.Request.Item.Inventory?"
// , "inventory.replenishQty": "Envelope.Request.Item.RelpenishQuantity?"
// , "inventory.isInventoryItem": { key: ["Envelope.Request.Item.OnInventory", null, "YES"] }
// , "price": ["Envelope.Request.Item.Price[].List", "Envelope.Request.Item.Price[].Value", "Test[]"]
// , "descriptions[0]": "Envelope.Request.Item.ShortDescription"
// , "descriptions[1]": "Envelope.Request.Item.LongDescription"
// };
// const expected = {
// Test: [100],
// Envelope: {
// Request: {
// Item: {
// SKU: "12345",
// UPC: "99999912345X",
// ShortTitle: "Test Item",
// Dimensions: {
// Length: 5,
// Width: 2,
// Height: 8
// },
// Weight: 10,
// Inventory: 0,
// RelpenishQuantity: null,
// OnInventory: "YES",
// Price: [{
// List: 100,
// Value: 100
// }],
// ShortDescription: "Short description",
// LongDescription: "Long description"
// }
// }
// }
// };
// const map = createMapper();
// let result = merge(obj, {}, map);
// map(["something.array.[].id"]).to("something.array.levels.[].id", value => value);
// expect(result).to.equal(expected);
// const actual = map.execute(source);
// map.sku = {
// key: "Envelope.Request.Item.SKU",
// transform: (val, objFrom, objTo) => {
// return "over-ridden-sku";
// }
// };
// expect(actual).to.equal(expected);
// expected.Envelope.Request.Item.SKU = "over-ridden-sku";
// result = merge(obj, {}, map);
// expect(result, "override sku").to.equal(expected);
// obj.inventory = null;
// expected.Envelope.Request.Item.Inventory = null;
// result = merge(obj, {}, map);
// expect(result, "null inventory").to.equal(expected);
// return done();
// });
// });
// });
// // group("array edge case", () => {
// // lab.test("A deep array maps properly", done => {
// // const source = {
// // array: [{ id: 1 }, { id: 2 }, { id: 3 }]
// // };
// // const expected = {
// // array: {
// // levels: [{ id: 1 }, { id: 2 }, { id: 3 }]
// // }
// // };
// // const map = createSut();
// // map("array.[].id").to("array.levels.[].id");
// // const actual = map.execute(source);
// // expect(actual).to.equal(expected);
// // return done();
// // });
// // lab.test("A null source should not do funky stuff when an array is in the mix", done => {
// // const source = {
// // array: null
// // };
// // const expected = {
// // };
// // const map = createSut();
// // map(["array.[].id"]).to("array.levels.[].id", value => value);
// // const actual = map.execute(source);
// // expect(actual).to.equal(expected);
// // return done();
// // });
// // lab.test("A undefined array in source should not do funky stuff when an array is in the mix", done => {
// // const source = {
// // };
// // const expected = {
// // };
// // const map = createSut();
// // map(["array.[].id"]).to("array.levels.[].id", value => value);
// // const actual = map.execute(source);
// // expect(actual).to.equal(expected);
// // return done();
// // });
// // lab.test("A undefined array in source should not do funky stuff when an array is in the mix", done => {
// // const source = {
// // "something": {
// // "array": undefined,
// // "id": "123"
// // }
// // };
// // const expected = {
// // };
// // const map = createSut();
// // map(["something.array.[].id"]).to("something.array.levels.[].id", value => value);
// // const actual = map.execute(source);
// // expect(actual).to.equal(expected);
// // return done();
// // });
// // });

@@ -33,18 +33,31 @@ "use strict";

group("The objectMapper() method", () => {
lab.experiment.skip("The objectMapper() method", () => {
lab.test("map object to another - simple", done => {
lab.test("map object to another - with three destinations for same value", done => {
const baseObject = {
test: 1
};
const obj = {
"foo": "bar"
"foo": {
"bar": "baz"
}
};
const expected = {
"bar": "bar"
test: 1,
bar: {
foo: [{
baz: "baz",
foo: "baz",
bar: ["baz"]
}]
}
};
const map = {
"foo": "bar"
"foo.bar": ["bar.foo[].baz", "bar.foo[].foo", "bar.foo[].bar[]"]
};
const result = (0, _objectMapper2.default)(obj, map);
const result = (0, _objectMapper2.default)(obj, baseObject, map);

@@ -55,3 +68,7 @@ (0, _code.expect)(result).to.equal(expected);

lab.test("map object to another - complexity 1", done => {
lab.test("map object to another - with key object notation with default value when key does not exists", done => {
const baseObject = {
test: 1
};
const obj = {

@@ -64,4 +81,7 @@ "foo": {

const expected = {
test: 1,
bar: {
foo: "baz"
foo: [{
baz: 10
}]
}

@@ -71,6 +91,9 @@ };

const map = {
"foo.bar": "bar.foo"
"notExistingKey": {
key: "bar.foo[].baz",
"default": 10
}
};
const result = (0, _objectMapper2.default)(obj, map);
const result = (0, _objectMapper2.default)(obj, baseObject, map);

@@ -81,3 +104,7 @@ (0, _code.expect)(result).to.equal(expected);

lab.test("map object to another - complexity 2", done => {
lab.test("map object to another - with key object notation with default function when key does not exists", done => {
const baseObject = {
test: 1
};
const obj = {

@@ -90,2 +117,3 @@ "foo": {

const expected = {
test: 1,
bar: {

@@ -99,5 +127,42 @@ foo: [{

const map = {
"foo.bar": "bar.foo[].baz"
"notExistingKey": {
key: "bar.foo[].baz",
default: function _default(fromObject, fromKey, toObject, toKey) {
return fromObject.foo.bar;
}
}
};
const result = (0, _objectMapper2.default)(obj, baseObject, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("map object to another - with key object notation with default function returning undefined when key does not exists", done => {
const obj = {
"a": 1234,
"foo": {
"bar": "baz"
}
};
const expected = {
bar: {
bar: "baz",
a: 1234
}
};
const map = {
"foo.bar": "bar.bar",
"notExistingKey": {
key: "bar.test",
default: function _default(fromObject, fromKey, toObject, toKey) {
return undefined;
}
},
"a": "bar.a"
};
const result = (0, _objectMapper2.default)(obj, map);

@@ -109,3 +174,3 @@

lab.test("map object to another - with base object", done => {
lab.test("map object to another - with key array notation with default value when key does not exists", done => {
const baseObject = {

@@ -125,3 +190,3 @@ test: 1

foo: [{
baz: "baz"
baz: 10
}]

@@ -132,3 +197,3 @@ }

const map = {
"foo.bar": "bar.foo[].baz"
"notExistingKey": [["bar.foo[].baz", null, 10]]
};

@@ -142,22 +207,44 @@

lab.test("map object to another - with two destinations for same value", done => {
const baseObject = {
test: 1
lab.test("mapping - map and append full array to existing mapped array", done => {
const obj = {
thing: [{ a: "a1", b: "b1" }, { a: "a2", b: "b2" }, { a: "a3", b: "b3" }],
thingOther: [{ a: "a4", b: "b4" }, { a: "a5", b: "b5" }, { a: "a6", b: "b6" }]
};
const map = {
"thing": "thing2[]+",
"thingOther": "thing2[]+"
};
const expected = {
"thing2": [[{ a: "a1", b: "b1" }, { a: "a2", b: "b2" }, { a: "a3", b: "b3" }], [{ a: "a4", b: "b4" }, { a: "a5", b: "b5" }, { a: "a6", b: "b6" }]]
};
const result = (0, _objectMapper2.default)(obj, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("map object to another - prevent null values from being mapped", done => {
const obj = {
"foo": "bar"
"a": 1234,
"foo": {
"bar": null
}
};
const expected = {
test: 1,
bar: "bar",
baz: "bar"
foo: {
a: 1234
},
bar: {}
};
const map = {
"foo": ["bar", "baz"]
"foo.bar": "bar.bar",
"a": "foo.a"
};
const result = (0, _objectMapper2.default)(obj, baseObject, map);
const result = (0, _objectMapper2.default)(obj, map);

@@ -167,10 +254,34 @@ (0, _code.expect)(result).to.equal(expected);

});
lab.test("map object to another - with two destinations for same value inside object", done => {
const baseObject = {
test: 1
lab.test("map object to another - allow null values", done => {
const obj = {
"a": 1234,
"foo": {
"bar": null
}
};
const expected = {
foo: {
a: 1234
},
bar: null
};
const map = {
"foo.bar": "bar?",
"a": "foo.a"
};
const result = (0, _objectMapper2.default)(obj, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("map object to another two levels - allow null values", done => {
const obj = {
"a": 1234,
"foo": {
"bar": "baz"
"bar": null
}

@@ -180,8 +291,7 @@ };

const expected = {
test: 1,
foo: {
a: 1234
},
bar: {
foo: {
baz: "baz",
foo: "baz"
}
bar: null
}

@@ -191,6 +301,7 @@ };

const map = {
"foo.bar": ["bar.foo.baz", "bar.foo.foo"]
"foo.bar": "bar.bar?",
"a": "foo.a"
};
const result = (0, _objectMapper2.default)(obj, baseObject, map);
const result = (0, _objectMapper2.default)(obj, map);

@@ -200,7 +311,26 @@ (0, _code.expect)(result).to.equal(expected);

});
lab.test("map object to another - with two destinations for same value inside array", done => {
const baseObject = {
test: 1
});
lab.experiment.skip("redundant tests", () => {
lab.test("map object to another - simple", done => {
const obj = {
"foo": "bar"
};
const expected = {
"bar": "bar"
};
const map = {
"foo": "bar"
};
const result = (0, _objectMapper2.default)(obj, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("map object to another - complexity 1", done => {
const obj = {

@@ -213,8 +343,4 @@ "foo": {

const expected = {
test: 1,
bar: {
foo: [{
baz: "baz",
foo: "baz"
}]
foo: "baz"
}

@@ -224,6 +350,6 @@ };

const map = {
"foo.bar": ["bar.foo[].baz", "bar.foo[].foo"]
"foo.bar": "bar.foo"
};
const result = (0, _objectMapper2.default)(obj, baseObject, map);
const result = (0, _objectMapper2.default)(obj, map);

@@ -234,7 +360,3 @@ (0, _code.expect)(result).to.equal(expected);

lab.test("map object to another - with three destinations for same value", done => {
const baseObject = {
test: 1
};
lab.test("map object to another - complexity 2", done => {
const obj = {

@@ -247,8 +369,5 @@ "foo": {

const expected = {
test: 1,
bar: {
foo: [{
baz: "baz",
foo: "baz",
bar: ["baz"]
baz: "baz"
}]

@@ -259,6 +378,6 @@ }

const map = {
"foo.bar": ["bar.foo[].baz", "bar.foo[].foo", "bar.foo[].bar[]"]
"foo.bar": "bar.foo[].baz"
};
const result = (0, _objectMapper2.default)(obj, baseObject, map);
const result = (0, _objectMapper2.default)(obj, map);

@@ -269,3 +388,3 @@ (0, _code.expect)(result).to.equal(expected);

lab.test("map object to another - with key object notation", done => {
lab.test("map object to another - with base object", done => {
const baseObject = {

@@ -291,5 +410,3 @@ test: 1

const map = {
"foo.bar": {
key: "bar.foo[].baz"
}
"foo.bar": "bar.foo[].baz"
};

@@ -303,3 +420,3 @@

lab.test("map object to another - with key object notation with default value when key does not exists", done => {
lab.test("map object to another - with two destinations for same value", done => {
const baseObject = {

@@ -310,2 +427,26 @@ test: 1

const obj = {
"foo": "bar"
};
const expected = {
test: 1,
bar: "bar",
baz: "bar"
};
const map = {
"foo": ["bar", "baz"]
};
const result = (0, _objectMapper2.default)(obj, baseObject, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("map object to another - with two destinations for same value inside object", done => {
const baseObject = {
test: 1
};
const obj = {
"foo": {

@@ -319,5 +460,6 @@ "bar": "baz"

bar: {
foo: [{
baz: 10
}]
foo: {
baz: "baz",
foo: "baz"
}
}

@@ -327,6 +469,3 @@ };

const map = {
"notExistingKey": {
key: "bar.foo[].baz",
"default": 10
}
"foo.bar": ["bar.foo.baz", "bar.foo.foo"]
};

@@ -339,4 +478,3 @@

});
lab.test("map object to another - with key object notation with default function when key does not exists", done => {
lab.test("map object to another - with two destinations for same value inside array", done => {
const baseObject = {

@@ -356,3 +494,4 @@ test: 1

foo: [{
baz: "baz"
baz: "baz",
foo: "baz"
}]

@@ -363,8 +502,3 @@ }

const map = {
"notExistingKey": {
key: "bar.foo[].baz",
default: function _default(fromObject, fromKey, toObject, toKey) {
return fromObject.foo.bar;
}
}
"foo.bar": ["bar.foo[].baz", "bar.foo[].foo"]
};

@@ -378,5 +512,8 @@

lab.test("map object to another - when target key is undefined it should be ignored", done => {
lab.test("map object to another - with key object notation", done => {
const baseObject = {
test: 1
};
const obj = {
"a": 1234,
"foo": {

@@ -388,4 +525,7 @@ "bar": "baz"

const expected = {
test: 1,
bar: {
bar: "baz"
foo: [{
baz: "baz"
}]
}

@@ -395,7 +535,8 @@ };

const map = {
"foo.bar": "bar.bar",
"a": undefined
"foo.bar": {
key: "bar.foo[].baz"
}
};
const result = (0, _objectMapper2.default)(obj, map);
const result = (0, _objectMapper2.default)(obj, baseObject, map);

@@ -406,3 +547,3 @@ (0, _code.expect)(result).to.equal(expected);

lab.test("map object to another - with key object notation with default function returning undefined when key does not exists", done => {
lab.test("map object to another - when target key is undefined it should be ignored", done => {
const obj = {

@@ -417,4 +558,3 @@ "a": 1234,

bar: {
bar: "baz",
a: 1234
bar: "baz"
}

@@ -425,9 +565,3 @@ };

"foo.bar": "bar.bar",
"notExistingKey": {
key: "bar.test",
default: function _default(fromObject, fromKey, toObject, toKey) {
return undefined;
}
},
"a": "bar.a"
"a": undefined
};

@@ -541,32 +675,3 @@

});
lab.test("map object to another - with key array notation with default value when key does not exists", done => {
const baseObject = {
test: 1
};
const obj = {
"foo": {
"bar": "baz"
}
};
const expected = {
test: 1,
bar: {
foo: [{
baz: 10
}]
}
};
const map = {
"notExistingKey": [["bar.foo[].baz", null, 10]]
};
const result = (0, _objectMapper2.default)(obj, baseObject, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("map object to another - with key array notation with default function when key does not exists", done => {

@@ -884,197 +989,2 @@ const baseObject = {

});
lab.test("mapping - map and append full array to existing mapped array", done => {
const obj = {
thing: [{ a: "a1", b: "b1" }, { a: "a2", b: "b2" }, { a: "a3", b: "b3" }],
thingOther: [{ a: "a4", b: "b4" }, { a: "a5", b: "b5" }, { a: "a6", b: "b6" }]
};
const map = {
"thing": "thing2[]+",
"thingOther": "thing2[]+"
};
const expected = {
"thing2": [[{ a: "a1", b: "b1" }, { a: "a2", b: "b2" }, { a: "a3", b: "b3" }], [{ a: "a4", b: "b4" }, { a: "a5", b: "b5" }, { a: "a6", b: "b6" }]]
};
const result = (0, _objectMapper2.default)(obj, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("map object to another - prevent null values from being mapped", done => {
const obj = {
"a": 1234,
"foo": {
"bar": null
}
};
const expected = {
foo: {
a: 1234
},
bar: {}
};
const map = {
"foo.bar": "bar.bar",
"a": "foo.a"
};
const result = (0, _objectMapper2.default)(obj, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("map object to another - allow null values", done => {
const obj = {
"a": 1234,
"foo": {
"bar": null
}
};
const expected = {
foo: {
a: 1234
},
bar: null
};
const map = {
"foo.bar": "bar?",
"a": "foo.a"
};
const result = (0, _objectMapper2.default)(obj, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("map object to another - allow null values", done => {
const obj = {
"a": 1234,
"foo": {
"bar": null
}
};
const expected = {
foo: {
a: 1234
},
bar: {
bar: null
}
};
const map = {
"foo.bar": "bar.bar?",
"a": "foo.a"
};
const result = (0, _objectMapper2.default)(obj, map);
(0, _code.expect)(result).to.equal(expected);
return done();
});
lab.test("original constious tests", done => {
const merge = _objectMapper2.default.merge;
const obj = {
"sku": "12345",
"upc": "99999912345X",
"title": "Test Item",
"descriptions": ["Short description", "Long description"],
"length": 5,
"width": 2,
"height": 8,
"inventory": {
"onHandQty": 0,
"replenishQty": null
},
"price": 100
};
const map = {
"sku": "Envelope.Request.Item.SKU",
"upc": "Envelope.Request.Item.UPC",
"title": "Envelope.Request.Item.ShortTitle",
"length": "Envelope.Request.Item.Dimensions.Length",
"width": "Envelope.Request.Item.Dimensions.Width",
"height": "Envelope.Request.Item.Dimensions.Height",
"weight": [["Envelope.Request.Item.Weight", null, function () {
return 10;
}]],
"weightUnits": [["Envelope.Request.Item.WeightUnits", null, function () {
return null;
}]],
"inventory.onHandQty": "Envelope.Request.Item.Inventory?",
"inventory.replenishQty": "Envelope.Request.Item.RelpenishQuantity?",
"inventory.isInventoryItem": { key: ["Envelope.Request.Item.OnInventory", null, "YES"] },
"price": ["Envelope.Request.Item.Price[].List", "Envelope.Request.Item.Price[].Value", "Test[]"],
"descriptions[0]": "Envelope.Request.Item.ShortDescription",
"descriptions[1]": "Envelope.Request.Item.LongDescription"
};
const expected = {
Test: [100],
Envelope: {
Request: {
Item: {
SKU: "12345",
UPC: "99999912345X",
ShortTitle: "Test Item",
Dimensions: {
Length: 5,
Width: 2,
Height: 8
},
Weight: 10,
Inventory: 0,
RelpenishQuantity: null,
OnInventory: "YES",
Price: [{
List: 100,
Value: 100
}],
ShortDescription: "Short description",
LongDescription: "Long description"
}
}
}
};
let result = merge(obj, {}, map);
(0, _code.expect)(result).to.equal(expected);
map.sku = {
key: "Envelope.Request.Item.SKU",
transform: function transform(val, objFrom, objTo) {
return "over-ridden-sku";
}
};
expected.Envelope.Request.Item.SKU = "over-ridden-sku";
result = merge(obj, {}, map);
(0, _code.expect)(result, "override sku").to.equal(expected);
obj["inventory"] = null;
expected.Envelope.Request.Item.Inventory = null;
result = merge(obj, {}, map);
(0, _code.expect)(result, "null inventory").to.equal(expected);
return done();
});
});

@@ -470,3 +470,3 @@ "use strict";

const expected = [];
const expected = null;

@@ -511,3 +511,3 @@ const map = createSut();

lab.test("A null parameter throws an error", done => {
lab.test("An undefined parameter does not throw an error", done => {
const map = createSut();

@@ -517,8 +517,14 @@

const throws = function throws() {
map.each(null);
};
(0, _code.expect)(map.each(undefined)).to.equal(null);
(0, _code.expect)(throws).to.throw();
return done();
});
lab.test("A null parameter does not throw an error", done => {
const map = createSut();
map("fieldName").to("field.name");
(0, _code.expect)(map.each(null)).to.equal(null);
return done();

@@ -977,100 +983,2 @@ });

});
lab.test.skip("original constious tests", done => {
// const merge = om.merge;
const obj = {
"sku": "12345",
"upc": "99999912345X",
"title": "Test Item",
"descriptions": ["Short description", "Long description"],
"length": 5,
"width": 2,
"height": 8,
"inventory": {
"onHandQty": 0,
"replenishQty": null
},
"price": 100
};
const mapper = createSut();
mapper.map("sku").to("Envelope.Request.Item.SKU").map("upc").to("Envelope.Request.Item.UPC").map("title").to("Envelope.Request.Item.ShortTitle").map("length").to("Envelope.Request.Item.Dimensions.Length").map("width").to("Envelope.Request.Item.Dimensions.Width").map("height").to("Envelope.Request.Item.Dimensions.Height").map("weight").to("Envelope.Request.Item.Weight", () => 10).map("weightUnits").to("Envelope.Request.Item.WeightUnits", () => null).map("inventory.onHandQty").to("Envelope.Request.Item.Inventory?").map("inventory.replenishQty").to("Envelope.Request.Item.RelpenishQuantity?").map("inventory.isInventoryItem").to("Envelope.Request.Item.OnInventory").map("price").to("Envelope.Request.Item.Price[].List").map("price").to("Envelope.Request.Item.Price[].Value").map("price").to("Test[]").map("descriptions[0]").to("Envelope.Request.Item.ShortDescription").map("descriptions[1]").to("Envelope.Request.Item.LongDescription");
const map = {
"sku": "Envelope.Request.Item.SKU",
"upc": "Envelope.Request.Item.UPC",
"title": "Envelope.Request.Item.ShortTitle",
"length": "Envelope.Request.Item.Dimensions.Length",
"width": "Envelope.Request.Item.Dimensions.Width",
"height": "Envelope.Request.Item.Dimensions.Height",
"weight": [["Envelope.Request.Item.Weight", null, function () {
return 10;
}]],
"weightUnits": [["Envelope.Request.Item.WeightUnits", null, function () {
return null;
}]],
"inventory.onHandQty": "Envelope.Request.Item.Inventory?",
"inventory.replenishQty": "Envelope.Request.Item.RelpenishQuantity?",
"inventory.isInventoryItem": { key: ["Envelope.Request.Item.OnInventory", null, "YES"] },
"price": ["Envelope.Request.Item.Price[].List", "Envelope.Request.Item.Price[].Value", "Test[]"],
"descriptions[0]": "Envelope.Request.Item.ShortDescription",
"descriptions[1]": "Envelope.Request.Item.LongDescription"
};
const expected = {
Test: [100],
Envelope: {
Request: {
Item: {
SKU: "12345",
UPC: "99999912345X",
ShortTitle: "Test Item",
Dimensions: {
Length: 5,
Width: 2,
Height: 8
},
Weight: 10,
Inventory: 0,
RelpenishQuantity: null,
OnInventory: "YES",
Price: [{
List: 100,
Value: 100
}],
ShortDescription: "Short description",
LongDescription: "Long description"
}
}
}
};
let result = merge(obj, {}, map);
(0, _code.expect)(result).to.equal(expected);
map.sku = {
key: "Envelope.Request.Item.SKU",
transform: (val, objFrom, objTo) => {
return "over-ridden-sku";
}
};
expected.Envelope.Request.Item.SKU = "over-ridden-sku";
result = merge(obj, {}, map);
(0, _code.expect)(result, "override sku").to.equal(expected);
obj.inventory = null;
expected.Envelope.Request.Item.Inventory = null;
result = merge(obj, {}, map);
(0, _code.expect)(result, "null inventory").to.equal(expected);
return done();
});
});

@@ -1077,0 +985,0 @@ });

@@ -7,123 +7,40 @@ "use strict";

var _code = require("code");
var _labSuite = require("lab-suite");
var _labTesting = require("lab-testing");
var labSuite = _interopRequireWildcard(_labSuite);
var _labTesting2 = _interopRequireDefault(_labTesting);
var _emptySourceSuite = require("./empty-source-suite");
var _labSuite = require("lab-suite");
var _emptySourceSuite2 = _interopRequireDefault(_emptySourceSuite);
var labSuite = _interopRequireWildcard(_labSuite);
var _mappingSuite = require("./mapping-suite");
var _mapFactory = require("../../lib/map-factory");
var _mappingSuite2 = _interopRequireDefault(_mappingSuite);
var _mapFactory2 = _interopRequireDefault(_mapFactory);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const suite = labSuite.create();
suite.expect("LABEL").to.be.a.string();
suite.expect("LABELS").to.be.an.array();
suite.expect("GET_ITEM").to.be.a.string();
suite.expect("SET_ITEM").to.be.a.string();
suite.expect("SOURCE").to.be.an.object();
suite.expect("EXPECTED").to.be.an.object();
suite.expect("NO_SOURCE_EXPECTED").to.be.an.object();
suite.expect("EXPECTED").to.be.anything();
suite.expect("NO_SOURCE_EXPECTED").to.be.an.anything();
suite.expect("MODIFY_VALUE").to.be.anything();
suite.expect("MODIFIED_EXPECTED").to.be.an.object();
suite.expect("MODIFIED_EXPECTED").to.be.an.anything();
suite.declare((lab, variables) => {
const testing = (0, _labTesting2.default)(lab);
const group = testing.createExperiment("map-factory", "notation");
variables.EXPERIMENTAL = true;
_mappingSuite2.default.run(lab, variables);
_emptySourceSuite2.default.run(lab, variables);
function createSut() {
return (0, _mapFactory2.default)({ experimental: true });
}
const LABEL = variables.LABEL,
GET_ITEM = variables.GET_ITEM,
SET_ITEM = variables.SET_ITEM,
SOURCE = variables.SOURCE,
EXPECTED = variables.EXPECTED,
NO_SOURCE_EXPECTED = variables.NO_SOURCE_EXPECTED,
MODIFY_VALUE = variables.MODIFY_VALUE,
MODIFIED_EXPECTED = variables.MODIFIED_EXPECTED;
group(`${LABEL}: when the source exists`, () => {
lab.test("can be selected and mapped to the target", done => {
const mapper = createSut();
const actual = mapper.map(GET_ITEM).to(SET_ITEM, value => value).execute(SOURCE);
(0, _code.expect)(actual).to.equal(EXPECTED);
return done();
});
lab.test("an array source can be selected and mapped to the target", done => {
const mapper = createSut();
const actual = mapper.map([GET_ITEM]).to(SET_ITEM, value => value).execute(SOURCE);
(0, _code.expect)(actual).to.equal(EXPECTED);
return done();
});
});
group(`${LABEL}: when source does not exist`, () => {
lab.test("the target field does not get created for a basic map", done => {
const mapper = createSut();
const actual = mapper.map(GET_ITEM).to(SET_ITEM).execute({});
(0, _code.expect)(actual).to.equal(NO_SOURCE_EXPECTED);
return done();
});
lab.test("the target field does get created with a modifying transform", done => {
const mapper = createSut();
const actual = mapper.map(GET_ITEM).to(SET_ITEM, () => MODIFY_VALUE).execute({});
(0, _code.expect)(actual).to.equal(MODIFIED_EXPECTED);
return done();
});
lab.test("the target field does not get created for an array source with a pass-through transform", done => {
const mapper = createSut();
const actual = mapper.map([GET_ITEM]).to(SET_ITEM, value => value).execute({});
(0, _code.expect)(actual).to.equal(NO_SOURCE_EXPECTED);
return done();
});
lab.test("the target field does get created for an array source with a modifying transform", done => {
const mapper = createSut();
const actual = mapper.map([GET_ITEM]).to(SET_ITEM, () => MODIFY_VALUE).execute({});
(0, _code.expect)(actual).to.equal(MODIFIED_EXPECTED);
return done();
});
});
variables.EXPERIMENTAL = false;
_mappingSuite2.default.run(lab, variables);
_emptySourceSuite2.default.run(lab, variables);
});
exports.default = suite;
{
"name": "map-factory",
"version": "1.6.2",
"description": "Object mapping tool",
"version": "1.6.3",
"description": "A simple object mapping utility that makes it easy to map data from one object to another. Create object mappers using fluent interface that supports deep references (dot notation), custom transformations, and object merging.",
"main": "./dist/lib/index.js",

@@ -39,2 +39,5 @@ "scripts": {

"mapper",
"dot-notation",
"recursive",
"keys",
"fluent",

@@ -61,3 +64,3 @@ "transform"

"lab": "^13.0.1",
"lab-suite": "^1.2.0",
"lab-suite": "^1.3.0",
"lab-testing": "^2.1.0",

@@ -64,0 +67,0 @@ "rimraf": "^2.6.0"

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc