0.1.3 • Published 5 years ago

map-assoc-core v0.1.3

Weekly downloads
5
License
ISC
Repository
github
Last release
5 years ago

map-assoc-core

Just the very basics of map-assoc: Map the values of a collection (array or object) and carry the keys.

For more magic, check the map-assoc-fancy package.

Promises support

No. Use p-props instead.

Usage

from test/usage.js:

var amap = require('map-assoc-core'), eq = require('equal-pmb'), preset;

function upper(s) { return String(s).toUpperCase(); }
function upperOrDouble(s) { return (s === +s ? s * 2 : s.toUpperCase()); }

eq(amap({ cat: 'meow', dog: 'woof', lion: 'roar' }, upper),
        { cat: 'MEOW', dog: 'WOOF', lion: 'ROAR' });

preset = amap(upper);
eq(preset({ cat: 'meow', dog: 'woof', lion: 'roar' }),
          { cat: 'MEOW', dog: 'WOOF', lion: 'ROAR' });

eq(amap([ 'mon', 'tue', 'wed' ], upper),
        [ 'MON', 'TUE', 'WED' ]);
eq(amap({ '0': 'mon', '1': 'tue', '2': 'wed', length: 3 }, upper),
        { '0': 'MON', '1': 'TUE', '2': 'WED', length: '3' });
eq(amap({ '0': 'mon', '1': 'tue', '2': 'wed', length: 3 }, upperOrDouble),
        { '0': 'MON', '1': 'TUE', '2': 'WED', length: 6 });

eq(preset('bar'), { '0': 'B', '1': 'A', '2': 'R' });
eachFor(function testFalseyValues(x) {
  eq(amap(x, upper), x);
  eq(preset(x), x);
}, [null, undefined, false, 0, NaN, '']);

Q&A

  • How is this different from lodash.mapValues?

It supports presets (see usage example above). It preserves the array-ness or null-object-ness of inputs (see test/cmp.lodash.js).

  • My string/buffer is a collection, too! It maps position numbers to characters/octets!

You're totally right, and map-assoc-fancy will handle that just as you'd expect, i.e. map them to a String/Buffer. The …-core package (this one) will return plain objects with number keys. (Exception: The empty string is returned verbatim.)

  • Can I use my GeneratorFunction as a collection?

Should work, since it should be an object, so it can hold keys, right? If you want to iterate over its results instead, have a look at map-assoc-fancy. (:TODO: publish that, and write the actual function name here.)

License

ISC