Merged PR 90: feat:#594 全角/半角を変換する

指定したフィールドの文字列を全角/半角指定した書式に変換する

Related work items: #594
This commit is contained in:
Kanaru Tsuda
2024-09-09 04:36:52 +00:00
committed by Takuto Yoshida(タクト)
2 changed files with 322 additions and 0 deletions

View File

@@ -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<IActionResult> {
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();

View File

@@ -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';