Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ const objectify = (array, getKey, getValue = (item) => item) => {
return acc;
}, {});
};
const mapify = (array, getKey, getValue = (item) => item) => {
const map = /* @__PURE__ */ new Map();
for (const item of array) {
map.set(getKey(item), getValue(item));
}
return map;
};
const select = (array, mapper, condition) => {
if (!array)
return [];
Expand Down Expand Up @@ -937,4 +944,4 @@ const trim = (str, charsToTrim = " ") => {
return str.replace(regex, "");
};

export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, mapify, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
8 changes: 8 additions & 0 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ var radash = (function (exports) {
return acc;
}, {});
};
const mapify = (array, getKey, getValue = (item) => item) => {
const map = /* @__PURE__ */ new Map();
for (const item of array) {
map.set(getKey(item), getValue(item));
}
return map;
};
const select = (array, mapper, condition) => {
if (!array)
return [];
Expand Down Expand Up @@ -991,6 +998,7 @@ var radash = (function (exports) {
exports.mapEntries = mapEntries;
exports.mapKeys = mapKeys;
exports.mapValues = mapValues;
exports.mapify = mapify;
exports.max = max;
exports.memo = memo;
exports.merge = merge;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ export const objectify = <T, Key extends string | number | symbol, Value = T>(
}, {} as Record<Key, Value>)
}

/**
* Builds a {@link Map} from an array, similar to {@link objectify} but returning
* a Map (which supports non-string keys and preserves insertion order). See
* issue #401.
*/
export const mapify = <T, Key, Value = T>(
array: readonly T[],
getKey: (item: T) => Key,
getValue: (item: T) => Value = item => item as unknown as Value
): Map<Key, Value> => {
const map = new Map<Key, Value>()
for (const item of array) {
map.set(getKey(item), getValue(item))
}
return map
}

/**
* Select performs a filter and a mapper inside of a reduce,
* only iterating the list one time.
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
iterate,
last,
list,
mapify,
max,
merge,
min,
Expand Down
31 changes: 31 additions & 0 deletions src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,34 @@ describe('array module', () => {
})
})
})

describe('mapify function', () => {
test('builds a Map using a key function with default value', () => {
const map = _.mapify(
[
{ id: 1, name: 'a' },
{ id: 2, name: 'b' }
],
i => i.id
)
assert.strictEqual(map.size, 2)
assert.deepEqual(map.get(1), { id: 1, name: 'a' })
assert.deepEqual(map.get(2), { id: 2, name: 'b' })
})
test('builds a Map using key and value functions', () => {
const map = _.mapify(
[1, 2, 3],
n => n,
n => n * 10
)
assert.strictEqual(map.get(2), 20)
})
test('returns an empty Map for an empty array', () => {
assert.strictEqual(_.mapify([], () => 0).size, 0)
})
test('keeps the last value for duplicate keys', () => {
const map = _.mapify(['a', 'b', 'a'], x => x)
assert.strictEqual(map.size, 2)
assert.strictEqual(map.get('a'), 'a')
})
})
Loading