Skip to content

Instantly share code, notes, and snippets.

@stuf
Created January 9, 2018 09:53
Show Gist options
  • Save stuf/0975acd933cb834e1c26e7234e08c0f9 to your computer and use it in GitHub Desktop.
Save stuf/0975acd933cb834e1c26e7234e08c0f9 to your computer and use it in GitHub Desktop.
/**
* Takes a list of elements and functions (`vfn` and `kfn`) for transforming the string.
* The resulting elements from applying each function will become the `key` and `value` in
* the resulting object.
*
* For example:
*
* ```js
* const tokens = ['foo-bar', 'top-kek'];
* const Obj = foldTokens_(tokens, R.split('-'), R.toUpper);
*
* // => { 'FOO-BAR': ['foo', 'bar'], 'TOP-KEK': ['top', 'kek'] }
* ```
*
* Useful for re-mapping token strings into some desired form, e.g. turning pascal case
* string tokens into camelCase.
*
* `vfn` and `kfn` will fall back to using Sanctuary's `id` function if not supplied.
*
* @sig Foldable a => a -> (a -> b) -> (a -> String) -> StrMap b
*/
const foldTokens_ =
def('foldTokens_',
{ a: [Z.Foldable] },
[a, $.Function([a, b]), $.Function([a, $.String]), $.StrMap(b)],
(xs, vfn, kfn) => L.foldl((o, k) => L.set(kfn(k), vfn(k), o), {}, L.elems, xs));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment