InfoLens / client /src /tests /utils /charIndexForByteLimit.test.ts
dqy08's picture
้‡ๆž„ไป“ๅบ“็›ฎๅฝ•๏ผ›ๅขžๅŠ Propagated attributionๅŠจ็”ป๏ผ›UIๆ”น่ฟ›
17037b0
Raw History Blame Contribute Delete
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);