fix(core-extensions): Apply mapKeys only to plain objects

This commit is contained in:
T. R. Bernstein
2025-07-04 12:10:59 +02:00
parent 4ceefae94f
commit 0d52c33b57
2 changed files with 7 additions and 4 deletions

View File

@@ -3,8 +3,8 @@ import type { MappedKeys } from '@/internal/mapped-keys.type.js'
function isRecord(item: any): item is Record<string, any> { function isRecord(item: any): item is Record<string, any> {
const isNotNull = item != null const isNotNull = item != null
const isObject = typeof item === 'object' const isObject = typeof item === 'object'
const isNotAnArray = !Array.isArray(item) const isPlainObject = isNotNull && isObject && Object.getPrototypeOf(item) === Object.prototype
return isNotNull && isNotAnArray && isObject return isPlainObject
} }
function mapKeysInArrayItems<T extends any[], R extends string>(arr: T, transform: (key: string) => R) { function mapKeysInArrayItems<T extends any[], R extends string>(arr: T, transform: (key: string) => R) {

View File

@@ -39,17 +39,20 @@ describe('mapKeys', () => {
it('maps keys with unusual values', async () => { it('maps keys with unusual values', async () => {
const func = () => 1 const func = () => 1
const date = new Date()
const obj = { const obj = {
a: func, a: func,
b: undefined, b: undefined,
c: null c: null,
d: date
} }
const result = mapKeys(obj, (key) => `${key}_`) const result = mapKeys(obj, (key) => `${key}_`)
expect(result).toEqual({ expect(result).toEqual({
a_: func, a_: func,
b_: undefined, b_: undefined,
c_: null c_: null,
d_: date
}) })
}) })
}) })