Upgrade dependencies (#23)

This commit is contained in:
Jaco Greeff
2017-11-30 20:25:06 +01:00
committed by GitHub
parent 7feeaf2476
commit 6d6ecb9ec2
7 changed files with 39 additions and 45 deletions
+23 -14
View File
@@ -8,11 +8,8 @@ const formatNoop = require('./noop');
const arrayTypeRegex = /\[\]$/;
function format (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
const isArrayType: boolean = arrayTypeRegex.test(type);
const formatter: FormatterFunction = isArrayType
? formatters[type.replace(arrayTypeRegex, '')]
: formatters[type];
function formatSingleType (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
const formatter: FormatterFunction = formatters[type];
if (isUndefined(formatter)) {
console.warn(`Unable to find default formatter for '${type}', falling back to noop`);
@@ -20,19 +17,31 @@ function format (formatters: { [any]: FormatterFunction }, type: string, value:
return formatNoop(value);
}
if (isArrayType && !Array.isArray(value)) {
try {
return formatter(value);
} catch (error) {
throw new Error(`Error formatting '${value}' as '${type}': ${error.message}`);
}
}
function formatArrayType (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
if (!Array.isArray(value)) {
throw new Error(`Unable to format non-array '${value}' as '${type}'`);
}
try {
if (isArrayType) {
return (value: Array<any>).map((value) => formatter(value));
} else {
return formatter(value);
}
} catch (error) {
throw new Error(`Error formatting '${value}' as '${type}'': ${error.message}`);
type = type.replace(arrayTypeRegex, '');
return (value: Array<any>).map((value) => {
return formatSingleType(formatters, type, value);
});
}
function format (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
if (arrayTypeRegex.test(type)) {
return formatArrayType(formatters, type, value);
}
return formatSingleType(formatters, type, value);
}
function formatArray (formatters: { [any]: FormatterFunction }, types: Array<string>, values: Array<any>): Array<any> {