@teleology/fp
Advanced tools
Comparing version 1.0.17 to 2.0.0
@@ -13,9 +13,17 @@ "use strict"; | ||
return t === `String` && v.length === 0 || t === `Object` && Object.keys(v).length === 0 || t === `Array` && v.length === 0 || v === null || v === undefined; | ||
}; | ||
exports.empty = empty; | ||
const coerce = v => { | ||
if (Array.isArray(v)) { | ||
return v.filter(it => !empty(it)); | ||
} | ||
return v; | ||
}; // Remove all undefined, null, empty strings, empty object and empty array values | ||
exports.empty = empty; | ||
const clean = src => JSON.parse(JSON.stringify(src), (k, v) => empty(v) ? undefined : coerce(v)); | ||
const clean = src => JSON.parse(JSON.stringify(src), (k, v) => empty(v) ? undefined : v); | ||
exports.clean = clean; |
@@ -6,7 +6,9 @@ "use strict"; | ||
}); | ||
exports.pick = void 0; | ||
exports.pick = exports.get = void 0; | ||
var _dot = require("./dot"); | ||
const pick = (path, def) => obj => { | ||
var _clean = require("./clean"); | ||
const get = (path, def) => obj => { | ||
try { | ||
@@ -19,2 +21,51 @@ return (0, _dot.dot)(path).reduce((a, b) => a[b], obj); | ||
exports.get = get; | ||
const pick = (paths, options = {}) => src => { | ||
const output = Array.isArray(src) ? [] : {}; | ||
for (let j = 0; j < paths.length; j++) { | ||
const originalPath = paths[j]; | ||
const path = (0, _dot.dot)(originalPath); | ||
const value = get(originalPath)(src); // don't try to find something that doesn't exist | ||
if (!path[0].match(/\d/g) && !src.hasOwnProperty(path[0])) { | ||
continue; | ||
} | ||
let target = output; | ||
for (let i = 0; i < path.length; i++) { | ||
const part = path[i]; | ||
const next = path[i + 1]; // already defined and not at the end | ||
if (target[part]) { | ||
target = target[part]; | ||
continue; | ||
} // next part is array | ||
if (next && next.match(/\d/g)) { | ||
target[part] = []; | ||
target = target[part]; | ||
continue; | ||
} | ||
if (next) { | ||
target[part] = {}; | ||
target = target[part]; | ||
} | ||
} | ||
const last = path[path.length - 1]; | ||
target[last] = value; | ||
} | ||
if (options.clean) { | ||
return (0, _clean.clean)(output); | ||
} | ||
return output; | ||
}; | ||
exports.pick = pick; |
{ | ||
"name": "@teleology/fp", | ||
"version": "1.0.17", | ||
"version": "2.0.0", | ||
"description": "A small collection of functional programming utils", | ||
@@ -41,3 +41,6 @@ "author": "Chris Sullivan <chrissullivan.dev@gmail.com>", | ||
"prettier": "^2.0.5" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.17.21" | ||
} | ||
} |
@@ -86,3 +86,3 @@ # @teleology/fp | ||
## pick | ||
## get | ||
@@ -93,5 +93,5 @@ Curry a dot notation path and default value, returns an invocable function requiring a target object. | ||
```javascript | ||
const { pick } = require('@teleology/fp'); | ||
const { get } = require('@teleology/fp'); | ||
pick('[0].a.b')([ | ||
get('[0].a.b')([ | ||
{ | ||
@@ -105,3 +105,28 @@ a: { | ||
## pick | ||
Curry an array of dot notation paths, with optional settings. Returns an invocable function requiring a target object. | ||
Example: | ||
```javascript | ||
const { pick } = require('@teleology/fp'); | ||
pick(['a.b', 'a.c[0]', 'a.c[2]'])({ | ||
a: { | ||
b: 'hi', | ||
c: [1, 2, 3], | ||
}, | ||
}); // { a: { b: 'hi', c: [ 1, <1 empty item>, 3 ] } } | ||
// Clear out missing values | ||
pick(['a.b', 'a.c[0]', 'a.c[2]'], { clean: true })({ | ||
a: { | ||
b: 'hi', | ||
c: [1, 2, 3], | ||
}, | ||
}); // { a: { b: 'hi', c: [ 1, 3 ] } } | ||
``` | ||
## map | ||
@@ -290,2 +315,6 @@ | ||
**2.0.0** | ||
- Changed how `pick` operates, to migrate use `get` which is now exposed | ||
- Updated `clean` to remove empty array items | ||
**1.0.14** | ||
@@ -292,0 +321,0 @@ - Added `noop` function |
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
20364
405
337
1
+ Addedlodash@^4.17.21
+ Addedlodash@4.17.21(transitive)