path-to-prop 0.0.3 → 2.0.0

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,33 +1,63 @@
1
1
  {
2
2
  "name": "path-to-prop",
3
- "version": "0.0.3",
3
+ "version": "2.0.0",
4
4
  "sideEffects": false,
5
- "description": "Retrieves a property from an object based on a path.to.that.prop",
6
- "main": "dist/index.cjs.js",
7
- "module": "dist/index.esm.js",
8
- "typings": "types/index.d.ts",
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
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=12.13",
22
+ "npm": ">=7"
23
+ },
9
24
  "scripts": {
10
- "test": "ava --verbose",
11
- "build": "npm run lint && npm run test && npm run rollup",
12
- "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
13
- "rollup": "rollup -c build/rollup.js"
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"
14
29
  },
15
- "keywords": ["path-to-prop"],
30
+ "keywords": [
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"
43
+ ],
16
44
  "author": "Luca Ban - Mesqueeb",
45
+ "funding": "https://github.com/sponsors/mesqueeb",
17
46
  "license": "MIT",
18
47
  "homepage": "https://github.com/mesqueeb/path-to-prop#readme",
19
- "dependencies": {},
20
48
  "devDependencies": {
21
- "@typescript-eslint/eslint-plugin": "^2.28.0",
22
- "@typescript-eslint/parser": "^2.28.0",
23
- "ava": "^3.7.0",
24
- "eslint": "^6.8.0",
25
- "eslint-config-prettier": "^6.10.1",
26
- "eslint-plugin-tree-shaking": "^1.8.0",
27
- "rollup": "^2.6.1",
28
- "rollup-plugin-typescript2": "^0.27.0",
29
- "ts-node": "^8.8.2",
30
- "typescript": "^3.8.3"
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"
31
61
  },
32
62
  "bugs": {
33
63
  "url": "https://github.com/mesqueeb/path-to-prop/issues"
@@ -36,8 +66,35 @@
36
66
  "type": "git",
37
67
  "url": "git+https://github.com/mesqueeb/path-to-prop.git"
38
68
  },
39
- "ava": {
40
- "extensions": ["ts"],
41
- "require": ["ts-node/register"]
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
+ }
42
99
  }
43
100
  }
package/.eslintignore DELETED
@@ -1,8 +0,0 @@
1
- # don't ever lint node_modules
2
- node_modules
3
- # don't lint build output (make sure it's set to your correct build folder name)
4
- dist
5
- # don't lint nyc coverage output
6
- coverage
7
-
8
- test
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
- }
@@ -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
@@ -1,9 +0,0 @@
1
- {
2
- "printWidth": 100,
3
- "tabWidth": 2,
4
- "singleQuote": true,
5
- "trailingComma": "es5",
6
- "semi": false,
7
- "bracketSpacing": true,
8
- "quoteProps": "consistent"
9
- }
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,33 +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[] {
8
- if (!path) return []
9
- // eslint-disable-next-line no-useless-escape
10
- return path.match(/[^\/^\.]+/g)
11
- }
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
- export function pathToProp (target: object, path: string): any {
21
- const keys = getKeysFromPath(path)
22
- if (!keys.length) return target
23
- let obj = target
24
- while (obj && keys.length > 1) {
25
- obj = obj[keys.shift()]
26
- }
27
- const key = keys.shift()
28
- if (obj && Object.hasOwnProperty.call(obj, key)) {
29
- return obj[key]
30
- }
31
- }
32
-
33
- export default pathToProp
package/test/index.ts DELETED
@@ -1,24 +0,0 @@
1
- import test from 'ava'
2
- import pathToProp from '../src'
3
-
4
- test('test', t => {
5
- const target = { a: { b: { c: { d: { e: 1 } } } } }
6
- const res1 = pathToProp(target, 'a.b.c.d.e')
7
- t.deepEqual(res1, 1)
8
- const res2 = pathToProp(target, 'a.b.c.d')
9
- t.deepEqual(res2, { e: 1 })
10
- const res3 = pathToProp(target, 'a.b')
11
- t.deepEqual(res3, { c: { d: { e: 1 } } })
12
- const res4 = pathToProp(target, 'a')
13
- t.deepEqual(res4, { b: { c: { d: { e: 1 } } } })
14
- })
15
-
16
- test('not an object', t => {
17
- const path = 'a.b'
18
- const res1 = pathToProp(null, path)
19
- t.is(res1, undefined)
20
- const res2 = pathToProp(new Date(), path)
21
- t.is(res2, undefined)
22
- const res3 = pathToProp(function () {}, path)
23
- t.is(res3, undefined)
24
- })
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "declaration": true,
4
- "declarationDir": "./types/"
5
- },
6
- "include": [
7
- "src/**/*"
8
- ]
9
- }
package/types/index.d.ts DELETED
@@ -1,16 +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 declare function getKeysFromPath(path: string): string[];
8
- /**
9
- * Gets a deep property in an object, based on a path to that property
10
- *
11
- * @param {object} target an object to wherefrom to retrieve the deep reference of
12
- * @param {string} path 'path/to.prop'
13
- * @returns {*} the last prop in the path
14
- */
15
- export declare function pathToProp(target: object, path: string): any;
16
- export default pathToProp;