Skip to content

Commit

Permalink
fix(utils): clone usage errors
Browse files Browse the repository at this point in the history
- clone cannot be used when constructors have unknown required parameters (e.g. custom classes)

Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Sep 17, 2023
1 parent e10348e commit 373a2d4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 46 deletions.
22 changes: 10 additions & 12 deletions src/utils/assign-with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type {
OwnPropertyKey
} from '#src/types'
import cast from './cast'
import clone from './clone'
import define from './define'
import descriptor from './descriptor'
import properties from './properties'
Expand All @@ -28,23 +27,22 @@ import reduce from './reduce'
type AssignCustomizer = Fn<[any, any, OwnPropertyKey]>

/**
* Assigns own properties of one or more `source` objects to a target object.
* Assigns all enumerable own properties of one or more `source` objects to a
* target object.
*
* A `customizer` is used to produce assigned values. The initial `base` object
* **will not** be modified.
*
* Source objects are applied from left to right. Subsequent sources overwrite
* property assignments of previous sources.
*
* New properties are *defined* rather than *assigned*. Both enumerable and
* non-enumerable properties will be copied from source objects. Inherited
* properties are not copied.
* property assignments of previous sources. New properties are *defined* rather
* than *assigned*.
*
* **Note**: The return type may differ from the actual return value when using
* a `customizer`. Additionally, TypeScript does not track inheritance. The
* return type may also differ from the actual return value when source objects
* contain inherited properties (e.g. `Map`, `Set`). In such cases, the return
* type will include more keys than present on the return value.
* a `customizer`. Additionally, TypeScript does not track enumerability or
* property inheritance. The return type may also differ from the actual return
* value when source objects contain non-enumerable or inherited properties
* (e.g. `Map`, `Set`). In such cases, the return type will include more keys
* than present on the return value.
*
* @see {@linkcode Assign}
* @see {@linkcode AssignCustomizer}
Expand All @@ -71,7 +69,7 @@ const assignWith = <T extends Objectify<any>, U extends readonly ObjectCurly[]>(
value: customizer(acc[key], src[key], key)
})
}, acc)
}, cast(clone(base)))
}, cast({ ...base }))
}

export { assignWith as default, type AssignCustomizer }
20 changes: 9 additions & 11 deletions src/utils/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ import type { Assign, ObjectCurly, Objectify } from '#src/types'
import assignWith from './assign-with'

/**
* Assigns own properties of one or more `source` objects to a target object.
* The initial `base` object **will not** be modified.
* Assigns all enumerable own properties of one or more `source` objects to a
* target object. The initial `base` object **will not** be modified.
*
* Source objects are applied from left to right. Subsequent sources overwrite
* property assignments of previous sources.
* property assignments of previous sources. New properties are *defined* rather
* than *assigned*.
*
* New properties are *defined* rather than *assigned*. Both enumerable and
* non-enumerable properties will be copied from source objects. Inherited
* properties are not copied.
*
* **Note**: TypeScript does not track inheritance. The return type may differ
* from the actual return value when source objects contain inherited properties
* (e.g. `Map`, `Set`). In such cases, the return type will include more keys
* than present on the return value.
* **Note**: TypeScript does not track enumerability or property inheritance.
* The return type may differ from the actual return value when source objects
* contain non-enumerable or inherited properties (e.g. `Map`, `Set`). In such
* cases, the return type will include more keys than present on the return
* value.
*
* @see {@linkcode Assign}
* @see {@linkcode assignWith}
Expand Down
17 changes: 9 additions & 8 deletions src/utils/merge-with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type {
OwnPropertyKey
} from '#src/types'
import cast from './cast'
import clone from './clone'
import define from './define'
import descriptor from './descriptor'
import isObjectPlain from './is-object-plain'
Expand All @@ -29,8 +28,9 @@ import reduce from './reduce'
type MergeCustomizer = Fn<[any, any, OwnPropertyKey]>

/**
* Recursively merges own properties of one or more `source` objects into a
* target object. The initial `base` object **will not** be modified.
* Recursively merges all enumerable own properties of one or more `source`
* objects into a target object. The initial `base` object **will not** be
* modified.
*
* A `customizer` is be used to produce merged values. Plain object properties
* are merged recursively. Other objects and value types are overridden by
Expand All @@ -39,10 +39,11 @@ type MergeCustomizer = Fn<[any, any, OwnPropertyKey]>
* Source objects are applied from left to right. Subsequent sources overwrite
* property assignments of previous sources.
*
* **Note**: TypeScript does not track inheritance. The return type may differ
* from the actual return value when source objects contain inherited properties
* (e.g. `Map`, `Set`). In such cases, the return type will include more keys
* than present on the return value.
* **Note**: TypeScript does not track enumerability or property inheritance.
* The return type may differ from the actual return value when source objects
* contain non-enumerable or inherited properties (e.g. `Map`, `Set`). In such
* cases, the return type will include more keys than present on the return
* value.
*
* @see {@linkcode Merge}
* @see {@linkcode MergeCustomizer}
Expand Down Expand Up @@ -89,7 +90,7 @@ const mergeWith = <T extends Objectify<any>, U extends readonly ObjectCurly[]>(
value: customizer(outgoing, incoming, key)
})
}, acc)
}, cast(clone(base)))
}, cast({ ...base }))
}

export { mergeWith as default, type MergeCustomizer }
17 changes: 8 additions & 9 deletions src/utils/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import type { Merge, ObjectCurly, Objectify } from '#src/types'
import mergeWith from './merge-with'

/**
* Recursively merges own properties of one or more `source` objects into a
* target object. The initial `base` object **will not** be modified.
*
* Plain object properties are merged recursively. Other objects and value types
* are overridden by assignment.
* Recursively merges all enumerable own properties of one or more `source`
* objects into a target object. The initial `base` object **will not** be
* modified.
*
* Source objects are applied from left to right. Subsequent sources overwrite
* property assignments of previous sources.
*
* **Note**: TypeScript does not track inheritance. The return type may differ
* from the actual return value when source objects contain inherited properties
* (e.g. `Map`, `Set`). In such cases, the return type will include more keys
* than present on the return value.
* **Note**: TypeScript does not track enumerability or property inheritance.
* The return type may differ from the actual return value when source objects
* contain non-enumerable or inherited properties (e.g. `Map`, `Set`). In such
* cases, the return type will include more keys than present on the return
* value.
*
* @see {@linkcode Merge}
* @see {@linkcode mergeWith}
Expand Down
3 changes: 1 addition & 2 deletions src/utils/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import type { Omit, PropertyKey, Spread } from '#src/types'
import cast from './cast'
import clone from './clone'
import reduce from './reduce'

/**
Expand Down Expand Up @@ -34,7 +33,7 @@ const omit = <T, K extends PropertyKey>(
return reduce(keys, (acc, key) => {
Reflect.deleteProperty(acc, key)
return acc
}, cast({ ...clone(target) }))
}, cast({ ...target }))
}

export default omit
3 changes: 1 addition & 2 deletions src/utils/overwrite-with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import type { Fn, ObjectCurly, Overwrite, OwnPropertyKey } from '#src/types'
import cast from './cast'
import clone from './clone'
import define from './define'
import descriptor from './descriptor'
import hasOwn from './has-own'
Expand Down Expand Up @@ -60,7 +59,7 @@ const overwriteWith = <T extends ObjectCurly, U extends readonly ObjectCurly[]>(
})
: acc
}, acc)
}, cast(clone(base)))
}, cast({ ...base }))
}

export { overwriteWith as default, type OverwriteCustomizer }
3 changes: 1 addition & 2 deletions src/utils/reverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import type { Reverse } from '#src/types'
import cast from './cast'
import clone from './clone'
import isArray from './is-array'
import join from './join'
import split from './split'
Expand All @@ -26,7 +25,7 @@ const reverse = <T extends string | readonly unknown[]>(
target: T
): Reverse<T> => {
return isArray(target)
? cast([...clone(target)].reverse())
? cast([...target].reverse())
: cast(join(split(target, '').reverse(), ''))
}

Expand Down

0 comments on commit 373a2d4

Please sign in to comment.