hangul-unicode-composer 1.6.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,101 @@
1
+ # hangul-unicode-composer
2
+
3
+ > Modern TypeScript port of HUC (Hangul Unicode Composer). A lightweight, zero-dependency library to compose Korean Jamo characters into Unicode Hangul syllables in the browser and Node.js.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install hangul-unicode-composer
9
+ ```
10
+
11
+ ## Features
12
+ * **Zero Dependencies**: Pure TypeScript/ESM module.
13
+ * **Standard Unicode Compliant**: Implements standard Hangul Syllable Composition rules (`0xAC00`).
14
+ * **Carry-over Composition**: Intuitively handles splitting complex consonants when new vowels are typed (e.g. `인` + `아` -> `이` + `나`).
15
+ * **Session Archives**: Save and restore typing buffers easily for multiple inputs.
16
+ * **Event & Callback hooks**: Simple native `EventTarget` listeners or direct callback functions (`onUpdate`, `onLimited`, `onError`).
17
+
18
+ ## Usage
19
+
20
+ ### 1. Basic Composition
21
+
22
+ ```typescript
23
+ import { HangulUnicodeComposer, HangulTextEvent } from 'hangul-unicode-composer';
24
+
25
+ const composer = new HangulUnicodeComposer();
26
+
27
+ // Listen to update events
28
+ composer.addEventListener(HangulTextEvent.UPDATE, (e) => {
29
+ const ev = e as HangulTextEvent;
30
+ console.log('Composed text:', ev.string);
31
+ });
32
+
33
+ // Or use a simple callback property
34
+ composer.onUpdate = (text) => {
35
+ console.log('Current buffer:', text);
36
+ };
37
+
38
+ // Type some Korean characters
39
+ composer.addJamo('ㅎ'); // 'ㅎ'
40
+ composer.addJamo('ㅏ'); // '하'
41
+ composer.addJamo('ㄴ'); // '한'
42
+ composer.addJamo('ㄱ'); // '한', 'ㄱ' carries over to the next syllable
43
+ composer.addJamo('ㅜ'); // '한구'
44
+ composer.addJamo('ㄱ'); // '한국'
45
+
46
+ console.log(composer.compositionString); // "한"
47
+ console.log(composer.extra); // "국"
48
+ console.log(composer.compositionString + composer.extra); // "한국"
49
+ ```
50
+
51
+ ### 2. Backspacing and Resetting
52
+
53
+ ```typescript
54
+ composer.backSpace(); // '한구'
55
+ composer.backSpace(); // '한'
56
+ composer.space(); // '한 '
57
+ composer.reset(); // clears everything
58
+ ```
59
+
60
+ ### 3. State Archiving and Restoring
61
+ Ideal for applications swapping input focus across multiple inputs using the same virtual keyboard instance.
62
+
63
+ ```typescript
64
+ // Type first field
65
+ composer.addJamo('ㄱ');
66
+ composer.addJamo('ㅏ'); // '가'
67
+ composer.archive('field1'); // Save state
68
+
69
+ // Switch to second field
70
+ composer.reset();
71
+ composer.addJamo('ㄴ');
72
+ composer.addJamo('ㅏ'); // '나'
73
+ composer.archive('field2'); // Save state
74
+
75
+ // Restore first field
76
+ composer.restore('field1');
77
+ console.log(composer.compositionString + composer.extra); // '가'
78
+ ```
79
+
80
+ ## API Reference
81
+
82
+ ### Properties
83
+ * `compositionString: string` - Fully completed Hangul syllables.
84
+ * `extra: string` - Currently active/composing syllable.
85
+ * `restrict: number` - Max text length constraint (default: `3000`).
86
+ * `instantChars: string[]` - Access the raw Jamo characters array in the active buffer.
87
+
88
+ ### Callbacks
89
+ * `onUpdate?: (text: string) => void` - Triggers whenever text changes.
90
+ * `onLimited?: (text: string) => void` - Triggers when text exceeds `restrict` length.
91
+ * `onError?: (text: string) => void` - Triggers on compilation error.
92
+
93
+ ### Static Methods
94
+ * `HangulUnicodeComposer.isHangulJaeum(char: string): boolean`
95
+ * `HangulUnicodeComposer.isHangulMoeum(char: string): boolean`
96
+ * `HangulUnicodeComposer.isHangulJamo(char: string): boolean`
97
+ * `HangulUnicodeComposer.getString3Syllables(init: string, mid: string, fin: string): string`
98
+
99
+ ## License
100
+
101
+ Apache License V2
@@ -0,0 +1,5 @@
1
+ export { HangulTextEvent } from "./text/HangulTextEvent.js";
2
+ export { HangulUnicodeComposer } from "./text/HangulUnicodeComposer.js";
3
+ export { KeyInfo } from "./keyboard/keyMeta/KeyInfo.js";
4
+ export { KeyType } from "./keyboard/keyMeta/KeyType.js";
5
+ export { KeyStatus } from "./keyboard/key/KeyStatus.js";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { HangulTextEvent } from "./text/HangulTextEvent.js";
2
+ export { HangulUnicodeComposer } from "./text/HangulUnicodeComposer.js";
3
+ export { KeyInfo } from "./keyboard/keyMeta/KeyInfo.js";
4
+ export { KeyType } from "./keyboard/keyMeta/KeyType.js";
5
+ export { KeyStatus } from "./keyboard/key/KeyStatus.js";
@@ -0,0 +1,6 @@
1
+ export declare class KeyStatus {
2
+ static readonly DEFAULT = "default";
3
+ static readonly OVER = "over";
4
+ static readonly DOWN = "down";
5
+ static readonly FOCUS = "focus";
6
+ }
@@ -0,0 +1,6 @@
1
+ export class KeyStatus {
2
+ static DEFAULT = "default";
3
+ static OVER = "over";
4
+ static DOWN = "down";
5
+ static FOCUS = "focus";
6
+ }
@@ -0,0 +1,5 @@
1
+ export declare class KeyInfo {
2
+ keyType: string;
3
+ keyValue: string;
4
+ keyValue2: string;
5
+ }
@@ -0,0 +1,5 @@
1
+ export class KeyInfo {
2
+ keyType = "";
3
+ keyValue = "";
4
+ keyValue2 = "";
5
+ }
@@ -0,0 +1,14 @@
1
+ export declare class KeyType {
2
+ /**
3
+ * Normal keyboard key (character key).
4
+ */
5
+ static readonly NORMAL_KEY = "normalKey";
6
+ /**
7
+ * Function key (Shift, Backspace, Enter, Language toggle, Space, etc.).
8
+ */
9
+ static readonly FUNC_KEY = "funcKey";
10
+ /**
11
+ * Special key (other keys outside default keyboard functionalities).
12
+ */
13
+ static readonly SPECIAL_KEY = "specialKey";
14
+ }
@@ -0,0 +1,14 @@
1
+ export class KeyType {
2
+ /**
3
+ * Normal keyboard key (character key).
4
+ */
5
+ static NORMAL_KEY = "normalKey";
6
+ /**
7
+ * Function key (Shift, Backspace, Enter, Language toggle, Space, etc.).
8
+ */
9
+ static FUNC_KEY = "funcKey";
10
+ /**
11
+ * Special key (other keys outside default keyboard functionalities).
12
+ */
13
+ static SPECIAL_KEY = "specialKey";
14
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Event class used in HangleUnicodeComposer.
3
+ * Replicates the AS3 HangleTextEvent behaviour.
4
+ */
5
+ export declare class HangleTextEvent extends Event {
6
+ /**
7
+ * Dispatched when the composed string changes.
8
+ */
9
+ static readonly UPDATE = "update";
10
+ /**
11
+ * Dispatched when the composed string length reaches the restricted limit.
12
+ */
13
+ static readonly LIMITED = "limited";
14
+ /**
15
+ * Dispatched when composition errors occur.
16
+ */
17
+ static readonly ERROR = "error";
18
+ /**
19
+ * Composed string data at the time of the event.
20
+ */
21
+ string: string;
22
+ constructor(type: string, stringData: string, bubbles?: boolean, cancelable?: boolean);
23
+ toString(): string;
24
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Event class used in HangleUnicodeComposer.
3
+ * Replicates the AS3 HangleTextEvent behaviour.
4
+ */
5
+ export class HangleTextEvent extends Event {
6
+ /**
7
+ * Dispatched when the composed string changes.
8
+ */
9
+ static UPDATE = "update";
10
+ /**
11
+ * Dispatched when the composed string length reaches the restricted limit.
12
+ */
13
+ static LIMITED = "limited";
14
+ /**
15
+ * Dispatched when composition errors occur.
16
+ */
17
+ static ERROR = "error";
18
+ /**
19
+ * Composed string data at the time of the event.
20
+ */
21
+ string;
22
+ constructor(type, stringData, bubbles = false, cancelable = false) {
23
+ super(type, { bubbles, cancelable });
24
+ this.string = stringData;
25
+ }
26
+ toString() {
27
+ return `${this.type} ${this.string}`;
28
+ }
29
+ }
@@ -0,0 +1,133 @@
1
+ /**
2
+ * HangleUnicodeComposer
3
+ * Ports the AS3 한글 조합 라이브러리 to TypeScript.
4
+ * Composes Korean Jamo characters into Unicode Hangul syllables.
5
+ */
6
+ export declare class HangleUnicodeComposer extends EventTarget {
7
+ /**
8
+ * Initial consonant (초성) Unicode values
9
+ */
10
+ private static readonly INITIAL;
11
+ /**
12
+ * Medial vowel (중성) Unicode values
13
+ */
14
+ private static readonly MEDIAL;
15
+ /**
16
+ * Final consonant (종성) Unicode values. Index 0 is empty (no final consonant).
17
+ */
18
+ private static readonly FINAL;
19
+ private static used;
20
+ /**
21
+ * Helper to make one letter by composing 3 syllables.
22
+ */
23
+ static getString3Syllables(init: string, mid: string, fin: string): string;
24
+ /**
25
+ * Returns true if the character is a Hangeul Jaeum (consonant) in the range 3131 ~ 314E.
26
+ */
27
+ static isHangleJaeum(char: string): boolean;
28
+ /**
29
+ * Returns true if the character is a Hangeul Jamo in the range 3130 ~ 318F.
30
+ */
31
+ static isHangleJamo(char: string): boolean;
32
+ /**
33
+ * Returns true if the character is a Hangeul Moeum (vowel) in the range 314F ~ 3163.
34
+ */
35
+ static isHangleMoeum(char: string): boolean;
36
+ compositionString: string;
37
+ extra: string;
38
+ restrict: number;
39
+ private instant;
40
+ private archives;
41
+ onUpdate?: (text: string) => void;
42
+ onLimited?: (text: string) => void;
43
+ onError?: (text: string) => void;
44
+ constructor();
45
+ ver(): string;
46
+ get instantChars(): string[];
47
+ set instantChars(value: string[]);
48
+ /**
49
+ * Input Jaeum or Moeum to compose.
50
+ */
51
+ addJamo(char: string): void;
52
+ /**
53
+ * Save current composed strings by key.
54
+ */
55
+ archive(key: string, txt?: string | null): number;
56
+ /**
57
+ * Restore composed state from the saved key.
58
+ */
59
+ restore(key: string): string;
60
+ /**
61
+ * Logic engine to process the composition of characters in instant array.
62
+ */
63
+ private instantUpdate;
64
+ /**
65
+ * Helper to dispatch events and invoke corresponding callbacks.
66
+ */
67
+ private dispatchComposerEvent;
68
+ /**
69
+ * Insert character that is not available to compose (e.g. English, numbers, symbols).
70
+ */
71
+ addSpecialChar(char: string, at?: number): void;
72
+ /**
73
+ * Add Jamo by Unicode character code.
74
+ */
75
+ addJamoUnicode(code: number): void;
76
+ /**
77
+ * Delete character by backspace.
78
+ */
79
+ backSpace(at?: number): void;
80
+ /**
81
+ * Composes two Jamos and returns array with composed character or split ones.
82
+ */
83
+ compare2Jamo(charA: string, charB: string): string[];
84
+ /**
85
+ * Composes three Jamos.
86
+ */
87
+ compare3Syllables(init: string, mid: string, fin: string): string[];
88
+ /**
89
+ * Checks if string is a valid Hangul syllable or Jamo.
90
+ */
91
+ compatibleHangleJamo(char: string): boolean;
92
+ /**
93
+ * Checks if character is a Jaeum.
94
+ */
95
+ compatibleJaeum(char: string): boolean;
96
+ /**
97
+ * Checks if character is a Moeum.
98
+ */
99
+ compatibleMoeum(char: string): boolean;
100
+ /**
101
+ * Deletes characters at target index.
102
+ */
103
+ del(at?: number): void;
104
+ /**
105
+ * Resets composer state.
106
+ */
107
+ reset(): void;
108
+ /**
109
+ * Checks if character is in FINAL.
110
+ */
111
+ isFinalJamo(char: string): boolean;
112
+ /**
113
+ * Checks if character is in INITIAL.
114
+ */
115
+ isInitialJamo(char: string): boolean;
116
+ /**
117
+ * Checks if character is in MEDIAL.
118
+ */
119
+ isMedialJamo(char: string): boolean;
120
+ /**
121
+ * Adds space.
122
+ */
123
+ space(at?: number): void;
124
+ /**
125
+ * Combines initial, medial, and final into a single Hangul syllable.
126
+ */
127
+ combine3Syllables(init: string, mid: string, fin: string): string;
128
+ /**
129
+ * Combine two characters (Jaeum + Jaeum, Moeum + Moeum, or Initial + Medial)
130
+ */
131
+ private combine;
132
+ private isCombinableFinalJamo;
133
+ }