Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid isObject for internal use #18

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/typed/is-object.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ isObject(['hello']) // => false
isObject(null) // => false
isObject({ say: 'hello' }) // => true
```

**Beware:** This function returns `false` for objects created with `Object.create(null)`.
9 changes: 6 additions & 3 deletions src/object/assign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from 'radashi'
import { isPlainObject } from 'radashi'

/**
* Merges two objects together recursivly into a new object applying
Expand All @@ -10,10 +10,13 @@ export const assign = <X extends Record<string | symbol | number, any>>(
override: X
): X => {
if (!initial || !override) return initial ?? override ?? {}
const merged = { ...initial }
const proto = Object.getPrototypeOf(initial)
const merged = proto
? { ...initial }
: Object.assign(Object.create(proto), initial)
for (const key in override) {
if (Object.prototype.hasOwnProperty.call(override, key)) {
merged[key] = isObject(initial[key])
merged[key] = isPlainObject(initial[key])
? assign(initial[key], override[key])
: override[key]
}
Expand Down
5 changes: 2 additions & 3 deletions src/object/keys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isArray } from 'radashi'
import { isObject } from 'radashi'
import { isArray, isPlainObject } from 'radashi'

/**
* Get a string list of all key names that exist in an object (deep).
Expand All @@ -11,7 +10,7 @@ import { isObject } from 'radashi'
export const keys = <TValue extends object>(value: TValue): string[] => {
if (!value) return []
const getKeys = (nested: any, paths: string[]): string[] => {
if (isObject(nested)) {
if (isPlainObject(nested)) {
return Object.entries(nested).flatMap(([k, v]) =>
getKeys(v, [...paths, k])
)
Expand Down
9 changes: 9 additions & 0 deletions src/object/tests/assign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ describe('assign function', () => {
const result = _.assign({}, { b: 'y' })
expect(result).toEqual({ b: 'y' })
})
test('works with Object.create(null)', () => {
const object = { a: Object.create(null) }
object.a.b = 1

const result = _.assign(object, { a: { c: 2 } })

expect(result).toEqual({ a: { b: 1, c: 2 } })
expect(Object.getPrototypeOf(result.a)).toBe(null)
})
})
8 changes: 8 additions & 0 deletions src/object/tests/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ describe('keys function', () => {
'enemies.0.power'
])
})
test('works with Object.create(null)', () => {
const object = Object.create(null)
object.a = 1
object.b = [2]
object.c = { d: 3 }
const result = _.keys(object)
expect(result).toEqual(['a', 'b.0', 'c.d'])
})
})