-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(options): merge options instead of replace them
resolves #12
- Loading branch information
Showing
7 changed files
with
265 additions
and
23 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
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,186 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`buildOptions should allow methods to be changed 1`] = ` | ||
Object { | ||
"debug": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`buildOptions should overwrite defaults 1`] = ` | ||
Object { | ||
"debug": Object { | ||
"injectFileName": false, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"error": Object { | ||
"injectFileName": false, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"exception": Object { | ||
"injectFileName": false, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"info": Object { | ||
"injectFileName": false, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"log": Object { | ||
"injectFileName": false, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"warn": Object { | ||
"injectFileName": false, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`buildOptions should return defaults for invalid args 1`] = ` | ||
Object { | ||
"debug": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"error": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"exception": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"info": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"log": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"warn": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`buildOptions should return defaults for invalid args 2`] = ` | ||
Object { | ||
"debug": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"error": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"exception": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"info": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"log": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"warn": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`buildOptions should return defaults for invalid args 3`] = ` | ||
Object { | ||
"debug": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"error": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"exception": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"info": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"log": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"warn": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`buildOptions should return defaults for invalid args 4`] = ` | ||
Object { | ||
"debug": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"error": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"exception": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"info": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"log": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
"warn": Object { | ||
"injectFileName": true, | ||
"injectScope": true, | ||
"injectVariableName": true, | ||
}, | ||
} | ||
`; |
Empty file.
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,36 @@ | ||
const defaultSettings = { | ||
injectScope: true, | ||
injectVariableName: true, | ||
injectFileName: true, | ||
}; | ||
|
||
const defaultMethods = ['debug', 'error', 'exception', 'info', 'log', 'warn']; | ||
|
||
// this should deep merge in the furture when we are dealing with more than just flags | ||
const mergeOptions = options => { | ||
const sanitizedOptions = Object.keys(options || {}) | ||
.filter(key => Object.keys(defaultSettings).includes(key)) | ||
.reduce( | ||
(acc, key) => ({ | ||
...acc, | ||
[key]: options[key], | ||
}), | ||
{} | ||
); | ||
return Object.assign({}, defaultSettings, sanitizedOptions); | ||
}; | ||
|
||
export const buildOptions = userOptions => { | ||
// remove ignore patterns from settings since it has been consumed already | ||
// eslint-disable-next-line no-unused-vars | ||
const { methods, ignorePatterns, ...options } = userOptions || {}; | ||
|
||
// output spreads the flags over each method | ||
// in the future this could be expanded to allow method level config | ||
return (methods || defaultMethods).reduce((acc, method) => { | ||
return { | ||
...acc, | ||
[method]: mergeOptions(options), | ||
}; | ||
}, {}); | ||
}; |
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,24 @@ | ||
import { buildOptions } from './pluginOptions'; | ||
|
||
describe('buildOptions', () => { | ||
test('should return defaults for invalid args', () => { | ||
let actual = buildOptions(); | ||
expect(actual).toMatchSnapshot(); | ||
actual = buildOptions(undefined); | ||
expect(actual).toMatchSnapshot(); | ||
actual = buildOptions({}); | ||
expect(actual).toMatchSnapshot(); | ||
actual = buildOptions({ unknownFlag: false }); | ||
expect(actual).toMatchSnapshot(); | ||
}); | ||
|
||
test('should allow methods to be changed', () => { | ||
let actual = buildOptions({ methods: ['debug'] }); | ||
expect(actual).toMatchSnapshot(); | ||
}); | ||
|
||
test('should overwrite defaults', () => { | ||
let actual = buildOptions({ injectFileName: false }); | ||
expect(actual).toMatchSnapshot(); | ||
}); | ||
}); |