Skip to content
David Cotelessa edited this page Mar 19, 2018 · 2 revisions

Form.l recipes

Formatters

Sorters

Sort State Areas

The strings in the array are comprised of two parts, a string and a number. ([CA10, CA8, AR14])

import sortRegex from 'form.l/sortRegex'
import toNumber from 'lodash/toNumber'
import padStart from 'lodash/padStart'

//create a Regex that matches the string and the number
const ltrNum = new RegExp(/(\D*)(\d*)/, 'i')

//create a function use lodash to sort first by the matched text, then the matched number
const sortTxtandNum = a => a.map(m => (isNaN(toNumber(m)) ? m : padStart(m, 4, '0'))).join('')

//since regex will if successful, return multiple matches, we set a range to find, not the full match
//but the string match and num match
export const sortStateAreas = sortRegex(ltrNum, 1, 3, sortTxtandNum)

sortRegex returns a function so we can apply different array of strings or a different order asc or desc

Validations

Clone this wiki locally