import { isPlainObject } from 'lodash' ; import { ValidationError , Validations , Values } from './interfaces' ; export function isArray ( value ) { return Array . isArray ( value ) ; } export function isObject ( value ) { return isPlainObject ( value ) ; } export function isTypeUndefined ( value ) { return typeof value === 'undefined' ; } export function isDate ( value ) { return value instanceof Date ; } export function isFunction ( value ) { return value !== null && typeof value === 'function' ; } export function isString ( value ) { return typeof value === 'string' ; } export function isNumber ( value ) { return typeof value === 'number' ; } export function isRegex ( value ) { return value instanceof RegExp ; } export function isValueStringEmpty ( value ) { return value === '' ; } export function isValueNullOrUndefined ( value ) { return value === null || value === undefined ; } export function isValueUndefined ( value ) { return value === undefined ; } export function noop ( ) { } export function protectAgainstParamReassignment ( value ) { if ( isObject ( value ) ) return { ... value } ; if ( isArray ( value ) ) return [ ... value ] ; return value ; } export function isSame ( a , b ) { if ( typeof a !== typeof b ) { return false ; } if ( isArray ( a ) && isArray ( b ) ) { if ( a . length !== b . length ) { return false ; } return a . every ( ( item , index ) => isSame ( item , b [ index ] ) ) ; } if ( isFunction ( a ) && isFunction ( b ) ) { return a . toString ( ) === b . toString ( ) ; } if ( isDate ( a ) && isDate ( b ) ) { return a . toString ( ) === b . toString ( ) ; } if ( isObject ( a ) && isObject ( b ) ) { if ( Object . keys ( a ) . length !== Object . keys ( b ) . length ) { return false ; } return Object . keys ( a ) . every ( ( key ) => isSame ( a [ key ] , b [ key ] ) ) ; } if ( isRegex ( a ) && isRegex ( b ) ) { return a . toString ( ) === b . toString ( ) ; } return a === b ; } interface RulesResult { errors : ValidationError [ ] ; failed : string [ ] ; success : string [ ] ; } export function runRules < V > ( value , currentValues , validations , validationRules ) { const results = { errors : [ ] , failed : [ ] , success : [ ] } ; Object . keys ( validations ) . forEach ( ( validationName ) => { const validationsVal = validations [ validationName ] ; const validationRulesVal = validationRules [ validationName ] ; const addToResults = ( validation ) => { if ( isString ( validation ) ) { results . errors . push ( validation ) ; results . failed . push ( validationName ) ; } else if ( ! validation ) { results . failed . push ( validationName ) ; } else { results . success . push ( validationName ) ; } } ; if ( validationRulesVal && isFunction ( validationsVal ) ) { throw new Error ( `Formsy does not allow you to override default validations: ${ validationName }` ) ; } if ( ! validationRulesVal && ! isFunction ( validationsVal ) ) { throw new Error ( `Formsy does not have the validation rule: ${ validationName }` ) ; } if ( isFunction ( validationsVal ) ) { return addToResults ( validationsVal ( currentValues , value ) ) ; } return addToResults ( validationRulesVal ( currentValues , value , validationsVal ) ) ; } ) ; return results ; } export function throttle ( callback , interval ) { let enableCall = true ; return function ( ... args ) { if ( ! enableCall ) return ; enableCall = false ; callback . apply ( this , args ) ; setTimeout ( ( ) => ( enableCall = true ) , interval ) ; } ; }