Skip to content

Commit

Permalink
fix: allow defaults to be given undefined for supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Apr 14, 2021
1 parent 29ffef1 commit 3654b58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/functions/defaults.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ describe('Defaults', () => {
expect(result.colors.red).toBe('#f00')
expect(result.build).toBe(true)
})

it('should allow supplied to be undefined', () => {
const defaultOptions = {
test: true
}

const results = defaults(undefined, defaultOptions)

expect(results.test).toBe(true)
})
})
4 changes: 2 additions & 2 deletions src/functions/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export type DeepPartial<T> = {
* @param supplied The supplied options, a `DeepPartial` of `defaultValues`.
* @param defaultValues The default values to fallback on. Should represent a full copy of the options object.
*/
export const defaults = <T extends {}>(supplied: DeepPartial<T>, defaultValues: T) => {
export const defaults = <T extends {}>(supplied: DeepPartial<T> | undefined, defaultValues: T) => {
const result: T = {...defaultValues}

Object.keys(supplied).forEach((key) => {
Object.keys(supplied || {}).forEach((key) => {
if(typeof (defaultValues as {[key: string]: string})[key] === 'object'){
(result as {[key: string]: string})[key] = defaults(
//eslint-disable-next-line
Expand Down

0 comments on commit 3654b58

Please sign in to comment.