forked from bryan-hunter/deepmerge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move utility functions to a utils file
- Loading branch information
Rebecca Stevens
committed
Nov 18, 2020
1 parent
4ad1ce6
commit a854d4c
Showing
2 changed files
with
53 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { deepmergeImpl } from './deepmerge'; | ||
|
||
function emptyTarget(value) { | ||
return Array.isArray(value) ? [] : {} | ||
} | ||
|
||
export function cloneUnlessOtherwiseSpecified(value, options) { | ||
return (options.clone !== false && options.isMergeable(value)) | ||
? deepmergeImpl(emptyTarget(value), value, options) | ||
: value | ||
} | ||
|
||
function getEnumerableOwnPropertySymbols(target) { | ||
return Object.getOwnPropertySymbols | ||
? Object.getOwnPropertySymbols(target).filter((symbol) => target.propertyIsEnumerable(symbol) | ||
) | ||
: []; | ||
} | ||
|
||
export function getKeys(target) { | ||
return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target)); | ||
} | ||
|
||
export function propertyIsOnObject(object, property) { | ||
try { | ||
return property in object; | ||
} catch (_) { | ||
return false; | ||
} | ||
} | ||
|
||
export function getMergeFunction(key, options) { | ||
if (!options.customMerge) { | ||
return deepmergeImpl | ||
} | ||
const customMerge = options.customMerge(key) | ||
return typeof customMerge === 'function' ? customMerge : deepmergeImpl | ||
} | ||
|
||
// Protects from prototype poisoning and unexpected merging up the prototype chain. | ||
export function propertyIsUnsafe(target, key) { | ||
return propertyIsOnObject(target, key) // Properties are safe to merge if they don't exist in the target yet, | ||
&& !(Object.hasOwnProperty.call(target, key) // unsafe if they exist up the prototype chain, | ||
&& Object.propertyIsEnumerable.call(target, key)) // and also unsafe if they're nonenumerable. | ||
} |