map-factory
Advanced tools
Comparing version 2.3.1 to 2.4.0
@@ -0,1 +1,35 @@ | ||
### 2.4.0 | ||
Added the ```removing()``` method that allows you to specify which fields you don't want instead of explicitly stating the fields you do want. | ||
```js | ||
const mapper = createMapper(); | ||
const src = { | ||
user: { | ||
name: "Tim", | ||
occupation: "Enchanter", | ||
password: "scary bunny" | ||
} | ||
}; | ||
mapper | ||
.map("user").removing("password").to("user"); | ||
.execute(src); | ||
/* | ||
The expected result is: | ||
{ | ||
user: { | ||
name: "Tim", | ||
occupation: "Enchanter" | ||
} | ||
} | ||
*/ | ||
``` | ||
### 2.3.1 | ||
@@ -2,0 +36,0 @@ |
@@ -17,2 +17,4 @@ "use strict"; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -186,3 +188,4 @@ | ||
alwaysSet = item.alwaysSet, | ||
alwaysTransform = item.alwaysTransform; | ||
alwaysTransform = item.alwaysTransform, | ||
pipelineTransformations = item.pipelineTransformations; | ||
@@ -206,3 +209,3 @@ var isCustomTransform = true; | ||
return { mode: mode, targetPath: targetPath, sourcePath: sourcePath, transform: transform, isCustomTransform: isCustomTransform, options: { alwaysSet: alwaysSet, alwaysTransform: alwaysTransform } }; | ||
return { mode: mode, targetPath: targetPath, sourcePath: sourcePath, transform: transform, isCustomTransform: isCustomTransform, options: { alwaysSet: alwaysSet, alwaysTransform: alwaysTransform, pipelineTransformations: pipelineTransformations } }; | ||
} | ||
@@ -237,2 +240,9 @@ }, { | ||
// default transformations | ||
if (this.exists_(value) && options.pipelineTransformations.length > 0) { | ||
options.pipelineTransformations.map(function (item) { | ||
value = item(value); | ||
}); | ||
} | ||
// Apply transform - will become optional | ||
@@ -298,5 +308,12 @@ if (this.exists_(value) || options.alwaysTransform === true) { | ||
// default transformations | ||
if (anyValues && options.pipelineTransformations.length > 0) { | ||
options.pipelineTransformations.map(function (item) { | ||
values = item(values); | ||
}); | ||
} | ||
// Apply transform if appropriate | ||
if (anyValues || options.alwaysTransform === true) { | ||
value = transform.apply(undefined, values); | ||
value = transform.apply(undefined, _toConsumableArray(values)); | ||
} | ||
@@ -336,3 +353,3 @@ | ||
// no transform | ||
// default transformations | ||
} catch (err) { | ||
@@ -353,2 +370,9 @@ _didIteratorError3 = true; | ||
if (this.exists_(orValue) && options.pipelineTransformations.length > 0) { | ||
options.pipelineTransformations.map(function (item) { | ||
orValue = item(orValue); | ||
}); | ||
} | ||
// no transform | ||
if (isCustomTransform === false) { | ||
@@ -355,0 +379,0 @@ |
@@ -18,2 +18,3 @@ import { IMapping, IMapFactory } from "./interfaces"; | ||
existing: IMapping; | ||
removing(keys: string | string[]): IMapping; | ||
} |
@@ -7,4 +7,16 @@ "use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _lodash = require("lodash.clonedeep"); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _lodash3 = require("lodash.unset"); | ||
var _lodash4 = _interopRequireDefault(_lodash3); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -25,2 +37,3 @@ | ||
this.alwaysTransform = options.alwaysTransform; | ||
this.pipelineTransformations = []; | ||
} | ||
@@ -105,2 +118,61 @@ | ||
}, { | ||
key: "removing", | ||
value: function removing(keys) { | ||
var _this = this; | ||
if (Array.isArray(keys) && keys.length > 0) { | ||
keys.map(function (key) { | ||
if (typeof key !== "string") { | ||
throw new Error("The removing method requires a string value or an array of strings"); | ||
} | ||
}); | ||
} | ||
if (Array.isArray(keys) === false && typeof keys !== "string") { | ||
throw new Error("The removing method requires a string value or an array of strings"); | ||
} | ||
this.pipelineTransformations.push(function (value) { | ||
var valueToUse = (0, _lodash2.default)(value); | ||
if ((typeof valueToUse === "undefined" ? "undefined" : _typeof(valueToUse)) !== "object" && !Array.isArray(valueToUse)) { | ||
return valueToUse; | ||
} | ||
var isArray = Array.isArray(valueToUse) && valueToUse.length > 0; | ||
if (isArray) { | ||
valueToUse = valueToUse.map(function (val) { | ||
if ((typeof val === "undefined" ? "undefined" : _typeof(val)) !== "object" && !Array.isArray(val)) { | ||
return val; | ||
} | ||
return _this.processRemoving_(keys, val); | ||
}); | ||
} else { | ||
valueToUse = _this.processRemoving_(keys, valueToUse); | ||
} | ||
return valueToUse; | ||
}); | ||
return this; | ||
} | ||
}, { | ||
key: "processRemoving_", | ||
value: function processRemoving_(keys, val) { | ||
if (Array.isArray(keys) && keys.length > 0) { | ||
keys.map(function (key) { | ||
(0, _lodash4.default)(val, key); | ||
}); | ||
return val; | ||
} | ||
if (typeof keys === "string") { | ||
(0, _lodash4.default)(val, keys); | ||
return val; | ||
} | ||
} | ||
}, { | ||
key: "always", | ||
@@ -107,0 +179,0 @@ get: function get() { |
{ | ||
"name": "map-factory", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"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.", | ||
@@ -70,3 +70,7 @@ "main": "./dist/lib/index.js", | ||
"dist" | ||
] | ||
], | ||
"dependencies": { | ||
"lodash.clonedeep": "^4.5.0", | ||
"lodash.unset": "^4.5.2" | ||
} | ||
} |
@@ -306,2 +306,33 @@ # map-factory | ||
``` | ||
Implicit mapping behaviour can be achieved using the ```removing()``` method. Removing can take either a single field name or an array of field names. | ||
```js | ||
const mapper = createMapper(); | ||
const src = { | ||
user: { | ||
name: "Tim", | ||
occupation: "Enchanter", | ||
password: "scary bunny" | ||
} | ||
}; | ||
mapper | ||
.map("user").removing("password").to("user"); | ||
.execute(src); | ||
/* | ||
The expected result is: | ||
{ | ||
user: { | ||
name: "Tim", | ||
occupation: "Enchanter" | ||
} | ||
} | ||
*/ | ||
``` | ||
### Working with arrays | ||
@@ -308,0 +339,0 @@ You can use ```[]``` to traverse the entries in an array. For example, here you can transform an array of objects to an array of strings. |
189905
37
4741
795
2
+ Addedlodash.clonedeep@^4.5.0
+ Addedlodash.unset@^4.5.2
+ Addedlodash.clonedeep@4.5.0(transitive)
+ Addedlodash.unset@4.5.2(transitive)