@sagold/json-pointer
Advanced tools
Comparing version 7.0.0 to 7.1.0
@@ -11,3 +11,3 @@ import { JsonPointer, JsonPath, JsonData } from "./types"; | ||
*/ | ||
export declare function get<T = any>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue: T): T; | ||
export declare function get<T = any>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue?: T): T | undefined; | ||
export declare function get<T = unknown>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue: T): T; | ||
export declare function get<T = unknown>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue?: T): T | undefined; |
@@ -10,2 +10,2 @@ import { JsonPointer, JsonPath } from "./types"; | ||
*/ | ||
export declare function remove<T = any>(data: T, pointer: JsonPointer | JsonPath, keepArrayIndices?: boolean): T; | ||
export declare function remove<T = unknown>(data: T, pointer: JsonPointer | JsonPath, keepArrayIndices?: boolean): T; |
@@ -5,2 +5,2 @@ /** | ||
*/ | ||
export declare function removeUndefinedItems<T = any>(array: Array<T>): Array<T>; | ||
export declare function removeUndefinedItems<T = unknown>(array: T[]): T[]; |
import { JsonPointer, JsonPath, JsonData } from "./types"; | ||
export declare function set<T = JsonData>(data: T, pointer: JsonPointer | JsonPath, value: any): T; | ||
export declare function set<T = JsonData>(data: T, pointer: JsonPointer | JsonPath, value: unknown): T; |
export type JsonPointer = string; | ||
export type JsonPath = Array<string>; | ||
export type JsonData = any; | ||
export type JsonPath = string[]; | ||
export type JsonData = unknown; |
@@ -14,5 +14,5 @@ import { split } from "./split"; | ||
*/ | ||
export function get<T = any>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue: T): T; | ||
export function get<T = any>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue?: T): T | undefined; | ||
export function get<T = any>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue = undefined): T | undefined { | ||
export function get<T = unknown>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue: T): T; | ||
export function get<T = unknown>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue?: T): T | undefined; | ||
export function get<T = unknown>(data: JsonData, pointer: JsonPointer | JsonPath, defaultValue = undefined): T | undefined { | ||
if (pointer == null || data == null) { | ||
@@ -22,3 +22,3 @@ return defaultValue; | ||
if (isRoot(pointer)) { | ||
return data; | ||
return data as T; | ||
} | ||
@@ -29,6 +29,6 @@ const result = run(data, split(pointer)); | ||
} | ||
return result; | ||
return result as T; | ||
} | ||
function run<T = any>(data: JsonData, path: JsonPath): T | undefined { | ||
function run<T = unknown>(data: JsonData, path: JsonPath): T | undefined { | ||
const property = path.shift(); | ||
@@ -40,3 +40,3 @@ if (data === undefined) { | ||
} | ||
return data; | ||
return data as T; | ||
} |
@@ -14,3 +14,3 @@ import { split } from "./split"; | ||
*/ | ||
export function remove<T = any>( | ||
export function remove<T = unknown>( | ||
data: T, | ||
@@ -17,0 +17,0 @@ pointer: JsonPointer | JsonPath, |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
export function removeUndefinedItems<T = any>(array: Array<T>): Array<T> { | ||
export function removeUndefinedItems<T = unknown>(array: T[]): T[] { | ||
let i = 0; | ||
@@ -8,0 +8,0 @@ let skip = 0; |
@@ -19,3 +19,3 @@ import { split } from "./split"; | ||
pointer: JsonPointer | JsonPath, | ||
value: any | ||
value: unknown | ||
): T { | ||
@@ -22,0 +22,0 @@ if (pointer == null) { |
export type JsonPointer = string; | ||
export type JsonPath = Array<string>; | ||
export type JsonData = any; | ||
export type JsonPath = string[]; | ||
export type JsonData = unknown; |
{ | ||
"name": "@sagold/json-pointer", | ||
"version": "7.0.0", | ||
"version": "7.1.0", | ||
"description": "json pointer - failsafe data retrieval from js and json objects", | ||
@@ -32,13 +32,9 @@ "main": "dist/jsonPointer.js", | ||
"devDependencies": { | ||
"@types/chai": "^4.3.14", | ||
"@eslint/js": "^9.20.0", | ||
"eslint": "^9.20.1", | ||
"typescript": "^5.7.3", | ||
"typescript-eslint": "^8.24.0", | ||
"@types/mocha": "^10.0.6", | ||
"@types/node": "^22.10.6", | ||
"@typescript-eslint/eslint-plugin": "^8.20.0", | ||
"@typescript-eslint/parser": "^8.20.0", | ||
"chai": "^4.4.1", | ||
"eslint": "^8.57.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-optimize-regex": "^1.2.1", | ||
"eslint-plugin-promise": "^7.2.1", | ||
"mocha": "^10.4.0", | ||
"mocha": "^11.1.0", | ||
"nyc": "^17.1.0", | ||
@@ -48,3 +44,2 @@ "terser-webpack-plugin": "^5.3.11", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.7.3", | ||
"webpack": "^5.97.1", | ||
@@ -51,0 +46,0 @@ "webpack-cli": "^6.0.1" |
@@ -122,3 +122,3 @@ <h1 align="left"><img src="./docs/sagold-json-pointer.png" width="100%" alt="@sagold/json-pointer"></h1> | ||
`set` will create arrays when encountering a number | ||
`set` will **create arrays** when encountering a number | ||
@@ -137,3 +137,3 @@ ```js | ||
append items using empty array syntax `[]` | ||
**append items** using empty array syntax `[]` | ||
@@ -145,3 +145,3 @@ ```js | ||
create object using object syntax `{index}` | ||
**create object** using object syntax `{index}` | ||
@@ -161,27 +161,2 @@ ```js | ||
#### array | ||
Per default `set` creates objects for each each missing data. Thus, a pointer to `/list/1` will create an object with `{ list: { 1: {} } }`. If you want to specify array-data you will need to wrap the array-index in array-brackets: | ||
```ts | ||
pointer.set({}, '/list/[1]', 42); | ||
// { list: [, 42] } | ||
``` | ||
Omitting the index from the array-brackets will **append** the item: | ||
```ts | ||
pointer.set({}, '/list/[]', 42); | ||
// { list: [42] } | ||
``` | ||
which also works for existing lists: | ||
```ts | ||
pointer.set({ list: ["first"] }, '/list/[]', 42); | ||
// { list: ["first", 42] } | ||
``` | ||
### remove | ||
@@ -188,0 +163,0 @@ |
13
25
437
28646
301