喵♂呜 的博客

一个刚毕业就当爹的程序猿 正在迷雾中寻找道路...

JavaScript 常用代码集合

JavaScript 常用的扩展函数和方法

字符串相关扩展

  • 字符串格式化
    1
    2
    3
    4
    5
    6
    String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (s, i) {
    return args[i];
    });
    };

日期相关处理扩展

  • 日期格式化字符串扩展
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    /**
    * 日期格式化
    * 例: new Date().format('yyyy-MM-dd hh:mm:ss.s') => "2017-08-24 16:15:40.693"
    * @param fmt 格式化字符串
    * @returns {*}
    */
    Date.prototype.format = function (fmt) { //author: meizz
    var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "") .substr(4 - RegExp.$1.length));
    for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? o[k] : (("00" + o[k]).substr(("" + o[k]).length)));
    }
    }
    return fmt;
    };
  • 计算两个日期之间的差值
    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
    /**
    * 获得时间差
    * @param time 时间
    * @param type 类型
    * @returns {Number} 差值
    */
    Date.prototype.diff = function (time, type) {
    //作为除数的数字
    var divNum = 1;
    switch (type.toLowerCase()) {
    case "second":
    divNum = 1000;
    break;
    case "minute":
    divNum = 1000 * 60;
    break;
    case "hour":
    divNum = 1000 * 3600;
    break;
    case "day":
    divNum = 1000 * 3600 * 24;
    break;
    default:
    break;
    }
    return parseInt((this.getTime() - time.getTime()) / parseInt(divNum));
    };
    /**
    * 获得相差秒数
    * @param time 时间
    * @returns {Number}
    */
    Date.prototype.diffSecond = function (time) {
    return this.diff(time, 'Second')
    };
    /**
    * 获得相差分钟
    * @param time 时间
    * @returns {Number}
    */
    Date.prototype.diffMinute = function (time) {
    return this.diff(time, 'Minute')
    };
    /**
    *
    * 获得相差小时
    * @param time 时间
    * @returns {Number}
    */
    Date.prototype.diffHour = function (time) {
    return this.diff(time, 'Hour')
    };
    /**
    * 获得相差日期
    * @param time 时间
    * @returns {Number}
    */
    Date.prototype.diffDay = function (time) {
    return this.diff(time, 'Day')
    };

比较两个对象之间的差异

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
var deepDiffMapper = function () {
return {
VALUE_CREATED: 'created',
VALUE_UPDATED: 'updated',
VALUE_DELETED: 'deleted',
VALUE_UNCHANGED: 'unchanged',
map: function (obj1, obj2) {
if (this.isFunction(obj1) || this.isFunction(obj2)) {
throw 'Invalid argument. Function given, object expected.';
}
if (this.isValue(obj1) || this.isValue(obj2)) {
return {
type: this.compareValues(obj1, obj2),
data: (obj1 === undefined) ? obj2 : obj1
};
}

var diff = {};
for (var key in obj1) {
if (this.isFunction(obj1[key])) {
continue;
}

var value2 = undefined;
if ('undefined' != typeof (obj2[key])) {
value2 = obj2[key];
}

diff[key] = this.map(obj1[key], value2);
}
for (var key in obj2) {
if (this.isFunction(obj2[key]) || ('undefined' != typeof (diff[key]))) {
continue;
}

diff[key] = this.map(undefined, obj2[key]);
}

return diff;

},
compareValues: function (value1, value2) {
if (value1 === value2) {
return this.VALUE_UNCHANGED;
}
if (this.isDate(value1) && this.isDate(value2) && value1.getTime() === value2.getTime()) {
return this.VALUE_UNCHANGED;
}
if ('undefined' == typeof (value1)) {
return this.VALUE_CREATED;
}
if ('undefined' == typeof (value2)) {
return this.VALUE_DELETED;
}

return this.VALUE_UPDATED;
},
isFunction: function (obj) {
return {}.toString.apply(obj) === '[object Function]';
},
isArray: function (obj) {
return {}.toString.apply(obj) === '[object Array]';
},
isDate: function (obj) {
return {}.toString.apply(obj) === '[object Date]';
},
isObject: function (obj) {
return {}.toString.apply(obj) === '[object Object]';
},
isValue: function (obj) {
return !this.isObject(obj) && !this.isArray(obj);
}
}
}();

var result = deepDiffMapper.map({
a: 'i am unchanged',
b: 'i am deleted',
e: {
a: 1,
b: false,
c: null
},
f: [1, {
a: 'same',
b: [{
a: 'same'
}, {
d: 'delete'
}]
}],
g: new Date('2017.11.25')
}, {
a: 'i am unchanged',
c: 'i am created',
e: {
a: '1',
b: '',
d: 'created'
},
f: [{
a: 'same',
b: [{
a: 'same'
}, {
c: 'create'
}]
}, 1],
g: new Date('2017.11.25')
});
console.log(result);

欢迎关注我的其它发布渠道