other components... (2)

This commit is contained in:
Paolo Arena
2021-09-16 21:08:02 +02:00
parent fcc4f61f07
commit f351673917
276 changed files with 17183 additions and 3371 deletions

23
src/validation/duplicate.ts Executable file
View File

@@ -0,0 +1,23 @@
type t = string | number
type fn = () => t[]
export function duplicate(matches: t[] | fn, ignoreCase: boolean): any {
if (Array.isArray(matches)) return factory(matches, ignoreCase)
return (value: any) => {
let cb = factory(matches(), ignoreCase)
return cb(value)
}
}
function factory(values: t[], ignoreCase: boolean): any {
return (value: any) => {
if (value === undefined || value === null || values.length === 0)
return true
else {
let flags = ignoreCase ? 'i' : ''
let exp = new RegExp(`^(${value})$`, flags)
return values.find(o => exp.test(o.toString())) === undefined
}
}
}