normalize-ja-input 0.1.1 → 0.3.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 +48 -5
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -5
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -16,6 +16,8 @@ normalizeJaInput(" TEL:090ー1234ー5678 ")
|
|
|
16
16
|
- 全角スペースを半角スペースに変換
|
|
17
17
|
- 連続する空白を1つに正規化
|
|
18
18
|
- 日本語入力で混ざりがちなハイフン類を `-` に統一
|
|
19
|
+
- 数字間のハイフンだけを変換したり、改行を残して空白をまとめたりする設定に対応
|
|
20
|
+
- `@`、`.`、`_`、`+` を必要に応じて半角に変換
|
|
19
21
|
- 依存ゼロ
|
|
20
22
|
- TypeScript対応
|
|
21
23
|
|
|
@@ -53,6 +55,7 @@ normalizeJaInput(input, {
|
|
|
53
55
|
digits: "halfwidth",
|
|
54
56
|
alphabets: "halfwidth",
|
|
55
57
|
hyphens: "ascii",
|
|
58
|
+
punctuation: "preserve",
|
|
56
59
|
})
|
|
57
60
|
```
|
|
58
61
|
|
|
@@ -70,14 +73,18 @@ normalizeJaInput(" 山田 ")
|
|
|
70
73
|
|
|
71
74
|
### `collapseSpaces`
|
|
72
75
|
|
|
73
|
-
Type: `boolean`
|
|
76
|
+
Type: `boolean | "horizontal"`
|
|
77
|
+
|
|
74
78
|
Default: `true`
|
|
75
79
|
|
|
76
|
-
|
|
80
|
+
`true` は半角スペース、タブ、改行の並びを半角スペース1つにします。単独のタブや改行も半角スペースになります。`"horizontal"` は半角スペースとタブだけをまとめ、途中の改行を残します。前後の改行も残すには `trim: false` を指定してください。`false` はこの処理を無効にします。
|
|
77
81
|
|
|
78
82
|
```ts
|
|
79
83
|
normalizeJaInput("山田 太郎")
|
|
80
84
|
// => "山田 太郎"
|
|
85
|
+
|
|
86
|
+
normalizeJaInput("東京都 港区\n芝公園 1丁目", { collapseSpaces: "horizontal" })
|
|
87
|
+
// => "東京都 港区\n芝公園 1丁目"
|
|
81
88
|
```
|
|
82
89
|
|
|
83
90
|
### `spaces`
|
|
@@ -85,7 +92,7 @@ normalizeJaInput("山田 太郎")
|
|
|
85
92
|
Type: `"preserve" | "halfwidth"`
|
|
86
93
|
Default: `"halfwidth"`
|
|
87
94
|
|
|
88
|
-
|
|
95
|
+
`"halfwidth"` は全角スペースを半角スペースにします。`"preserve"` を選んでも、`trim` が有効なら前後の全角スペースは取り除かれます。
|
|
89
96
|
|
|
90
97
|
```ts
|
|
91
98
|
normalizeJaInput("山田 太郎")
|
|
@@ -118,16 +125,50 @@ normalizeJaInput("ABCxyz")
|
|
|
118
125
|
|
|
119
126
|
### `hyphens`
|
|
120
127
|
|
|
121
|
-
Type: `"preserve" | "ascii"`
|
|
128
|
+
Type: `"preserve" | "ascii" | "between-digits"`
|
|
129
|
+
|
|
122
130
|
Default: `"ascii"`
|
|
123
131
|
|
|
124
|
-
|
|
132
|
+
`"ascii"` は長音符 `ー` を含むハイフン類を ASCII の `-` に統一します。`"between-digits"` は対象の記号の直前と直後がどちらも半角または全角の数字の場合にだけ変換します。`"preserve"` はハイフン類をそのまま残します。
|
|
125
133
|
|
|
126
134
|
```ts
|
|
127
135
|
normalizeJaInput("090ー1234−5678")
|
|
128
136
|
// => "090-1234-5678"
|
|
137
|
+
|
|
138
|
+
normalizeJaInput("コーヒー 090ー1234", { hyphens: "between-digits" })
|
|
139
|
+
// => "コーヒー 090-1234"
|
|
140
|
+
|
|
141
|
+
normalizeJaInput("090ー1234", { hyphens: "between-digits", digits: "preserve" })
|
|
142
|
+
// => "090-1234"
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### `punctuation`
|
|
146
|
+
|
|
147
|
+
Type: `"preserve" | "ascii"`
|
|
148
|
+
|
|
149
|
+
Default: `"preserve"`
|
|
150
|
+
|
|
151
|
+
`"ascii"` は `@`、`.`、`_`、`+` をそれぞれ `@`、`.`、`_`、`+` にします。`"preserve"` ではこれらの文字をそのまま残します。ハイフン類の変換は `hyphens` で設定します。
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
normalizeJaInput(" test+1@example.jp ", {
|
|
155
|
+
punctuation: "ascii",
|
|
156
|
+
hyphens: "preserve",
|
|
157
|
+
})
|
|
158
|
+
// => "test+1@example.jp"
|
|
129
159
|
```
|
|
130
160
|
|
|
161
|
+
## 入力欄ごとの設定例
|
|
162
|
+
|
|
163
|
+
| 入力欄 | オプション | 入力 → 出力 |
|
|
164
|
+
| --- | --- | --- |
|
|
165
|
+
| 電話番号 | `{ hyphens: "between-digits" }` | `090ー1234` → `090-1234` |
|
|
166
|
+
| 氏名 | `{ hyphens: "preserve" }` | `メアリー 山田` → `メアリー 山田` |
|
|
167
|
+
| メールアドレス・ID | `{ punctuation: "ascii", hyphens: "preserve" }` | `a_1@example.jp` → `a_1@example.jp` |
|
|
168
|
+
| 複数行の住所 | `{ collapseSpaces: "horizontal" }` | 各行のスペースやタブをまとめ、途中の改行を残す |
|
|
169
|
+
|
|
170
|
+
メールアドレスや ID の妥当性は、変換後にフォーム側で確認してください。
|
|
171
|
+
|
|
131
172
|
---
|
|
132
173
|
|
|
133
174
|
## Live Demo
|
|
@@ -137,6 +178,8 @@ normalizeJaInput("090ー1234−5678")
|
|
|
137
178
|
- GitHub → `https://github.com/fusaihanabi/normalize-ja-input`
|
|
138
179
|
- Official → `https://dl.fusai-hanabi.com`
|
|
139
180
|
|
|
181
|
+
デモを更新するときは `npm run build` で `dist/` と `docs/index.js` を生成し、GitHub Pages 用の `docs/index.js` をソースの変更と一緒にコミットします。
|
|
182
|
+
|
|
140
183
|
---
|
|
141
184
|
|
|
142
185
|
## What this library does not do
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
export type NormalizeJaInputOptions = {
|
|
2
2
|
/** Trim leading and trailing whitespace after normalization. */
|
|
3
3
|
trim?: boolean;
|
|
4
|
-
/** Collapse
|
|
5
|
-
collapseSpaces?: boolean;
|
|
4
|
+
/** Collapse ASCII whitespace, or only spaces and tabs with "horizontal". */
|
|
5
|
+
collapseSpaces?: boolean | "horizontal";
|
|
6
6
|
/** Convert full-width spaces to half-width spaces. */
|
|
7
7
|
spaces?: "preserve" | "halfwidth";
|
|
8
8
|
/** Convert full-width digits to half-width digits. */
|
|
9
9
|
digits?: "preserve" | "halfwidth";
|
|
10
10
|
/** Convert full-width Latin alphabets to half-width alphabets. */
|
|
11
11
|
alphabets?: "preserve" | "halfwidth";
|
|
12
|
-
/** Convert
|
|
13
|
-
hyphens?: "preserve" | "ascii";
|
|
12
|
+
/** Convert hyphen variants everywhere or only between ASCII or full-width digits. */
|
|
13
|
+
hyphens?: "preserve" | "ascii" | "between-digits";
|
|
14
|
+
/** Convert @, ., _, and + to ASCII. */
|
|
15
|
+
punctuation?: "preserve" | "ascii";
|
|
14
16
|
};
|
|
15
17
|
/**
|
|
16
18
|
* Normalize common Japanese form input variations.
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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,
|
|
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,4EAA4E;IAC5E,cAAc,CAAC,EAAE,OAAO,GAAG,YAAY,CAAA;IAEvC,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,qFAAqF;IACrF,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,GAAG,gBAAgB,CAAA;IAEjD,uCAAuC;IACvC,WAAW,CAAC,EAAE,UAAU,GAAG,OAAO,CAAA;CACnC,CAAA;AAgBD;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,uBAA4B,GACpC,MAAM,CA8CR"}
|
package/dist/index.js
CHANGED
|
@@ -5,10 +5,11 @@ const DEFAULT_OPTIONS = {
|
|
|
5
5
|
digits: "halfwidth",
|
|
6
6
|
alphabets: "halfwidth",
|
|
7
7
|
hyphens: "ascii",
|
|
8
|
+
punctuation: "preserve",
|
|
8
9
|
};
|
|
9
|
-
const
|
|
10
|
-
const FULLWIDTH_ALPHABET_OFFSET = 0xfee0;
|
|
10
|
+
const FULLWIDTH_ASCII_OFFSET = 0xfee0;
|
|
11
11
|
const HYPHEN_LIKE_CHARS = /[ー-―‐‑‒–—−﹣ー]/g;
|
|
12
|
+
const DIGIT = /[0-90-9]/;
|
|
12
13
|
/**
|
|
13
14
|
* Normalize common Japanese form input variations.
|
|
14
15
|
*
|
|
@@ -25,16 +26,25 @@ export function normalizeJaInput(input, options = {}) {
|
|
|
25
26
|
value = value.replace(/\u3000/g, " ");
|
|
26
27
|
}
|
|
27
28
|
if (opts.digits === "halfwidth") {
|
|
28
|
-
value = value.replace(/[0-9]/g, char => String.fromCharCode(char.charCodeAt(0) -
|
|
29
|
+
value = value.replace(/[0-9]/g, char => String.fromCharCode(char.charCodeAt(0) - FULLWIDTH_ASCII_OFFSET));
|
|
29
30
|
}
|
|
30
31
|
if (opts.alphabets === "halfwidth") {
|
|
31
|
-
value = value.replace(/[A-Za-z]/g, char => String.fromCharCode(char.charCodeAt(0) -
|
|
32
|
+
value = value.replace(/[A-Za-z]/g, char => String.fromCharCode(char.charCodeAt(0) - FULLWIDTH_ASCII_OFFSET));
|
|
32
33
|
}
|
|
33
34
|
if (opts.hyphens === "ascii") {
|
|
34
35
|
value = value.replace(HYPHEN_LIKE_CHARS, "-");
|
|
35
36
|
}
|
|
37
|
+
else if (opts.hyphens === "between-digits") {
|
|
38
|
+
value = value.replace(HYPHEN_LIKE_CHARS, (char, index, input) => DIGIT.test(input.charAt(index - 1)) && DIGIT.test(input.charAt(index + 1))
|
|
39
|
+
? "-"
|
|
40
|
+
: char);
|
|
41
|
+
}
|
|
42
|
+
if (opts.punctuation === "ascii") {
|
|
43
|
+
value = value.replace(/[@._+]/g, char => String.fromCharCode(char.charCodeAt(0) - FULLWIDTH_ASCII_OFFSET));
|
|
44
|
+
}
|
|
36
45
|
if (opts.collapseSpaces) {
|
|
37
|
-
|
|
46
|
+
const whitespace = opts.collapseSpaces === "horizontal" ? /[ \t]+/g : /[ \t\r\n]+/g;
|
|
47
|
+
value = value.replace(whitespace, " ");
|
|
38
48
|
}
|
|
39
49
|
if (opts.trim) {
|
|
40
50
|
value = value.trim();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAuBA,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;IAChB,WAAW,EAAE,UAAU;CACxB,CAAA;AAED,MAAM,sBAAsB,GAAG,MAAM,CAAA;AACrC,MAAM,iBAAiB,GAAG,gBAAgB,CAAA;AAC1C,MAAM,KAAK,GAAG,UAAU,CAAA;AAExB;;;;;;;;GAQG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAAa,EACb,UAAmC,EAAE;IAErC,MAAM,IAAI,GAAG,EAAE,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IAED,IAAI,IAAI,CAAC,WAAW,KAAK,OAAO,EAAE,CAAC;QACjC,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,IAAI,CAAC,EAAE,CACtC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,sBAAsB,CAAC,CACjE,CAAA;IACH,CAAC;IAED,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;QACxB,MAAM,UAAU,GAAG,IAAI,CAAC,cAAc,KAAK,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,aAAa,CAAA;QACnF,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,EAAE,GAAG,CAAC,CAAA;IACxC,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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "normalize-ja-input",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Tiny TypeScript utility for normalizing Japanese form input.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"LICENSE"
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
|
-
"build": "tsc",
|
|
20
|
+
"build": "tsc && npm run build:demo",
|
|
21
|
+
"build:demo": "tsc --outDir docs --declaration false --declarationMap false --sourceMap false",
|
|
21
22
|
"test": "vitest",
|
|
22
23
|
"test:run": "vitest run",
|
|
23
24
|
"typecheck": "tsc --noEmit",
|