path-to-prop
Advanced tools
Comparing version 0.0.3 to 1.0.0
@@ -26,3 +26,3 @@ /* eslint-disable */ | ||
const plugins = [ | ||
typescript({useTsconfigDeclarationDir: true}), | ||
typescript({ useTsconfigDeclarationDir: true, tsconfigOverride: { exclude: ['test/**/*'] } }), | ||
] | ||
@@ -37,10 +37,13 @@ | ||
plugins, | ||
external | ||
external, | ||
} | ||
// defaults.output | ||
config.output = config.output.map(output => { | ||
return Object.assign({ | ||
sourcemap: false, | ||
name: className, | ||
}, output) | ||
return Object.assign( | ||
{ | ||
sourcemap: false, | ||
name: className, | ||
}, | ||
output | ||
) | ||
}) | ||
@@ -54,6 +57,6 @@ return Object.assign(defaults, config) | ||
output: [ | ||
{file: 'dist/index.cjs.js', format: 'cjs'}, | ||
{file: 'dist/index.esm.js', format: 'esm'}, | ||
{ file: 'dist/index.cjs.js', format: 'cjs' }, | ||
{ file: 'dist/index.esm.js', format: 'esm' }, | ||
], | ||
}), | ||
] |
@@ -12,30 +12,23 @@ 'use strict'; | ||
function getKeysFromPath(path) { | ||
if (!path) | ||
return []; | ||
// eslint-disable-next-line no-useless-escape | ||
return path.match(/[^\/^\.]+/g); | ||
return path.split ? path.split(/\.|\//) : path; | ||
} | ||
/** | ||
* Gets a deep property in an object, based on a path to that property | ||
* Gets a deep property in an object, based on a path to that property. Pass the path as array when you have prop names with `.` or `/` in them | ||
* | ||
* @param {object} target an object to wherefrom to retrieve the deep reference of | ||
* @param {string} path 'path/to.prop' | ||
* @returns {*} the last prop in the path | ||
* @param {Record<string, unknown>} obj an object to wherefrom to retrieve the deep reference of | ||
* @param {string | string[]} path 'path/to.prop' or ['path', 'to', 'prop'] | ||
* @returns {unknown} the last prop in the path | ||
*/ | ||
function pathToProp(target, path) { | ||
function pathToProp(obj, path) { | ||
var keys = getKeysFromPath(path); | ||
if (!keys.length) | ||
return target; | ||
var obj = target; | ||
while (obj && keys.length > 1) { | ||
obj = obj[keys.shift()]; | ||
var p; | ||
for (p = 0; p < keys.length; p++) { | ||
obj = (obj ? obj[keys[p]] : undefined); | ||
} | ||
var key = keys.shift(); | ||
if (obj && obj.hasOwnProperty(key)) { | ||
return obj[key]; | ||
} | ||
return obj === undefined ? undefined : obj; | ||
} | ||
var getProp = pathToProp; | ||
exports.default = pathToProp; | ||
exports.getKeysFromPath = getKeysFromPath; | ||
exports.getProp = getProp; | ||
exports.pathToProp = pathToProp; |
@@ -8,29 +8,21 @@ /** | ||
function getKeysFromPath(path) { | ||
if (!path) | ||
return []; | ||
// eslint-disable-next-line no-useless-escape | ||
return path.match(/[^\/^\.]+/g); | ||
return path.split ? path.split(/\.|\//) : path; | ||
} | ||
/** | ||
* Gets a deep property in an object, based on a path to that property | ||
* Gets a deep property in an object, based on a path to that property. Pass the path as array when you have prop names with `.` or `/` in them | ||
* | ||
* @param {object} target an object to wherefrom to retrieve the deep reference of | ||
* @param {string} path 'path/to.prop' | ||
* @returns {*} the last prop in the path | ||
* @param {Record<string, unknown>} obj an object to wherefrom to retrieve the deep reference of | ||
* @param {string | string[]} path 'path/to.prop' or ['path', 'to', 'prop'] | ||
* @returns {unknown} the last prop in the path | ||
*/ | ||
function pathToProp(target, path) { | ||
function pathToProp(obj, path) { | ||
var keys = getKeysFromPath(path); | ||
if (!keys.length) | ||
return target; | ||
var obj = target; | ||
while (obj && keys.length > 1) { | ||
obj = obj[keys.shift()]; | ||
var p; | ||
for (p = 0; p < keys.length; p++) { | ||
obj = (obj ? obj[keys[p]] : undefined); | ||
} | ||
var key = keys.shift(); | ||
if (obj && obj.hasOwnProperty(key)) { | ||
return obj[key]; | ||
} | ||
return obj === undefined ? undefined : obj; | ||
} | ||
var getProp = pathToProp; | ||
export default pathToProp; | ||
export { getKeysFromPath, pathToProp }; | ||
export { getKeysFromPath, getProp, pathToProp }; |
{ | ||
"name": "path-to-prop", | ||
"version": "0.0.3", | ||
"version": "1.0.0", | ||
"sideEffects": false, | ||
"description": "Retrieves a property from an object based on a path.to.that.prop", | ||
"description": "Retrieves a property from an object based on a 'path/to.that.prop'", | ||
"main": "dist/index.cjs.js", | ||
@@ -11,7 +11,20 @@ "module": "dist/index.esm.js", | ||
"test": "ava --verbose", | ||
"build": "npm run lint && npm run test && npm run rollup", | ||
"build": "rm -rf ./types && rm -rf ./dist && npm run lint && npm run test && npm run rollup", | ||
"lint": "eslint . --ext .js,.jsx,.ts,.tsx", | ||
"rollup": "rollup -c build/rollup.js" | ||
}, | ||
"keywords": ["path-to-prop"], | ||
"keywords": [ | ||
"path-to-prop", | ||
"dot-prop", | ||
"dotprop", | ||
"propdot", | ||
"dot-notation", | ||
"dotnotation-prop", | ||
"delve", | ||
"prop-at-path", | ||
"get-prop-at-path", | ||
"get-property-at-path", | ||
"get-dot-prop", | ||
"get-dot-property" | ||
], | ||
"author": "Luca Ban - Mesqueeb", | ||
@@ -22,12 +35,13 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^2.28.0", | ||
"@typescript-eslint/parser": "^2.28.0", | ||
"ava": "^3.7.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.10.1", | ||
"@typescript-eslint/eslint-plugin": "^3.10.1", | ||
"@typescript-eslint/parser": "^3.10.1", | ||
"ava": "^3.12.1", | ||
"eslint": "^7.7.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-tree-shaking": "^1.8.0", | ||
"rollup": "^2.6.1", | ||
"rollup-plugin-typescript2": "^0.27.0", | ||
"ts-node": "^8.8.2", | ||
"typescript": "^3.8.3" | ||
"rollup": "^2.26.7", | ||
"rollup-plugin-typescript2": "^0.27.2", | ||
"tsconfig-paths": "^3.9.0", | ||
"ts-node": "^9.0.0", | ||
"typescript": "^4.0.2" | ||
}, | ||
@@ -42,5 +56,10 @@ "bugs": { | ||
"ava": { | ||
"extensions": ["ts"], | ||
"require": ["ts-node/register"] | ||
"extensions": [ | ||
"ts" | ||
], | ||
"require": [ | ||
"tsconfig-paths/register", | ||
"ts-node/register" | ||
] | ||
} | ||
} |
@@ -7,14 +7,29 @@ # Path to prop 🛤 | ||
Retrieves a property from an object based on a path.to.that.prop | ||
Retrieves a property from an object based on a `'path/to.that.prop'` | ||
- Supports paths with both `/` and `.` to separate props | ||
- Safely typed (with TypeScript) - returns `unknown` | ||
## Usage | ||
```js | ||
import pathToProp from 'path-to-prop' | ||
import { getProp } from 'path-to-prop' | ||
const target = {a: {b: {c: {d: {e: 1}}}}} | ||
const path = 'a.b.c.d.e' | ||
const path = 'a/b/c.d.e' | ||
pathToProp(target, path) | ||
getProp(target, path) | ||
// returns 1 | ||
getProp(target, 'nonexistent.prop') | ||
// returns undefined | ||
``` | ||
When you have `/` or `.` in your prop names, use an array: | ||
```js | ||
const target = {'a/b': {'c.d': 1}} | ||
getProp(target, ['a/b', 'c.d']) | ||
// returns 1 | ||
``` |
@@ -7,28 +7,22 @@ /** | ||
*/ | ||
export function getKeysFromPath (path: string): string[] { | ||
if (!path) return [] | ||
// eslint-disable-next-line no-useless-escape | ||
return path.match(/[^\/^\.]+/g) | ||
export function getKeysFromPath (path: string | string[]): string[] { | ||
return (path as any).split ? (path as string).split(/\.|\//) : (path as string[]) | ||
} | ||
/** | ||
* Gets a deep property in an object, based on a path to that property | ||
* Gets a deep property in an object, based on a path to that property. Pass the path as array when you have prop names with `.` or `/` in them | ||
* | ||
* @param {object} target an object to wherefrom to retrieve the deep reference of | ||
* @param {string} path 'path/to.prop' | ||
* @returns {*} the last prop in the path | ||
* @param {Record<string, unknown>} obj an object to wherefrom to retrieve the deep reference of | ||
* @param {string | string[]} path 'path/to.prop' or ['path', 'to', 'prop'] | ||
* @returns {unknown} the last prop in the path | ||
*/ | ||
export function pathToProp (target: object, path: string): any { | ||
export function pathToProp (obj: Record<string, unknown>, path: string | string[]): unknown { | ||
const keys = getKeysFromPath(path) | ||
if (!keys.length) return target | ||
let obj = target | ||
while (obj && keys.length > 1) { | ||
obj = obj[keys.shift()] | ||
} | ||
const key = keys.shift() | ||
if (obj && Object.hasOwnProperty.call(obj, key)) { | ||
return obj[key] | ||
} | ||
let p | ||
for (p = 0; p < keys.length; p++) { | ||
obj = (obj ? obj[keys[p]] : undefined) as any | ||
} | ||
return obj === undefined ? undefined : obj | ||
} | ||
export default pathToProp | ||
export const getProp = pathToProp |
import test from 'ava' | ||
import pathToProp from '../src' | ||
import { getProp } from '../src' | ||
const target = { a: { b: { c: { d: { e: 1 } } } } } | ||
test('test', t => { | ||
const target = { a: { b: { c: { d: { e: 1 } } } } } | ||
const res1 = pathToProp(target, 'a.b.c.d.e') | ||
const res1 = getProp(target, 'a.b.c.d.e') | ||
t.deepEqual(res1, 1) | ||
const res2 = pathToProp(target, 'a.b.c.d') | ||
const res1b = getProp(target, 'a/b/c/d/e') | ||
t.deepEqual(res1b, 1) | ||
const res1c = getProp(target, 'a/b.c/d.e') | ||
t.deepEqual(res1c, 1) | ||
const res2 = getProp(target, 'a.b.c.d') | ||
t.deepEqual(res2, { e: 1 }) | ||
const res3 = pathToProp(target, 'a.b') | ||
const res3 = getProp(target, 'a.b') | ||
t.deepEqual(res3, { c: { d: { e: 1 } } }) | ||
const res4 = pathToProp(target, 'a') | ||
const res4 = getProp(target, 'a') | ||
t.deepEqual(res4, { b: { c: { d: { e: 1 } } } }) | ||
}) | ||
const dotTarget = { 'a.b': { 'c/d': { 'e.f': 1 } } } | ||
test('test ./ paths', t => { | ||
const res1 = getProp(dotTarget, ['a.b', 'c/d', 'e.f']) | ||
t.deepEqual(res1, 1) | ||
const res1b = getProp(dotTarget, 'a.b.c/d.e.f') | ||
t.deepEqual(res1b, undefined) | ||
const res2 = getProp(dotTarget, ['a.b', 'c/d']) | ||
t.deepEqual(res2, { 'e.f': 1 }) | ||
const res2b = getProp(dotTarget, 'a.b.c/d') | ||
t.deepEqual(res2b, undefined) | ||
const res3 = getProp(dotTarget, ['a.b']) | ||
t.deepEqual(res3, { 'c/d': { 'e.f': 1 } }) | ||
const res3b = getProp(dotTarget, 'a.b') | ||
t.deepEqual(res3b, undefined) | ||
}) | ||
test('test non existent props', t => { | ||
const res1 = getProp(target, 'a.b.c.d.X') | ||
t.deepEqual(res1, undefined) | ||
const res2 = getProp(target, 'X.b.c.d') | ||
t.deepEqual(res2, undefined) | ||
const res3 = getProp(target, 'a.X') | ||
t.deepEqual(res3, undefined) | ||
const res4 = getProp(target, 'X') | ||
t.deepEqual(res4, undefined) | ||
}) | ||
test('not an object', t => { | ||
const path = 'a.b' | ||
const res1 = pathToProp(null, path) | ||
const res1 = getProp(null, path) | ||
t.is(res1, undefined) | ||
const res2 = pathToProp(new Date(), path) | ||
const res2 = getProp(new Date() as any, path) | ||
t.is(res2, undefined) | ||
const res3 = pathToProp(function () {}, path) | ||
const res3 = getProp(function () {} as any, path) | ||
t.is(res3, undefined) | ||
}) |
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"declaration": true, | ||
@@ -7,4 +8,5 @@ "declarationDir": "./types/" | ||
"include": [ | ||
"src/**/*" | ||
"src/**/*", | ||
"test/**/*" | ||
] | ||
} |
@@ -7,11 +7,11 @@ /** | ||
*/ | ||
export declare function getKeysFromPath(path: string): string[]; | ||
export declare function getKeysFromPath(path: string | string[]): string[]; | ||
/** | ||
* Gets a deep property in an object, based on a path to that property | ||
* Gets a deep property in an object, based on a path to that property. Pass the path as array when you have prop names with `.` or `/` in them | ||
* | ||
* @param {object} target an object to wherefrom to retrieve the deep reference of | ||
* @param {string} path 'path/to.prop' | ||
* @returns {*} the last prop in the path | ||
* @param {Record<string, unknown>} obj an object to wherefrom to retrieve the deep reference of | ||
* @param {string | string[]} path 'path/to.prop' or ['path', 'to', 'prop'] | ||
* @returns {unknown} the last prop in the path | ||
*/ | ||
export declare function pathToProp(target: object, path: string): any; | ||
export default pathToProp; | ||
export declare function pathToProp(obj: Record<string, unknown>, path: string | string[]): unknown; | ||
export declare const getProp: typeof pathToProp; |
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12725
231
0
35
11