text-input-guard 0.2.0 → 0.2.1

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.
@@ -402,7 +402,7 @@
402
402
  * @typedef {Object} AttachOptions
403
403
  * @property {Rule[]} [rules] - 適用するルール配列(順番がフェーズ内実行順になる)
404
404
  * @property {boolean} [warn] - 非対応ルールなどを console.warn するか
405
- * @property {string} [invalidClass] - エラー時に付けるclass名
405
+ * @property {string} [invalidClass="is-invalid"] - エラー時に付けるclass名
406
406
  * @property {SeparateValueOptions} [separateValue] - 表示値と内部値の分離設定
407
407
  * @property {(result: ValidateResult) => void} [onValidate] - 評価完了時の通知(input/commitごと)
408
408
  */
@@ -2705,6 +2705,104 @@
2705
2705
  return comma();
2706
2706
  };
2707
2707
 
2708
+ /**
2709
+ * The script is part of TextInputGuard.
2710
+ *
2711
+ * AUTHOR:
2712
+ * natade-jp (https://github.com/natade-jp)
2713
+ *
2714
+ * LICENSE:
2715
+ * The MIT license https://opensource.org/licenses/MIT
2716
+ */
2717
+
2718
+ /**
2719
+ * IMEオフ入力相当の文字変換テーブル
2720
+ * @type {Record<string, string>}
2721
+ */
2722
+ /* eslint-disable quote-props */
2723
+ const IME_OFF_MAP = {
2724
+ "\u3000": "\u0020", // 全角スペース → space
2725
+ "\u3001": "\u002C", // 、 → ,
2726
+ "\u3002": "\u002E", // 。 → .
2727
+ "\u300C": "\u005B", // 「 → [
2728
+ "\u300D": "\u005D", // 」 → ]
2729
+ "\u301C": "\u007E", // 〜 → ~
2730
+ "\u30FC": "\u002D", // ー → -
2731
+ "\uFFE5": "\u005C" // ¥ → \
2732
+ };
2733
+ /* eslint-enable quote-props */
2734
+
2735
+ /**
2736
+ * ASCII入力欄に日本語IMEで入った文字をASCIIへ矯正する
2737
+ * @param {string} text - 変換したいテキスト
2738
+ * @returns {string} 変換後のテキスト
2739
+ */
2740
+ const toImeOff = function (text) {
2741
+ return Array.from(String(text), (ch) => {
2742
+ // 個別マップ
2743
+ if (ch in IME_OFF_MAP) {
2744
+ return IME_OFF_MAP[ch];
2745
+ }
2746
+
2747
+ const code = ch.charCodeAt(0);
2748
+
2749
+ // 全角ASCII
2750
+ if (code >= 0xFF01 && code <= 0xFF5E) {
2751
+ return String.fromCharCode(code - 0xFEE0);
2752
+ }
2753
+
2754
+ // シングルクォート系
2755
+ if (code >= 0x2018 && code <= 0x201B) {
2756
+ return "'";
2757
+ }
2758
+
2759
+ // ダブルクォート系
2760
+ if (code >= 0x201C && code <= 0x201F) {
2761
+ return '"';
2762
+ }
2763
+
2764
+ return ch;
2765
+ }).join("");
2766
+ };
2767
+
2768
+ /**
2769
+ * ASCII入力欄に日本語IMEで入った文字をASCIIへ矯正する
2770
+ *
2771
+ * 注意:
2772
+ * - これは「半角化」ではなく「IMEオフ入力相当への寄せ」
2773
+ * - ascii() とは責務が異なる
2774
+ *
2775
+ * @returns {Rule}
2776
+ */
2777
+ function imeOff() {
2778
+ return {
2779
+ name: "imeOff",
2780
+ targets: ["input", "textarea"],
2781
+
2782
+ normalizeChar(value, ctx) {
2783
+ return toImeOff(value);
2784
+ }
2785
+ };
2786
+ }
2787
+
2788
+ /**
2789
+ * dataset から imeOff ルールを生成する
2790
+ *
2791
+ * 対応する data 属性
2792
+ * - data-tig-rules-ime-off
2793
+ *
2794
+ * @param {DOMStringMap} dataset
2795
+ * @param {HTMLInputElement|HTMLTextAreaElement} _el
2796
+ * @returns {Rule|null}
2797
+ */
2798
+ imeOff.fromDataset = function fromDataset(dataset, _el) {
2799
+ if (dataset.tigRulesImeOff == null) {
2800
+ return null;
2801
+ }
2802
+
2803
+ return imeOff();
2804
+ };
2805
+
2708
2806
  /**
2709
2807
  * The script is part of Mojix for TextInputGuard.
2710
2808
  *
@@ -5926,7 +6024,7 @@
5926
6024
  function ascii(options = {}) {
5927
6025
  /** @type {AsciiRuleOptions} */
5928
6026
  const opt = {
5929
- case: options.case ?? null
6027
+ case: options.case ?? "none"
5930
6028
  };
5931
6029
 
5932
6030
  return {
@@ -5939,6 +6037,10 @@
5939
6037
  // まず半角へ正規化
5940
6038
  s = Mojix.toHalfWidthAsciiCode(s);
5941
6039
 
6040
+ // toHalfWidthAsciiCode で対応できていない文字も実施
6041
+ s = s.replace(/\uFFE5/g, "\u005C"); //¥
6042
+ s = s.replace(/[\u2010-\u2015\u2212\u30FC\uFF0D\uFF70]/g, "\u002D"); //ハイフンに似ている記号
6043
+
5942
6044
  // 英字の大文字/小文字統一
5943
6045
  if (opt.case === "upper") {
5944
6046
  s = s.toUpperCase();
@@ -7240,6 +7342,7 @@
7240
7342
  { name: "numeric", fromDataset: numeric.fromDataset },
7241
7343
  { name: "digits", fromDataset: digits.fromDataset },
7242
7344
  { name: "comma", fromDataset: comma.fromDataset },
7345
+ { name: "imeOff", fromDataset: imeOff.fromDataset },
7243
7346
  { name: "kana", fromDataset: kana.fromDataset },
7244
7347
  { name: "ascii", fromDataset: ascii.fromDataset },
7245
7348
  { name: "filter", fromDataset: filter.fromDataset },
@@ -7264,6 +7367,7 @@
7264
7367
  numeric,
7265
7368
  digits,
7266
7369
  comma,
7370
+ imeOff,
7267
7371
  kana,
7268
7372
  ascii,
7269
7373
  filter,
@@ -7277,11 +7381,11 @@
7277
7381
 
7278
7382
  /**
7279
7383
  * バージョン(ビルド時に置換したいならここを差し替える)
7280
- * 例: rollup replace で ""0.2.0"" を package.json の version に置換
7384
+ * 例: rollup replace で ""0.2.1"" を package.json の version に置換
7281
7385
  */
7282
7386
  // @ts-ignore
7283
7387
  // eslint-disable-next-line no-undef
7284
- const version = "0.2.0" ;
7388
+ const version = "0.2.1" ;
7285
7389
 
7286
7390
  exports.ascii = ascii;
7287
7391
  exports.attach = attach;
@@ -7291,6 +7395,7 @@
7291
7395
  exports.comma = comma;
7292
7396
  exports.digits = digits;
7293
7397
  exports.filter = filter;
7398
+ exports.imeOff = imeOff;
7294
7399
  exports.kana = kana;
7295
7400
  exports.length = length;
7296
7401
  exports.numeric = numeric;