@clickbar/dot-diver
Advanced tools
Comparing version 1.0.1 to 1.0.3
@@ -1,20 +0,26 @@ | ||
function o(i, n) { | ||
return n.split(".").reduce((t, e) => t == null ? void 0 : t[e], i); | ||
const r = Object.prototype.hasOwnProperty; | ||
function d(s, n) { | ||
return n.split(".").reduce((o, t) => { | ||
if (!(typeof o != "object" || !r.call(o, t))) | ||
return o == null ? void 0 : o[t]; | ||
}, s); | ||
} | ||
function y(i, n, s) { | ||
const t = n.split("."), e = t.pop(); | ||
if (e === void 0) | ||
function l(s, n, y) { | ||
const o = n.split("."), t = o.pop(); | ||
if (t === void 0) | ||
throw new Error("Path is empty"); | ||
const h = t.reduce( | ||
(r, p) => r == null ? void 0 : r[p], | ||
i | ||
); | ||
if (h === void 0) | ||
throw new Error("Path is invalid"); | ||
h[e] = s; | ||
const f = o.reduce((e, i) => { | ||
if (typeof e != "object" || !r.call(e, i)) | ||
throw new Error(`Property ${i} is undefined`); | ||
const p = e == null ? void 0 : e[i]; | ||
if (p == null) | ||
throw new Error(`Property ${i} is undefined`); | ||
return p; | ||
}, s); | ||
f[t] = y; | ||
} | ||
export { | ||
o as getByPath, | ||
y as setByPath | ||
d as getByPath, | ||
l as setByPath | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@clickbar/dot-diver", | ||
"version": "1.0.1", | ||
"version": "1.0.3", | ||
"description": "Types and utilities to access object properties by dot notation.", | ||
"packageManager": "pnpm@8.8.0", | ||
"types": "dist/index.d.ts", | ||
@@ -40,9 +41,9 @@ "exports": { | ||
"devDependencies": { | ||
"@clickbar/eslint-config-typescript": "^4.3.0", | ||
"eslint": "^8.36.0", | ||
"prettier": "^2.8.7", | ||
"typescript": "^5.0.2", | ||
"vite": "^4.2.1", | ||
"vite-plugin-dts": "^2.1.0", | ||
"vitest": "^0.29.7" | ||
"@clickbar/eslint-config-typescript": "^9.0.1", | ||
"eslint": "^8.51.0", | ||
"prettier": "^3.0.3", | ||
"typescript": "^5.2.2", | ||
"vite": "^4.4.11", | ||
"vite-plugin-dts": "^3.6.0", | ||
"vitest": "^0.34.6" | ||
}, | ||
@@ -53,4 +54,6 @@ "scripts": { | ||
"lint": "eslint ./ --ext=js,ts", | ||
"fix": "eslint ./ --ext=js,ts --fix" | ||
"prettier": "prettier . --cache", | ||
"fix": "pnpm lint --fix && pnpm format", | ||
"format": "pnpm prettier --write" | ||
} | ||
} |
@@ -8,2 +8,3 @@ # Dot Diver ๐๐ | ||
Example: | ||
```typescript | ||
@@ -18,2 +19,3 @@ import { getByPath } from '@clickbar/dot-diver' | ||
``` | ||
<br> | ||
@@ -197,5 +199,5 @@ | ||
function getByPathDepth10<T extends SearchableObject, P extends Path<T, 5> & string>( | ||
function getByPathDepth5<T extends SearchableObject, P extends Path<T, 5> & string>( | ||
object: T, | ||
path: P | ||
path: P, | ||
): PathValue<T, P, 5> { | ||
@@ -205,15 +207,15 @@ return getByPath(object, path) as PathValue<T, P, 5> | ||
function setByPathDepth10< | ||
function setByPathDepth5< | ||
T extends SearchableObject, | ||
P extends Path<T, 5> & string, | ||
V extends PathValue<T, P, 5> | ||
V extends PathValue<T, P, 5>, | ||
>(object: T, path: P, value: V): void { | ||
setByPath(object, path, value as PathValue<T, P, 10>) | ||
setByPath(object, path, value as PathValue<T, P>) | ||
} | ||
export { getByPathDepth10 as getByPath, setByPathDepth10 as setByPath } | ||
export { getByPathDepth5 as getByPath, setByPathDepth5 as setByPath } | ||
``` | ||
The intersection between `Path<T, 5>` and `string` is necessary for TypeScript to successfully narrow down the type of `P` based on the user-provided `path` input. | ||
Without the intersection, the `path` would just be of type `Path<T, 10>` and `PathValueEntry` would be a union of all possible return types. | ||
Without the intersection, the `path` would just be of type `Path<T, 5>` and `PathValueEntry` would be a union of all possible return types. | ||
By using the intersection, TypeScript is forced to apply the `Path` constraints and infer the type from the provided user input. | ||
@@ -220,0 +222,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
40490
11
115
281
1