path-to-prop 0.0.1 → 1.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 CHANGED
@@ -1,19 +1,37 @@
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
  ```
6
9
 
7
- Retrieves a property from an object based on a path.to.that.prop
10
+ Retrieves a property from an object based on a `'path/to.that.prop'`
11
+
12
+ - Supports paths with both `/` and `.` to separate props
13
+ - Safely typed (with TypeScript) - returns `unknown`
8
14
 
9
15
  ## Usage
10
16
 
11
17
  ```js
12
- import pathToProp from 'path-to-prop'
18
+ import { getProp } from 'path-to-prop'
13
19
 
14
20
  const target = {a: {b: {c: {d: {e: 1}}}}}
15
- const path = 'a.b.c.d.e'
21
+ const path = 'a/b/c.d.e'
22
+
23
+ getProp(target, path)
24
+ // returns 1
25
+
26
+ getProp(target, 'nonexistent.prop')
27
+ // returns undefined
28
+ ```
29
+
30
+ When you have `/` or `.` in your prop names, use an array:
31
+
32
+ ```js
33
+ const target = {'a/b': {'c.d': 1}}
16
34
 
17
- pathToProp(target, path)
35
+ getProp(target, ['a/b', 'c.d'])
18
36
  // returns 1
19
37
  ```
package/dist/index.cjs ADDED
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * Returns the keys of a path
7
+ *
8
+ * @param {string} path a/path/like.this
9
+ * @returns {string[]} with keys
10
+ */
11
+ function getKeysFromPath(path) {
12
+ return path.split ? path.split(/\.|\//) : path;
13
+ }
14
+ /**
15
+ * 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
16
+ *
17
+ * @param {Record<string, unknown>} obj an object to wherefrom to retrieve the deep reference of
18
+ * @param {string | string[]} path 'path/to.prop' or ['path', 'to', 'prop']
19
+ * @returns {unknown} the last prop in the path
20
+ */
21
+ function pathToProp(obj, path) {
22
+ const keys = getKeysFromPath(path);
23
+ let p;
24
+ for (p = 0; p < keys.length; p++) {
25
+ obj = (obj ? obj[keys[p]] : undefined);
26
+ }
27
+ return obj === undefined ? undefined : obj;
28
+ }
29
+ const getProp = pathToProp;
30
+
31
+ exports.getKeysFromPath = getKeysFromPath;
32
+ exports.getProp = getProp;
33
+ exports.pathToProp = pathToProp;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Returns the keys of a path
3
+ *
4
+ * @param {string} path a/path/like.this
5
+ * @returns {string[]} with keys
6
+ */
7
+ function getKeysFromPath(path) {
8
+ return path.split ? path.split(/\.|\//) : path;
9
+ }
10
+ /**
11
+ * 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
12
+ *
13
+ * @param {Record<string, unknown>} obj an object to wherefrom to retrieve the deep reference of
14
+ * @param {string | string[]} path 'path/to.prop' or ['path', 'to', 'prop']
15
+ * @returns {unknown} the last prop in the path
16
+ */
17
+ function pathToProp(obj, path) {
18
+ const keys = getKeysFromPath(path);
19
+ let p;
20
+ for (p = 0; p < keys.length; p++) {
21
+ obj = (obj ? obj[keys[p]] : undefined);
22
+ }
23
+ return obj === undefined ? undefined : obj;
24
+ }
25
+ const getProp = pathToProp;
26
+
27
+ export { getKeysFromPath, getProp, pathToProp };
@@ -0,0 +1,16 @@
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 declare function getKeysFromPath(path: string | string[]): string[];
8
+ /**
9
+ * 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
10
+ *
11
+ * @param {Record<string, unknown>} obj an object to wherefrom to retrieve the deep reference of
12
+ * @param {string | string[]} path 'path/to.prop' or ['path', 'to', 'prop']
13
+ * @returns {unknown} the last prop in the path
14
+ */
15
+ export declare function pathToProp(obj: Record<string, unknown>, path: string | string[]): unknown;
16
+ export declare const getProp: typeof pathToProp;
package/package.json CHANGED
@@ -1,32 +1,100 @@
1
1
  {
2
2
  "name": "path-to-prop",
3
- "version": "0.0.1",
4
- "description": "Retrieves a property from an object based on a path.to.that.prop",
5
- "main": "dist/index.cjs.js",
6
- "module": "dist/index.esm.js",
7
- "typings": "types/index.d.ts",
8
- "scripts": {
9
- "test": "ava",
10
- "rollup": "rollup -c build/rollup.js",
11
- "build": "npm run rollup && npm run test"
3
+ "version": "1.0.1",
4
+ "sideEffects": false,
5
+ "type": "module",
6
+ "description": "Retrieves a property from an object based on a 'path/to.that.prop'",
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
+ }
12
16
  },
13
- "repository": {
14
- "type": "git",
15
- "url": "git+https://github.com/mesqueeb/path-to-prop.git"
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=12.13",
22
+ "npm": ">=7"
23
+ },
24
+ "scripts": {
25
+ "test": "vitest run",
26
+ "lint": "tsc --noEmit && eslint ./src --ext .ts",
27
+ "build": "rollup -c ./scripts/build.js",
28
+ "release": "npm run lint && del dist && npm run build && np"
16
29
  },
17
30
  "keywords": [
18
- "path-to-prop"
31
+ "path-to-prop",
32
+ "dot-prop",
33
+ "dotprop",
34
+ "propdot",
35
+ "dot-notation",
36
+ "dotnotation-prop",
37
+ "delve",
38
+ "prop-at-path",
39
+ "get-prop-at-path",
40
+ "get-property-at-path",
41
+ "get-dot-prop",
42
+ "get-dot-property"
19
43
  ],
20
44
  "author": "Luca Ban - Mesqueeb",
45
+ "funding": "https://github.com/sponsors/mesqueeb",
21
46
  "license": "MIT",
47
+ "homepage": "https://github.com/mesqueeb/path-to-prop#readme",
48
+ "devDependencies": {
49
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
50
+ "@typescript-eslint/parser": "^5.10.1",
51
+ "del-cli": "^4.0.1",
52
+ "eslint": "^8.7.0",
53
+ "eslint-config-prettier": "^8.3.0",
54
+ "eslint-plugin-tree-shaking": "^1.10.0",
55
+ "np": "^7.6.0",
56
+ "prettier": "^2.5.1",
57
+ "rollup": "^2.66.0",
58
+ "rollup-plugin-typescript2": "^0.31.1",
59
+ "typescript": "^4.5.5",
60
+ "vitest": "^0.2.1"
61
+ },
22
62
  "bugs": {
23
63
  "url": "https://github.com/mesqueeb/path-to-prop/issues"
24
64
  },
25
- "homepage": "https://github.com/mesqueeb/path-to-prop#readme",
26
- "devDependencies": {
27
- "ava": "^2.4.0",
28
- "rollup": "^1.25.1",
29
- "rollup-plugin-typescript2": "^0.24.3",
30
- "typescript": "^3.6.4"
65
+ "repository": {
66
+ "type": "git",
67
+ "url": "git+https://github.com/mesqueeb/path-to-prop.git"
68
+ },
69
+ "np": {
70
+ "yarn": false,
71
+ "branch": "production"
72
+ },
73
+ "eslintConfig": {
74
+ "ignorePatterns": [
75
+ "node_modules",
76
+ "dist",
77
+ "scripts",
78
+ "test"
79
+ ],
80
+ "root": true,
81
+ "parser": "@typescript-eslint/parser",
82
+ "plugins": [
83
+ "@typescript-eslint",
84
+ "tree-shaking"
85
+ ],
86
+ "extends": [
87
+ "eslint:recommended",
88
+ "plugin:@typescript-eslint/eslint-recommended",
89
+ "plugin:@typescript-eslint/recommended",
90
+ "prettier"
91
+ ],
92
+ "rules": {
93
+ "@typescript-eslint/no-empty-function": "off",
94
+ "@typescript-eslint/no-explicit-any": "off",
95
+ "@typescript-eslint/ban-ts-ignore": "off",
96
+ "tree-shaking/no-side-effects-in-initialization": "error",
97
+ "@typescript-eslint/ban-ts-comment": "off"
98
+ }
31
99
  }
32
100
  }
package/build/rollup.js DELETED
@@ -1,56 +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}),
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
- sourcemap: false,
42
- name: className,
43
- }, output)
44
- })
45
- return Object.assign(defaults, config)
46
- }
47
-
48
- export default [
49
- defaults({
50
- input: 'src/index.ts',
51
- output: [
52
- {file: 'dist/index.cjs.js', format: 'cjs'},
53
- {file: 'dist/index.esm.js', format: 'esm'},
54
- ],
55
- }),
56
- ]
package/dist/index.cjs.js DELETED
@@ -1,40 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- /**
6
- * Returns the keys of a path
7
- *
8
- * @param {string} path a/path/like.this
9
- * @returns {string[]} with keys
10
- */
11
- function getKeysFromPath(path) {
12
- if (!path)
13
- return [];
14
- // eslint-disable-next-line no-useless-escape
15
- return path.match(/[^\/^\.]+/g);
16
- }
17
- /**
18
- * Gets a deep property in an object, based on a path to that property
19
- *
20
- * @param {object} target an object to wherefrom to retrieve the deep reference of
21
- * @param {string} path 'path/to.prop'
22
- * @returns {*} the last prop in the path
23
- */
24
- function pathToProp(target, path) {
25
- var keys = getKeysFromPath(path);
26
- if (!keys.length)
27
- return target;
28
- var obj = target;
29
- while (obj && keys.length > 1) {
30
- obj = obj[keys.shift()];
31
- }
32
- var key = keys.shift();
33
- if (obj && obj.hasOwnProperty(key)) {
34
- return obj[key];
35
- }
36
- }
37
-
38
- exports.default = pathToProp;
39
- exports.getKeysFromPath = getKeysFromPath;
40
- exports.pathToProp = pathToProp;
package/dist/index.esm.js DELETED
@@ -1,35 +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
- function getKeysFromPath(path) {
8
- if (!path)
9
- return [];
10
- // eslint-disable-next-line no-useless-escape
11
- return path.match(/[^\/^\.]+/g);
12
- }
13
- /**
14
- * Gets a deep property in an object, based on a path to that property
15
- *
16
- * @param {object} target an object to wherefrom to retrieve the deep reference of
17
- * @param {string} path 'path/to.prop'
18
- * @returns {*} the last prop in the path
19
- */
20
- function pathToProp(target, path) {
21
- var keys = getKeysFromPath(path);
22
- if (!keys.length)
23
- return target;
24
- var obj = target;
25
- while (obj && keys.length > 1) {
26
- obj = obj[keys.shift()];
27
- }
28
- var key = keys.shift();
29
- if (obj && obj.hasOwnProperty(key)) {
30
- return obj[key];
31
- }
32
- }
33
-
34
- export default pathToProp;
35
- export { getKeysFromPath, pathToProp };
package/src/index.ts DELETED
@@ -1,34 +0,0 @@
1
-
2
- /**
3
- * Returns the keys of a path
4
- *
5
- * @param {string} path a/path/like.this
6
- * @returns {string[]} with keys
7
- */
8
- export function getKeysFromPath (path: string): string[] {
9
- if (!path) return []
10
- // eslint-disable-next-line no-useless-escape
11
- return path.match(/[^\/^\.]+/g)
12
- }
13
-
14
- /**
15
- * Gets a deep property in an object, based on a path to that property
16
- *
17
- * @param {object} target an object to wherefrom to retrieve the deep reference of
18
- * @param {string} path 'path/to.prop'
19
- * @returns {*} the last prop in the path
20
- */
21
- export function pathToProp (target: object, path: string): any {
22
- const keys = getKeysFromPath(path)
23
- if (!keys.length) return target
24
- let obj = target
25
- while (obj && keys.length > 1) {
26
- obj = obj[keys.shift()]
27
- }
28
- const key = keys.shift()
29
- if (obj && obj.hasOwnProperty(key)) {
30
- return obj[key]
31
- }
32
- }
33
-
34
- export default pathToProp
package/test/index.js DELETED
@@ -1,36 +0,0 @@
1
- import test from 'ava'
2
- import pathToProp from '../dist/index.cjs'
3
-
4
- test('test', t => {
5
- let res, target, path
6
- target = {a: {b: {c: {d: {e: 1}}}}}
7
- path = 'a.b.c.d.e'
8
- res = pathToProp(target, path)
9
- t.is(res, 1)
10
- path = 'a.b.c.d'
11
- res = pathToProp(target, path)
12
- t.deepEqual(res, {e: 1})
13
- path = 'a.b'
14
- res = pathToProp(target, path)
15
- t.deepEqual(res, {c: {d: {e: 1}}})
16
- })
17
-
18
- test('not an object', t => {
19
- let target, path, res
20
- path = 'a.b'
21
- target = null
22
- res = pathToProp(target, path)
23
- t.is(res, undefined)
24
- target = 1
25
- res = pathToProp(target, path)
26
- t.is(res, undefined)
27
- target = true
28
- res = pathToProp(target, path)
29
- t.is(res, undefined)
30
- target = new Date()
31
- res = pathToProp(target, path)
32
- t.is(res, undefined)
33
- target = function () {}
34
- res = pathToProp(target, path)
35
- t.is(res, undefined)
36
- })