Utility.js
3.5 KB
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
// Generated by CoffeeScript 1.10.0
(function() {
var assign, camelCase, capitalize, isArray, isEmpty, isFunction, isObject, isPlainObject, kebabCase, snakeCase, titleCase, words,
slice = [].slice,
hasProp = {}.hasOwnProperty;
assign = function() {
var i, key, len, source, sources, target;
target = arguments[0], sources = 2 <= arguments.length ? slice.call(arguments, 1) : [];
if (isFunction(Object.assign)) {
Object.assign.apply(null, arguments);
} else {
for (i = 0, len = sources.length; i < len; i++) {
source = sources[i];
if (source != null) {
for (key in source) {
if (!hasProp.call(source, key)) continue;
target[key] = source[key];
}
}
}
}
return target;
};
isFunction = function(val) {
return !!val && Object.prototype.toString.call(val) === '[object Function]';
};
isObject = function(val) {
var ref;
return !!val && ((ref = typeof val) === 'function' || ref === 'object');
};
isArray = function(val) {
if (isFunction(Array.isArray)) {
return Array.isArray(val);
} else {
return Object.prototype.toString.call(val) === '[object Array]';
}
};
isEmpty = function(val) {
var key;
if (isArray(val)) {
return !val.length;
} else {
for (key in val) {
if (!hasProp.call(val, key)) continue;
return false;
}
return true;
}
};
isPlainObject = function(val) {
var ctor, proto;
return isObject(val) && (proto = Object.getPrototypeOf(val)) && (ctor = proto.constructor) && (typeof ctor === 'function') && (ctor instanceof ctor) && (Function.prototype.toString.call(ctor) === Function.prototype.toString.call(Object));
};
words = function(val) {
return (val.split(/[-_\s]+|(?=[A-Z][a-z])/) || []).filter(function(n) {
return !!n;
});
};
camelCase = function(val) {
var i, index, len, r, ref, word;
r = '';
ref = words(val);
for (index = i = 0, len = ref.length; i < len; index = ++i) {
word = ref[index];
r += index ? capitalize(word.toLowerCase()) : word.toLowerCase();
}
return r;
};
titleCase = function(val) {
var i, index, len, r, ref, word;
r = '';
ref = words(val);
for (index = i = 0, len = ref.length; i < len; index = ++i) {
word = ref[index];
r += capitalize(word.toLowerCase());
}
return r;
};
kebabCase = function(val) {
var i, index, len, r, ref, word;
r = '';
ref = words(val);
for (index = i = 0, len = ref.length; i < len; index = ++i) {
word = ref[index];
r += (index ? '-' : '') + word.toLowerCase();
}
return r;
};
snakeCase = function(val) {
var i, index, len, r, ref, word;
r = '';
ref = words(val);
for (index = i = 0, len = ref.length; i < len; index = ++i) {
word = ref[index];
r += (index ? '_' : '') + word.toLowerCase();
}
return r;
};
capitalize = function(val) {
return val.charAt(0).toUpperCase() + val.slice(1);
};
module.exports.assign = assign;
module.exports.isFunction = isFunction;
module.exports.isObject = isObject;
module.exports.isArray = isArray;
module.exports.isEmpty = isEmpty;
module.exports.isPlainObject = isPlainObject;
module.exports.camelCase = camelCase;
module.exports.titleCase = titleCase;
module.exports.kebabCase = kebabCase;
module.exports.snakeCase = snakeCase;
module.exports.capitalize = capitalize;
module.exports.words = words;
}).call(this);