Skip to content

Commit b45d100

Browse files
authored
Merge pull request #6 from stackbox-dev/fix/sumBy-default-array
🐛 default to empty array in sumBy & countBy
2 parents 43a11c5 + 08da452 commit b45d100

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/lang.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,22 @@ export const maxBy = <T>(
232232
};
233233

234234
export const sumBy = <T>(
235-
items: ReadonlyArray<T>,
235+
items: ReadonlyArray<T> | undefined,
236236
fn: (x: T) => number,
237237
): number => {
238238
let s = 0;
239-
for (const item of items) {
239+
for (const item of items ?? []) {
240240
s += fn(item);
241241
}
242242
return s;
243243
};
244244

245245
export const countBy = <T>(
246-
items: ReadonlyArray<T>,
246+
items: ReadonlyArray<T> | undefined,
247247
fn: (x: T) => boolean,
248248
): number => {
249249
let s = 0;
250-
for (const item of items) {
250+
for (const item of items ?? []) {
251251
if (fn(item)) {
252252
s++;
253253
}

0 commit comments

Comments
 (0)