@blakek/deep
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -1,12 +0,8 @@ | ||
export declare type Path = string | Array<string | number>; | ||
export declare type ObjectLike = Record<string | number, any>; | ||
export declare function isObject(object: unknown): object is ObjectLike; | ||
export declare function clone<T extends unknown>(value: T): T; | ||
export declare function traverseObject(object: unknown, path: string[]): unknown; | ||
export declare const get: import("@blakek/curry").Curry2<Path, ObjectLike, unknown>; | ||
export declare const getOr: import("@blakek/curry").Curry3<unknown, Path, ObjectLike, unknown>; | ||
export declare const has: import("@blakek/curry").Curry2<Path, ObjectLike, boolean>; | ||
export declare const omit: import("@blakek/curry").Curry2<Path[], ObjectLike, ObjectLike>; | ||
export declare const pluck: import("@blakek/curry").Curry2<Path[], ObjectLike, unknown>; | ||
export declare const remove: import("@blakek/curry").Curry2<Path, any, Partial<any>>; | ||
export declare const set: import("@blakek/curry").Curry3<unknown, Path, ObjectLike, ObjectLike>; | ||
export * from './clone'; | ||
export * from './get'; | ||
export * from './has'; | ||
export * from './omit'; | ||
export * from './pluck'; | ||
export * from './remove'; | ||
export * from './set'; | ||
export * from './shared'; |
@@ -1,2 +0,138 @@ | ||
import{curry as r}from"@blakek/curry";import{parse as n}from"pathington";var t=Symbol("curriable placeholder");function e(r){if(null===r)return!1;var n=typeof r;return"object"===n||"function"===n}function u(r){if(r instanceof Date)return new Date(r.getTime());if(r instanceof Map){var n=new Map;return r.forEach(function(r,t){n.set(t,u(r))}),n}if(r instanceof RegExp)return new RegExp(r.source,r.flags);if(r instanceof Set){var t=new Set;return r.forEach(function(r){t.add(u(r))}),t}if("object"==typeof r){if(null===r)return null;var e=Array.isArray(r)?[]:{};for(var i in r)e[i]=u(r[i]);return e}return r}function i(r,n){if(0===n.length)return r;if(!e(r))return t;var u=n[0],o=n.slice(1);return u in r?i(r[u],o):t}function o(r,e,u){if(void 0===e)return u;var o=i(u,n(e));return o===t||void 0===o?r:o}var f=function(r,n){return o(void 0,r,n)};function a(r,t,u){var i=n(t),o=u;return i.forEach(function(n,t){t===i.length-1?o[n]=r:(e(o[n])||(o[n]={}),o=o[n])}),u}var c=r(f),l=r(o),v=r(function(r,e){return i(e,n(r))!==t}),p=r(function(r,n){var t=u(n);return r.forEach(function(r){return d(r,t)}),t}),s=r(function(r,n){return r.reduce(function(r,t){return a(f(t,n),t,r)},{})}),d=r(function(r,t){if(void 0===r)return t;var u=n(r),o=u.slice(0,-1),f=u[u.length-1],a=i(t,n(o));return e(a)&&delete a[f],t}),h=r(a);export{u as clone,c as get,l as getOr,v as has,e as isObject,p as omit,s as pluck,d as remove,h as set,i as traverseObject}; | ||
import { parse } from 'pathington'; | ||
export { parse as parsePath } from 'pathington'; | ||
function clone(value) { | ||
if (value instanceof Date) { | ||
return new Date(value.getTime()); | ||
} | ||
if (value instanceof Map) { | ||
var result_1 = new Map(); | ||
value.forEach(function (nestedValue, key) { | ||
result_1.set(key, clone(nestedValue)); | ||
}); | ||
return result_1; | ||
} | ||
if (value instanceof RegExp) { | ||
return new RegExp(value.source, value.flags); | ||
} | ||
if (value instanceof Set) { | ||
var result_2 = new Set(); | ||
value.forEach(function (x) { | ||
result_2.add(clone(x)); | ||
}); | ||
return result_2; | ||
} | ||
if (typeof value === 'object') { | ||
if (value === null) { | ||
return null; | ||
} | ||
var result = (Array.isArray(value) ? [] : {}); | ||
for (var key in value) { | ||
result[key] = clone(value[key]); | ||
} | ||
return result; | ||
} | ||
return value; | ||
} | ||
var NotFound = Symbol('value was not found'); | ||
function isObject(object) { | ||
if (object === null) { | ||
return false; | ||
} | ||
var type = typeof object; | ||
return type === 'object' || type === 'function'; | ||
} | ||
function traverseObject(object, path) { | ||
// If the path has been exhausted, return the current object | ||
if (path.length === 0) { | ||
return object; | ||
} | ||
// If the value could not be found, return `defaultValue` | ||
if (!isObject(object)) { | ||
return NotFound; | ||
} | ||
var key = path[0], keys = path.slice(1); | ||
// Search deeper in the object | ||
if (key in object) { | ||
return traverseObject(object[key], keys); | ||
} | ||
// The key was not found in the object. | ||
return NotFound; | ||
} | ||
function get(path, object, fallbackValue) { | ||
var parsedPath = parse(path); | ||
var value = traverseObject(object, parsedPath); | ||
if (value === NotFound || value === undefined) { | ||
return fallbackValue; | ||
} | ||
return value; | ||
} | ||
function createGetter(path, fallbackValue) { | ||
return function (object) { return get(path, object, fallbackValue); }; | ||
} | ||
function has(path, object) { | ||
var value = traverseObject(object, parse(path)); | ||
return value !== NotFound; | ||
} | ||
function createHas(path) { | ||
return function (object) { return has(path, object); }; | ||
} | ||
function remove(path, object) { | ||
if (path === undefined) { | ||
return object; | ||
} | ||
var parsedPath = parse(path); | ||
var referencePath = parsedPath.slice(0, -1); | ||
var finalPath = parsedPath[parsedPath.length - 1]; | ||
var reference = traverseObject(object, parse(referencePath)); | ||
if (isObject(reference)) { | ||
delete reference[finalPath]; | ||
} | ||
return object; | ||
} | ||
function createRemove(path) { | ||
return function (object) { return remove(path, object); }; | ||
} | ||
function omit(properties, object) { | ||
var cloned = clone(object); | ||
properties.forEach(function (property) { return remove(property, cloned); }); | ||
return cloned; | ||
} | ||
function createOmit(properties) { | ||
return function (object) { return omit(properties, object); }; | ||
} | ||
function set(value, path, object) { | ||
var parsedPath = parse(path); | ||
var reference = object; | ||
parsedPath.forEach(function (key, index) { | ||
var isLastElement = index === parsedPath.length - 1; | ||
if (isLastElement) { | ||
reference[key] = value; | ||
return; | ||
} | ||
if (!isObject(reference[key])) { | ||
reference[key] = {}; | ||
} | ||
reference = reference[key]; | ||
}); | ||
return object; | ||
} | ||
function createSetter(path, object) { | ||
return function (value) { return set(value, path, object); }; | ||
} | ||
function pluck(properties, object) { | ||
return properties.reduce(function (subset, property) { return set(get(property, object), property, subset); }, {}); | ||
} | ||
function createPluck(properties) { | ||
return function (object) { return pluck(properties, object); }; | ||
} | ||
export { NotFound, clone, createGetter, createHas, createOmit, createPluck, createRemove, createSetter, get, has, isObject, omit, pluck, remove, set, traverseObject }; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -1,2 +0,160 @@ | ||
var r=require("@blakek/curry"),e=require("pathington"),n=Symbol("curriable placeholder");function t(r){if(null===r)return!1;var e=typeof r;return"object"===e||"function"===e}function u(r){if(r instanceof Date)return new Date(r.getTime());if(r instanceof Map){var e=new Map;return r.forEach(function(r,n){e.set(n,u(r))}),e}if(r instanceof RegExp)return new RegExp(r.source,r.flags);if(r instanceof Set){var n=new Set;return r.forEach(function(r){n.add(u(r))}),n}if("object"==typeof r){if(null===r)return null;var t=Array.isArray(r)?[]:{};for(var o in r)t[o]=u(r[o]);return t}return r}function o(r,e){if(0===e.length)return r;if(!t(r))return n;var u=e[0],i=e.slice(1);return u in r?o(r[u],i):n}function i(r,t,u){if(void 0===t)return u;var i=o(u,e.parse(t));return i===n||void 0===i?r:i}var c=function(r,e){return i(void 0,r,e)};function a(r,n,u){var o=e.parse(n),i=u;return o.forEach(function(e,n){n===o.length-1?i[e]=r:(t(i[e])||(i[e]={}),i=i[e])}),u}var f=r.curry(c),s=r.curry(i),p=r.curry(function(r,t){return o(t,e.parse(r))!==n}),l=r.curry(function(r,e){var n=u(e);return r.forEach(function(r){return y(r,n)}),n}),v=r.curry(function(r,e){return r.reduce(function(r,n){return a(c(n,e),n,r)},{})}),y=r.curry(function(r,n){if(void 0===r)return n;var u=e.parse(r),i=u.slice(0,-1),c=u[u.length-1],a=o(n,e.parse(i));return t(a)&&delete a[c],n}),x=r.curry(a);exports.clone=u,exports.get=f,exports.getOr=s,exports.has=p,exports.isObject=t,exports.omit=l,exports.pluck=v,exports.remove=y,exports.set=x,exports.traverseObject=o; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var pathington = require('pathington'); | ||
function clone(value) { | ||
if (value instanceof Date) { | ||
return new Date(value.getTime()); | ||
} | ||
if (value instanceof Map) { | ||
var result_1 = new Map(); | ||
value.forEach(function (nestedValue, key) { | ||
result_1.set(key, clone(nestedValue)); | ||
}); | ||
return result_1; | ||
} | ||
if (value instanceof RegExp) { | ||
return new RegExp(value.source, value.flags); | ||
} | ||
if (value instanceof Set) { | ||
var result_2 = new Set(); | ||
value.forEach(function (x) { | ||
result_2.add(clone(x)); | ||
}); | ||
return result_2; | ||
} | ||
if (typeof value === 'object') { | ||
if (value === null) { | ||
return null; | ||
} | ||
var result = (Array.isArray(value) ? [] : {}); | ||
for (var key in value) { | ||
result[key] = clone(value[key]); | ||
} | ||
return result; | ||
} | ||
return value; | ||
} | ||
var NotFound = Symbol('value was not found'); | ||
function isObject(object) { | ||
if (object === null) { | ||
return false; | ||
} | ||
var type = typeof object; | ||
return type === 'object' || type === 'function'; | ||
} | ||
function traverseObject(object, path) { | ||
// If the path has been exhausted, return the current object | ||
if (path.length === 0) { | ||
return object; | ||
} | ||
// If the value could not be found, return `defaultValue` | ||
if (!isObject(object)) { | ||
return NotFound; | ||
} | ||
var key = path[0], keys = path.slice(1); | ||
// Search deeper in the object | ||
if (key in object) { | ||
return traverseObject(object[key], keys); | ||
} | ||
// The key was not found in the object. | ||
return NotFound; | ||
} | ||
function get(path, object, fallbackValue) { | ||
var parsedPath = pathington.parse(path); | ||
var value = traverseObject(object, parsedPath); | ||
if (value === NotFound || value === undefined) { | ||
return fallbackValue; | ||
} | ||
return value; | ||
} | ||
function createGetter(path, fallbackValue) { | ||
return function (object) { return get(path, object, fallbackValue); }; | ||
} | ||
function has(path, object) { | ||
var value = traverseObject(object, pathington.parse(path)); | ||
return value !== NotFound; | ||
} | ||
function createHas(path) { | ||
return function (object) { return has(path, object); }; | ||
} | ||
function remove(path, object) { | ||
if (path === undefined) { | ||
return object; | ||
} | ||
var parsedPath = pathington.parse(path); | ||
var referencePath = parsedPath.slice(0, -1); | ||
var finalPath = parsedPath[parsedPath.length - 1]; | ||
var reference = traverseObject(object, pathington.parse(referencePath)); | ||
if (isObject(reference)) { | ||
delete reference[finalPath]; | ||
} | ||
return object; | ||
} | ||
function createRemove(path) { | ||
return function (object) { return remove(path, object); }; | ||
} | ||
function omit(properties, object) { | ||
var cloned = clone(object); | ||
properties.forEach(function (property) { return remove(property, cloned); }); | ||
return cloned; | ||
} | ||
function createOmit(properties) { | ||
return function (object) { return omit(properties, object); }; | ||
} | ||
function set(value, path, object) { | ||
var parsedPath = pathington.parse(path); | ||
var reference = object; | ||
parsedPath.forEach(function (key, index) { | ||
var isLastElement = index === parsedPath.length - 1; | ||
if (isLastElement) { | ||
reference[key] = value; | ||
return; | ||
} | ||
if (!isObject(reference[key])) { | ||
reference[key] = {}; | ||
} | ||
reference = reference[key]; | ||
}); | ||
return object; | ||
} | ||
function createSetter(path, object) { | ||
return function (value) { return set(value, path, object); }; | ||
} | ||
function pluck(properties, object) { | ||
return properties.reduce(function (subset, property) { return set(get(property, object), property, subset); }, {}); | ||
} | ||
function createPluck(properties) { | ||
return function (object) { return pluck(properties, object); }; | ||
} | ||
Object.defineProperty(exports, 'parsePath', { | ||
enumerable: true, | ||
get: function () { | ||
return pathington.parse; | ||
} | ||
}); | ||
exports.NotFound = NotFound; | ||
exports.clone = clone; | ||
exports.createGetter = createGetter; | ||
exports.createHas = createHas; | ||
exports.createOmit = createOmit; | ||
exports.createPluck = createPluck; | ||
exports.createRemove = createRemove; | ||
exports.createSetter = createSetter; | ||
exports.get = get; | ||
exports.has = has; | ||
exports.isObject = isObject; | ||
exports.omit = omit; | ||
exports.pluck = pluck; | ||
exports.remove = remove; | ||
exports.set = set; | ||
exports.traverseObject = traverseObject; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@blakek/deep", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "🐡 Get, set, remove, and test for deeply nested properties", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.esm.js", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"import": "./dist/index.esm.js", | ||
"require": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"react-native": "./dist/index.esm.js", | ||
"sideEffects": false, | ||
"source": "src/index.ts", | ||
"exports": "./dist/index.modern.js", | ||
"main": "./dist/index.cjs.js", | ||
"module": "./dist/index.esm.js", | ||
"unpkg": "./dist/index.umd.js", | ||
"browser": "./dist/index.umd.js", | ||
"types": "./dist/index.d.ts", | ||
"author": "Blake Knight <oss.ideas@gmail.com> (https://blakek.me/)", | ||
"description": "🐡 Get, set, remove, and test for deeply nested properties", | ||
"bugs": { | ||
@@ -18,9 +26,17 @@ "url": "https://github.com/blakek/deep/issues" | ||
"deep", | ||
"get", | ||
"getter", | ||
"nested", | ||
"object", | ||
"prop", | ||
"properties", | ||
"property", | ||
"set" | ||
"get", | ||
"getter", | ||
"set", | ||
"setter", | ||
"path", | ||
"clone", | ||
"has", | ||
"omit", | ||
"pluck", | ||
"remove" | ||
], | ||
@@ -35,7 +51,4 @@ "repository": { | ||
], | ||
"sideEffects": false, | ||
"types": "dist/index.d.ts", | ||
"ava": { | ||
"extensions": [ | ||
"js", | ||
"ts" | ||
@@ -59,4 +72,4 @@ ], | ||
"dependencies": { | ||
"@blakek/curry": "^2.0.2", | ||
"pathington": "^1.1.7" | ||
"pathington": "^1.1.7", | ||
"ts-toolbelt": "^9.6.0" | ||
}, | ||
@@ -66,8 +79,8 @@ "devDependencies": { | ||
"ava": "^3.15.0", | ||
"microbundle": "^0.13.3", | ||
"bunchee": "^1.7.3", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.3.2", | ||
"prettier": "^2.4.1", | ||
"rimraf": "^3.0.2", | ||
"ts-node": "^10.0.0", | ||
"typescript": "^4.3.5" | ||
"ts-node": "^10.4.0", | ||
"typescript": "^4.4.4" | ||
}, | ||
@@ -78,3 +91,3 @@ "peerDependencies": {}, | ||
"build:clean": "rimraf ./dist", | ||
"build:compile": "microbundle --globals '@blakek/curry=curry'", | ||
"build:compile": "bunchee ./src/index.ts", | ||
"format-check": "amper-scripts format-check .", | ||
@@ -86,4 +99,4 @@ "format": "amper-scripts format-write .", | ||
"type-check": "tsc --noEmit", | ||
"validate": "run-p format-check lint type-check" | ||
"validate": "run-p format-check lint test type-check" | ||
} | ||
} |
105
README.md
@@ -24,3 +24,20 @@ # deep | ||
```js | ||
import { clone, get, getOr, has, omit, pluck, remove, set } from '@blakek/deep'; | ||
import { | ||
clone, | ||
createGetter, | ||
get, | ||
has, | ||
omit, | ||
pluck, | ||
remove, | ||
set | ||
// also available: | ||
// - createHas | ||
// - createOmit | ||
// - createPluck | ||
// - createRemove | ||
// - createSetter | ||
// - isObject | ||
// - traverseObject | ||
} from '@blakek/deep'; | ||
@@ -47,9 +64,9 @@ const user = { | ||
// Arguments can be partially applied | ||
const githubUsername = get('sites.github.username'); | ||
githubUsername(user); //» 'blakek' | ||
// Get a property value with a fallback other than `undefined` | ||
getOr('no-account', 'sites.facebook.username', user); //» 'no-account' | ||
get('sites.facebook.username', user, 'no-account'); //» 'no-account' | ||
// Create a function to get a property value later | ||
const getUsername = createGetter('sites.github.username'); | ||
getUsername(user); //» 'blakek' | ||
// Test for a property value | ||
@@ -77,3 +94,2 @@ has('sites.github', user); //» true | ||
- `path` can be either a dot-notation string or array of path parts | ||
- arguments can be partially applied | ||
@@ -96,3 +112,3 @@ ### `clone` | ||
### `get` | ||
### `get` / `createGetter` | ||
@@ -102,3 +118,8 @@ Gets the value for a given path with an optional fallback value. | ||
```ts | ||
function get(path: Path, object: any): any; | ||
function get(path: Path, object: object, fallbackValue?: any): unknown; | ||
function createGetter( | ||
path: Path, | ||
fallbackValue?: any | ||
): (object: object) => unknown; | ||
``` | ||
@@ -122,35 +143,12 @@ | ||
get('sites.github.username', user); //» 'blakek' | ||
get('sites.github.avatar.src', user, 'default.png'); //» 'default.png' | ||
const getID = get('id'); | ||
getID(user); //» 'abf87de' | ||
``` | ||
### `getOr` | ||
Like `get`, gets a value from an object. Will return a fallback other than | ||
`undefined` if the value was not found equal to `undefined`. | ||
```ts | ||
function getOr(defaultValue: any, path: Path, object: any): any; | ||
``` | ||
```js | ||
const user = { | ||
id: 'abf87de', | ||
roles: ['alert:create', 'alert:read'], | ||
sites: { | ||
github: { | ||
username: 'blakek' | ||
} | ||
} | ||
}; | ||
getOr('/images/placeholder.png', 'sites.github.image', user); //» '/images/placeholder.png' | ||
const getRoles = getOr([], 'roles'); | ||
const getRoles = createGetter('roles'); | ||
getRoles(user); //» ['alert:create', 'alert:read'] | ||
getRoles({}); //» [] | ||
``` | ||
### `has` | ||
### `has` / `createHas` | ||
@@ -162,2 +160,4 @@ Returns `true` if a value was found at the given path or `false` if nothing was | ||
function has(path: Path, object: any): boolean; | ||
function createHas(path: Path): (object: any) => boolean; | ||
``` | ||
@@ -179,9 +179,12 @@ | ||
// `get()` should be used if you want to ensure a value is not `undefined` | ||
getOr(false, 'attributes.isCool', product); //» false | ||
const hasMaterials = createHas('attributes.materials'); | ||
hasMaterials(product); //» true | ||
// NOTE: `get()` should be used if you want to ensure a value is not `undefined` | ||
get('attributes.isCool', product, false); //» false | ||
``` | ||
### `omit` | ||
### `omit` / `createOmit` | ||
Returns a clone of an object with some properties removed. | ||
Returns a clone of an object with a list of properties removed. | ||
@@ -193,2 +196,4 @@ Note: `omit()` returns a clone with properties removed. If you'd rather modify | ||
function omit(properties: Path[], object: any): any; | ||
function createOmit(properties: Path[]): (object: any) => any; | ||
``` | ||
@@ -210,5 +215,8 @@ | ||
//» { sites: { github: { username: 'blakek' } } } | ||
const omitExtra = createOmit(['roles, sites']); | ||
omitExtra(user); //» { username: 'blakek' } | ||
``` | ||
### `pluck` | ||
### `pluck` / `createPluck` | ||
@@ -235,5 +243,8 @@ Gets a subset of properties from an object. | ||
//» { username: 'blakek', roles: [ 'alert:create', 'alert:read' ] } | ||
const permissionInfo = pluck(['roles', 'username']); | ||
permissionInfo(user); //» { roles: [ 'alert:create', 'alert:read' ], username: 'blakek' } | ||
``` | ||
### `remove` | ||
### `remove` / `createRemove` | ||
@@ -251,2 +262,4 @@ Removes a value at a path and returns the object. | ||
function remove(path: Path, object: any): any; | ||
function createRemove(path: Path): (object: any) => any; | ||
``` | ||
@@ -263,5 +276,8 @@ | ||
//» { username: 'blakek' } (same object from previous line) | ||
const removePassword = createRemove('password'); | ||
removePassword({ username: 'bob', password: 'laskjfl' }); //» { username: 'bob' } | ||
``` | ||
### `set` | ||
### `set` / `createSetter` | ||
@@ -278,2 +294,4 @@ Sets a value at a path and returns the object. | ||
function set(value: any, path: Path, object: any): any; | ||
function createSetter(path: Path, object: any): (value: any) => any; | ||
``` | ||
@@ -295,2 +313,5 @@ | ||
logout(user); //» { profile: null } | ||
const setUsername = createSetter('username', user); | ||
setUsername('blakek'); //» { profile: { bgColor: 'tomato', bgImage: '/images/user.png' }, username: 'blakek' } | ||
``` | ||
@@ -297,0 +318,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
88878
17
328
0
331
+ Addedts-toolbelt@^9.6.0
+ Addedts-toolbelt@9.6.0(transitive)
- Removed@blakek/curry@^2.0.2
- Removed@blakek/curry@2.0.2(transitive)