form.l
utility functions for forms, including functions, formatters, normalizers, sanitizers, sorters, and validations.
Functions
diff(baseObj = object, targetObj = object) - deep difference between two objects. Uses lodash's transform, isEqual, isObject.
const objA = a: 1 b: c: 1 d: 0 e: 0const objB = a: 1 b: c: 1 d: 1 e: 1 const objC = // => { b: { d: 1 }, e: 1 }
ifValidText(text = str, arr = [func]) - returns function(txt). if array of functions are true , it returns defined text, else it returns undefined. Useful for validation functions.
const includesText = txtconst isThisText = txt === 'Match me.'const validIncludeText = const validText =
Formatters
Formatters in this context, formats string and returns it.
includePrefix(prefix = string) - returns function(str) to add prefix to beginning of str. Useful for currency.
const currencyPrefix = const result = // => '$100'
includeSuffix(suffix = string) - returns function(str) to add suffix to end of str. Useful for percentages.
const percentagePrefix = const result = // => '100%'
Parsers
Parsers parse strings, returning a string that conforms to a certain pattern.
regFind(rgx, num = 0) - returns function(str) to parse string with regex. Use num to tell function which array index should be returned.
const trimZeros = const result = // => '1111'
regReplace(rgx, str = '') - returns function(str) to replace strings with regex. Use str to tell function what to replace found matches with.
const trimZeros = const result = // => 'hmmm'
Sanitizers
Sanitizers are functions to clean data, either while getting from database to use in a form, or to ready data for posting to a database.
Sorters
Sorters are functions to sort list arrays.
sortRegex(rgx, min = 0, max = 0, func) - returns function(arr = [], order = 'asc') for sorting to apply func to sliced matches of the regex. Useful to apply
const ltrNum = // 'i'const sortTxtandNum = a const sortStateAreas = const result = // => ['BC01', 'BC02', 'CA11', 'CB01']
Validations
Validations will return a boolean if data is valid. Used mostly to validate field data, but might be used across form data.
ifBlank(str) - returns true if str is blank ('').
const result = // => trueconst result2 = // => false
ifRegex(rgx) - returns function(str) that returns true if rgx finds a match within str.
const ltrnum = // => true const result = // => trueconst result2 = // => false
ifYear(year, range = 0) - returns function(str) if str is a year between 'year' and 'year plus range'.
const year2000 = // => true const result = // => trueconst result2 = // => false