Last active
October 8, 2018 19:08
-
-
Save IgnusG/d85232e1be3d9d455872320b3450ef23 to your computer and use it in GitHub Desktop.
Playing around with string templates in JS (for validation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isArray = (o) => o.map !== undefined; | |
const generateValidation = (string, ...validationLiterals) => { | |
const [ head, ...strings ] = isArray(string) ? string : [string]; | |
return (...validationArg) => { | |
const validationResults = validationLiterals.map((validation) => validation(...validationArg)); | |
if (validationResults.every((e) => e === null)) { | |
return { valid: true }; | |
} | |
return { valid: false, message: strings.reduce((ac, s, i) => { | |
return `${ac}${validationResults[ i ]}${s}` | |
}, head)}; | |
}; | |
}; | |
const isNotEmpty = generateValidation`Value of '${(value, field) => value !== '' ? null : field}' cannot be empty`; | |
const hasDuplicates = (array) => new Set(array).size !== array.length; | |
const findDuplicates = (array) => new Set(array.filter((e, i) => array.indexOf(e, i+1) !== -1)); | |
const filterUnderLimit = (limit, head, rest) => rest.reduce((acc, duplicate) => { | |
if ([...acc, duplicate].join(', ').length > limit) { return acc; } | |
return [...acc, duplicate]; | |
}, [ head ]); | |
const wrapValues = (char, array) => array.map((e) => `${char}${e}${char}`); | |
const areDuplicatesInKeywords = (limit) => (array) => { | |
if (!hasDuplicates(array)) { return null; } | |
const [ head, ...rest ] = findDuplicates(array); | |
if (head.length > limit) { return ''; } | |
const duplicates = filterUnderLimit(limit, head, rest); | |
if (duplicates.length < rest.length + 1) { return `: ${ wrapValues('\'', duplicates).join(', ') }...` } | |
return `: ${ wrapValues('\'', duplicates).join(', ') }`; | |
}; | |
const noDuplicateKeywords = generateValidation`Keywords cannot contains duplicates${areDuplicatesInKeywords(12)}`; | |
const normalNoDuplicateKeywords = generateValidation('Keyword duplicates are not allowed', areDuplicatesInKeywords(12)); | |
console.log(isNotEmpty("something", "Campaign Name")); | |
console.log(isNotEmpty("", "Campaign Name")); | |
console.log(noDuplicateKeywords(['something'])); | |
console.log(noDuplicateKeywords(['something', 'cool', 'something', 'awesome', 'cool', 'a' , 'a'])); | |
console.log(noDuplicateKeywords(['something', 'cool', 'cool'])); | |
console.log(noDuplicateKeywords(['something', 'something', 'awesome', 'cool'])); | |
console.log(noDuplicateKeywords(['somethingawesomeandcool', 'something', 'somethingawesomeandcool', 'cool'])); | |
console.log(normalNoDuplicateKeywords(['something'])); | |
console.log(normalNoDuplicateKeywords(['something', 'cool', 'something', 'awesome', 'cool', 'a' , 'a'])); | |
console.log(normalNoDuplicateKeywords(['something', 'cool', 'cool'])); | |
console.log(normalNoDuplicateKeywords(['something', 'something', 'awesome', 'cool'])); | |
console.log(normalNoDuplicateKeywords(['somethingawesomeandcool', 'something', 'somethingawesomeandcool', 'cool'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment