normalize-ja-input 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # normalize-ja-input
2
+
3
+ Tiny TypeScript utility for normalizing Japanese form input.
4
+
5
+ ```ts
6
+ import { normalizeJaInput } from "normalize-ja-input"
7
+
8
+ normalizeJaInput(" TEL:090ー1234ー5678 ")
9
+ // => "TEL:090-1234-5678"
10
+ ```
11
+
12
+ ## Features
13
+
14
+ - 全角数字を半角数字に変換
15
+ - 全角英字を半角英字に変換
16
+ - 全角スペースを半角スペースに変換
17
+ - 連続する空白を1つに正規化
18
+ - 日本語入力で混ざりがちなハイフン類を `-` に統一
19
+ - 依存ゼロ
20
+ - TypeScript対応
21
+
22
+ ## Installation
23
+
24
+ ```sh
25
+ npm install normalize-ja-input
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```ts
31
+ import { normalizeJaInput } from "normalize-ja-input"
32
+
33
+ normalizeJaInput(" 山田 太郎 ")
34
+ // => "山田 太郎"
35
+
36
+ normalizeJaInput("12345")
37
+ // => "12345"
38
+
39
+ normalizeJaInput("ABCxyz")
40
+ // => "ABCxyz"
41
+
42
+ normalizeJaInput("090ー1234−5678")
43
+ // => "090-1234-5678"
44
+ ```
45
+
46
+ ## Options
47
+
48
+ ```ts
49
+ normalizeJaInput(input, {
50
+ trim: true,
51
+ collapseSpaces: true,
52
+ spaces: "halfwidth",
53
+ digits: "halfwidth",
54
+ alphabets: "halfwidth",
55
+ hyphens: "ascii",
56
+ })
57
+ ```
58
+
59
+ ### `trim`
60
+
61
+ Type: `boolean`
62
+ Default: `true`
63
+
64
+ 前後の空白を削除します。
65
+
66
+ ```ts
67
+ normalizeJaInput(" 山田 ")
68
+ // => "山田"
69
+ ```
70
+
71
+ ### `collapseSpaces`
72
+
73
+ Type: `boolean`
74
+ Default: `true`
75
+
76
+ 連続する半角スペース、タブ、改行を1つの半角スペースにします。
77
+
78
+ ```ts
79
+ normalizeJaInput("山田 太郎")
80
+ // => "山田 太郎"
81
+ ```
82
+
83
+ ### `spaces`
84
+
85
+ Type: `"preserve" | "halfwidth"`
86
+ Default: `"halfwidth"`
87
+
88
+ 全角スペースを半角スペースにします。
89
+
90
+ ```ts
91
+ normalizeJaInput("山田 太郎")
92
+ // => "山田 太郎"
93
+ ```
94
+
95
+ ### `digits`
96
+
97
+ Type: `"preserve" | "halfwidth"`
98
+ Default: `"halfwidth"`
99
+
100
+ 全角数字を半角数字にします。
101
+
102
+ ```ts
103
+ normalizeJaInput("123")
104
+ // => "123"
105
+ ```
106
+
107
+ ### `alphabets`
108
+
109
+ Type: `"preserve" | "halfwidth"`
110
+ Default: `"halfwidth"`
111
+
112
+ 全角英字を半角英字にします。
113
+
114
+ ```ts
115
+ normalizeJaInput("ABCxyz")
116
+ // => "ABCxyz"
117
+ ```
118
+
119
+ ### `hyphens`
120
+
121
+ Type: `"preserve" | "ascii"`
122
+ Default: `"ascii"`
123
+
124
+ 日本語入力で混ざりがちなハイフン類を ASCII の `-` に統一します。
125
+
126
+ ```ts
127
+ normalizeJaInput("090ー1234−5678")
128
+ // => "090-1234-5678"
129
+ ```
130
+
131
+ ## What this library does not do
132
+
133
+ This library intentionally does not validate Japanese phone numbers, postal codes, addresses, or names.
134
+
135
+ It only normalizes common input variations before validation or storage.
136
+
137
+ ## License
138
+
139
+ MIT
@@ -0,0 +1,25 @@
1
+ export type NormalizeJaInputOptions = {
2
+ /** Trim leading and trailing whitespace after normalization. */
3
+ trim?: boolean;
4
+ /** Collapse consecutive ASCII whitespace into a single space. */
5
+ collapseSpaces?: boolean;
6
+ /** Convert full-width spaces to half-width spaces. */
7
+ spaces?: "preserve" | "halfwidth";
8
+ /** Convert full-width digits to half-width digits. */
9
+ digits?: "preserve" | "halfwidth";
10
+ /** Convert full-width Latin alphabets to half-width alphabets. */
11
+ alphabets?: "preserve" | "halfwidth";
12
+ /** Convert common Japanese hyphen-like characters to ASCII hyphen-minus. */
13
+ hyphens?: "preserve" | "ascii";
14
+ };
15
+ /**
16
+ * Normalize common Japanese form input variations.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * normalizeJaInput(" TEL:090ー1234ー5678 ")
21
+ * // => "TEL:090-1234-5678"
22
+ * ```
23
+ */
24
+ export declare function normalizeJaInput(input: string, options?: NormalizeJaInputOptions): string;
25
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,uBAAuB,GAAG;IACpC,gEAAgE;IAChE,IAAI,CAAC,EAAE,OAAO,CAAA;IAEd,iEAAiE;IACjE,cAAc,CAAC,EAAE,OAAO,CAAA;IAExB,sDAAsD;IACtD,MAAM,CAAC,EAAE,UAAU,GAAG,WAAW,CAAA;IAEjC,sDAAsD;IACtD,MAAM,CAAC,EAAE,UAAU,GAAG,WAAW,CAAA;IAEjC,kEAAkE;IAClE,SAAS,CAAC,EAAE,UAAU,GAAG,WAAW,CAAA;IAEpC,4EAA4E;IAC5E,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAA;CAC/B,CAAA;AAeD;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,uBAA4B,GACpC,MAAM,CAiCR"}
package/dist/index.js ADDED
@@ -0,0 +1,44 @@
1
+ const DEFAULT_OPTIONS = {
2
+ trim: true,
3
+ collapseSpaces: true,
4
+ spaces: "halfwidth",
5
+ digits: "halfwidth",
6
+ alphabets: "halfwidth",
7
+ hyphens: "ascii",
8
+ };
9
+ const FULLWIDTH_DIGIT_OFFSET = 0xfee0;
10
+ const FULLWIDTH_ALPHABET_OFFSET = 0xfee0;
11
+ const HYPHEN_LIKE_CHARS = /[ー-―‐‑‒–—−﹣ー]/g;
12
+ /**
13
+ * Normalize common Japanese form input variations.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * normalizeJaInput(" TEL:090ー1234ー5678 ")
18
+ * // => "TEL:090-1234-5678"
19
+ * ```
20
+ */
21
+ export function normalizeJaInput(input, options = {}) {
22
+ const opts = { ...DEFAULT_OPTIONS, ...options };
23
+ let value = input;
24
+ if (opts.spaces === "halfwidth") {
25
+ value = value.replace(/\u3000/g, " ");
26
+ }
27
+ if (opts.digits === "halfwidth") {
28
+ value = value.replace(/[0-9]/g, char => String.fromCharCode(char.charCodeAt(0) - FULLWIDTH_DIGIT_OFFSET));
29
+ }
30
+ if (opts.alphabets === "halfwidth") {
31
+ value = value.replace(/[A-Za-z]/g, char => String.fromCharCode(char.charCodeAt(0) - FULLWIDTH_ALPHABET_OFFSET));
32
+ }
33
+ if (opts.hyphens === "ascii") {
34
+ value = value.replace(HYPHEN_LIKE_CHARS, "-");
35
+ }
36
+ if (opts.collapseSpaces) {
37
+ value = value.replace(/[ \t\r\n]+/g, " ");
38
+ }
39
+ if (opts.trim) {
40
+ value = value.trim();
41
+ }
42
+ return value;
43
+ }
44
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAoBA,MAAM,eAAe,GAAsC;IACzD,IAAI,EAAE,IAAI;IACV,cAAc,EAAE,IAAI;IACpB,MAAM,EAAE,WAAW;IACnB,MAAM,EAAE,WAAW;IACnB,SAAS,EAAE,WAAW;IACtB,OAAO,EAAE,OAAO;CACjB,CAAA;AAED,MAAM,sBAAsB,GAAG,MAAM,CAAA;AACrC,MAAM,yBAAyB,GAAG,MAAM,CAAA;AACxC,MAAM,iBAAiB,GAAG,gBAAgB,CAAA;AAE1C;;;;;;;;GAQG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAAa,EACb,UAAmC,EAAE;IAErC,MAAM,IAAI,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAA;IAC/C,IAAI,KAAK,GAAG,KAAK,CAAA;IAEjB,IAAI,IAAI,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;QAChC,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;IACvC,CAAC;IAED,IAAI,IAAI,CAAC,MAAM,KAAK,WAAW,EAAE,CAAC;QAChC,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CACrC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,sBAAsB,CAAC,CACjE,CAAA;IACH,CAAC;IAED,IAAI,IAAI,CAAC,SAAS,KAAK,WAAW,EAAE,CAAC;QACnC,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,IAAI,CAAC,EAAE,CACxC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,yBAAyB,CAAC,CACpE,CAAA;IACH,CAAC;IAED,IAAI,IAAI,CAAC,OAAO,KAAK,OAAO,EAAE,CAAC;QAC7B,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,iBAAiB,EAAE,GAAG,CAAC,CAAA;IAC/C,CAAC;IAED,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;QACxB,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC,CAAA;IAC3C,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;QACd,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;IACtB,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "normalize-ja-input",
3
+ "version": "0.1.0",
4
+ "description": "Tiny TypeScript utility for normalizing Japanese form input.",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ }
11
+ },
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "files": [
15
+ "dist",
16
+ "README.md",
17
+ "LICENSE"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc",
21
+ "test": "vitest",
22
+ "test:run": "vitest run",
23
+ "typecheck": "tsc --noEmit",
24
+ "prepublishOnly": "npm run test:run && npm run build"
25
+ },
26
+ "keywords": [
27
+ "japanese",
28
+ "normalize",
29
+ "normalizer",
30
+ "form",
31
+ "input",
32
+ "zenkaku",
33
+ "hankaku",
34
+ "typescript"
35
+ ],
36
+ "author": "",
37
+ "license": "MIT",
38
+ "devDependencies": {
39
+ "typescript": "^5.4.0",
40
+ "vitest": "^1.6.0"
41
+ }
42
+ }