90 lines
2.5 KiB
JavaScript
90 lines
2.5 KiB
JavaScript
class StringUtil {
|
|
|
|
static printMonthDay(date) {
|
|
let dateTime = new Date(date);
|
|
return (dateTime.getMonth() + 1) + "월 " + dateTime.getDate() + "일";
|
|
}
|
|
|
|
static getRecordTextWithoutUnit(value) {
|
|
let number = Math.floor(value);
|
|
return NumberUtil.numberWithCommas(number);
|
|
}
|
|
|
|
static getRecordTextWithUnit(value) {
|
|
let number = Math.floor(value);
|
|
let numberWithCommas = NumberUtil.numberWithCommas(number);
|
|
|
|
if(isTypingGame())
|
|
return numberWithCommas + " 타";
|
|
else
|
|
return numberWithCommas + " 점";
|
|
}
|
|
|
|
static getNumberStartWithZero(number, digitCount) {
|
|
let n = number + '';
|
|
return n.length >= digitCount ? n : new Array(digitCount - n.length + 1).join('0') + n;
|
|
}
|
|
|
|
|
|
static toKoreanAlphabets(text) {
|
|
const cCho = [ 'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' ];
|
|
const cJung = [ 'ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ' ];
|
|
const cJong = [ '', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' ];
|
|
|
|
let cho, jung, jong;
|
|
let str = text;
|
|
let cnt = str.length;
|
|
let chars = [], cCode;
|
|
|
|
for (let i = 0; i < cnt; i++) {
|
|
cCode = str.charCodeAt(i);
|
|
// if (cCode == 32) { continue; } // 한글이 아닌 경우
|
|
if (cCode < 0xAC00 || cCode > 0xD7A3) {
|
|
chars.push(str.charAt(i));
|
|
continue;
|
|
}
|
|
cCode = str.charCodeAt(i) - 0xAC00;
|
|
jong = cCode % 28; // 종성
|
|
jung = ((cCode - jong) / 28 ) % 21; // 중성
|
|
cho = (((cCode - jong) / 28 ) - jung ) / 21; // 초성
|
|
|
|
chars.push(cCho[cho], cJung[jung]);
|
|
if (cJong[jong] !== '') {
|
|
chars.push(cJong[jong]);
|
|
}
|
|
}
|
|
return chars;
|
|
};
|
|
|
|
|
|
static getUnicodeAlphabetCount(text) {
|
|
let chars = StringUtil.toKoreanAlphabets(text);
|
|
// console.log('char : ' + chars);
|
|
|
|
let doubleJaum = [
|
|
'ㄲ', 'ㅉ', 'ㄸ', 'ㄲ', 'ㅆ',
|
|
'ㄳ', 'ㄵ', 'ㄶ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅄ'
|
|
];
|
|
let doubleMoum = [
|
|
'ㅒ', 'ㅖ',
|
|
'ㅘ', 'ㅙ', 'ㅚ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅢ'
|
|
];
|
|
|
|
let alphabetCount = 0;
|
|
let alphabet;
|
|
for(let i = 0; i < chars.length; i++) {
|
|
alphabet = chars[i];
|
|
if(doubleJaum.indexOf(alphabet) > -1 || doubleMoum.indexOf(alphabet) > -1) {
|
|
// console.log('double alphabet : ' + alphabet);
|
|
alphabetCount += 2;
|
|
}
|
|
else
|
|
alphabetCount++;
|
|
}
|
|
|
|
// return chars;
|
|
return alphabetCount;
|
|
};
|
|
|
|
|
|
} |