Comparing version 2.2.0 to 2.2.1
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://homer0.github.io/wootils/", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"repository": "homer0/wootils", | ||
@@ -8,0 +8,0 @@ "author": "Leonardo Apiwan (@homer0) <me@homer0.com>", |
@@ -58,3 +58,3 @@ const extend = require('extend'); | ||
*/ | ||
static get(target, objPath, pathDelimiter = '.', failWithError = true) { | ||
static get(target, objPath, pathDelimiter = '.', failWithError = false) { | ||
const parts = objPath.split(pathDelimiter); | ||
@@ -110,3 +110,3 @@ const first = parts.shift(); | ||
pathDelimiter = '.', | ||
failWithError = true | ||
failWithError = false | ||
) { | ||
@@ -168,5 +168,5 @@ let result = this.copy(target); | ||
* // Will output { name: 'Rosario', age: 3, address: { planet: 'earth' } } | ||
* @param {Object} target The object from where the | ||
* @param {Object} target The object from where the | ||
* property/properties will be extracted. | ||
* @param {Array|Object|string} objPaths This can be a single path or a list of | ||
* @param {Array|Object|string} objPaths This can be a single path or a list of | ||
* them. And for this method, the paths are | ||
@@ -177,5 +177,5 @@ * not only strings but can also be an object | ||
* value the path on the target object. | ||
* @param {string} [pathDelimiter='.'] The delimiter that will separate the | ||
* @param {string} [pathDelimiter='.'] The delimiter that will separate the | ||
* path components. | ||
* @param {boolean} [failWithError=true] Whether or not to throw an error when the | ||
* @param {boolean} [failWithError=false] Whether or not to throw an error when the | ||
* path is invalid. If this is `false`, the | ||
@@ -185,3 +185,3 @@ * method will silently fail an empty object. | ||
*/ | ||
static extract(target, objPaths, pathDelimiter = '.', failWithError = true) { | ||
static extract(target, objPaths, pathDelimiter = '.', failWithError = false) { | ||
const copied = this.copy(target); | ||
@@ -210,11 +210,11 @@ let result = {}; | ||
const value = this.get(copied, pathInfo.origin, pathDelimiter, failWithError); | ||
if (typeof value === 'undefined') { | ||
breakLoop = true; | ||
} else if (pathInfo.customDest) { | ||
result = this.set(result, pathInfo.dest, value, pathDelimiter, failWithError); | ||
if (typeof result === 'undefined') { | ||
breakLoop = true; | ||
if (typeof value !== 'undefined') { | ||
if (pathInfo.customDest) { | ||
result = this.set(result, pathInfo.dest, value, pathDelimiter, failWithError); | ||
if (typeof result === 'undefined') { | ||
breakLoop = true; | ||
} | ||
} else { | ||
result[pathInfo.dest] = value; | ||
} | ||
} else { | ||
result[pathInfo.dest] = value; | ||
} | ||
@@ -251,3 +251,3 @@ | ||
* object is found. | ||
* @param {boolean} [failWithError=true] Whether or not to throw an error when the path | ||
* @param {boolean} [failWithError=false] Whether or not to throw an error when the path | ||
* is invalid. If this is `false`, the method will | ||
@@ -262,3 +262,3 @@ * silently fail. | ||
cleanEmptyProperties = true, | ||
failWithError = true | ||
failWithError = false | ||
) { | ||
@@ -265,0 +265,0 @@ const parts = objPath.split(pathDelimiter); |
@@ -116,49 +116,49 @@ jest.unmock('/shared/objectUtils'); | ||
it('should throw an error when trying to read a property that doesn\'t exist', () => { | ||
it('shouldn\'t throw an error when trying to read a property that doesn\'t exist', () => { | ||
// Given | ||
const target = {}; | ||
const fakePath = 'something'; | ||
// When/Then | ||
expect(() => ObjectUtils.get(target, fakePath)) | ||
.toThrow(new RegExp(`there's nothing on '${fakePath}'`, 'i')); | ||
let result = null; | ||
// When | ||
result = ObjectUtils.get(target, fakePath); | ||
// Then | ||
expect(result).toBeUndefined(); | ||
}); | ||
it('should throw an error when trying to read a path that doesn\'t exist', () => { | ||
it('shouldn\'t throw an error when trying to read a path that doesn\'t exist', () => { | ||
// Given | ||
const topElement = 'person'; | ||
const childElement = 'name'; | ||
const grandChildElement = 'first'; | ||
const target = { | ||
[topElement]: {}, | ||
}; | ||
const fakePath = `${topElement}.${childElement}.${grandChildElement}`; | ||
const fakePath = `${topElement}.${childElement}`; | ||
let result = null; | ||
// When | ||
result = ObjectUtils.get(target, fakePath, '.', false); | ||
// When/Then | ||
expect(() => ObjectUtils.get(target, fakePath)) | ||
.toThrow(new RegExp(`there's nothing on '${topElement}.${childElement}'`, 'i')); | ||
expect(result).toBeUndefined(); | ||
}); | ||
it('shouldn\'t throw an error when trying to read a property that doesn\'t exist', () => { | ||
it('should throw an error when trying to read a property that doesn\'t exist', () => { | ||
// Given | ||
const target = {}; | ||
const fakePath = 'something'; | ||
let result = null; | ||
// When | ||
result = ObjectUtils.get(target, fakePath, '.', false); | ||
// Then | ||
expect(result).toBeUndefined(); | ||
// When/Then | ||
expect(() => ObjectUtils.get(target, fakePath, '.', true)) | ||
.toThrow(new RegExp(`there's nothing on '${fakePath}'`, 'i')); | ||
}); | ||
it('shouldn\'t throw an error when trying to read a path that doesn\'t exist', () => { | ||
it('should throw an error when trying to read a path that doesn\'t exist', () => { | ||
// Given | ||
const topElement = 'person'; | ||
const childElement = 'name'; | ||
const grandChildElement = 'first'; | ||
const target = { | ||
[topElement]: {}, | ||
}; | ||
const fakePath = `${topElement}.${childElement}`; | ||
let result = null; | ||
// When | ||
result = ObjectUtils.get(target, fakePath, '.', false); | ||
const fakePath = `${topElement}.${childElement}.${grandChildElement}`; | ||
// When/Then | ||
expect(result).toBeUndefined(); | ||
expect(() => ObjectUtils.get(target, fakePath, '.', true)) | ||
.toThrow(new RegExp(`there's nothing on '${topElement}.${childElement}'`, 'i')); | ||
}); | ||
@@ -260,3 +260,3 @@ }); | ||
it('should throw an error when trying to set a property on an non object path', () => { | ||
it('shouldn\'t throw an error when trying to set a property on an non object path', () => { | ||
// Given | ||
@@ -273,11 +273,10 @@ const topElement = 'people'; | ||
const objPath = `${topElement}.${childElement}.${grandChildElement}`; | ||
// When/Then | ||
expect(() => ObjectUtils.set(target, objPath, value)) | ||
.toThrow(new RegExp( | ||
`There's already an element of type 'string' on '${topElement}.${childElement}'`, | ||
'i' | ||
)); | ||
let result = null; | ||
// When | ||
result = ObjectUtils.set(target, objPath, value); | ||
// Then | ||
expect(result).toBeUndefined(); | ||
}); | ||
it('shouldn\'t throw an error when trying to set a property on an non object path', () => { | ||
it('should throw an error when trying to set a property on an non object path', () => { | ||
// Given | ||
@@ -294,7 +293,8 @@ const topElement = 'people'; | ||
const objPath = `${topElement}.${childElement}.${grandChildElement}`; | ||
let result = null; | ||
// When | ||
result = ObjectUtils.set(target, objPath, value, '.', false); | ||
// Then | ||
expect(result).toBeUndefined(); | ||
// When/Then | ||
expect(() => ObjectUtils.set(target, objPath, value, '.', true)) | ||
.toThrow(new RegExp( | ||
`There's already an element of type 'string' on '${topElement}.${childElement}'`, | ||
'i' | ||
)); | ||
}); | ||
@@ -381,23 +381,23 @@ }); | ||
it('should throw an error when trying to exact from an invalid path', () => { | ||
it('shouldn\'t throw an error when trying to extract from an invalid path', () => { | ||
// Given | ||
const topElement = 'person'; | ||
const target = {}; | ||
// When/Then | ||
expect(() => ObjectUtils.extract(target, topElement)) | ||
.toThrow(new RegExp(`There's nothing on '${topElement}'`, 'i')); | ||
let result = null; | ||
// When | ||
result = ObjectUtils.extract(target, topElement); | ||
// Then | ||
expect(result).toEqual({}); | ||
}); | ||
it('shouldn\'t throw an error when trying to exact from an invalid path', () => { | ||
it('should throw an error when trying to extract from an invalid path', () => { | ||
// Given | ||
const topElement = 'person'; | ||
const target = {}; | ||
let result = null; | ||
// When | ||
result = ObjectUtils.extract(target, topElement, '.', false); | ||
// Then | ||
expect(result).toEqual({}); | ||
// When/Then | ||
expect(() => ObjectUtils.extract(target, topElement, '.', true)) | ||
.toThrow(new RegExp(`There's nothing on '${topElement}'`, 'i')); | ||
}); | ||
it('should throw an error when trying to exact reusing a path', () => { | ||
it('shouldn\'t throw an error when trying to extract reusing a path', () => { | ||
// Given | ||
@@ -414,14 +414,16 @@ const topElement = 'person'; | ||
}; | ||
// When/Then | ||
expect(() => ObjectUtils.extract(target, [ | ||
{ [topElement]: `${topElement}.${childElement}` }, | ||
{ [`${topElement}.${childElement}`]: `${topElement}.${childElement}` }, | ||
])) | ||
.toThrow(new RegExp( | ||
`There's already an element of type 'string' on '${topElement}'`, | ||
'i' | ||
)); | ||
let result = null; | ||
// When | ||
result = ObjectUtils.extract( | ||
target, | ||
[ | ||
{ [topElement]: `${topElement}.${childElement}` }, | ||
{ [`${topElement}.${childElement}`]: `${topElement}.${childElement}` }, | ||
] | ||
); | ||
// Then | ||
expect(result).toBeUndefined(); | ||
}); | ||
it('shouldn\'t throw an error when trying to exact reusing a path', () => { | ||
it('should throw an error when trying to extract reusing a path', () => { | ||
// Given | ||
@@ -438,5 +440,4 @@ const topElement = 'person'; | ||
}; | ||
let result = null; | ||
// When | ||
result = ObjectUtils.extract( | ||
// When/Then | ||
expect(() => ObjectUtils.extract( | ||
target, | ||
@@ -448,6 +449,8 @@ [ | ||
'.', | ||
false | ||
); | ||
// Then | ||
expect(result).toBeUndefined(); | ||
true | ||
)) | ||
.toThrow(new RegExp( | ||
`There's already an element of type 'string' on '${topElement}'`, | ||
'i' | ||
)); | ||
}); | ||
@@ -454,0 +457,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
538293
6855