diff --git a/src/libs/utils.ts b/src/libs/utils.ts index 9c4c1db..2bdd2bc 100644 --- a/src/libs/utils.ts +++ b/src/libs/utils.ts @@ -45,7 +45,7 @@ export function shallowEqualObjects (objA: Record, objB: Record { if (string.indexOf($1) !== 0 && string[string.indexOf($1) - 1] !== '_') { @@ -57,6 +57,16 @@ export function snakeToCamel (string: string | any) { return string } +/** + * Return the rule name with the applicable modifier as an array. + */ +function parseModifier (ruleName: any): [string|any, string|null] { + if (typeof ruleName === 'string' && /^[\^]/.test(ruleName.charAt(0))) { + return [snakeToCamel(ruleName.substr(1)), ruleName.charAt(0)] + } + return [snakeToCamel(ruleName), null] +} + /** * Converts to array. * If given parameter is not string, object ot array, result will be an empty array. @@ -78,22 +88,6 @@ export function arrayify (item: any): any[] { return [] } -/** - * Given an array or string return an array of callables. - * @param {array|string} validation - * @param {array} rules and array of functions - * @return {array} an array of functions - */ -export function parseRules (validation: any, rules: any): any[] { - if (typeof validation === 'string') { - return parseRules(validation.split('|'), rules) - } - if (!Array.isArray(validation)) { - return [] - } - return validation.map(rule => parseRule(rule, rules)).filter(f => !!f) -} - /** * Given a string or function, parse it and return an array in the format * [fn, [...arguments]] @@ -129,13 +123,19 @@ function parseRule (rule: any, rules: Record) { } /** - * Return the rule name with the applicable modifier as an array. + * Given an array or string return an array of callables. + * @param {array|string} validation + * @param {array} rules and array of functions + * @return {array} an array of functions */ -function parseModifier (ruleName: any): [string|any, string|null] { - if (typeof ruleName === 'string' && /^[\^]/.test(ruleName.charAt(0))) { - return [snakeToCamel(ruleName.substr(1)), ruleName.charAt(0)] +export function parseRules (validation: any[]|string, rules: any): any[] { + if (typeof validation === 'string') { + return parseRules(validation.split('|'), rules) } - return [snakeToCamel(ruleName), null] + if (!Array.isArray(validation)) { + return [] + } + return validation.map(rule => parseRule(rule, rules)).filter(f => !!f) } /** @@ -222,7 +222,7 @@ export function regexForFormat (format: string) { * Check if * @param {*} data */ -export function isScalar (data: any) { +export function isScalar (data: any): boolean { switch (typeof data) { case 'symbol': case 'number':