path-to-prop 1.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/{index.cjs.js → index.cjs} +3 -3
- package/dist/{index.esm.js → index.es.js} +3 -3
- package/{types → dist/types}/index.d.ts +0 -0
- package/package.json +64 -27
- package/.eslintignore +0 -9
- package/.eslintrc.js +0 -19
- package/.github/FUNDING.yml +0 -12
- package/.prettierrc +0 -9
- package/build/rollup.js +0 -59
- package/src/index.ts +0 -27
- package/test/index.ts +0 -55
- package/tsconfig.json +0 -11
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# Path to prop 🛤
|
|
2
2
|
|
|
3
|
+
<a href="https://www.npmjs.com/package/path-to-prop"><img src="https://img.shields.io/npm/v/path-to-prop.svg" alt="Total Downloads"></a>
|
|
4
|
+
<a href="https://www.npmjs.com/package/path-to-prop"><img src="https://img.shields.io/npm/dw/path-to-prop.svg" alt="Latest Stable Version"></a>
|
|
5
|
+
|
|
3
6
|
```
|
|
4
7
|
npm i path-to-prop
|
|
5
8
|
```
|
|
@@ -19,14 +19,14 @@ function getKeysFromPath(path) {
|
|
|
19
19
|
* @returns {unknown} the last prop in the path
|
|
20
20
|
*/
|
|
21
21
|
function pathToProp(obj, path) {
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
const keys = getKeysFromPath(path);
|
|
23
|
+
let p;
|
|
24
24
|
for (p = 0; p < keys.length; p++) {
|
|
25
25
|
obj = (obj ? obj[keys[p]] : undefined);
|
|
26
26
|
}
|
|
27
27
|
return obj === undefined ? undefined : obj;
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
const getProp = pathToProp;
|
|
30
30
|
|
|
31
31
|
exports.getKeysFromPath = getKeysFromPath;
|
|
32
32
|
exports.getProp = getProp;
|
|
@@ -15,13 +15,13 @@ function getKeysFromPath(path) {
|
|
|
15
15
|
* @returns {unknown} the last prop in the path
|
|
16
16
|
*/
|
|
17
17
|
function pathToProp(obj, path) {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const keys = getKeysFromPath(path);
|
|
19
|
+
let p;
|
|
20
20
|
for (p = 0; p < keys.length; p++) {
|
|
21
21
|
obj = (obj ? obj[keys[p]] : undefined);
|
|
22
22
|
}
|
|
23
23
|
return obj === undefined ? undefined : obj;
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
const getProp = pathToProp;
|
|
26
26
|
|
|
27
27
|
export { getKeysFromPath, getProp, pathToProp };
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,16 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "path-to-prop",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
+
"type": "module",
|
|
5
6
|
"description": "Retrieves a property from an object based on a 'path/to.that.prop'",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"module": "./dist/index.es.js",
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"types": "./dist/types/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.es.js",
|
|
13
|
+
"require": "./dist/index.cjs",
|
|
14
|
+
"types": "./dist/types/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=12.13"
|
|
22
|
+
},
|
|
9
23
|
"scripts": {
|
|
10
|
-
"test": "
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
24
|
+
"test": "vitest run",
|
|
25
|
+
"lint": "tsc --noEmit && eslint ./src --ext .ts",
|
|
26
|
+
"build": "rollup -c ./scripts/build.js",
|
|
27
|
+
"release": "npm run lint && del dist && npm run build && np"
|
|
14
28
|
},
|
|
15
29
|
"keywords": [
|
|
16
30
|
"path-to-prop",
|
|
@@ -27,21 +41,22 @@
|
|
|
27
41
|
"get-dot-property"
|
|
28
42
|
],
|
|
29
43
|
"author": "Luca Ban - Mesqueeb",
|
|
44
|
+
"funding": "https://github.com/sponsors/mesqueeb",
|
|
30
45
|
"license": "MIT",
|
|
31
46
|
"homepage": "https://github.com/mesqueeb/path-to-prop#readme",
|
|
32
|
-
"dependencies": {},
|
|
33
47
|
"devDependencies": {
|
|
34
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
35
|
-
"@typescript-eslint/parser": "^
|
|
36
|
-
"
|
|
37
|
-
"eslint": "^
|
|
38
|
-
"eslint-config-prettier": "^
|
|
39
|
-
"eslint-plugin-tree-shaking": "^1.
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"typescript": "^4.
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^5.10.1",
|
|
49
|
+
"@typescript-eslint/parser": "^5.10.1",
|
|
50
|
+
"del-cli": "^4.0.1",
|
|
51
|
+
"eslint": "^8.7.0",
|
|
52
|
+
"eslint-config-prettier": "^8.3.0",
|
|
53
|
+
"eslint-plugin-tree-shaking": "^1.10.0",
|
|
54
|
+
"np": "^7.6.0",
|
|
55
|
+
"prettier": "^2.5.1",
|
|
56
|
+
"rollup": "^2.66.0",
|
|
57
|
+
"rollup-plugin-typescript2": "^0.31.1",
|
|
58
|
+
"typescript": "^4.5.5",
|
|
59
|
+
"vitest": "^0.2.1"
|
|
45
60
|
},
|
|
46
61
|
"bugs": {
|
|
47
62
|
"url": "https://github.com/mesqueeb/path-to-prop/issues"
|
|
@@ -50,13 +65,35 @@
|
|
|
50
65
|
"type": "git",
|
|
51
66
|
"url": "git+https://github.com/mesqueeb/path-to-prop.git"
|
|
52
67
|
},
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
|
|
68
|
+
"np": {
|
|
69
|
+
"yarn": false,
|
|
70
|
+
"branch": "production"
|
|
71
|
+
},
|
|
72
|
+
"eslintConfig": {
|
|
73
|
+
"ignorePatterns": [
|
|
74
|
+
"node_modules",
|
|
75
|
+
"dist",
|
|
76
|
+
"scripts",
|
|
77
|
+
"test"
|
|
78
|
+
],
|
|
79
|
+
"root": true,
|
|
80
|
+
"parser": "@typescript-eslint/parser",
|
|
81
|
+
"plugins": [
|
|
82
|
+
"@typescript-eslint",
|
|
83
|
+
"tree-shaking"
|
|
84
|
+
],
|
|
85
|
+
"extends": [
|
|
86
|
+
"eslint:recommended",
|
|
87
|
+
"plugin:@typescript-eslint/eslint-recommended",
|
|
88
|
+
"plugin:@typescript-eslint/recommended",
|
|
89
|
+
"prettier"
|
|
56
90
|
],
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
|
|
91
|
+
"rules": {
|
|
92
|
+
"@typescript-eslint/no-empty-function": "off",
|
|
93
|
+
"@typescript-eslint/no-explicit-any": "off",
|
|
94
|
+
"@typescript-eslint/ban-ts-ignore": "off",
|
|
95
|
+
"tree-shaking/no-side-effects-in-initialization": "error",
|
|
96
|
+
"@typescript-eslint/ban-ts-comment": "off"
|
|
97
|
+
}
|
|
61
98
|
}
|
|
62
99
|
}
|
package/.eslintignore
DELETED
package/.eslintrc.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// npm i -D @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-prettier eslint-plugin-tree-shaking
|
|
2
|
-
module.exports = {
|
|
3
|
-
root: true,
|
|
4
|
-
parser: '@typescript-eslint/parser',
|
|
5
|
-
plugins: ['@typescript-eslint', 'tree-shaking'],
|
|
6
|
-
extends: [
|
|
7
|
-
'eslint:recommended',
|
|
8
|
-
'plugin:@typescript-eslint/eslint-recommended',
|
|
9
|
-
'plugin:@typescript-eslint/recommended',
|
|
10
|
-
'prettier/@typescript-eslint',
|
|
11
|
-
],
|
|
12
|
-
rules: {
|
|
13
|
-
'@typescript-eslint/no-explicit-any': 'off',
|
|
14
|
-
'@typescript-eslint/ban-ts-ignore': 'off',
|
|
15
|
-
'tree-shaking/no-side-effects-in-initialization': 'error',
|
|
16
|
-
'comma-dangle': ['error', 'always-multiline'],
|
|
17
|
-
'no-unused-vars': 'off',
|
|
18
|
-
},
|
|
19
|
-
}
|
package/.github/FUNDING.yml
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
# These are supported funding model platforms
|
|
2
|
-
|
|
3
|
-
github: mesqueeb
|
|
4
|
-
patreon: # Replace with a single Patreon username
|
|
5
|
-
open_collective: # Replace with a single Open Collective username
|
|
6
|
-
ko_fi: # Replace with a single Ko-fi username
|
|
7
|
-
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
|
8
|
-
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
9
|
-
liberapay: # Replace with a single Liberapay username
|
|
10
|
-
issuehunt: # Replace with a single IssueHunt username
|
|
11
|
-
otechie: # Replace with a single Otechie username
|
|
12
|
-
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
package/.prettierrc
DELETED
package/build/rollup.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
|
|
3
|
-
// npm install rollup-plugin-typescript2 typescript --save-dev
|
|
4
|
-
import typescript from 'rollup-plugin-typescript2'
|
|
5
|
-
// import { terser } from 'rollup-plugin-terser'
|
|
6
|
-
// import resolve from 'rollup-plugin-node-resolve'
|
|
7
|
-
|
|
8
|
-
// ------------------------------------------------------------------------------------------
|
|
9
|
-
// formats
|
|
10
|
-
// ------------------------------------------------------------------------------------------
|
|
11
|
-
// amd – Asynchronous Module Definition, used with module loaders like RequireJS
|
|
12
|
-
// cjs – CommonJS, suitable for Node and Browserify/Webpack
|
|
13
|
-
// esm – Keep the bundle as an ES module file
|
|
14
|
-
// iife – A self-executing function, suitable for inclusion as a <script> tag. (If you want to create a bundle for your application, you probably want to use this, because it leads to smaller file sizes.)
|
|
15
|
-
// umd – Universal Module Definition, works as amd, cjs and iife all in one
|
|
16
|
-
// system – Native format of the SystemJS loader
|
|
17
|
-
|
|
18
|
-
// ------------------------------------------------------------------------------------------
|
|
19
|
-
// setup
|
|
20
|
-
// ------------------------------------------------------------------------------------------
|
|
21
|
-
const pkg = require('../package.json')
|
|
22
|
-
const name = pkg.name
|
|
23
|
-
const className = name.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase())
|
|
24
|
-
const external = Object.keys(pkg.dependencies || [])
|
|
25
|
-
const plugins = [
|
|
26
|
-
typescript({ useTsconfigDeclarationDir: true, tsconfigOverride: { exclude: ['test/**/*'] } }),
|
|
27
|
-
]
|
|
28
|
-
|
|
29
|
-
// ------------------------------------------------------------------------------------------
|
|
30
|
-
// Builds
|
|
31
|
-
// ------------------------------------------------------------------------------------------
|
|
32
|
-
function defaults (config) {
|
|
33
|
-
// defaults
|
|
34
|
-
const defaults = {
|
|
35
|
-
plugins,
|
|
36
|
-
external,
|
|
37
|
-
}
|
|
38
|
-
// defaults.output
|
|
39
|
-
config.output = config.output.map(output => {
|
|
40
|
-
return Object.assign(
|
|
41
|
-
{
|
|
42
|
-
sourcemap: false,
|
|
43
|
-
name: className,
|
|
44
|
-
},
|
|
45
|
-
output
|
|
46
|
-
)
|
|
47
|
-
})
|
|
48
|
-
return Object.assign(defaults, config)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export default [
|
|
52
|
-
defaults({
|
|
53
|
-
input: 'src/index.ts',
|
|
54
|
-
output: [
|
|
55
|
-
{ file: 'dist/index.cjs.js', format: 'cjs' },
|
|
56
|
-
{ file: 'dist/index.esm.js', format: 'esm' },
|
|
57
|
-
],
|
|
58
|
-
}),
|
|
59
|
-
]
|
package/src/index.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the keys of a path
|
|
3
|
-
*
|
|
4
|
-
* @param {string} path a/path/like.this
|
|
5
|
-
* @returns {string[]} with keys
|
|
6
|
-
*/
|
|
7
|
-
export function getKeysFromPath (path: string | string[]): string[] {
|
|
8
|
-
return (path as any).split ? (path as string).split(/\.|\//) : (path as string[])
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 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
|
|
13
|
-
*
|
|
14
|
-
* @param {Record<string, unknown>} obj an object to wherefrom to retrieve the deep reference of
|
|
15
|
-
* @param {string | string[]} path 'path/to.prop' or ['path', 'to', 'prop']
|
|
16
|
-
* @returns {unknown} the last prop in the path
|
|
17
|
-
*/
|
|
18
|
-
export function pathToProp (obj: Record<string, unknown>, path: string | string[]): unknown {
|
|
19
|
-
const keys = getKeysFromPath(path)
|
|
20
|
-
let p
|
|
21
|
-
for (p = 0; p < keys.length; p++) {
|
|
22
|
-
obj = (obj ? obj[keys[p]] : undefined) as any
|
|
23
|
-
}
|
|
24
|
-
return obj === undefined ? undefined : obj
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const getProp = pathToProp
|
package/test/index.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import test from 'ava'
|
|
2
|
-
import { getProp } from '../src'
|
|
3
|
-
|
|
4
|
-
const target = { a: { b: { c: { d: { e: 1 } } } } }
|
|
5
|
-
test('test', t => {
|
|
6
|
-
const res1 = getProp(target, 'a.b.c.d.e')
|
|
7
|
-
t.deepEqual(res1, 1)
|
|
8
|
-
const res1b = getProp(target, 'a/b/c/d/e')
|
|
9
|
-
t.deepEqual(res1b, 1)
|
|
10
|
-
const res1c = getProp(target, 'a/b.c/d.e')
|
|
11
|
-
t.deepEqual(res1c, 1)
|
|
12
|
-
const res2 = getProp(target, 'a.b.c.d')
|
|
13
|
-
t.deepEqual(res2, { e: 1 })
|
|
14
|
-
const res3 = getProp(target, 'a.b')
|
|
15
|
-
t.deepEqual(res3, { c: { d: { e: 1 } } })
|
|
16
|
-
const res4 = getProp(target, 'a')
|
|
17
|
-
t.deepEqual(res4, { b: { c: { d: { e: 1 } } } })
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
const dotTarget = { 'a.b': { 'c/d': { 'e.f': 1 } } }
|
|
21
|
-
test('test ./ paths', t => {
|
|
22
|
-
const res1 = getProp(dotTarget, ['a.b', 'c/d', 'e.f'])
|
|
23
|
-
t.deepEqual(res1, 1)
|
|
24
|
-
const res1b = getProp(dotTarget, 'a.b.c/d.e.f')
|
|
25
|
-
t.deepEqual(res1b, undefined)
|
|
26
|
-
const res2 = getProp(dotTarget, ['a.b', 'c/d'])
|
|
27
|
-
t.deepEqual(res2, { 'e.f': 1 })
|
|
28
|
-
const res2b = getProp(dotTarget, 'a.b.c/d')
|
|
29
|
-
t.deepEqual(res2b, undefined)
|
|
30
|
-
const res3 = getProp(dotTarget, ['a.b'])
|
|
31
|
-
t.deepEqual(res3, { 'c/d': { 'e.f': 1 } })
|
|
32
|
-
const res3b = getProp(dotTarget, 'a.b')
|
|
33
|
-
t.deepEqual(res3b, undefined)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
test('test non existent props', t => {
|
|
37
|
-
const res1 = getProp(target, 'a.b.c.d.X')
|
|
38
|
-
t.deepEqual(res1, undefined)
|
|
39
|
-
const res2 = getProp(target, 'X.b.c.d')
|
|
40
|
-
t.deepEqual(res2, undefined)
|
|
41
|
-
const res3 = getProp(target, 'a.X')
|
|
42
|
-
t.deepEqual(res3, undefined)
|
|
43
|
-
const res4 = getProp(target, 'X')
|
|
44
|
-
t.deepEqual(res4, undefined)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
test('not an object', t => {
|
|
48
|
-
const path = 'a.b'
|
|
49
|
-
const res1 = getProp(null, path)
|
|
50
|
-
t.is(res1, undefined)
|
|
51
|
-
const res2 = getProp(new Date() as any, path)
|
|
52
|
-
t.is(res2, undefined)
|
|
53
|
-
const res3 = getProp(function () {} as any, path)
|
|
54
|
-
t.is(res3, undefined)
|
|
55
|
-
})
|