Last active
November 9, 2016 02:56
-
-
Save halan/516b94c5ebc26ac26f7118c0ff1c8c54 to your computer and use it in GitHub Desktop.
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
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}), {} | |
) |
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 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) |
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 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