Last active
July 6, 2020 16:28
-
-
Save gustavoguichard/be74eebb0ccbc8e90064ceac7232d267 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import compact from 'lodash/compact' | |
import get from 'lodash/get' | |
import head from 'lodash/head' | |
import orderBy from 'lodash/orderBy' | |
const getFirstValue = (el, path) => { | |
const paths = path.split('|') | |
const values = paths.map(path => get(el, path)) | |
return head(compact(values)) | |
} | |
const padStringWithLeadingNumbers = (str, size = 9) => { | |
const matches = String(str).match(/^(\d+)(.+)?/) | |
if (matches) { | |
const [_, num, word] = matches | |
const largenum = num.padStart(size, '0') | |
return [largenum, word].join('') | |
} | |
return str | |
} | |
const naturalSortingValue = el => { | |
const str = getFirstValue(el, 'customFieldValues.sorting|name') | |
return padStringWithLeadingNumbers(str, 9) | |
} | |
orderBy([], naturalSortingValue) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment