-
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.
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const last = <T>(lastItemsToKeep = 1): TransformStream<T> => { | ||
const lastItems: T[] = []; | ||
|
||
return new TransformStream<T, T>({ | ||
transform(chunk, controller) { | ||
lastItems.push(chunk); | ||
|
||
if (lastItems.length > lastItemsToKeep) { | ||
lastItems.splice(0, 1); | ||
} | ||
}, | ||
|
||
flush(controller) { | ||
const itemsLength = lastItems.length; | ||
|
||
for (let i = 0; i < itemsLength; i++) { | ||
controller.enqueue(lastItems[i]); | ||
} | ||
}, | ||
}); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { fromIterable } from "../src/fromIterable"; | ||
import { last } from "../src/last"; | ||
import { pipeline } from "../src/pipeline"; | ||
import { toArray } from "../src/toArray"; | ||
|
||
describe("last", () => { | ||
test("should keep last 2 items", async () => { | ||
const sourceArray = [1, 2, 3]; | ||
const destinationArray = []; | ||
|
||
await pipeline( | ||
fromIterable(sourceArray), | ||
last(2), | ||
toArray(destinationArray), | ||
); | ||
|
||
expect(destinationArray).toEqual([2, 3]); | ||
}); | ||
|
||
test("should keep items even if parameter greater than number of items", async () => { | ||
const sourceArray = [1, 2, 3]; | ||
const destinationArray = []; | ||
|
||
await pipeline( | ||
fromIterable(sourceArray), | ||
last(4), | ||
toArray(destinationArray), | ||
); | ||
|
||
expect(destinationArray).toEqual([1, 2, 3]); | ||
}); | ||
|
||
test("should not break for an empty array", async () => { | ||
const sourceArray = []; | ||
const destinationArray = []; | ||
|
||
await pipeline( | ||
fromIterable(sourceArray), | ||
last(1), | ||
toArray(destinationArray), | ||
) | ||
|
||
expect(destinationArray).toEqual(sourceArray); | ||
}) | ||
|
||
test("should work even if parameter is not defined", async () => { | ||
const sourceArray = [1, 2, 3]; | ||
const destinationArray = []; | ||
|
||
await pipeline( | ||
fromIterable(sourceArray), | ||
last(), | ||
toArray(destinationArray), | ||
) | ||
|
||
expect(destinationArray).toEqual([3]); | ||
}) | ||
|
||
test("should skip everything if parameter is negative", async () => { | ||
const sourceArray = [1, 2, 3]; | ||
const destinationArray = []; | ||
|
||
await pipeline( | ||
fromIterable(sourceArray), | ||
last(-1), | ||
toArray(destinationArray), | ||
) | ||
|
||
expect(destinationArray).toEqual([]); | ||
}) | ||
}); |