diff --git a/packages/dnb-eufemia/src/extensions/forms/utils/json-pointer/__tests__/json-pointer.test.ts b/packages/dnb-eufemia/src/extensions/forms/utils/json-pointer/__tests__/json-pointer.test.ts index bbc433b4054..4123a3a0fbd 100644 --- a/packages/dnb-eufemia/src/extensions/forms/utils/json-pointer/__tests__/json-pointer.test.ts +++ b/packages/dnb-eufemia/src/extensions/forms/utils/json-pointer/__tests__/json-pointer.test.ts @@ -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 @@ -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 + '"', () => { @@ -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( @@ -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>) + ).toBe(p) }) }) }) diff --git a/packages/dnb-eufemia/src/extensions/forms/utils/json-pointer/json-pointer.ts b/packages/dnb-eufemia/src/extensions/forms/utils/json-pointer/json-pointer.ts index 12e42b4fba1..8584a5495a0 100644 --- a/packages/dnb-eufemia/src/extensions/forms/utils/json-pointer/json-pointer.ts +++ b/packages/dnb-eufemia/src/extensions/forms/utils/json-pointer/json-pointer.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ + export type PointerPath = string | Array export type JsonValue = any export type JsonObject = any @@ -5,10 +7,7 @@ export type JsonObject = any /** * Lookup a json pointer in an object */ -export function get( - obj: JsonObject | T, - pointer: PointerPath -) { +export function get(obj: T, pointer: PointerPath) { const refTokens = Array.isArray(pointer) ? pointer : parse(pointer) for (let i = 0; i < refTokens.length; ++i) { @@ -25,12 +24,14 @@ export function get( /** * Sets a value on an object */ -export function set( - obj: JsonObject, +export function set( + obj: T, pointer: PointerPath, value: JsonValue ) { - const refTokens = Array.isArray(pointer) ? pointer : parse(pointer) + const refTokens = ( + Array.isArray(pointer) ? pointer : parse(pointer) + ) as Array let nextTok = refTokens[0] if (refTokens.length === 0) { @@ -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)) { @@ -74,7 +75,7 @@ export function set( /** * Removes an attribute */ -export function remove(obj: JsonObject, pointer: PointerPath) { +export function remove(obj: T, pointer: PointerPath) { const refTokens = Array.isArray(pointer) ? pointer : parse(pointer) const finalToken = refTokens[refTokens.length - 1] if (finalToken === undefined) { @@ -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(obj: T, descend = null) { const results = {} walk( obj, @@ -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(obj: T, iterator, descend = null) { const refTokens = [] descend = @@ -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( - obj: JsonObject | T, - pointer: PointerPath -) { +export function has(obj: T, pointer: PointerPath) { try { get(obj, pointer) } catch (e) { @@ -162,21 +160,21 @@ export function has( /** * 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 { if (pointer === '') { return [] } @@ -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>) { if (refTokens.length === 0) { return '' }