mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-22 10:52:11 +00:00
feat: introduce objectRemap function
This commit introduces a new function `objectRemap` in `object-util` package. It takes an object as input and applies the provided mapping function to each key-value pair. It produces a new object with transformed keys and values. The `objectRemap` function is more advanced than `objectMap` as it transforms an object back into another object. It works on objects and arrays. If multiple keys are the same, only the last value will be set by default, but enabling the `strict` option will throw an error if the same key is set twice. This commit also updates the import statement in tests for this new function. Signed-off-by: Avior <f.bouillon@aptatio.com>
This commit is contained in:
parent
7cc3ca98c1
commit
fd24924d10
@ -1,6 +1,6 @@
|
|||||||
/// <reference types="jest" />
|
/// <reference types="jest" />
|
||||||
|
|
||||||
import { isObject, objectClean, objectClone, objectEqual, objectKeys, objectLoop, objectMap, objectOmit, objectSet, objectSize, objectSort, objectValues } from '../src/ObjectUtil'
|
import { isObject, objectClean, objectClone, objectEqual, objectKeys, objectLoop, objectMap, objectOmit, objectRemap, objectSet, objectSize, objectSort, objectValues } from '../src/ObjectUtil'
|
||||||
|
|
||||||
describe('Throw if parameter is not an object', () => {
|
describe('Throw if parameter is not an object', () => {
|
||||||
it('should works', () => {
|
it('should works', () => {
|
||||||
@ -386,3 +386,30 @@ describe('Is Object Tests', () => {
|
|||||||
})
|
})
|
||||||
it('array is an object', () => expect(isObject([])).toBe(true))
|
it('array is an object', () => expect(isObject([])).toBe(true))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
describe('object remap tests', () => {
|
||||||
|
it('should works on objects', () => {
|
||||||
|
expect(objectRemap({a: "pouet"}, (value, key) => {
|
||||||
|
return {key: key + 'a', value}
|
||||||
|
})).toEqual({aa: "pouet"})
|
||||||
|
})
|
||||||
|
it('should works on arrays', () => {
|
||||||
|
const pouet: [string] = ['pokemon']
|
||||||
|
expect(objectRemap(pouet, (value, key: number) => {
|
||||||
|
return {key: key + 2, value}
|
||||||
|
})).toEqual({2: "pokemon"})
|
||||||
|
})
|
||||||
|
it('should replace value', () => {
|
||||||
|
expect(objectRemap({a: 'a', b: 'b'}, (value) => {
|
||||||
|
return {key: 'b', value}
|
||||||
|
})).toEqual({b: 'b'})
|
||||||
|
})
|
||||||
|
it('should throw an error in strict mode', () => {
|
||||||
|
expect(() => {
|
||||||
|
objectRemap({a: 'a', b: 'b'}, (value) => {
|
||||||
|
return {key: 'b', value}
|
||||||
|
}, {strict: true})
|
||||||
|
}).toThrow()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
@ -22,6 +22,38 @@ export function objectMap<T = any, J = any, K extends BasicObjectKeys = BasicObj
|
|||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a more advanced map function that transform an object back into another object
|
||||||
|
*
|
||||||
|
* note: if multiple key are the same only the last value will be set unless options.strict is enabled
|
||||||
|
*
|
||||||
|
* note2: for an array you will have to add manual typing to the `key` like this: `key: number`
|
||||||
|
*
|
||||||
|
* warn: the value is not a clone
|
||||||
|
*
|
||||||
|
* @param obj the object to remap (it will not be changed)
|
||||||
|
* @param fn the function to run through
|
||||||
|
* @param options optionnal options that change how the function works
|
||||||
|
* @param options.strict (default: false) enabling this will throw an error if the same key is set twice
|
||||||
|
* @returns a not deeply cloned object with it's key/values set from the [fn] function
|
||||||
|
*/
|
||||||
|
export function objectRemap<T = any, J extends BasicObject = BasicObject, K extends BasicObjectKeys = BasicObjectKeys>(
|
||||||
|
obj: BasicObject<K, T>,
|
||||||
|
fn: (value: T, key: K, index: number) => {key: keyof J, value: J[typeof key]},
|
||||||
|
options?: {strict?: boolean}
|
||||||
|
): J {
|
||||||
|
mustBeObject(obj)
|
||||||
|
const clone: J = {} as any
|
||||||
|
objectLoop(obj, (item, oldKey, index) => {
|
||||||
|
const { key, value } = fn(item, oldKey, index)
|
||||||
|
if (options?.strict && key in clone) {
|
||||||
|
throw new Error('objectRemap strict mode active, you can\'t remap the same key twice')
|
||||||
|
}
|
||||||
|
clone[key] = value
|
||||||
|
})
|
||||||
|
return clone
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loop through the object
|
* Loop through the object
|
||||||
*
|
*
|
||||||
@ -296,6 +328,7 @@ export function mustBeObject(item: any): item is BasicObject {
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
objectMap,
|
objectMap,
|
||||||
|
objectRemap,
|
||||||
objectLoop,
|
objectLoop,
|
||||||
objectToArray,
|
objectToArray,
|
||||||
objectKeys,
|
objectKeys,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user