forked from shouldjs/should.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
157 lines (123 loc) · 3.05 KB
/
util.js
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
/*!
* Should
* Copyright(c) 2010-2014 TJ Holowaychuk <[email protected]>
* MIT Licensed
*/
/**
* Check if given obj just a primitive type wrapper
* @param {Object} obj
* @returns {boolean}
* @api private
*/
exports.isWrapperType = function(obj) {
return obj instanceof Number || obj instanceof String || obj instanceof Boolean;
};
/**
* Merge object b with object a.
*
* var a = { foo: 'bar' }
* , b = { bar: 'baz' };
*
* utils.merge(a, b);
* // => { foo: 'bar', bar: 'baz' }
*
* @param {Object} a
* @param {Object} b
* @return {Object}
* @api private
*/
exports.merge = function(a, b){
if (a && b) {
for (var key in b) {
a[key] = b[key];
}
}
return a;
};
function isArray(arr) {
return isObject(arr) && (arr.__ArrayLike || Array.isArray(arr));
}
exports.isArray = isArray;
function isNumber(arg) {
return typeof arg === 'number';
}
exports.isNumber = isNumber;
function isString(arg) {
return typeof arg === 'string';
}
function isBoolean(arg) {
return typeof arg === 'boolean';
}
exports.isBoolean = isBoolean;
exports.isString = isString;
function isBuffer(arg) {
return typeof Buffer !== 'undefined' && arg instanceof Buffer;
}
exports.isBuffer = isBuffer;
function isDate(d) {
return isObject(d) && objectToString(d) === '[object Date]';
}
exports.isDate = isDate;
function objectToString(o) {
return Object.prototype.toString.call(o);
}
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
exports.isObject = isObject;
function isRegExp(re) {
return isObject(re) && objectToString(re) === '[object RegExp]';
}
exports.isRegExp = isRegExp;
function isNullOrUndefined(arg) {
return arg == null;
}
exports.isNullOrUndefined = isNullOrUndefined;
function isNull(arg) {
return arg === null;
}
exports.isNull = isNull;
function isArguments(object) {
return objectToString(object) === '[object Arguments]';
}
exports.isArguments = isArguments;
exports.isFunction = function(arg) {
return typeof arg === 'function' || arg instanceof Function;
};
function isError(e) {
return (isObject(e) && objectToString(e) === '[object Error]') || (e instanceof Error);
}
exports.isError = isError;
function isUndefined(arg) {
return arg === void 0;
}
exports.isUndefined = isUndefined;
exports.AssertionError = require('assert').AssertionError;
var hasOwnProperty = Object.prototype.hasOwnProperty;
exports.forOwn = function(obj, f, context) {
for(var prop in obj) {
if(hasOwnProperty.call(obj, prop)) {
f.call(context, obj[prop], prop);
}
}
};
var functionNameRE = /^\s*function\s*(\S*)\s*\(/;
exports.functionName = function(f) {
if(f.name) {
return f.name;
}
var name = f.toString().match(functionNameRE)[1];
return name;
};
exports.formatProp = function(name) {
name = JSON.stringify('' + name);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
name = name.substr(1, name.length - 2);
} else {
name = name.replace(/'/g, "\\'")
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'")
.replace(/\\\\/g, '\\');
}
return name;
}