-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize_phone_number.gs
61 lines (60 loc) · 1.64 KB
/
normalize_phone_number.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
/*! (c) 2014-2017 みらい研究室実行委員会
* Released under the MIT license.
*/
/**
* @brief detect type.
* @param obj {any} target variable
* @return {string}
*/
function decltype(obj){
return (typeof(obj) === "undefined") ? "undefined" : Object.prototype.toString.call(obj).slice(8, -1);
}
/**
* @brief check type.
* @param type {string} typename(String,Number,Boolean,Date,Error,Array,Function,RegExp,Object, ...)
* @param obj {any} target variable
* @return {boolean}
*/
function is(type, obj) {
return obj !== undefined && obj !== null && decltype(obj) === type;
}
/**
* @brief normalize phone number.
* @param args {string|string[]|string[][]} base string(s).
* @return {string|string[]|string[][]} normalized string(s).
*/
function normalize_phone_number(args){
switch(decltype(args)){
case "String":
if(0 === args.length) return "";
var re = args
.replace(/[- ー-]/g, "")
// 全角英数⇒半角英数
.replace(/[A-Za-z0-9]/g, function(s) {
return String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
})
//必要なら先頭に'0'を追加
switch(re.length){
case 10:
if(re.charCodeAt(0) < "7" || '9' < re.charCodeAt(0)) break;
/* FALLTHROUGH */
case 9:
re = ('0' != re.charCodeAt(0)) ? "0" : "" + re;
break;
default:
return "error. unknown length.";
}
return re;
case "Number":
return "0" + args.toString(10);
case "Array":
return function(){
var i;
var re = [];
for(i = 0; i < args.length; ++i) re.push(normalize_phone_number(args[i]));
return re;
}();
default:
return "unsupported type.";
}
}