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 2.0.0 to 2.0.1

35

CHANGELOG.md

@@ -0,1 +1,36 @@

### 2.0.0
In ```v1``` the default behaviour was the equivalent of setting the options as ```{alwaysTransform: true, alwaysSet: true}```. In ```v2``` the default behaviour is now the equivalent of setting the options as ```{alwaysTransform: false, alwaysSet: false}```.
We feel these are generally better defaults. This is a **breaking** change.
If you prefer the ```v1``` behaviour you just need to change your code as follows:
#### Old Code
```js
const mapper = createMapper();
```
#### New Code
```js
const options = {
alwaysTransform: true,
alwaysSet: true
};
const mapper = createMapper(options);
```
Additionally, the ```v1``` behaviour would consider a null an acceptable value if a ```?``` was appended to the ```to()``` field. In ```v2``` this behaviour will only work in combination with the ```always``` flag.
#### Old Code
```js
map("foo.bar").to("bar.bar?");
```
#### New Code
```js
map("foo.bar").always.to("bar.bar?");
```
### 1.7.2

@@ -2,0 +37,0 @@

5

dist/lib/map-factory.js

@@ -22,5 +22,4 @@ "use strict";

// v1 will default as below but the reverse will be true in v2
opts.alwaysSet = typeof opts.alwaysSet === "boolean" ? opts.alwaysSet : true;
opts.alwaysTransform = typeof opts.alwaysTransform === "boolean" ? opts.alwaysTransform : true;
opts.alwaysSet = typeof opts.alwaysSet === "boolean" ? opts.alwaysSet : false;
opts.alwaysTransform = typeof opts.alwaysTransform === "boolean" ? opts.alwaysTransform : false;
opts.experimental = typeof opts.experimental === "boolean" ? opts.experimental : false;

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

@@ -20,2 +20,4 @@ "use strict";

const isValueArray = new RegExp(/^\[\]|\[\d+\]/);
class Mapper {

@@ -256,5 +258,13 @@

if (this.isEmptyObject_(destinationObject) && isValueArray.exec(targetPath) !== null) {
return [];
}
return destinationObject;
}
isEmptyObject_(object) {
return typeof object === "object" && Array.isArray(object) === false && Object.keys(object).length === 0;
}
exists_(value) {

@@ -261,0 +271,0 @@ return value !== null && value !== undefined;

6

dist/test/arrays-test.js

@@ -33,7 +33,3 @@ "use strict";

},
NO_SOURCE_EXPECTED: {
array: {
levels: []
}
},
NO_SOURCE_EXPECTED: {},
MODIFY_VALUE: "modified",

@@ -40,0 +36,0 @@ MODIFIED_EXPECTED: {

@@ -35,5 +35,3 @@ "use strict";

},
NO_SOURCE_EXPECTED: {
field: {}
},
NO_SOURCE_EXPECTED: {},
MODIFY_VALUE: "modified",

@@ -40,0 +38,0 @@ MODIFIED_EXPECTED: {

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

(0, _code.expect)(mapping).to.be.an.object();
(0, _code.expect)(mapping.alwaysSet).to.be.true();
(0, _code.expect)(mapping.alwaysTransform).to.be.true();
(0, _code.expect)(mapping.alwaysSet).to.be.false();
(0, _code.expect)(mapping.alwaysTransform).to.be.false();

@@ -56,4 +56,4 @@ return done();

(0, _code.expect)(mapping).to.be.an.object();
(0, _code.expect)(mapping.alwaysSet).to.be.true();
(0, _code.expect)(mapping.alwaysTransform).to.be.true();
(0, _code.expect)(mapping.alwaysSet).to.be.false();
(0, _code.expect)(mapping.alwaysTransform).to.be.false();

@@ -65,3 +65,3 @@ return done();

const map = (0, _index2.default)({ alwaysTransform: false });
const map = (0, _index2.default)({ alwaysTransform: true });

@@ -71,4 +71,4 @@ const mapping = map("a");

(0, _code.expect)(mapping).to.be.an.object();
(0, _code.expect)(mapping.alwaysSet).to.be.true();
(0, _code.expect)(mapping.alwaysTransform).to.be.false();
(0, _code.expect)(mapping.alwaysSet).to.be.false();
(0, _code.expect)(mapping.alwaysTransform).to.be.true();

@@ -80,3 +80,3 @@ return done();

const map = (0, _index2.default)({ alwaysSet: false });
const map = (0, _index2.default)({ alwaysSet: true });

@@ -86,4 +86,4 @@ const mapping = map("a");

(0, _code.expect)(mapping).to.be.an.object();
(0, _code.expect)(mapping.alwaysSet).to.be.false();
(0, _code.expect)(mapping.alwaysTransform).to.be.true();
(0, _code.expect)(mapping.alwaysSet).to.be.true();
(0, _code.expect)(mapping.alwaysTransform).to.be.false();

@@ -90,0 +90,0 @@ return done();

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

lab.test("the target field does get created with a modifying transform", done => {
lab.test("the target field does not get created with a modifying transform", done => {

@@ -78,2 +78,13 @@ const mapper = createSut();

(0, _code.expect)(actual).to.equal(NO_SOURCE_EXPECTED);
return done();
});
lab.test("the target field does get created with a modifying transform with always flag", done => {
const mapper = createSut();
const actual = mapper.map(GET_ITEM).always.to(SET_ITEM, () => MODIFY_VALUE).execute({});
(0, _code.expect)(actual).to.equal(MODIFIED_EXPECTED);

@@ -95,7 +106,7 @@

lab.test("the target field does get created for an array source with a modifying transform", done => {
lab.test("the target field does get created for an array source with a modifying transform with always flag", done => {
const mapper = createSut();
const actual = mapper.map([GET_ITEM]).to(SET_ITEM, () => MODIFY_VALUE).execute({});
const actual = mapper.map([GET_ITEM]).always.to(SET_ITEM, () => MODIFY_VALUE).execute({});

@@ -106,2 +117,13 @@ (0, _code.expect)(actual).to.equal(MODIFIED_EXPECTED);

});
lab.test("the target field does not 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(NO_SOURCE_EXPECTED);
return done();
});
});

@@ -108,0 +130,0 @@ });

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

map("foo.bar").to("bar.bar?");
map("foo.bar").always.to("bar.bar?");
map("a").to("foo.a");

@@ -941,0 +941,0 @@

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

_notationSuite2.default.run(lab, {
LABELS: ["value arrays", "single mapping array"],
LABELS: ["value arrays", "single mapping array specifying index"],
GET_ITEM: "item",

@@ -35,2 +35,15 @@ SET_ITEM: "[0]",

_notationSuite2.default.run(lab, {
LABELS: ["value arrays", "single mapping array"],
GET_ITEM: "item",
SET_ITEM: "[]",
SOURCE: {
item: 123
},
EXPECTED: [123],
NO_SOURCE_EXPECTED: [],
MODIFY_VALUE: "modified",
MODIFIED_EXPECTED: ["modified"]
});
// size is missing from source

@@ -37,0 +50,0 @@ const labels = ["value arrays", "mix of available and missing data"];

{
"name": "map-factory",
"version": "2.0.0",
"version": "2.0.1",
"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.",

@@ -5,0 +5,0 @@ "main": "./dist/lib/index.js",

@@ -65,4 +65,4 @@ # map-factory

- always call a transform if one has been provided even if no source value was found in the source object.
- always create a nested structure even if no source value was found on the source object.
- only call a transform (if one has been provided) if a source value was found in the source object.
- only create a nested structure if a source value was found on the source object.

@@ -73,9 +73,9 @@ While this default is good some use cases it is not suitable for all use cases. To cater for this, behaviour modifiers are available.

The ```createMapper()``` function now takes an (optional) options parameter to change the default behaviour.
The ```createMapper()``` function takes an (optional) options parameter that will override the default behaviour.
- **alwaysTransform: boolean**
- **alwaysTransform: boolean**
- if *true* then a transform will always be called even if the source value was not available on the source object.
- if *false* then a transform will only be called if the source value was available on the source object.
- **alwaysSet: boolean**
- **alwaysSet: boolean**
- if *true* then nested structure will be created even if the source value was not available on the source object.

@@ -88,4 +88,4 @@ - if *false* then structure will only be create if the source value was available on the source object.

const options = {
alwaysTransform: false,
alwaysSet: false
alwaysTransform: true,
alwaysSet: true
};

@@ -97,3 +97,3 @@

Additionally, you can also modify the behaviour on an individual mapping by using the ```always``` and ```existing``` modifier on the mapping like this:
Additionally, you can also modify the behaviour on an individual mapping by using the ```always``` or ```existing``` modifiers on the mapping like this:

@@ -103,3 +103,3 @@ ```js

const mapper = createMapper(options);
const mapper = createMapper();

@@ -106,0 +106,0 @@ mapper

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