Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
'use strict';
2
3
Object.defineProperty(exports, "__esModule", {
4
value: true
5
});
6
exports.stringContains = exports.objectContains = exports.arrayContains = exports.functionThrows = exports.isA = exports.isObject = exports.isArray = exports.isFunction = exports.isEqual = exports.whyNotEqual = undefined;
7
8
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
9
10
var _isRegex = require('is-regex');
11
12
var _isRegex2 = _interopRequireDefault(_isRegex);
13
14
var _why = require('is-equal/why');
15
16
var _why2 = _interopRequireDefault(_why);
17
18
var _objectKeys = require('object-keys');
19
20
var _objectKeys2 = _interopRequireDefault(_objectKeys);
21
22
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24
/**
25
* Returns the reason why the given arguments are not *conceptually*
26
* equal, if any; the empty string otherwise.
27
*/
28
var whyNotEqual = exports.whyNotEqual = function whyNotEqual(a, b) {
29
return a == b ? '' : (0, _why2.default)(a, b);
30
};
31
32
/**
33
* Returns true if the given arguments are *conceptually* equal.
34
*/
35
var isEqual = exports.isEqual = function isEqual(a, b) {
36
return whyNotEqual(a, b) === '';
37
};
38
39
/**
40
* Returns true if the given object is a function.
41
*/
42
var isFunction = exports.isFunction = function isFunction(object) {
43
return typeof object === 'function';
44
};
45
46
/**
47
* Returns true if the given object is an array.
48
*/
49
var isArray = exports.isArray = function isArray(object) {
50
return Array.isArray(object);
51
};
52
53
/**
54
* Returns true if the given object is an object.
55
*/
56
var isObject = exports.isObject = function isObject(object) {
57
return object && !isArray(object) && (typeof object === 'undefined' ? 'undefined' : _typeof(object)) === 'object';
58
};
59
60
/**
61
* Returns true if the given object is an instanceof value
62
* or its typeof is the given value.
63
*/
64
var isA = exports.isA = function isA(object, value) {
65
if (isFunction(value)) return object instanceof value;
66
67
if (value === 'array') return Array.isArray(object);
68
69
return (typeof object === 'undefined' ? 'undefined' : _typeof(object)) === value;
70
};
71
72
/**
73
* Returns true if the given function throws the given value
74
* when invoked. The value may be:
75
*
76
* - undefined, to merely assert there was a throw
77
* - a constructor function, for comparing using instanceof
78
* - a regular expression, to compare with the error message
79
* - a string, to find in the error message
80
*/
81
var functionThrows = exports.functionThrows = function functionThrows(fn, context, args, value) {
82
try {
83
fn.apply(context, args);
84
} catch (error) {
85
if (value == null) return true;
86
87
if (isFunction(value) && error instanceof value) return true;
88
89
var message = error.message || error;
90
91
if (typeof message === 'string') {
92
if ((0, _isRegex2.default)(value) && value.test(error.message)) return true;
93
94
if (typeof value === 'string' && message.indexOf(value) !== -1) return true;
95
}
96
}
97
98
return false;
99
};
100
101
/**
102
* Returns true if the given array contains the value, false
103
* otherwise. The compareValues function must return false to
104
* indicate a non-match.
105
*/
106
var arrayContains = exports.arrayContains = function arrayContains(array, value, compareValues) {
107
return array.some(function (item) {
108
return compareValues(item, value) !== false;
109
});
110
};
111
112
var ownEnumerableKeys = function ownEnumerableKeys(object) {
113
if ((typeof Reflect === 'undefined' ? 'undefined' : _typeof(Reflect)) === 'object' && typeof Reflect.ownKeys === 'function') {
114
return Reflect.ownKeys(object).filter(function (key) {
115
return Object.getOwnPropertyDescriptor(object, key).enumerable;
116
});
117
}
118
119
if (typeof Object.getOwnPropertySymbols === 'function') {
120
return Object.getOwnPropertySymbols(object).filter(function (key) {
121
return Object.getOwnPropertyDescriptor(object, key).enumerable;
122
}).concat((0, _objectKeys2.default)(object));
123
}
124
125
return (0, _objectKeys2.default)(object);
126
};
127
128
/**
129
* Returns true if the given object contains the value, false
130
* otherwise. The compareValues function must return false to
131
* indicate a non-match.
132
*/
133
var objectContains = exports.objectContains = function objectContains(object, value, compareValues) {
134
return ownEnumerableKeys(value).every(function (k) {
135
if (isObject(object[k]) && isObject(value[k])) return objectContains(object[k], value[k], compareValues);
136
137
return compareValues(object[k], value[k]);
138
});
139
};
140
141
/**
142
* Returns true if the given string contains the value, false otherwise.
143
*/
144
var stringContains = exports.stringContains = function stringContains(string, value) {
145
return string.indexOf(value) !== -1;
146
};
147