-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_other.gs
173 lines (125 loc) · 4.7 KB
/
08_other.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* 開始値から終了値までの整数を生成する
*
* @param {number} start - 開始値
* @param {number} end - 終了値
* @return {Array.<number>}
*
*/
function generateNumbers(start, end) {
console.info('generateNumbers()を実行中');
console.info('08_otherに記載');
const array = Array.from({length: end}, (_, index) => start + index);
console.log(array);
return array
}
/**
* ランダムな値を返す
* @param {number} end - 最大数値
* @return {number} number - ランダムな値
*
*/
function getRandomNumber(end) {
console.info('getRandomNumber()を実行中');
console.info('08_otherに記載');
const random = Math.floor(Math.random() * end) + 1;
console.log(`出力されたランダムな整数: ${random}`);
return random;
}
/**
* 1次元配列の中で1番大きな数値を返す
*
* @param {Array.<number>} array - 数値が格納された1次元配列
* @return {number}
*/
function findLargestNumber(array){
console.info('findLargestNumber()を実行中');
console.info('08_otherに記載');
const result = array.reduce((accumulator, current) => {
return accumulator > current ? accumulator : current;
});
console.log(`配列内の最大数値は、${result}です`);
return result
}
/**
* ハイフンなしの携帯電話番号を、ハイフンありで返す関数
*
* @param {string | number} original - 携帯電話の文字列 '09012345678'
* @param {boolean} hasLog - 省略可・実行中の関数名を表示する
* @return {string} ハイフンありの携帯電話 '090-1234-5678'
*
*/
function convertCellPhoneNumber(original, hasLog) {
if(hasLog){
console.info(`getCellPhoneNumber()を実行中`);
console.info('08_otherに記載');
}
// 数値型だったら文字列化し、文字列だったら引数をそのまま使用する
const originalString = typeof original === 'number' ? String(original) : original;
const cellPhoneNumber = (originalString.length < 11) ? `0${sliceStringNumber_(originalString, 2)}` : sliceStringNumber_(originalString, 3);
console.warn(`成形後:${cellPhoneNumber}`);
return cellPhoneNumber
}
/**
* 携帯電話番号の文字列を作成する
*
* @param {string} string - 携帯電話の文字列を生成する
* @param {number} quantity - 文字をスライスする量
* @param {boolean} hasLog - 実行中の関数名を表示する
* @return {string}
*
*/
function sliceStringNumber_(string, quantity, hasLog) {
if(hasLog){
console.info(`sliceStringNumber_()を実行中`);
console.info('08_otherに記載');
}
console.log(`成形前:${string}, string.length: ${string.length}`);
const parts = [];
parts.push(string.slice(0, quantity)); // 最初の quantity 桁
parts.push(string.slice(quantity, quantity + 4)); // 次の 4 桁
parts.push(string.slice(quantity + 4)); // 残りの部分
console.log(parts);
return parts.join('-');
}
/**
* 全角数字を半角数字に置換するため2次元配列を作成する
*
* @param {boolean} isCombined - アルファベットの正規表現と統合するかどうか
* @return {Array.<Array.<string>>} [/置換対象の文字列/, '置換後の文字列']
*
*/
function generateTwoByteRegex(isCombined){
console.info(`generateTwoByteRegex()を実行中`);
console.info('08_otherに記載');
const twoByteCharacter = '0123456789'; //全角
const halfSizeCharacter = '0123456789'; //半角
const list = twoByteCharacter.split('');
// 全角数字を半角数字に置換するため2次元配列を作成する
const regexList = list.map((string, i) => [new RegExp(string, 'g'), halfSizeCharacter[i]]);
// 全角数字置換用に加えて、アルファベットも置換できるように既存の配列に加える
if(isCombined){
const newValues = generateTwoByteAlphabetValues_();
regexList.push(...newValues);
}
console.log(regexList);
return regexList
}
/**
*
* 全角のアルファベットと半角のアルファベットの置換用の2次元配列を作成
* @param {boolean} hasLog - 実行中の関数を表示する
* @return {Array.<Array.<string>>}
*
*/
function generateTwoByteAlphabetValues_(hasLog){
if(hasLog){
console.info(`generateTwoByteAlphabetValues_()を実行中`);
console.info('08_otherに記載');
}
const twoByteAlphabet = Array.from({length: 26}, (_, i) => String.fromCharCode('a'.charCodeAt(0) + i));
const halfSizeAlphabet = Array.from({length: 26}, (_, i) => String.fromCharCode('a'.charCodeAt(0) + i));
const lists = twoByteAlphabet.map((alphabet, i) => [new RegExp(alphabet, 'g'), halfSizeAlphabet[i]]);
console.log(lists);
return lists;
}