自動採番アクション追加・ドメイン追加
This commit is contained in:
71
plugin/kintone-addins/src/util/format.ts
Normal file
71
plugin/kintone-addins/src/util/format.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
export class Formatter{
|
||||
static numberFormat(num:number,format:string):string{
|
||||
let integerPart = Math.floor(Math.abs(num)).toString();
|
||||
let fractionPart = Math.abs(num).toString().split('.')[1] || '';
|
||||
let isNegative = num < 0;
|
||||
let isPercent = format.includes('%');
|
||||
// %
|
||||
if (isPercent) {
|
||||
num *= 100;
|
||||
integerPart = Math.floor(Math.abs(num)).toString();
|
||||
fractionPart = Math.abs(num).toString().split('.')[1] || '';
|
||||
}
|
||||
|
||||
// 小数と整数部分の処理
|
||||
let [integerFormat, fractionFormat] = format.split('.');
|
||||
integerPart = integerFormat ? integerPart.padStart(integerFormat.replace(/[^0]/g, '').length, '0') : integerPart;
|
||||
fractionPart = fractionPart.padEnd(fractionFormat ? fractionFormat.length : 0, '0');
|
||||
|
||||
// カマ区切
|
||||
if (/,/.test(integerFormat)) {
|
||||
const parts = [];
|
||||
while (integerPart.length) {
|
||||
parts.unshift(integerPart.slice(-3));
|
||||
integerPart = integerPart.slice(0, -3);
|
||||
}
|
||||
integerPart = parts.join(',');
|
||||
}
|
||||
|
||||
// すべて組合わせ
|
||||
let result = fractionFormat ? `${integerPart}.${fractionPart}` : integerPart;
|
||||
result = isNegative ? `-${result}` : result;
|
||||
return isPercent ? `${result}%` : result;
|
||||
}
|
||||
|
||||
static dateFormat(date: Date, format: string): string {
|
||||
const year = date.getFullYear();
|
||||
const month = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
const hour = date.getHours();
|
||||
const minute = date.getMinutes();
|
||||
const second = date.getSeconds();
|
||||
const millisecond = date.getMilliseconds();
|
||||
const timeZoneOffset = -date.getTimezoneOffset() / 60;
|
||||
|
||||
const replacements = {
|
||||
'yyyy': year.toString(),
|
||||
'yy': year.toString().slice(-2),
|
||||
'MM': month.toString().padStart(2, '0'),
|
||||
'M': month.toString(),
|
||||
'dd': day.toString().padStart(2, '0'),
|
||||
'd': day.toString(),
|
||||
'HH': hour.toString().padStart(2, '0'),
|
||||
'H': hour.toString(),
|
||||
'hh': (hour > 12 ? hour - 12 : hour).toString().padStart(2, '0'),
|
||||
'h': (hour > 12 ? hour - 12 : hour).toString(),
|
||||
'mm': minute.toString().padStart(2, '0'),
|
||||
'm': minute.toString(),
|
||||
'ss': second.toString().padStart(2, '0'),
|
||||
's': second.toString(),
|
||||
'fff': millisecond.toString().padStart(3, '0'),
|
||||
'zzz': (timeZoneOffset >= 0 ? '+' : '-') + Math.abs(timeZoneOffset).toString().padStart(2, '0') + ':00'
|
||||
};
|
||||
|
||||
return format.replace(/yyyy|yy|MM|M|dd|d|HH|H|hh|h|mm|m|ss|s|fff|zzz/g, (match) => {
|
||||
return replacements[match as keyof typeof replacements] || match;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user