Skip to content

Commit

Permalink
Add index param to select (#198)
Browse files Browse the repository at this point in the history
* add index param to select
  • Loading branch information
sodiray authored Dec 16, 2022
1 parent 5445226 commit 0e51711
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
9 changes: 6 additions & 3 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,13 @@ const objectify = (array, getKey, getValue = (item) => item) => {
);
};
const select = (array, mapper, condition) => {
return array.reduce((acc, item) => {
if (!condition(item))
if (!array)
return [];
return array.reduce((acc, item, index) => {
if (!condition(item, index))
return acc;
return [...acc, mapper(item)];
acc.push(mapper(item, index));
return acc;
}, []);
};
const max = (array, getter) => {
Expand Down
9 changes: 6 additions & 3 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,13 @@ var radash = (function (exports) {
);
};
const select = (array, mapper, condition) => {
return array.reduce((acc, item) => {
if (!condition(item))
if (!array)
return [];
return array.reduce((acc, item, index) => {
if (!condition(item, index))
return acc;
return [...acc, mapper(item)];
acc.push(mapper(item, index));
return acc;
}, []);
};
const max = (array, getter) => {
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "10.2.0",
"version": "10.3.0",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
15 changes: 9 additions & 6 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,19 @@ export const objectify = <T, Key extends string | number | symbol, Value = T>(
* Select performs a filter and a mapper inside of a reduce,
* only iterating the list one time.
*
* Ex. select([1, 2, 3, 4], x => x*x, x > 2) == [9, 16]
* @example
* select([1, 2, 3, 4], x => x*x, x > 2) == [9, 16]
*/
export const select = <T, K>(
array: readonly T[],
mapper: (item: T) => K,
condition: (item: T) => boolean
mapper: (item: T, index: number) => K,
condition: (item: T, index: number) => boolean
) => {
return array.reduce((acc, item) => {
if (!condition(item)) return acc
return [...acc, mapper(item)]
if (!array) return []
return array.reduce((acc, item, index) => {
if (!condition(item, index)) return acc
acc.push(mapper(item, index))
return acc
}, [] as K[])
}

Expand Down
27 changes: 27 additions & 0 deletions src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,24 @@ describe('array module', () => {
})

describe('select function', () => {
test('does not fail on bad input', () => {
assert.deepEqual(
_.select(
null as unknown as any[],
x => x,
x => x
),
[]
)
assert.deepEqual(
_.select(
undefined as unknown as any[],
x => x,
x => x
),
[]
)
})
test('returns mapped and filtered values', () => {
const list = [
{ group: 'a', word: 'hello' },
Expand All @@ -274,6 +292,15 @@ describe('array module', () => {
)
assert.deepEqual(result, [])
})
test('works with index', () => {
const letters = ['a', 'b', 'c', 'd']
const result = _.select(
letters,
(l, idx) => `${l}${idx}`,
(l, idx) => idx > 1
)
assert.deepEqual(result, ['c2', 'd3'])
})
})

describe('max function', () => {
Expand Down

0 comments on commit 0e51711

Please sign in to comment.