unicode-width-approximation 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Unicode Width Approximation (WASM)
2
+
3
+ A library for calculating the display width of Unicode strings in terminal/monospace environments.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install unicode-width-approximation
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```javascript
14
+ import {
15
+ getStringWidth,
16
+ getCodepointWidth,
17
+ isWideChar,
18
+ isZeroWidth
19
+ } from "unicode-width-approximation";
20
+
21
+ // Get width of strings
22
+ console.log(getStringWidth("hello")); // 5
23
+ console.log(getStringWidth("δΈ­ζ–‡")); // 4
24
+ console.log(getStringWidth("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦")); // 2
25
+
26
+ // Get width of single code points
27
+ console.log(getCodepointWidth(0x41)); // 1 (ASCII 'A')
28
+ console.log(getCodepointWidth(0x4E00)); // 2 (CJK)
29
+ console.log(getCodepointWidth(0x1F600)); // 2 (emoji)
30
+ ```
31
+
32
+ ## TypeScript
33
+
34
+ TypeScript type definitions are included.
35
+
36
+ ```typescript
37
+ import { getStringWidth, getCodepointWidth } from "unicode-width-approximation";
38
+
39
+ const width: number = getStringWidth("helloδΈ–η•Œ");
40
+ console.log(width); // 9
41
+ ```
42
+
43
+ ## API
44
+
45
+ ### `getStringWidth(s: string): number`
46
+
47
+ Calculate the total display width of a string.
48
+
49
+ ### `getCodepointWidth(code: number): number`
50
+
51
+ Get the display width of a single Unicode code point (0, 1, or 2).
52
+
53
+ ### `isWideChar(code: number): boolean`
54
+
55
+ Check if a code point is a wide character (East Asian Wide or Fullwidth).
56
+
57
+ ### `isZeroWidth(code: number): boolean`
58
+
59
+ Check if a code point is a zero-width character.
60
+
61
+ ## Building
62
+
63
+ Requires [Emscripten](https://emscripten.org/) to be installed.
64
+
65
+ ```bash
66
+ npm run build
67
+ ```
68
+
69
+ ## Testing
70
+
71
+ ```bash
72
+ npm test
73
+ ```
74
+
75
+ ## License
76
+
77
+ MIT License
package/index.d.ts ADDED
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Calculate the total display width of a string.
3
+ *
4
+ * This function segments the string into grapheme clusters and calculates
5
+ * the width based on the base character of each cluster.
6
+ *
7
+ * @param s - The input string.
8
+ * @returns The total display width in columns.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { getStringWidth } from "unicode-width-approximation";
13
+ *
14
+ * getStringWidth("hello"); // 5
15
+ * getStringWidth("δΈ­ζ–‡"); // 4
16
+ * getStringWidth("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦"); // 2
17
+ * ```
18
+ */
19
+ export function getStringWidth(s: string): number;
20
+
21
+ /**
22
+ * Get the display width of a single Unicode code point.
23
+ *
24
+ * @param code - The Unicode code point.
25
+ * @returns The display width (0, 1, or 2).
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * import { getCodepointWidth } from "unicode-width-approximation";
30
+ *
31
+ * getCodepointWidth(0x41); // 1 (ASCII 'A')
32
+ * getCodepointWidth(0x4E00); // 2 (CJK)
33
+ * getCodepointWidth(0x0300); // 0 (combining mark)
34
+ * ```
35
+ */
36
+ export function getCodepointWidth(code: number): number;
37
+
38
+ /**
39
+ * Check if a code point is a wide character (East Asian Wide or Fullwidth).
40
+ *
41
+ * @param code - The Unicode code point.
42
+ * @returns True if the character has width 2.
43
+ */
44
+ export function isWideChar(code: number): boolean;
45
+
46
+ /**
47
+ * Check if a code point is a zero-width character.
48
+ *
49
+ * @param code - The Unicode code point.
50
+ * @returns True if the character has zero width.
51
+ */
52
+ export function isZeroWidth(code: number): boolean;
package/index.js ADDED
@@ -0,0 +1,46 @@
1
+ import createModule from "./wasm/UnicodeWidthApproximationWASM.js";
2
+
3
+ const UnicodeWidthApproximationWASM = await createModule();
4
+
5
+ /**
6
+ * Calculate the total display width of a string.
7
+ *
8
+ * This function segments the string into grapheme clusters and calculates
9
+ * the width based on the base character of each cluster.
10
+ *
11
+ * @param {string} s - The input string.
12
+ * @returns {number} The total display width in columns.
13
+ */
14
+ export function getStringWidth(s) {
15
+ return UnicodeWidthApproximationWASM._getStringWidth(s);
16
+ }
17
+
18
+ /**
19
+ * Get the display width of a single Unicode code point.
20
+ *
21
+ * @param {number} code - The Unicode code point.
22
+ * @returns {number} The display width (0, 1, or 2).
23
+ */
24
+ export function getCodepointWidth(code) {
25
+ return UnicodeWidthApproximationWASM._getCodepointWidth(code);
26
+ }
27
+
28
+ /**
29
+ * Check if a code point is a wide character (East Asian Wide or Fullwidth).
30
+ *
31
+ * @param {number} code - The Unicode code point.
32
+ * @returns {boolean} True if the character has width 2.
33
+ */
34
+ export function isWideChar(code) {
35
+ return UnicodeWidthApproximationWASM._isWideChar(code);
36
+ }
37
+
38
+ /**
39
+ * Check if a code point is a zero-width character.
40
+ *
41
+ * @param {number} code - The Unicode code point.
42
+ * @returns {boolean} True if the character has zero width.
43
+ */
44
+ export function isZeroWidth(code) {
45
+ return UnicodeWidthApproximationWASM._isZeroWidth(code);
46
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "unicode-width-approximation",
3
+ "version": "1.0.0",
4
+ "description": "A library for calculating the display width of Unicode strings.",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "types": "index.d.ts",
8
+ "files": [
9
+ "index.js",
10
+ "index.d.ts",
11
+ "wasm/UnicodeWidthApproximationWASM.js",
12
+ "wasm/UnicodeWidthApproximationWASM.wasm"
13
+ ],
14
+ "devDependencies": {
15
+ "@eslint/js": "^9.39.1",
16
+ "chai": "^0.1.7",
17
+ "eslint": "^9.39.1",
18
+ "globals": "^16.5.0",
19
+ "mocha": "^11.7.5"
20
+ },
21
+ "scripts": {
22
+ "build": "bash build.sh",
23
+ "lint": "npx eslint index.js test/**/*.js",
24
+ "test": "mocha test/**/*.js"
25
+ },
26
+ "homepage": "https://github.com/CyberZHG/UnicodeWidthApproximation",
27
+ "repository": "github:CyberZHG/UnicodeWidthApproximation",
28
+ "license": "MIT",
29
+ "keywords": [
30
+ "unicode",
31
+ "width",
32
+ "terminal",
33
+ "monospace",
34
+ "cjk",
35
+ "emoji",
36
+ "east-asian-width"
37
+ ]
38
+ }