path-to-prop 2.0.2 → 2.0.4

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
@@ -17,21 +17,29 @@ Retrieves a property from an object based on a `'path/to.that.prop'`
17
17
  ```js
18
18
  import { getProp } from 'path-to-prop'
19
19
 
20
- const target = {a: {b: {c: {d: {e: 1}}}}}
20
+ getProp({ nested: { prop: 1 } }, 'nested.prop') // 1
21
+
22
+ getProp({ nested: { prop: 1 } }, 'nested') // { prop: 1 }
23
+ ```
24
+
25
+ You can use `/` or `.` to access nested props:
26
+
27
+ ```js
28
+ const obj = {a: {b: {c: {d: {e: 1}}}}}
21
29
  const path = 'a/b/c.d.e'
22
30
 
23
- getProp(target, path)
31
+ getProp(obj, path)
24
32
  // returns 1
25
33
 
26
- getProp(target, 'nonexistent.prop')
34
+ getProp(obj, 'nonexistent.prop')
27
35
  // returns undefined
28
36
  ```
29
37
 
30
38
  When you have `/` or `.` in your prop names, use an array:
31
39
 
32
40
  ```js
33
- const target = {'a/b': {'c.d': 1}}
41
+ const obj = {'a/b': {'c.d': 1}}
34
42
 
35
- getProp(target, ['a/b', 'c.d'])
43
+ getProp(obj, ['a/b', 'c.d'])
36
44
  // returns 1
37
45
  ```
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ function getKeysFromPath(path) {
4
+ return path.split ? path.split(/\.|\//) : path;
5
+ }
6
+ function pathToProp(obj, path) {
7
+ const keys = getKeysFromPath(path);
8
+ let p;
9
+ for (p = 0; p < keys.length; p++) {
10
+ obj = obj ? obj[keys[p]] : void 0;
11
+ }
12
+ return obj === void 0 ? void 0 : obj;
13
+ }
14
+ const getProp = pathToProp;
15
+
16
+ exports.getKeysFromPath = getKeysFromPath;
17
+ exports.getProp = getProp;
18
+ exports.pathToProp = pathToProp;
@@ -1,27 +1,18 @@
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;
1
+ /**
2
+ * Returns the keys of a path
3
+ *
4
+ * @param {string} path a/path/like.this
5
+ * @returns {string[]} with keys
6
+ */
7
+ 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
+ declare function pathToProp(obj: Record<string, unknown>, path: string | string[]): unknown;
16
+ declare const getProp: typeof pathToProp;
26
17
 
27
18
  export { getKeysFromPath, getProp, pathToProp };
@@ -1,16 +1,18 @@
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;
1
+ /**
2
+ * Returns the keys of a path
3
+ *
4
+ * @param {string} path a/path/like.this
5
+ * @returns {string[]} with keys
6
+ */
7
+ 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
+ declare function pathToProp(obj: Record<string, unknown>, path: string | string[]): unknown;
16
+ declare const getProp: typeof pathToProp;
17
+
18
+ export { getKeysFromPath, getProp, pathToProp };
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ function getKeysFromPath(path) {
2
+ return path.split ? path.split(/\.|\//) : path;
3
+ }
4
+ function pathToProp(obj, path) {
5
+ const keys = getKeysFromPath(path);
6
+ let p;
7
+ for (p = 0; p < keys.length; p++) {
8
+ obj = obj ? obj[keys[p]] : void 0;
9
+ }
10
+ return obj === void 0 ? void 0 : obj;
11
+ }
12
+ const getProp = pathToProp;
13
+
14
+ export { getKeysFromPath, getProp, pathToProp };
package/package.json CHANGED
@@ -1,17 +1,22 @@
1
1
  {
2
2
  "name": "path-to-prop",
3
- "version": "2.0.2",
4
- "sideEffects": false,
5
- "type": "module",
3
+ "version": "2.0.4",
6
4
  "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",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "types": "./dist/index.d.ts",
8
+ "module": "./dist/index.js",
9
+ "main": "./dist/index.js",
10
10
  "exports": {
11
11
  ".": {
12
- "import": "./dist/index.es.js",
13
- "require": "./dist/index.cjs",
14
- "types": "./dist/types/index.d.ts"
12
+ "require": {
13
+ "types": "./dist/cjs/index.d.cts",
14
+ "default": "./dist/cjs/index.cjs"
15
+ },
16
+ "import": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ }
15
20
  }
16
21
  },
17
22
  "files": [
@@ -23,7 +28,7 @@
23
28
  "scripts": {
24
29
  "test": "vitest run",
25
30
  "lint": "tsc --noEmit && eslint ./src --ext .ts",
26
- "build": "rollup -c ./scripts/build.js",
31
+ "build": "rollup -c ./rollup.config.js",
27
32
  "release": "npm run lint && del dist && npm run build && np"
28
33
  },
29
34
  "keywords": [
@@ -45,18 +50,19 @@
45
50
  "license": "MIT",
46
51
  "homepage": "https://github.com/mesqueeb/path-to-prop#readme",
47
52
  "devDependencies": {
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
+ "@typescript-eslint/eslint-plugin": "^5.59.2",
54
+ "@typescript-eslint/parser": "^5.59.2",
55
+ "del-cli": "^5.0.0",
56
+ "eslint": "^8.40.0",
57
+ "eslint-config-prettier": "^8.8.0",
53
58
  "eslint-plugin-tree-shaking": "^1.10.0",
54
- "np": "^7.6.0",
55
- "prettier": "^2.5.1",
56
- "rollup": "^2.66.1",
57
- "rollup-plugin-typescript2": "^0.31.1",
58
- "typescript": "^4.5.5",
59
- "vitest": "^0.2.3"
59
+ "np": "^7.7.0",
60
+ "prettier": "^2.8.8",
61
+ "rollup": "^3.23.0",
62
+ "rollup-plugin-dts": "^5.3.0",
63
+ "rollup-plugin-esbuild": "^5.0.0",
64
+ "typescript": "^4.9.5",
65
+ "vitest": "^0.31.0"
60
66
  },
61
67
  "bugs": {
62
68
  "url": "https://github.com/mesqueeb/path-to-prop/issues"
package/dist/index.cjs DELETED
@@ -1,33 +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
- 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;