mirror of
https://github.com/TextPlace/CoreTextPlace.git
synced 2025-05-01 13:11:10 +00:00
Compare commits
No commits in common. "main" and "0.3.2" have entirely different histories.
4 changed files with 17 additions and 21 deletions
|
@ -1,9 +1,8 @@
|
||||||
{
|
{
|
||||||
"name": "@textplace/core",
|
"name": "@textplace/core",
|
||||||
"version": "0.4.1",
|
"version": "0.3.2",
|
||||||
"exports": "./mod.ts",
|
"exports": "./mod.ts",
|
||||||
"imports": {
|
"imports": {
|
||||||
"@deno/dnt": "jsr:@deno/dnt@^0.41.3",
|
"@deno/dnt": "jsr:@deno/dnt@^0.41.3"
|
||||||
"@std/cli": "jsr:@std/cli@^1.0.11"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
7
deno.lock
generated
7
deno.lock
generated
|
@ -7,7 +7,6 @@
|
||||||
"jsr:@std/assert@0.223": "0.223.0",
|
"jsr:@std/assert@0.223": "0.223.0",
|
||||||
"jsr:@std/assert@0.226": "0.226.0",
|
"jsr:@std/assert@0.226": "0.226.0",
|
||||||
"jsr:@std/bytes@0.223": "0.223.0",
|
"jsr:@std/bytes@0.223": "0.223.0",
|
||||||
"jsr:@std/cli@^1.0.11": "1.0.11",
|
|
||||||
"jsr:@std/fmt@0.223": "0.223.0",
|
"jsr:@std/fmt@0.223": "0.223.0",
|
||||||
"jsr:@std/fmt@1": "1.0.3",
|
"jsr:@std/fmt@1": "1.0.3",
|
||||||
"jsr:@std/fs@0.223": "0.223.0",
|
"jsr:@std/fs@0.223": "0.223.0",
|
||||||
|
@ -55,9 +54,6 @@
|
||||||
"@std/bytes@0.223.0": {
|
"@std/bytes@0.223.0": {
|
||||||
"integrity": "84b75052cd8680942c397c2631318772b295019098f40aac5c36cead4cba51a8"
|
"integrity": "84b75052cd8680942c397c2631318772b295019098f40aac5c36cead4cba51a8"
|
||||||
},
|
},
|
||||||
"@std/cli@1.0.11": {
|
|
||||||
"integrity": "ec219619fdcd31bcf0d8e53bee1e2706ec9a02f70255365a094f69755dadd340"
|
|
||||||
},
|
|
||||||
"@std/fmt@0.223.0": {
|
"@std/fmt@0.223.0": {
|
||||||
"integrity": "6deb37794127dfc7d7bded2586b9fc6f5d50e62a8134846608baf71ffc1a5208"
|
"integrity": "6deb37794127dfc7d7bded2586b9fc6f5d50e62a8134846608baf71ffc1a5208"
|
||||||
},
|
},
|
||||||
|
@ -155,8 +151,7 @@
|
||||||
},
|
},
|
||||||
"workspace": {
|
"workspace": {
|
||||||
"dependencies": [
|
"dependencies": [
|
||||||
"jsr:@deno/dnt@~0.41.3",
|
"jsr:@deno/dnt@~0.41.3"
|
||||||
"jsr:@std/cli@^1.0.11"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { unicodeWidth } from "@std/cli/unicode-width";
|
|
||||||
|
|
||||||
const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
|
const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
|
||||||
|
const cjkRegex =
|
||||||
|
/[\p{Unified_Ideograph}\u30A0-\u30FF\u3040-\u309F\u31F0-\u31FF]/u;
|
||||||
|
const printableASCIIRegex = /^[\x20-\x7E]$/;
|
||||||
|
|
||||||
export function getCharacterWidth(ch: string): number {
|
export function getCharacterWidth(ch: string): number {
|
||||||
const segments = [...segmenter.segment(ch)];
|
const segments = [...segmenter.segment(ch)];
|
||||||
|
@ -10,6 +11,11 @@ export function getCharacterWidth(ch: string): number {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Properly fix this.
|
const matchesASCII = ch.match(printableASCIIRegex);
|
||||||
return Math.min(unicodeWidth(ch), 2);
|
const matchesCJK = ch.match(cjkRegex);
|
||||||
|
|
||||||
|
if (!matchesASCII && !matchesCJK) throw new Error(`Invalid character: ${ch}`);
|
||||||
|
|
||||||
|
// TODO: Support Emojis.
|
||||||
|
return matchesCJK ? 2 : 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,18 +26,14 @@ Deno.test("getCharacterWidth CJK", () => {
|
||||||
assertEquals(getCharacterWidth("グ"), 2);
|
assertEquals(getCharacterWidth("グ"), 2);
|
||||||
assertEquals(getCharacterWidth("ソ"), 2);
|
assertEquals(getCharacterWidth("ソ"), 2);
|
||||||
|
|
||||||
assertEquals(getCharacterWidth("?"), 2);
|
assertThrows(() => getCharacterWidth("?"));
|
||||||
assertEquals(getCharacterWidth("!"), 2);
|
assertThrows(() => getCharacterWidth("!"));
|
||||||
assertThrows(() => getCharacterWidth("你好"));
|
assertThrows(() => getCharacterWidth("你好"));
|
||||||
assertThrows(() => getCharacterWidth("ヨスガノ"));
|
assertThrows(() => getCharacterWidth("ヨスガノ"));
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("getCharacterWidth Emoji", () => {
|
|
||||||
assertEquals(getCharacterWidth("👋"), 2);
|
|
||||||
assertEquals(getCharacterWidth("🌲️"), 2);
|
|
||||||
assertEquals(getCharacterWidth("👨👩👧👦"), 2);
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.test("getCharacterWidth previously faulty cases", () => {
|
Deno.test("getCharacterWidth previously faulty cases", () => {
|
||||||
assertEquals(getCharacterWidth("𤲶"), 2);
|
assertEquals(getCharacterWidth("𤲶"), 2);
|
||||||
|
|
||||||
|
assertThrows(() => getCharacterWidth("𤲶"[0]));
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue