Skip to content

Commit a3a9d3c

Browse files
authored
feat: Adding types separately instead of auto generated types
feat: Adding types separately instead of auto generated types
2 parents 60e8eed + 6270012 commit a3a9d3c

File tree

5 files changed

+111
-22
lines changed

5 files changed

+111
-22
lines changed

.github/workflows/package-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
node-version: [14.x, 16.x, 18.x, 20.x]
15+
node-version: [20.x]
1616

1717
steps:
1818
- name: Checkout
@@ -43,4 +43,4 @@ jobs:
4343
- name: Run package test
4444
run: |
4545
cd test-pkg
46-
node package-test.js
46+
node package-test.js

index.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Returns the width of a single character in terminal columns.
3+
* @param ucs - The Unicode code point of the character
4+
* @returns The width of the character in terminal columns:
5+
* - 0 for null character
6+
* - -1 for control characters
7+
* - 0 for non-spacing characters
8+
* - 1 for normal characters
9+
* - 2 for wide characters (East Asian)
10+
*/
11+
declare function wcwidth(ucs: number): number;
12+
13+
/**
14+
* Returns the width of a string in terminal columns.
15+
* @param pwcs - The string to measure
16+
* @returns The width of the string in terminal columns, or -1 if the string contains control characters
17+
*/
18+
declare function wcswidth(pwcs: string): number;
19+
20+
export { wcwidth, wcswidth };

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0",
44
"description": "Simplified JS/TS implementation of wcswidth() written by Markus Kuhn in C",
55
"main": "dist/index.js",
6-
"types": "dist/index.d.ts",
6+
"types": "index.d.ts",
77
"repository": "[email protected]:console-table-printer/simple-wcswidth.git",
88
"author": "Nahiyan Kamal <[email protected]>",
99
"license": "MIT",
@@ -20,7 +20,8 @@
2020
"console-width"
2121
],
2222
"files": [
23-
"dist"
23+
"dist",
24+
"index.d.ts"
2425
],
2526
"devDependencies": {
2627
"@eslint/js": "^9.0.0",

test/githubActionsTest/package-test.js

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,53 @@ const assert = require('assert');
44
function runAllTests() {
55
// Test 1: Basic ASCII Characters
66
console.log('Running basic ASCII character tests...');
7-
assert.strictEqual(wcswidth('Hello'), 5, 'ASCII string should have correct width');
7+
assert.strictEqual(
8+
wcswidth('Hello'),
9+
5,
10+
'ASCII string should have correct width'
11+
);
812
assert.strictEqual(wcswidth('123'), 3, 'Numbers should have correct width');
9-
assert.strictEqual(wcswidth('!@#'), 3, 'Special characters should have correct width');
13+
assert.strictEqual(
14+
wcswidth('!@#'),
15+
3,
16+
'Special characters should have correct width'
17+
);
1018

1119
// Test 2: CJK Characters (Chinese, Japanese, Korean)
1220
console.log('Running CJK character tests...');
13-
assert.strictEqual(wcswidth('你好'), 4, 'Chinese characters should have width 2 each');
14-
assert.strictEqual(wcswidth('こんにちは'), 10, 'Japanese characters should have width 2 each');
15-
assert.strictEqual(wcswidth('안녕하세요'), 10, 'Korean characters should have width 2 each');
21+
assert.strictEqual(
22+
wcswidth('你好'),
23+
4,
24+
'Chinese characters should have width 2 each'
25+
);
26+
assert.strictEqual(
27+
wcswidth('こんにちは'),
28+
10,
29+
'Japanese characters should have width 2 each'
30+
);
31+
assert.strictEqual(
32+
wcswidth('안녕하세요'),
33+
10,
34+
'Korean characters should have width 2 each'
35+
);
1636

1737
// Test 3: Mixed Content
1838
console.log('Running mixed content tests...');
19-
assert.strictEqual(wcswidth('Hello 世界'), 10, 'Mixed ASCII and CJK should have correct width');
20-
assert.strictEqual(wcswidth('123 你好'), 8, 'Mixed numbers and CJK should have correct width');
21-
assert.strictEqual(wcswidth('!@# こんにちは'), 14, 'Mixed special chars and CJK should have correct width');
39+
assert.strictEqual(
40+
wcswidth('Hello 世界'),
41+
10,
42+
'Mixed ASCII and CJK should have correct width'
43+
);
44+
assert.strictEqual(
45+
wcswidth('123 你好'),
46+
8,
47+
'Mixed numbers and CJK should have correct width'
48+
);
49+
assert.strictEqual(
50+
wcswidth('!@# こんにちは'),
51+
14,
52+
'Mixed special chars and CJK should have correct width'
53+
);
2254

2355
// Test 4: Control Characters
2456
console.log('Running control character tests...');
@@ -29,21 +61,57 @@ function runAllTests() {
2961

3062
// Test 5: Fullwidth Characters
3163
console.log('Running fullwidth character tests...');
32-
assert.strictEqual(wcswidth('ABC'), 6, 'Fullwidth Latin should have width 2 each');
33-
assert.strictEqual(wcswidth('123'), 6, 'Fullwidth numbers should have width 2 each');
34-
assert.strictEqual(wcswidth('!?'), 4, 'Fullwidth punctuation should have width 2 each');
64+
assert.strictEqual(
65+
wcswidth('ABC'),
66+
6,
67+
'Fullwidth Latin should have width 2 each'
68+
);
69+
assert.strictEqual(
70+
wcswidth('123'),
71+
6,
72+
'Fullwidth numbers should have width 2 each'
73+
);
74+
assert.strictEqual(
75+
wcswidth('!?'),
76+
4,
77+
'Fullwidth punctuation should have width 2 each'
78+
);
3579

3680
// Test 6: Combining Characters
3781
console.log('Running combining character tests...');
38-
assert.strictEqual(wcswidth('e\u0301'), 1, 'Combining character should have width 1');
39-
assert.strictEqual(wcswidth('a\u0308'), 1, 'Combining character should have width 1');
82+
assert.strictEqual(
83+
wcswidth('e\u0301'),
84+
1,
85+
'Combining character should have width 1'
86+
);
87+
assert.strictEqual(
88+
wcswidth('a\u0308'),
89+
1,
90+
'Combining character should have width 1'
91+
);
4092

4193
// Test 7: Individual Character Widths
4294
console.log('Running individual character width tests...');
43-
assert.strictEqual(wcwidth('A'.charCodeAt(0)), 1, 'ASCII character should have width 1');
44-
assert.strictEqual(wcwidth('你'.charCodeAt(0)), 2, 'CJK character should have width 2');
45-
assert.strictEqual(wcwidth('\t'.charCodeAt(0)), -1, 'Control character should return -1');
46-
assert.strictEqual(wcwidth('A'.charCodeAt(0)), 2, 'Fullwidth character should have width 2');
95+
assert.strictEqual(
96+
wcwidth('A'.charCodeAt(0)),
97+
1,
98+
'ASCII character should have width 1'
99+
);
100+
assert.strictEqual(
101+
wcwidth('你'.charCodeAt(0)),
102+
2,
103+
'CJK character should have width 2'
104+
);
105+
assert.strictEqual(
106+
wcwidth('\t'.charCodeAt(0)),
107+
-1,
108+
'Control character should return -1'
109+
);
110+
assert.strictEqual(
111+
wcwidth('A'.charCodeAt(0)),
112+
2,
113+
'Fullwidth character should have width 2'
114+
);
47115

48116
// Test 8: Edge Cases
49117
console.log('Running edge case tests...');

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"noImplicitAny": true,
66
"strictNullChecks": true,
77
"module": "commonjs",
8-
"declaration": true,
8+
"declaration": false,
99
"lib": ["es2017", "dom"],
1010
"strict": true,
1111
"types": ["jest"],

0 commit comments

Comments
 (0)