chore: Rearranged functions in utils.ts, additional typehinting
This commit is contained in:
parent
54bf820824
commit
323fbf4bf4
@ -45,7 +45,7 @@ export function shallowEqualObjects (objA: Record<string, any>, objB: Record<str
|
|||||||
* Given a string, convert snake_case to camelCase
|
* Given a string, convert snake_case to camelCase
|
||||||
* @param {String} string
|
* @param {String} string
|
||||||
*/
|
*/
|
||||||
export function snakeToCamel (string: string | any) {
|
export function snakeToCamel (string: string | any): string | any {
|
||||||
if (typeof string === 'string') {
|
if (typeof string === 'string') {
|
||||||
return string.replace(/([_][a-z0-9])/ig, ($1) => {
|
return string.replace(/([_][a-z0-9])/ig, ($1) => {
|
||||||
if (string.indexOf($1) !== 0 && string[string.indexOf($1) - 1] !== '_') {
|
if (string.indexOf($1) !== 0 && string[string.indexOf($1) - 1] !== '_') {
|
||||||
@ -57,6 +57,16 @@ export function snakeToCamel (string: string | any) {
|
|||||||
return string
|
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.
|
* Converts to array.
|
||||||
* If given parameter is not string, object ot array, result will be an empty 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 []
|
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
|
* Given a string or function, parse it and return an array in the format
|
||||||
* [fn, [...arguments]]
|
* [fn, [...arguments]]
|
||||||
@ -129,13 +123,19 @@ function parseRule (rule: any, rules: Record<string, any>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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] {
|
export function parseRules (validation: any[]|string, rules: any): any[] {
|
||||||
if (typeof ruleName === 'string' && /^[\^]/.test(ruleName.charAt(0))) {
|
if (typeof validation === 'string') {
|
||||||
return [snakeToCamel(ruleName.substr(1)), ruleName.charAt(0)]
|
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
|
* Check if
|
||||||
* @param {*} data
|
* @param {*} data
|
||||||
*/
|
*/
|
||||||
export function isScalar (data: any) {
|
export function isScalar (data: any): boolean {
|
||||||
switch (typeof data) {
|
switch (typeof data) {
|
||||||
case 'symbol':
|
case 'symbol':
|
||||||
case 'number':
|
case 'number':
|
||||||
|
Loading…
Reference in New Issue
Block a user