From c7eb8171ef044f976d8d3a7be9963862e167aba5 Mon Sep 17 00:00:00 2001 From: kanarutsuda <–Kanaru.Tsuda@jp.ricoh.com> Date: Thu, 5 Sep 2024 14:49:03 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20#594=20=E5=85=A8=E8=A7=92/=E5=8D=8A?= =?UTF-8?q?=E8=A7=92=E3=82=92=E5=A4=89=E6=8F=9B=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit {detail}:指定したフィールドを全角か半角に変換する --- .../src/actions/half-full-conversion.ts | 321 ++++++++++++++++++ .../src/types/action-process.ts | 1 + 2 files changed, 322 insertions(+) create mode 100644 plugin/kintone-addins/src/actions/half-full-conversion.ts diff --git a/plugin/kintone-addins/src/actions/half-full-conversion.ts b/plugin/kintone-addins/src/actions/half-full-conversion.ts new file mode 100644 index 0000000..2ccd2d7 --- /dev/null +++ b/plugin/kintone-addins/src/actions/half-full-conversion.ts @@ -0,0 +1,321 @@ +import { actionAddins } from "."; +import { + IAction, + IActionResult, + IActionNode, + IActionProperty, + IField, + IContext, + IVarName, +} from "../types/ActionTypes"; +import { ConditionTree } from "../types/Conditions"; + +/** + * アクションの属性定義 + */ +interface IConversionProps { + field: IField; + conversion: string; + verName: IVarName; +} + +const fullWidthToHalfWidthMap: { [key: string]: string } = { + ガ: "ガ", + ギ: "ギ", + グ: "グ", + ゲ: "ゲ", + ゴ: "ゴ", + ザ: "ザ", + ジ: "ジ", + ズ: "ズ", + ゼ: "ゼ", + ゾ: "ゾ", + ダ: "ダ", + ヂ: "ヂ", + ヅ: "ヅ", + デ: "デ", + ド: "ド", + バ: "バ", + ビ: "ビ", + ブ: "ブ", + ベ: "ベ", + ボ: "ボ", + パ: "パ", + ピ: "ピ", + プ: "プ", + ペ: "ペ", + ポ: "ポ", + ヴ: "ヴ", + ヷ: "ヷ", + ヺ: "ヺ", + ア: "ア", + イ: "イ", + ウ: "ウ", + エ: "エ", + オ: "オ", + カ: "カ", + キ: "キ", + ク: "ク", + ケ: "ケ", + コ: "コ", + サ: "サ", + シ: "シ", + ス: "ス", + セ: "セ", + ソ: "ソ", + タ: "タ", + チ: "チ", + ツ: "ツ", + テ: "テ", + ト: "ト", + ナ: "ナ", + ニ: "ニ", + ヌ: "ヌ", + ネ: "ネ", + ノ: "ノ", + ハ: "ハ", + ヒ: "ヒ", + フ: "フ", + ヘ: "ヘ", + ホ: "ホ", + マ: "マ", + ミ: "ミ", + ム: "ム", + メ: "メ", + モ: "モ", + ヤ: "ヤ", + ユ: "ユ", + ヨ: "ヨ", + ラ: "ラ", + リ: "リ", + ル: "ル", + レ: "レ", + ロ: "ロ", + ワ: "ワ", + ヲ: "ヲ", + ン: "ン", + ァ: "ァ", + ィ: "ィ", + ゥ: "ゥ", + ェ: "ェ", + ォ: "ォ", + ッ: "ッ", + ャ: "ャ", + ュ: "ュ", + ョ: "ョ", + "。": "。", + "、": "、", + ー: "ー", + "「": "「", + "」": "」", + "・": "・", + " ": " ", // 全角スペースも半角スペースに変換 +}; + +const halfWidthToFullWidthMap: { [key: string]: string } = { + ガ: "ガ", + ギ: "ギ", + グ: "グ", + ゲ: "ゲ", + ゴ: "ゴ", + ザ: "ザ", + ジ: "ジ", + ズ: "ズ", + ゼ: "ゼ", + ゾ: "ゾ", + ダ: "ダ", + ヂ: "ヂ", + ヅ: "ヅ", + デ: "デ", + ド: "ド", + バ: "バ", + ビ: "ビ", + ブ: "ブ", + ベ: "ベ", + ボ: "ボ", + パ: "パ", + ピ: "ピ", + プ: "プ", + ペ: "ペ", + ポ: "ポ", + ヴ: "ヴ", + ヷ: "ヷ", + ヺ: "ヺ", + ア: "ア", + イ: "イ", + ウ: "ウ", + エ: "エ", + オ: "オ", + カ: "カ", + キ: "キ", + ク: "ク", + ケ: "ケ", + コ: "コ", + サ: "サ", + シ: "シ", + ス: "ス", + セ: "セ", + ソ: "ソ", + タ: "タ", + チ: "チ", + ツ: "ツ", + テ: "テ", + ト: "ト", + ナ: "ナ", + ニ: "ニ", + ヌ: "ヌ", + ネ: "ネ", + ノ: "ノ", + ハ: "ハ", + ヒ: "ヒ", + フ: "フ", + ヘ: "ヘ", + ホ: "ホ", + マ: "マ", + ミ: "ミ", + ム: "ム", + メ: "メ", + モ: "モ", + ヤ: "ヤ", + ユ: "ユ", + ヨ: "ヨ", + ラ: "ラ", + リ: "リ", + ル: "ル", + レ: "レ", + ロ: "ロ", + ワ: "ワ", + ヲ: "ヲ", + ン: "ン", + ァ: "ァ", + ィ: "ィ", + ゥ: "ゥ", + ェ: "ェ", + ォ: "ォ", + ッ: "ッ", + ャ: "ャ", + ュ: "ュ", + ョ: "ョ", + "。": "。", + "、": "、", + ー: "ー", + "「": "「", + "」": "」", + "・": "・", + " ": " ", +}; + +/** + * 全角/半角変換アクション + */ +export class FullHalfConversionAction implements IAction { + name: string; + actionProps: IActionProperty[]; + props: IConversionProps; + constructor() { + this.name = "全角/半角変換"; + this.actionProps = []; + this.props = { + field: { code: "" }, + conversion: "", + verName: { name: "" }, + }; + this.register(); + } + + /** + * アクションの実行を呼び出す + * @param actionNode + * @param event + * @returns + */ + async process( + actionNode: IActionNode, + event: any, + context: IContext + ): Promise { + let result = { + canNext: true, + result: false, + }; + try { + //属性設定を取得する + this.actionProps = actionNode.actionProps; + if ( + !("field" in actionNode.ActionValue) && + !("conversion" in actionNode.ActionValue) && + !("verName" in actionNode.ActionValue) + ) { + return result; + } + + this.props = actionNode.ActionValue as IConversionProps; + + //条件式の計算結果を取得 + const record = event.record; + if (!(this.props.field.code in record)) { + throw new Error( + `フィールド「${this.props.field.code}」が見つかりません。` + ); + } + // + const value = record[this.props.field.code]?.value; + + //条件分岐 + //未入力時は何も処理をせず終了 + if (value === undefined || value === "") { + record[this.props.field.code].error = null; + } + if (this.props.conversion === "全角") { + context.variables[this.props.verName.name] = this.toFullWidth(value); + } + if (this.props.conversion === "半角") { + context.variables[this.props.verName.name] = this.toHalfWidth(value); + } else { + record[this.props.field.code].error = null; + } + //resultプロパティ指定 + result = { + canNext: true, + result: true, + }; + return result; + + //例外処理 + } catch (error) { + context.errors.handleError(error, actionNode); + result.canNext = false; + return result; + } + } + + // 半角から全角に変換 + toFullWidth(str: string): string { + //半角の英数字と記号を検索し、半角から全角に変換(半角コードに 0xFEE0 を足して全角にする) + let retStr = str.replace(/[A-Za-z0-9!-~]/g, function (s) { + return String.fromCharCode(s.charCodeAt(0) + 0xfee0); + //半角カタカナなどを検索し、全角に変換するためのマッピングオブジェクト(halfWidthToFullWidthMap)で全角文字に変換 + }); + retStr = retStr.replace(/[\uFF61-\uFF9F ]/g, function (s) { + return halfWidthToFullWidthMap[s] || s; + }); + return retStr; + } + + toHalfWidth(str: string): string { + //全角の英数字や記号を検索し、全角から半角に変換します(全角コードから 0xFEE0 を引いて半角にする) + let retStr = str.replace(/[!-~]/g, function (s) { + return String.fromCharCode(s.charCodeAt(0) - 0xfee0); + //全角片仮名記号スペースなどを検索し、半角に変換するためのマッピングオブジェクト( halfWidthToFullWidthMap )で半角文字に変換 + }); + retStr = retStr.replace(/[ガ-ヴァ-ン。、ー「」・ ]/g, function (s) { + return fullWidthToHalfWidthMap[s] || s; + }); + return retStr; + } + + register(): void { + actionAddins[this.name] = this; + } +} +new FullHalfConversionAction(); diff --git a/plugin/kintone-addins/src/types/action-process.ts b/plugin/kintone-addins/src/types/action-process.ts index c4466f8..7088b25 100644 --- a/plugin/kintone-addins/src/types/action-process.ts +++ b/plugin/kintone-addins/src/types/action-process.ts @@ -18,6 +18,7 @@ import '../actions/value-getter'; import '../actions/string-join'; import '../actions/validation-fullwidth'; import '../actions/validation-halfwidth'; +import '../actions/half-full-conversion'; import '../actions/login-user-getter'; import '../actions/auto-lookup'; import '../actions/field-disable';