Skip to content

Commit 0e51711

Browse files
authored
Add index param to select (#198)
* add index param to select
1 parent 5445226 commit 0e51711

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

Diff for: cdn/radash.esm.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,13 @@ const objectify = (array, getKey, getValue = (item) => item) => {
170170
);
171171
};
172172
const select = (array, mapper, condition) => {
173-
return array.reduce((acc, item) => {
174-
if (!condition(item))
173+
if (!array)
174+
return [];
175+
return array.reduce((acc, item, index) => {
176+
if (!condition(item, index))
175177
return acc;
176-
return [...acc, mapper(item)];
178+
acc.push(mapper(item, index));
179+
return acc;
177180
}, []);
178181
};
179182
const max = (array, getter) => {

Diff for: cdn/radash.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,13 @@ var radash = (function (exports) {
173173
);
174174
};
175175
const select = (array, mapper, condition) => {
176-
return array.reduce((acc, item) => {
177-
if (!condition(item))
176+
if (!array)
177+
return [];
178+
return array.reduce((acc, item, index) => {
179+
if (!condition(item, index))
178180
return acc;
179-
return [...acc, mapper(item)];
181+
acc.push(mapper(item, index));
182+
return acc;
180183
}, []);
181184
};
182185
const max = (array, getter) => {

Diff for: cdn/radash.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "radash",
3-
"version": "10.2.0",
3+
"version": "10.3.0",
44
"description": "Functional utility library - modern, simple, typed, powerful",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.mjs",

Diff for: src/array.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,19 @@ export const objectify = <T, Key extends string | number | symbol, Value = T>(
217217
* Select performs a filter and a mapper inside of a reduce,
218218
* only iterating the list one time.
219219
*
220-
* Ex. select([1, 2, 3, 4], x => x*x, x > 2) == [9, 16]
220+
* @example
221+
* select([1, 2, 3, 4], x => x*x, x > 2) == [9, 16]
221222
*/
222223
export const select = <T, K>(
223224
array: readonly T[],
224-
mapper: (item: T) => K,
225-
condition: (item: T) => boolean
225+
mapper: (item: T, index: number) => K,
226+
condition: (item: T, index: number) => boolean
226227
) => {
227-
return array.reduce((acc, item) => {
228-
if (!condition(item)) return acc
229-
return [...acc, mapper(item)]
228+
if (!array) return []
229+
return array.reduce((acc, item, index) => {
230+
if (!condition(item, index)) return acc
231+
acc.push(mapper(item, index))
232+
return acc
230233
}, [] as K[])
231234
}
232235

Diff for: src/tests/array.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,24 @@ describe('array module', () => {
250250
})
251251

252252
describe('select function', () => {
253+
test('does not fail on bad input', () => {
254+
assert.deepEqual(
255+
_.select(
256+
null as unknown as any[],
257+
x => x,
258+
x => x
259+
),
260+
[]
261+
)
262+
assert.deepEqual(
263+
_.select(
264+
undefined as unknown as any[],
265+
x => x,
266+
x => x
267+
),
268+
[]
269+
)
270+
})
253271
test('returns mapped and filtered values', () => {
254272
const list = [
255273
{ group: 'a', word: 'hello' },
@@ -274,6 +292,15 @@ describe('array module', () => {
274292
)
275293
assert.deepEqual(result, [])
276294
})
295+
test('works with index', () => {
296+
const letters = ['a', 'b', 'c', 'd']
297+
const result = _.select(
298+
letters,
299+
(l, idx) => `${l}${idx}`,
300+
(l, idx) => idx > 1
301+
)
302+
assert.deepEqual(result, ['c2', 'd3'])
303+
})
277304
})
278305

279306
describe('max function', () => {

0 commit comments

Comments
 (0)