dot-path-value
Advanced tools
Comparing version 0.0.2 to 0.0.3
export type Primitive = null | undefined | string | number | boolean | symbol | bigint; | ||
type ArrayKey = number; | ||
type IsTuple<T extends readonly any[]> = number extends T["length"] ? false : true; | ||
type IsTuple<T extends readonly any[]> = number extends T['length'] ? false : true; | ||
type TupleKeys<T extends readonly any[]> = Exclude<keyof T, keyof any[]>; | ||
@@ -5,0 +5,0 @@ export type PathConcat<TKey extends string | number, TValue> = TValue extends Primitive ? `${TKey}` : `${TKey}` | `${TKey}.${Path<TValue>}`; |
{ | ||
"name": "dot-path-value", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Safely get deep nested properties using dot notation", | ||
@@ -28,4 +28,5 @@ "author": "German Makarov <cxdcjs@gmail.com>", | ||
"prettier": "prettier --write src/**/*", | ||
"prepublishOnly": "npm run build", | ||
"postversion": "git push && git push --tags" | ||
"prepublishOnly": "npm run test && npm run build", | ||
"postversion": "git push && git push --tags", | ||
"test": "jest --passWithNoTests" | ||
}, | ||
@@ -39,4 +40,7 @@ "sideEffects": false, | ||
"devDependencies": { | ||
"@types/jest": "^29.2.6", | ||
"jest": "^29.3.1", | ||
"microbundle": "^0.15.1", | ||
"prettier": "^2.8.3" | ||
"prettier": "^2.8.3", | ||
"ts-jest": "^29.0.5" | ||
}, | ||
@@ -43,0 +47,0 @@ "keywords": [ |
@@ -24,2 +24,3 @@ # dot-path-value | ||
- No dependencies | ||
- Utility types `Path` and `PathValue` | ||
@@ -40,13 +41,27 @@ ## Installation | ||
```ts | ||
import { getByPath } from "dot-path-value"; | ||
import { getByPath } from 'dot-path-value'; | ||
const obj = { | ||
a: { | ||
b: { | ||
c: "hello", | ||
}, | ||
b: 'hello', | ||
d: [ | ||
{ | ||
e: 'world', | ||
} | ||
], | ||
}, | ||
}; | ||
getByPath(obj, "a.b.c"); // outputs 'hello' | ||
// access though object | ||
getByPath(obj, 'a.b'); // outputs 'hello' with type `string` | ||
// access though array | ||
getByPath(obj, 'a.d.0.e'); // outputs 'world' with type `string` | ||
getByPath(obj, 'a.d.0'); // outputs '{ e: 'world' }' with type `{ e: string }` | ||
// also you can pass array as first argument | ||
getByPath([{ a: 1 }], '0.a'); // outputs '1' with type `number` | ||
// typescript errors | ||
getByPath(obj, 'a.b.c'); // `c` property does not exist | ||
``` | ||
@@ -62,1 +77,17 @@ | ||
| `PathValue<T, TPath>` | returns the type of the value at the specified path | | ||
```ts | ||
const obj = { | ||
a: { | ||
b: 'hello', | ||
d: [ | ||
{ | ||
e: 'world', | ||
} | ||
], | ||
}, | ||
}; | ||
type Foo = Path<typeof obj>; // 'a.d' | 'a' | 'a.b' | `a.d.${number}` | `a.d.${number}.e` | ||
type Bar = PathValue<typeof obj, 'a.b'>; // 'string' | ||
``` |
@@ -1,20 +0,10 @@ | ||
export type Primitive = | ||
| null | ||
| undefined | ||
| string | ||
| number | ||
| boolean | ||
| symbol | ||
| bigint; | ||
export type Primitive = null | undefined | string | number | boolean | symbol | bigint; | ||
type ArrayKey = number; | ||
type IsTuple<T extends readonly any[]> = number extends T["length"] | ||
? false | ||
: true; | ||
type IsTuple<T extends readonly any[]> = number extends T['length'] ? false : true; | ||
type TupleKeys<T extends readonly any[]> = Exclude<keyof T, keyof any[]>; | ||
export type PathConcat< | ||
TKey extends string | number, | ||
TValue | ||
> = TValue extends Primitive | ||
export type PathConcat<TKey extends string | number, TValue> = TValue extends Primitive | ||
? `${TKey}` | ||
@@ -33,6 +23,3 @@ : `${TKey}` | `${TKey}.${Path<TValue>}`; | ||
type ArrayPathConcat< | ||
TKey extends string | number, | ||
TValue | ||
> = TValue extends Primitive | ||
type ArrayPathConcat<TKey extends string | number, TValue> = TValue extends Primitive | ||
? never | ||
@@ -77,8 +64,5 @@ : TValue extends readonly (infer U)[] | ||
obj: T, | ||
path: TPath | ||
path: TPath, | ||
): PathValue<T, TPath> { | ||
return path.split(".").reduce((acc, key) => acc[key], obj) as PathValue< | ||
T, | ||
TPath | ||
>; | ||
return path.split('.').reduce((acc, key) => acc[key], obj) as PathValue<T, TPath>; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
16697
12
91
5
98