-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made it possible to define custom converters
Also made the code more modular
- Loading branch information
Showing
12 changed files
with
186 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { isBoolean, isString, isRegExp } from 'lodash'; | ||
import { toArray, toRegExp, toBoolean, toInteger, toObject } from '../converters'; | ||
|
||
export default function automaticConverter(value, name) { | ||
if (isBoolean(value)) { | ||
return (input) => toBoolean(input, value, name); | ||
} else if (isRegExp(value)) { | ||
return toRegExp; | ||
} else if (Array.isArray(value)) { | ||
return toArray; | ||
} else if (Number.isInteger(value)) { | ||
return toInteger; | ||
} else if (!isString(value) && (!value || Object.keys(value).length === 0)) { | ||
return toObject; | ||
} | ||
|
||
return (input) => input; | ||
} |
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 |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export toArray from './to-array'; | ||
export toRegExp from './to-regexp'; | ||
export toBoolean from './to-boolean'; | ||
export toInteger from './to-integer'; | ||
export toObject from './to-object'; | ||
export automatic from './automatic'; |
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,29 @@ | ||
import chalk from 'chalk'; | ||
import { isBoolean } from 'lodash'; | ||
|
||
import { warningLabel, feedbackMessage } from '../helpers/style'; | ||
|
||
/** | ||
* Given an input the function will return a boolean. | ||
* | ||
* @param {object} input - The input to be converted. | ||
* @param {boolean} defaultValue - Default value to use if conversion fails. | ||
* @param {string} name - The name of of what is converted. | ||
* | ||
* @returns {bool} - The converted result. | ||
*/ | ||
export default function toBoolean(input, defaultValue, name) { | ||
if (isBoolean(input)) { | ||
return input; | ||
} | ||
if (input === 'true' || input === 'false') { | ||
return input === 'true'; | ||
} | ||
|
||
console.log(feedbackMessage( | ||
warningLabel('Warning', 'Conversion Failed'), | ||
`Invalid value given for ${chalk.bold(name)}. Will use the default ${chalk.bold(defaultValue)}.` | ||
)); | ||
|
||
return defaultValue; | ||
} |
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,16 @@ | ||
import { isInteger } from 'lodash'; | ||
|
||
/** | ||
* Given an input the function will return an number. | ||
* | ||
* @param {object} input - The input to be converted. | ||
* | ||
* @returns {number} - The converted result. | ||
*/ | ||
export default function toInteger(input) { | ||
if (isInteger(input)) { | ||
return input; | ||
} | ||
|
||
return parseInt(input, 10); | ||
} |
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,16 @@ | ||
import { isPlainObject } from 'lodash'; | ||
|
||
/** | ||
* Given an input the function will return an object. | ||
* | ||
* @param {object} input - The input to be converted. | ||
* | ||
* @returns {object} - The converted result. | ||
*/ | ||
export default function toObject(input) { | ||
if (isPlainObject(input)) { | ||
return input; | ||
} | ||
|
||
return JSON.parse(input); | ||
} |
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
Oops, something went wrong.