Download client/src/tests/utils/charIndexForByteLimit.test.ts from dqy08/InfoLens: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/spaces/dqy08/InfoLens/resolve/main/client/src/tests/utils/charIndexForByteLimit.test.ts
- Command line
-
hf download hf://spaces/dqy08/InfoLens/client/src/tests/utils/charIndexForByteLimit.test.ts
-
curl -L -o charIndexForByteLimit.test.ts https://huggingface.co/spaces/dqy08/InfoLens/resolve/main/client/src/tests/utils/charIndexForByteLimit.test.ts
5.01 kB
| /** | |
| * charIndexForByteLimit ๅๅ ๆต่ฏ | |
| * ่ฟ่ก: cd client/src && npx tsx ts/utils/charIndexForByteLimit.test.ts | |
| */ | |
| import { charIndexForByteLimit } from "../../shared/cross/semanticUtils"; | |
| let passed = 0; | |
| let failed = 0; | |
| function assert(desc: string, actual: number, expected: number) { | |
| if (actual === expected) { | |
| console.log(` โ ${desc}`); | |
| passed++; | |
| } else { | |
| console.error(` โ ${desc} โ expected ${expected}, got ${actual}`); | |
| failed++; | |
| } | |
| } | |
| const enc = new TextEncoder(); | |
| function bytes(s: string) { return enc.encode(s).byteLength; } | |
| // โโ 1. ๅบๆฌ่พน็ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| console.log("1. ๅบๆฌ่พน็"); | |
| assert("็ฉบๅญ็ฌฆไธฒ๏ผlimit=0", charIndexForByteLimit("", 0, 0), 0); | |
| assert("็ฉบๅญ็ฌฆไธฒ๏ผlimit=10", charIndexForByteLimit("", 0, 10), 0); | |
| assert("limit=0 ๆถ็ซๅปๅๆญข", charIndexForByteLimit("abc", 0, 0), 0); | |
| assert("limit ๆฐๅฅฝ็ญไบๅ จๆๅญ่ๆฐ", charIndexForByteLimit("abc", 0, 3), 3); | |
| assert("limit ๅคงไบๅ จๆๅญ่ๆฐ", charIndexForByteLimit("abc", 0, 100), 3); | |
| // โโ 2. ็บฏ ASCII๏ผๆฏๅญ็ฌฆ 1 ๅญ่๏ผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| console.log("2. ็บฏ ASCII"); | |
| assert("limit=1 ๅ 1 ๅญ็ฌฆ", charIndexForByteLimit("hello", 0, 1), 1); | |
| assert("limit=3 ๅ 3 ๅญ็ฌฆ", charIndexForByteLimit("hello", 0, 3), 3); | |
| assert("limit=5 ๅๅ จ้จ", charIndexForByteLimit("hello", 0, 5), 5); | |
| assert("start=2๏ผlimit=2", charIndexForByteLimit("hello", 2, 2), 4); | |
| // โโ 3. CJK๏ผๆฏๅญ็ฌฆ 3 ๅญ่๏ผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| console.log("3. CJK ๅญ็ฌฆ๏ผ3 ๅญ่/ๅญ๏ผ"); | |
| const cjk = "ไฝ ๅฅฝไธ็"; // 4 ๅญ็ฌฆ๏ผ12 ๅญ่ | |
| assert("limit=3 โ 1 ๅญ็ฌฆ", charIndexForByteLimit(cjk, 0, 3), 1); | |
| assert("limit=4 โ 1 ๅญ็ฌฆ๏ผไธญ้ดๅไธๅผ๏ผ", charIndexForByteLimit(cjk, 0, 4), 1); | |
| assert("limit=6 โ 2 ๅญ็ฌฆ", charIndexForByteLimit(cjk, 0, 6), 2); | |
| assert("limit=11 โ 3 ๅญ็ฌฆ", charIndexForByteLimit(cjk, 0, 11), 3); | |
| assert("limit=12 โ 4 ๅญ็ฌฆ", charIndexForByteLimit(cjk, 0, 12), 4); | |
| assert("start=1๏ผlimit=3 โ idx=2", charIndexForByteLimit(cjk, 1, 3), 2); | |
| // โโ 4. Emoji๏ผ4 ๅญ่๏ผJS ไปฃ็ๅฏน้ฟๅบฆ=2๏ผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| console.log("4. Emoji๏ผ4 ๅญ่/ๅญ๏ผJS charLen=2๏ผ"); | |
| const emoji = "๐๐๐"; // 3 emoji๏ผ12 ๅญ่๏ผJS length=6 | |
| assert("emoji limit=4 โ idx=2๏ผ1 emoji๏ผ", charIndexForByteLimit(emoji, 0, 4), 2); | |
| assert("emoji limit=5 โ idx=2๏ผๅไธๅผ๏ผ", charIndexForByteLimit(emoji, 0, 5), 2); | |
| assert("emoji limit=8 โ idx=4๏ผ2 emoji๏ผ", charIndexForByteLimit(emoji, 0, 8), 4); | |
| assert("emoji limit=12 โ idx=6๏ผๅ จ้จ๏ผ", charIndexForByteLimit(emoji, 0, 12), 6); | |
| assert("emoji start=2๏ผlimit=4 โ idx=4", charIndexForByteLimit(emoji, 2, 4), 4); | |
| // โโ 5. ๆททๅ ASCII + CJK + Emoji โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| console.log("5. ๆททๅๅญ็ฌฆ"); | |
| // "Aๅฅฝ๐" = 1+3+4 = 8 ๅญ่๏ผJS length=4 | |
| const mixed = "Aๅฅฝ๐"; | |
| assert("ๆททๅ limit=1 โ 1(A)", charIndexForByteLimit(mixed, 0, 1), 1); | |
| assert("ๆททๅ limit=3 โ 1(Aไธๅคๅฅฝ)", charIndexForByteLimit(mixed, 0, 3), 1); | |
| assert("ๆททๅ limit=4 โ 2(Aๅฅฝ)", charIndexForByteLimit(mixed, 0, 4), 2); | |
| assert("ๆททๅ limit=7 โ 2(ไธๅคemoji)",charIndexForByteLimit(mixed, 0, 7), 2); | |
| assert("ๆททๅ limit=8 โ 4(ๅ จ้จ)", charIndexForByteLimit(mixed, 0, 8), 4); | |
| assert("ๆททๅ start=1๏ผlimit=3 โ 2(ๅฅฝ)", charIndexForByteLimit(mixed, 1, 3), 2); | |
| // โโ 6. start ่ถ ๅบๆๆฌๆซๅฐพ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| console.log("6. start >= text.length"); | |
| assert("start=5 on 'abc' โ 5", charIndexForByteLimit("abc", 5, 10), 5); | |
| // โโ 7. ๆข่ก็ฌฆ๏ผๅฑไบ ASCII๏ผ1 ๅญ่๏ผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| console.log("7. ๅซๆข่ก็ฌฆ"); | |
| const nl = "a\nb\n"; | |
| assert("ๆข่ก limit=2 โ 2", charIndexForByteLimit(nl, 0, 2), 2); | |
| assert("ๆข่ก limit=3 โ 3", charIndexForByteLimit(nl, 0, 3), 3); | |
| // โโ ็ปๆๆฑๆป โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| console.log(`\n็ปๆ: ${passed} ้่ฟ / ${failed} ๅคฑ่ดฅ`); | |
| if (failed > 0) process.exit(1); | |