Created
August 23, 2017 05:59
-
-
Save sricho/5563ba949b21cc132305da027779ba7a 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
const test = require('tape') | |
const reduce = require('lodash').reduce | |
// [Element] -> {Name: Value} | |
const parseFormValues = (elements = []) => ( | |
elements.reduce((result, { name, value }) => { | |
if (!name || !value) { return result } | |
return Object.assign(result, { | |
[name]: value, | |
}) | |
}, {}) | |
) | |
// { Key: Value } -> String | |
const stringifyFormValues = (values) => ( | |
reduce(values, (result, currentValue, key) => ( | |
result + `&${key}=${currentValue}` | |
), '').substring(1) | |
) | |
test('parseFormValues', (t) => { | |
t.equals(typeof parseFormValues, 'function') | |
t.end() | |
}) | |
test('parseFormValues with no values', (t) => { | |
const values = parseFormValues() | |
t.deepEqual(values, {}) | |
t.end() | |
}); | |
test('parseFormValues with empty array', (t) => { | |
const values = parseFormValues([]) | |
t.deepEqual(values, {}) | |
t.end() | |
}); | |
test('parseFormValues with values', (t) => { | |
const values = parseFormValues([{ | |
name: 'awesome', | |
value: 'sauce' | |
}, { | |
name: 'lame', | |
value: 'sausage', | |
}]) | |
t.deepEqual(values, { awesome: 'sauce', lame: 'sausage' }) | |
t.end() | |
}); | |
test('stringifyFormValues', (t) => { | |
t.equals(typeof stringifyFormValues, 'function') | |
t.end() | |
}) | |
test('stringifyFormValues with no values', (t) => { | |
const s = stringifyFormValues() | |
t.equal(s, '') | |
t.end() | |
}); | |
test('stringifyFormValues with empty object', (t) => { | |
const s = stringifyFormValues({}) | |
t.equal(s, '') | |
t.end() | |
}); | |
test('stringifyFormValues with values', (t) => { | |
const s = stringifyFormValues({ awesome: 'sauce', lame: 'sausage' }) | |
t.equal(s, 'awesome=sauce&lame=sausage') | |
t.end() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment