split.test.ts
1 import t, { type Test } from "tap"; 2 import * as u8 from "uint8arrays"; 3 4 import { split } from "./split.js"; 5 6 t.test("split with single Uint8Array", (t: Test): void => { 7 const text = "hello world foo bar"; 8 const input = u8.fromString(text); 9 10 t.same( 11 split(input, " ").map((chunk) => u8.toString(chunk)), 12 text.split(" "), 13 ); 14 15 t.end(); 16 }); 17 18 t.test("split with array of Uint8Arrays", (t: Test): void => { 19 const parts = ["hello ", "world ", "foo ", "bar"]; 20 const input = parts.map((part) => u8.fromString(part)); 21 22 const result = split(input, " ").map((chunk) => u8.toString(chunk)); 23 24 t.same(result, ["hello", "world", "foo", "bar"]); 25 26 t.end(); 27 }); 28 29 t.test("split with string needle", (t: Test): void => { 30 const text = "one||two||three"; 31 const input = u8.fromString(text); 32 33 t.same( 34 split(input, "||").map((chunk) => u8.toString(chunk)), 35 text.split("||"), 36 ); 37 38 t.end(); 39 }); 40 41 t.test("split with Uint8Array needle", (t: Test): void => { 42 const text = "one||two||three"; 43 const input = u8.fromString(text); 44 const needle = u8.fromString("||"); 45 46 t.same( 47 split(input, needle).map((chunk) => u8.toString(chunk)), 48 text.split("||"), 49 ); 50 51 t.end(); 52 });