import * as ArrayLikeCheck from "./array-like"; export enum ArrayErrorCode { NOT_ARRAY = "NOT_ARRAY" } export function checkArray (mixed : any) : undefined|ArrayErrorCode { if (mixed instanceof Array) { return undefined; } else { return ArrayErrorCode.NOT_ARRAY; } } export function checkArrayPredicate (mixed : any, predicate : (arr : any[]) => undefined|ErrorT) : undefined|ArrayErrorCode|ErrorT { const error = checkArray(mixed); if (error !== undefined) { return error; } return predicate(mixed); } export type MinLengthErrorCodeType = ArrayErrorCode|ArrayLikeCheck.MinLengthErrorCodeType; export function checkMinLength (mixed : any, min : number) : undefined|MinLengthErrorCodeType { return checkArrayPredicate(mixed, (arr) => { return ArrayLikeCheck.checkMinLength(arr, min); }); } export type MaxLengthErrorCodeType = ArrayErrorCode|ArrayLikeCheck.MaxLengthErrorCodeType; export function checkMaxLength (mixed : any, max : number) : undefined|MaxLengthErrorCodeType { return checkArrayPredicate(mixed, (arr) => { return ArrayLikeCheck.checkMaxLength(arr, max); }); } export type LengthErrorCodeType = ArrayErrorCode|ArrayLikeCheck.LengthErrorCodeType; export function checkLength (mixed : any, args : { min? : number, max? : number }) : undefined|LengthErrorCodeType { return checkArrayPredicate(mixed, (arr) => { return ArrayLikeCheck.checkLength(arr, args); }); } export type NonEmptyErrorCodeType = ArrayErrorCode|ArrayLikeCheck.NonEmptyErrorCodeType; export function checkNonEmpty (mixed : any) : undefined|NonEmptyErrorCodeType { return checkArrayPredicate(mixed, (arr) => { return ArrayLikeCheck.checkNonEmpty(arr); }); } export type ContainsErrorCodeType = ArrayErrorCode|ArrayLikeCheck.ContainsErrorCodeType; export function checkContains (mixed : any, x : any) : undefined|ContainsErrorCodeType { return checkArrayPredicate(mixed, (arr) => { return ArrayLikeCheck.checkContains(arr, x); }); } export type NoDuplicateErrorCodeType = ArrayErrorCode|ArrayLikeCheck.NoDuplicateErrorCodeType; export function checkNoDuplicate (mixed : any) : undefined|NoDuplicateErrorCodeType { return checkArrayPredicate(mixed, (arr) => { return ArrayLikeCheck.checkNoDuplicate(arr); }); }