-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace take to spread syntax
- Loading branch information
Showing
9 changed files
with
40 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { take } from "../more-itertools/take"; | ||
import { zip } from "./zip"; | ||
|
||
describe("zip", () => { | ||
it("zips an iterable correctly", () => { | ||
const result = take(Number.POSITIVE_INFINITY, zip([1, 2], [3, 4])); | ||
const result = [...zip([1, 2], [3, 4])]; | ||
expect(result).toEqual([ | ||
[1, 3], | ||
[2, 4], | ||
]); | ||
}); | ||
|
||
it("return an empty iterables when pass empty iterables", () => { | ||
const result = take(Number.POSITIVE_INFINITY, zip([], [])); | ||
const result = [...zip([], [])]; | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("throw Error pass different length iterables when strict option is true", () => { | ||
expect(() => { | ||
take(Number.POSITIVE_INFINITY, zip([1, 2], [1, 2, 3], { strict: true })); | ||
[...zip([1, 2], [1, 2, 3], { strict: true })]; | ||
}).toThrow("zip() argument 2 is longer than argument 1"); | ||
|
||
expect(() => { | ||
take(Number.POSITIVE_INFINITY, zip([1, 2, 3], [1, 2], { strict: true })); | ||
[...zip([1, 2, 3], [1, 2], { strict: true })]; | ||
}).toThrow("zip() argument 1 is longer than argument 2"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,16 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { take } from "../more-itertools"; | ||
import { accumulate } from "./accumulate"; | ||
|
||
describe("accumulate", () => { | ||
it("should accumulate values from iterator", () => { | ||
const iterable = accumulate([1, 2, 3], (x, y) => x + y); | ||
|
||
expect(take(Number.POSITIVE_INFINITY, iterable)).toEqual([1, 3, 6]); | ||
expect([...iterable]).toEqual([1, 3, 6]); | ||
}); | ||
|
||
it("should accumulate values from iterator with initial value", () => { | ||
const iterable = accumulate([1, 2, 3, 4, 5], (a, b) => a + b, 100); | ||
|
||
expect(take(Number.POSITIVE_INFINITY, iterable)).toEqual([ | ||
100, 101, 103, 106, 110, 115, | ||
]); | ||
expect([...iterable]).toEqual([100, 101, 103, 106, 110, 115]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { take } from "../more-itertools"; | ||
import { compress } from "./compress"; | ||
|
||
describe("compress", () => { | ||
it("compress on empty list", () => { | ||
expect(take(Number.POSITIVE_INFINITY, compress([], []))).toEqual([]); | ||
expect([...compress([], [])]).toEqual([]); | ||
}); | ||
|
||
it("compress removes selected items", () => { | ||
expect( | ||
take(Number.POSITIVE_INFINITY, compress("ABCDEF", [1, 0, 1, 0, 1, 1])), | ||
).toEqual(["A", "C", "E", "F"]); | ||
expect([...compress("ABCDEF", [1, 0, 1, 0, 1, 1])]).toEqual([ | ||
"A", | ||
"C", | ||
"E", | ||
"F", | ||
]); | ||
|
||
expect( | ||
take( | ||
Number.POSITIVE_INFINITY, | ||
compress("ABCDEF", [true, false, true, false, true, true]), | ||
), | ||
).toEqual(["A", "C", "E", "F"]); | ||
expect([ | ||
...compress("ABCDEF", [true, false, true, false, true, true]), | ||
]).toEqual(["A", "C", "E", "F"]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,15 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { take } from "../more-itertools"; | ||
|
||
import { dropwhile } from "./dropwhile"; | ||
|
||
describe("dropwhile", () => { | ||
it("dropwhile on empty list", () => { | ||
expect( | ||
take( | ||
Number.POSITIVE_INFINITY, | ||
dropwhile((x) => x < 2, []), | ||
), | ||
).toEqual([]); | ||
expect([...dropwhile((x) => x < 2, [])]).toEqual([]); | ||
}); | ||
|
||
it("dropwhile on list", () => { | ||
expect( | ||
take( | ||
Number.POSITIVE_INFINITY, | ||
dropwhile((x) => x % 2 === 0, [1]), | ||
), | ||
).toEqual([1]); | ||
expect( | ||
take( | ||
Number.POSITIVE_INFINITY, | ||
dropwhile((x) => x % 2 === 1, [1]), | ||
), | ||
).toEqual([]); | ||
expect( | ||
take( | ||
Number.POSITIVE_INFINITY, | ||
dropwhile((x) => x < 5, [1, 4, 6, 3, 8]), | ||
), | ||
).toEqual([6, 3, 8]); | ||
expect([...dropwhile((x) => x % 2 === 0, [1])]).toEqual([1]); | ||
expect([...dropwhile((x) => x % 2 === 1, [1])]).toEqual([]); | ||
expect([...dropwhile((x) => x < 5, [1, 4, 6, 3, 8])]).toEqual([6, 3, 8]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,18 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { take } from "../more-itertools"; | ||
|
||
import { filterfalse } from "./filterfalse"; | ||
|
||
describe("filterfalse", () => { | ||
it("should filter elements predicate return true", () => { | ||
expect( | ||
take( | ||
Number.POSITIVE_INFINITY, | ||
filterfalse([1, 4, 6, 3, 8], (x) => x < 5), | ||
), | ||
).toEqual([6, 8]); | ||
expect([...filterfalse([1, 4, 6, 3, 8], (x) => x < 5)]).toEqual([6, 8]); | ||
}); | ||
|
||
it("should filter truthy value if predicate did not specify", () => { | ||
expect( | ||
take( | ||
Number.POSITIVE_INFINITY, | ||
filterfalse([1, undefined, 2, null, 3, "", 4, 0, 5]), | ||
), | ||
).toEqual([undefined, null, "", 0]); | ||
expect([...filterfalse([1, undefined, 2, null, 3, "", 4, 0, 5])]).toEqual([ | ||
undefined, | ||
null, | ||
"", | ||
0, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters