Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reduce): Implement reduce #925

Merged
merged 31 commits into from
Feb 1, 2025
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
37b9281
feat : reduce implement
dasom-jo Jan 2, 2025
806ffc6
feat : reduce_function
dasom-jo Jan 2, 2025
61986fd
fix : EsLink ERROR
dasom-jo Jan 2, 2025
71a4584
fix : EsLint error
dasom-jo Jan 2, 2025
5b45837
fix : EsLint error
dasom-jo Jan 2, 2025
886a683
fix : EsLint error
dasom-jo Jan 2, 2025
309667c
fix : EsLint error
dasom-jo Jan 2, 2025
8e4a239
fix : EsLint error
dasom-jo Jan 2, 2025
6813a2e
Add reduce module to index file
dasom-jo Jan 2, 2025
b03c705
fix : EsLint error
dasom-jo Jan 2, 2025
7146596
fix : EsLint error
dasom-jo Jan 2, 2025
2f2fd53
fix : EsLint error
dasom-jo Jan 2, 2025
c89c102
fix : EsLint error
dasom-jo Jan 2, 2025
c194648
fix : EsLInt error
dasom-jo Jan 2, 2025
b6e532f
fix : EsLint error
dasom-jo Jan 7, 2025
f894c83
fix : EsLint error
dasom-jo Jan 7, 2025
8e60674
fix : EsLInt error
dasom-jo Jan 7, 2025
72bf1a8
Merge branch 'main' into reduce_function
dasom-jo Jan 22, 2025
12a2313
Merge branch 'main' into reduce_function
raon0211 Jan 30, 2025
9d25bf3
Fix: Fixed incorrect reduce function
dasom-jo Jan 30, 2025
8d7c740
fix : eslint
dasom-jo Jan 30, 2025
35f3698
fix:eslint
dasom-jo Jan 30, 2025
31e363d
fix : eslint
dasom-jo Jan 30, 2025
744058a
fix: eslint
dasom-jo Jan 30, 2025
9b9726a
fix:eslint
dasom-jo Jan 30, 2025
d95c18e
fix:eslint
dasom-jo Jan 30, 2025
f12e519
fix : eslint
dasom-jo Jan 30, 2025
fa3db30
fix : eslint
dasom-jo Jan 31, 2025
91fc0e8
feat(reduce): Match the behavior of lodash's reduce
raon0211 Jan 31, 2025
8ddd82a
unnecessary export
raon0211 Jan 31, 2025
167e741
Merge branch 'main' into reduce_function
raon0211 Feb 1, 2025
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
Prev Previous commit
Next Next commit
fix : eslint
dasom-jo committed Jan 30, 2025
commit 8d7c740b53e71b39c68315dcc1d1a098d684b895
28 changes: 14 additions & 14 deletions src/array/reduce.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { describe, expect, it } from "vitest";
import { reduce } from "./reduce";
import { describe, expect, it } from 'vitest';
import { reduce } from './reduce';

describe("reduce", () => {
it("sums an array of numbers", () => {
describe('reduce', () => {
it('sums an array of numbers', () => {
const result = reduce([1, 2, 3, 4, 5], (acc, cur) => acc + cur, 0);
expect(result).toBe(15);
});

it("multiplies an array of numbers", () => {
it('multiplies an array of numbers', () => {
const result = reduce([1, 2, 3, 4], (acc, cur) => acc * cur, 1);
expect(result).toBe(24);
});

it("concatenates an array of strings", () => {
const result = reduce(["H", "e", "l", "l", "o"], (acc, cur) => acc + cur, "");
expect(result).toBe("Hello");
it('concatenates an array of strings', () => {
const result = reduce(['H', 'e', 'l', 'l', 'o'], (acc, cur) => acc + cur, '');
expect(result).toBe('Hello');
});

it("works with an empty array and returns the initial value", () => {
it('works with an empty array and returns the initial value', () => {
const result = reduce<number, number>([], (acc, cur) => acc + cur, 10);
expect(result).toBe(10);
});

it("creates an object counting occurrences of elements", () => {
const input = ["a", "b", "a", "c", "b", "a"];
it('creates an object counting occurrences of elements', () => {
const input = ['a', 'b', 'a', 'c', 'b', 'a'];
const result = reduce(
input,
(acc, cur) => {
@@ -36,17 +36,17 @@ describe("reduce", () => {
expect(result).toEqual({ a: 3, b: 2, c: 1 });
});

it("reduces an array to find the maximum value", () => {
it('reduces an array to find the maximum value', () => {
const result = reduce([5, 1, 8, 3, 10], (acc, cur) => (cur > acc ? cur : acc), -Infinity);
expect(result).toBe(10);
});

it("reduces an array to find the minimum value", () => {
it('reduces an array to find the minimum value', () => {
const result = reduce([5, 1, 8, 3, 10], (acc, cur) => (cur < acc ? cur : acc), Infinity);
expect(result).toBe(1);
});

it("flattens a nested array", () => {
it('flattens a nested array', () => {
const input = [[1, 2], [3, 4], [5]];
const result = reduce(input, (acc, cur) => acc.concat(cur), [] as number[]);
expect(result).toEqual([1, 2, 3, 4, 5]);