// GraalJS (engine type: 2)
//
// Notes:
// Similar to Excel's LEFT function or JavaScript's Slice function.
// Can be used to cut out the beginning of long texts.
// Can be used for pre-processing to replace emails or phone numbers with asterisks.
// - Use [Update Data] for character string combination. (M227)
// - In some cases, #sformat function (format by "java.lang.String") can be substituted.
//
// Notes (ja):
// エクセルの LEFT 関数や JavaScript の Slice 関数と同等の処理を実現できます。
// テキストのリード文を切り出す場合などに活用できます。
// 伏字加工(メールアドレスや電話番号等)の前処理にも活用できます。
// - 文字列の結合処理には、[データ更新]を利用します(M227)
// - #sformat 関数("java.lang.String" による format )で代用可能なケースもあります
//////// START "main()" ////////////////////////////////////////////////////////////////
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const strA = configs.get( "conf_StrA" ) + ""; // required
const numB = configs.get( "conf_NumB" ) + ""; // required
const strPad = configs.get( "conf_StrPad" ) + ""; // not required
const dataIdD = configs.get( "conf_DataIdD" ) + ""; // required
if( isNaN( parseInt(numB) )){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {B} is not an integer \n" );
}
engine.log( " AutomatedTask Substring Length: " + numB );
engine.log( " AutomatedTask Padding String: " + strPad );
//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing, except Expression Language config)
//// == Calculating / 演算 ==
let strOutput = "";
if( 0 < numB && numB < strA.length ){
strOutput += strA.slice( 0, numB );
}else if( strA.length < numB ){
strOutput += strA;
if( strPad !== "" ){
strOutput = myPadEnd( strOutput, numB, strPad );
}
}else if( numB < 0 && -numB < strA.length ){
strOutput += strA.slice( numB );
}else if( strA.length < -numB ){
strOutput += strA;
if( strPad !== "" ){
strOutput = myPadStart( strOutput, -numB, strPad );
}
}
//// == ワークフローデータへの代入 / Data Updating ==
engine.setDataByNumber( dataIdD, strOutput );
} //////// END "main()" ////////////////////////////////////////////////////////////////
function myPadEnd( strTarget, numLength, strPad ){
if( strTarget.length >= numLength ){
return strTarget;
}else{
numLength = numLength - strTarget.length;
if( numLength > strPad.length ){
strPad += strPad.repeat( numLength / strPad.length );
}
return strTarget + strPad.slice( 0, numLength );
}
}
function myPadStart( strTarget, numLength, strPad ){
if( strTarget.length >= numLength){
return strTarget;
}else{
numLength = numLength - strTarget.length;
if( numLength > strPad.length ){
strPad += strPad.repeat( numLength / strPad.length );
}
return strPad.slice( 0, numLength ) + strTarget;
}
}