Skip to content

Instantly share code, notes, and snippets.

@halan
Last active November 9, 2016 02:56
Show Gist options
  • Save halan/516b94c5ebc26ac26f7118c0ff1c8c54 to your computer and use it in GitHub Desktop.
Save halan/516b94c5ebc26ac26f7118c0ff1c8c54 to your computer and use it in GitHub Desktop.
str.split(',')
.map(section => section.split(';'))
.map(([page, rel]) => [
Number(rxPage.exec(page)[1]),
rxRel.exec(rel)[1]
]).reduce((acc, [ page, rel ]) => (
{...acc, [rel]: page}), {}
)
const splitInSections = str => str.split(',')
const splitFields = section => section.split(';')
const formatFields = ([page, rel]) => [
Number(rxPage.exec(page)[1]),
rxRel.exec(rel)[1]
]
const indexing = (indexed, [page, rel]) => (
{ ...indexed, [rel]: page}
)
splitInSections(str)
.map(splitFields)
.map(formatFields)
.reduce(indexing)
const rxPage = /[?|&]page=(\d+)/
const rxRel = /rel="(.+)"/
const throwMustBeString = () => throw new Error('str must be a string')
const throwMustBeValid = () => throw new Error('str is a invalid string')
const isEmpty = str => str.length === 0
const isString = str => typeof str !== 'string'
const assertString = str => isString(str) ? throwMustBeString() : str
const assertValid = str => !isEmpty ? throwMustBeValid : str
const asserts = str => assertString(assertValid(str))
const splitInSections = str => str.split(',')
const splitFields = section => section.split(';')
const regexFields = ([page, rel]) => [ rxPage.exec(page), rxRel.exec(rel) ]
const mount = (indexed, [[, page], [, rel]]) => ({ ...indexed, [rel]: Number(page)})
const parser = str =>
splitInSections(asserts(str))
.map(splitFields)
.map(regexFields)
.reduce(mount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment