typed-path
Advanced tools
Comparing version 2.1.2 to 2.2.0-beta.0
export declare type TypedPathKey = string | symbol | number; | ||
export declare type TypedPathFunction<T> = (...args: any[]) => T; | ||
export declare type TypedPathHandlersConfig = Record<string, <T extends TypedPathHandlersConfig = Record<never, never>>(path: string[], additionalHandlers?: T) => any>; | ||
export declare type TypedPathHandlersConfig = Record<string, <T extends TypedPathHandlersConfig = Record<never, never>>(path: TypedPathKey[], additionalHandlers?: T) => any>; | ||
declare const defaultHandlersConfig: { | ||
$path: (path: string[]) => string; | ||
$raw: (path: string[]) => string[]; | ||
toString: (path: string[]) => () => string; | ||
[Symbol.toStringTag]: (path: string[]) => () => string; | ||
valueOf: (path: string[]) => () => string; | ||
$path: (path: TypedPathKey[]) => string; | ||
/** | ||
* @deprecated This method transforms all path chunks to strings. | ||
* If you need the path with numbers and Symbols - use $rawPath | ||
*/ | ||
$raw: (path: TypedPathKey[]) => string[]; | ||
$rawPath: (path: TypedPathKey[]) => (string | number | symbol)[]; | ||
toString: (path: TypedPathKey[]) => () => string; | ||
[Symbol.toStringTag]: (path: TypedPathKey[]) => () => string; | ||
valueOf: (path: TypedPathKey[]) => () => string; | ||
}; | ||
@@ -23,3 +28,3 @@ export declare type TypedPathHandlers<T extends TypedPathHandlersConfig> = { | ||
}) & TypedPathHandlers<TPH>; | ||
export declare function typedPath<T, K extends TypedPathHandlersConfig = Record<never, never>>(additionalHandlers?: K, path?: string[], defaultsApplied?: boolean): TypedPathWrapper<T, K & typeof defaultHandlersConfig>; | ||
export declare function typedPath<T, K extends TypedPathHandlersConfig = Record<never, never>>(additionalHandlers?: K, path?: TypedPathKey[], defaultsApplied?: boolean): TypedPathWrapper<T, K & typeof defaultHandlersConfig>; | ||
export {}; |
23
index.js
@@ -25,7 +25,7 @@ "use strict"; | ||
return path.reduce(function (current, next) { | ||
if (+next === +next) { | ||
if (typeof next === 'number') { | ||
current += "[" + next + "]"; | ||
} | ||
else { | ||
current += current === '' ? "" + next : "." + next; | ||
current += current === '' ? next.toString() : "." + next.toString(); | ||
} | ||
@@ -37,3 +37,8 @@ return current; | ||
$path: function (path) { return pathToString(path); }, | ||
$raw: function (path) { return path; }, | ||
/** | ||
* @deprecated This method transforms all path chunks to strings. | ||
* If you need the path with numbers and Symbols - use $rawPath | ||
*/ | ||
$raw: function (path) { return path.map(function (chunk) { return chunk.toString(); }); }, | ||
$rawPath: function (path) { return path; }, | ||
toString: function (path) { return function () { return pathToString(path); }; } | ||
@@ -44,6 +49,7 @@ }, | ||
_a); | ||
var emptyObject = {}; | ||
function typedPath(additionalHandlers, path, defaultsApplied) { | ||
if (path === void 0) { path = []; } | ||
if (defaultsApplied === void 0) { defaultsApplied = false; } | ||
return new Proxy({}, { | ||
return new Proxy(emptyObject, { | ||
get: function (target, name) { | ||
@@ -60,3 +66,10 @@ var handlersConfig; | ||
} | ||
return typedPath(handlersConfig, __spreadArrays(path, [name.toString()]), true); | ||
var newChunk = name; | ||
if (typeof newChunk === 'string') { | ||
var nameAsNumber = +newChunk; | ||
if (nameAsNumber === nameAsNumber) { | ||
newChunk = nameAsNumber; | ||
} | ||
} | ||
return typedPath(handlersConfig, __spreadArrays(path, [newChunk]), true); | ||
} | ||
@@ -63,0 +76,0 @@ }); |
74
index.ts
@@ -1,7 +0,7 @@ | ||
function pathToString(path: string[]): string { | ||
return path.reduce((current, next) => { | ||
if (+next === +next) { | ||
function pathToString(path: TypedPathKey[]): string { | ||
return path.reduce<string>((current, next) => { | ||
if (typeof next === 'number') { | ||
current += `[${next}]`; | ||
} else { | ||
current += current === '' ? `${next}` : `.${next}`; | ||
current += current === '' ? next.toString() : `.${next.toString()}`; | ||
} | ||
@@ -17,11 +17,19 @@ | ||
export type TypedPathHandlersConfig = Record<string, <T extends TypedPathHandlersConfig = Record<never, never>>(path: string[], additionalHandlers?: T) => any>; | ||
export type TypedPathHandlersConfig = Record< | ||
string, | ||
<T extends TypedPathHandlersConfig = Record<never, never>>(path: TypedPathKey[], additionalHandlers?: T) => any | ||
>; | ||
const defaultHandlersConfig = { | ||
$path: (path: string[]) => pathToString(path), | ||
$raw: (path: string[]) => path, | ||
toString: (path: string[]) => () => pathToString(path), | ||
[Symbol.toStringTag]: (path: string[]) => () => pathToString(path), | ||
valueOf: (path: string[]) => () => pathToString(path), | ||
} | ||
$path: (path: TypedPathKey[]) => pathToString(path), | ||
/** | ||
* @deprecated This method transforms all path chunks to strings. | ||
* If you need the path with numbers and Symbols - use $rawPath | ||
*/ | ||
$raw: (path: TypedPathKey[]) => path.map((chunk) => chunk.toString()), | ||
$rawPath: (path: TypedPathKey[]) => path, | ||
toString: (path: TypedPathKey[]) => () => pathToString(path), | ||
[Symbol.toStringTag]: (path: TypedPathKey[]) => () => pathToString(path), | ||
valueOf: (path: TypedPathKey[]) => () => pathToString(path) | ||
}; | ||
@@ -34,17 +42,22 @@ export type TypedPathHandlers<T extends TypedPathHandlersConfig> = { | ||
? { | ||
[index: number]: TypedPathWrapper<Z, TPH>; | ||
} | ||
[index: number]: TypedPathWrapper<Z, TPH>; | ||
} | ||
: T extends TypedPathFunction<infer RET> | ||
? { | ||
(): TypedPathWrapper<RET, TPH>; | ||
} & { | ||
[P in keyof Required<RET>]: TypedPathWrapper<RET[P], TPH>; | ||
} | ||
: { | ||
[P in keyof Required<T>]: TypedPathWrapper<T[P], TPH>; | ||
} | ||
) & TypedPathHandlers<TPH>; | ||
? { | ||
(): TypedPathWrapper<RET, TPH>; | ||
} & { | ||
[P in keyof Required<RET>]: TypedPathWrapper<RET[P], TPH>; | ||
} | ||
: { | ||
[P in keyof Required<T>]: TypedPathWrapper<T[P], TPH>; | ||
}) & | ||
TypedPathHandlers<TPH>; | ||
export function typedPath<T, K extends TypedPathHandlersConfig = Record<never, never>>(additionalHandlers?: K, path: string[] = [], defaultsApplied: boolean = false): TypedPathWrapper<T, K & typeof defaultHandlersConfig> { | ||
return <TypedPathWrapper<T, K & typeof defaultHandlersConfig>>new Proxy({}, { | ||
const emptyObject = {}; | ||
export function typedPath<T, K extends TypedPathHandlersConfig = Record<never, never>>( | ||
additionalHandlers?: K, | ||
path: TypedPathKey[] = [], | ||
defaultsApplied: boolean = false | ||
): TypedPathWrapper<T, K & typeof defaultHandlersConfig> { | ||
return <TypedPathWrapper<T, K & typeof defaultHandlersConfig>>new Proxy(emptyObject, { | ||
get(target: T, name: TypedPathKey) { | ||
@@ -56,3 +69,3 @@ let handlersConfig: TypedPathHandlersConfig; | ||
} else { | ||
handlersConfig = { ...(additionalHandlers ?? {}), ...defaultHandlersConfig }; | ||
handlersConfig = {...(additionalHandlers ?? {}), ...defaultHandlersConfig}; | ||
} | ||
@@ -64,5 +77,14 @@ | ||
return typedPath(handlersConfig, [...path, name.toString()], true); | ||
let newChunk = name; | ||
if (typeof newChunk === 'string') { | ||
const nameAsNumber = +newChunk; | ||
if (nameAsNumber === nameAsNumber) { | ||
newChunk = nameAsNumber; | ||
} | ||
} | ||
return typedPath(handlersConfig, [...path, newChunk], true); | ||
} | ||
}); | ||
} |
{ | ||
"name": "typed-path", | ||
"version": "2.1.2", | ||
"version": "2.2.0-beta.0", | ||
"description": "Type safe object string paths for typescript.", | ||
@@ -30,4 +30,5 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "mocha --require ts-node/register index.spec.ts", | ||
"test": "jest index.spec.ts", | ||
"lint": "tslint 'src/**/*.ts' 'src/**/*.tsx'", | ||
"format": "prettier --parser typescript --write index.ts index.spec.ts", | ||
"prepush": "yarn lint && yarn build && git diff --exit-code", | ||
@@ -38,7 +39,6 @@ "prepare": "yarn test && yarn build", | ||
"devDependencies": { | ||
"@types/chai": "^3.4.34", | ||
"@types/mocha": "^2.2.39", | ||
"chai": "^3.5.0", | ||
"husky": "^0.13.1", | ||
"mocha": "^6.2.0", | ||
"jest": "^26.6.3", | ||
"prettier": "2.2.0", | ||
"ts-jest": "^26.4.4", | ||
"ts-node": "^9.0.0", | ||
@@ -45,0 +45,0 @@ "tslint": "^4.4.2", |
@@ -34,3 +34,5 @@ # Typed Path | ||
[@dcbrwn](https://github.com/dcbrwn) | ||
If you need a raw path, which is of type `string[]` - you can get it using `$raw` special field. | ||
If you need a raw path, which is of type `string[]` - you can get it using `$raw` special field. | ||
*Deprecated, since it transforms symbols and numbers to strings, which might be not an expected behavior (the method name is "raw"). | ||
Please use `.$rawPath`* | ||
```js | ||
@@ -40,2 +42,8 @@ console.log(tp<TestType>().a.b.c.d.$raw); // this will output ["a", "b", "c", "d"] | ||
#### .$rawPath | ||
If you need a raw path, which is of type `(string | number | Symbol)[]` - you can get it using `$rawPath` special field. | ||
```js | ||
console.log(tp<TestType>().a.b[5].c.d.$rawPath); // this will output ["a", "b", 5, "c", "d"] | ||
``` | ||
#### Additional handlers | ||
@@ -42,0 +50,0 @@ [@nick-lvov-dev](https://github.com/nick-lvov-dev) |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
13678
7
176
83
2