Skip to content

Commit

Permalink
Enhance types
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Sep 29, 2024
1 parent dade554 commit 4c10765
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ describe('json-pointer', () => {
expect(get(obj, '')).toBe(obj)
})

Object.keys(rfcValues).forEach(function (p) {
Object.keys(rfcValues).forEach((p) => {
it('should work for "' + p + '"', () => {
const expectedValue = rfcValues[p]
expect(get(rfcExample, p)).toBe(expectedValue)
})
})

Object.keys(rfcParsed).forEach(function (p) {
Object.keys(rfcParsed).forEach((p) => {
const tokens = [...rfcParsed[p].tokens]
it('should work for ' + JSON.stringify(tokens), function () {
const expectedValue = rfcParsed[p].value
Expand Down Expand Up @@ -177,7 +177,7 @@ describe('json-pointer', () => {
})

describe('remove', () => {
Object.keys(rfcValues).forEach(function (p) {
Object.keys(rfcValues).forEach((p) => {
if (p === '' || p === '/foo/0') return

it('should work for "' + p + '"', () => {
Expand All @@ -198,7 +198,7 @@ describe('json-pointer', () => {
expect(() => get(pointer, rfcExample)).toThrow(Error)
})

Object.keys(rfcParsed).forEach(function (p) {
Object.keys(rfcParsed).forEach((p) => {
if (p === '' || p === '/foo/0') return

it(
Expand Down Expand Up @@ -350,9 +350,11 @@ describe('json-pointer', () => {
})

describe('parse and then #compile pointer', () => {
Object.keys(rfcValues).forEach(function (p) {
Object.keys(rfcValues).forEach((p) => {
it('should equal for "' + p + '"', () => {
expect(compile(parse(p))).toBe(p)
expect(
compile(parse(p) as Extract<PointerPath, Array<string>>)
).toBe(p)
})
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

export type PointerPath = string | Array<string>
export type JsonValue = any
export type JsonObject = any

/**
* Lookup a json pointer in an object
*/
export function get<T = JsonObject>(
obj: JsonObject | T,
pointer: PointerPath
) {
export function get<T = JsonObject>(obj: T, pointer: PointerPath) {
const refTokens = Array.isArray(pointer) ? pointer : parse(pointer)

for (let i = 0; i < refTokens.length; ++i) {
Expand All @@ -25,12 +24,14 @@ export function get<T = JsonObject>(
/**
* Sets a value on an object
*/
export function set(
obj: JsonObject,
export function set<T = JsonObject>(
obj: T,
pointer: PointerPath,
value: JsonValue
) {
const refTokens = Array.isArray(pointer) ? pointer : parse(pointer)
const refTokens = (
Array.isArray(pointer) ? pointer : parse(pointer)
) as Array<number | string>
let nextTok = refTokens[0]

if (refTokens.length === 0) {
Expand All @@ -52,16 +53,16 @@ export function set(
if (tok === '-' && Array.isArray(obj)) {
tok = obj.length
}
nextTok = refTokens[i + 1]
nextTok = refTokens[i + 1] as string

if (!(tok in obj)) {
if (!(tok in (obj as JsonObject))) {
if (nextTok.match(/^(\d+|-)$/)) {
obj[tok] = []
} else {
obj[tok] = {}
}
}
obj = obj[tok] as JsonObject
obj = obj[tok] as T
}

if (nextTok === '-' && Array.isArray(obj)) {
Expand All @@ -74,7 +75,7 @@ export function set(
/**
* Removes an attribute
*/
export function remove(obj: JsonObject, pointer: PointerPath) {
export function remove<T = JsonObject>(obj: T, pointer: PointerPath) {
const refTokens = Array.isArray(pointer) ? pointer : parse(pointer)
const finalToken = refTokens[refTokens.length - 1]
if (finalToken === undefined) {
Expand All @@ -97,7 +98,7 @@ export function remove(obj: JsonObject, pointer: PointerPath) {
/**
* Returns a (pointer -> value) dictionary for an object
*/
export function dict(obj: JsonObject, descend = null) {
export function dict<T = JsonObject>(obj: T, descend = null) {
const results = {}
walk(
obj,
Expand All @@ -112,7 +113,7 @@ export function dict(obj: JsonObject, descend = null) {
/**
* Iterates over an object
*/
export function walk(obj: JsonObject, iterator, descend = null) {
export function walk<T = JsonObject>(obj: T, iterator, descend = null) {
const refTokens = []

descend =
Expand Down Expand Up @@ -147,10 +148,7 @@ export function walk(obj: JsonObject, iterator, descend = null) {
/**
* Tests if an object has a value for a json pointer
*/
export function has<T = JsonObject>(
obj: JsonObject | T,
pointer: PointerPath
) {
export function has<T = JsonObject>(obj: T, pointer: PointerPath) {
try {
get<T>(obj, pointer)
} catch (e) {
Expand All @@ -162,21 +160,21 @@ export function has<T = JsonObject>(
/**
* Escapes a reference token
*/
export function escape(str) {
export function escape(str: string) {
return str.toString().replace(/~/g, '~0').replace(/\//g, '~1')
}

/**
* Unescape a reference token
*/
export function unescape(str) {
export function unescape(str: string) {
return str.replace(/~1/g, '/').replace(/~0/g, '~')
}

/**
* Converts a json pointer into a array of reference tokens
*/
export function parse(pointer) {
export function parse(pointer: Extract<PointerPath, string>): PointerPath {
if (pointer === '') {
return []
}
Expand All @@ -189,7 +187,7 @@ export function parse(pointer) {
/**
* Builds a json pointer from a array of reference tokens
*/
export function compile(refTokens) {
export function compile(refTokens: Extract<PointerPath, Array<string>>) {
if (refTokens.length === 0) {
return ''
}
Expand Down

0 comments on commit 4c10765

Please sign in to comment.