UNPKG

7.03 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.exists = undefined;
7
8var _keys = require('babel-runtime/core-js/object/keys');
9
10var _keys2 = _interopRequireDefault(_keys);
11
12var _getIterator2 = require('babel-runtime/core-js/get-iterator');
13
14var _getIterator3 = _interopRequireDefault(_getIterator2);
15
16exports.is_object = is_object;
17exports.extend = extend;
18exports.merge = merge;
19exports.clone = clone;
20exports.convert_from_camel_case = convert_from_camel_case;
21exports.replace_all = replace_all;
22exports.starts_with = starts_with;
23exports.ends_with = ends_with;
24exports.is_empty = is_empty;
25exports.not_empty = not_empty;
26exports.repeat = repeat;
27exports.is_blank = is_blank;
28exports.zip = zip;
29exports.last = last;
30exports.camel_case = camel_case;
31exports.alias_properties_with_camel_case = alias_properties_with_camel_case;
32
33function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
35// // if the variable is defined
36var exists = exports.exists = function exists(what) {
37 return typeof what !== 'undefined';
38};
39
40// used for JSON object type checking
41var object_constructor = {}.constructor;
42
43// detects a JSON object
44function is_object(object) {
45 return exists(object) && object !== null && object.constructor === object_constructor;
46}
47
48// extends the first object with
49/* istanbul ignore next: some weird transpiled code, not testable */
50function extend() {
51 for (var _len = arguments.length, objects = Array(_len), _key = 0; _key < _len; _key++) {
52 objects[_key] = arguments[_key];
53 }
54
55 objects = objects.filter(function (x) {
56 return exists(x);
57 });
58
59 if (objects.length === 0) {
60 return;
61 }
62
63 if (objects.length === 1) {
64 return objects[0];
65 }
66
67 var to = objects[0];
68 var from = objects[1];
69
70 if (objects.length > 2) {
71 var _last = objects.pop();
72 var intermediary_result = extend.apply(this, objects);
73 return extend(intermediary_result, _last);
74 }
75
76 var _iteratorNormalCompletion = true;
77 var _didIteratorError = false;
78 var _iteratorError = undefined;
79
80 try {
81 for (var _iterator = (0, _getIterator3.default)((0, _keys2.default)(from)), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
82 var key = _step.value;
83
84 if (is_object(from[key])) {
85 if (!is_object(to[key])) {
86 to[key] = {};
87 }
88
89 extend(to[key], from[key]);
90 } else if (Array.isArray(from[key])) {
91 if (!Array.isArray(to[key])) {
92 to[key] = [];
93 }
94
95 to[key] = to[key].concat(clone(from[key]));
96 } else {
97 to[key] = from[key];
98 }
99 }
100 } catch (err) {
101 _didIteratorError = true;
102 _iteratorError = err;
103 } finally {
104 try {
105 if (!_iteratorNormalCompletion && _iterator.return) {
106 _iterator.return();
107 }
108 } finally {
109 if (_didIteratorError) {
110 throw _iteratorError;
111 }
112 }
113 }
114
115 return to;
116}
117
118function merge() {
119 var parameters = Array.prototype.slice.call(arguments, 0);
120 parameters.unshift({});
121 return extend.apply(this, parameters);
122}
123
124function clone(object) {
125 if (is_object(object)) {
126 return merge({}, object);
127 } else if (Array.isArray(object)) {
128 return object.map(function (x) {
129 return clone(x);
130 });
131 } else {
132 return object;
133 }
134}
135
136// converts all camelCased keys of an object to lodash style
137function convert_from_camel_case(object) {
138 var _iteratorNormalCompletion2 = true;
139 var _didIteratorError2 = false;
140 var _iteratorError2 = undefined;
141
142 try {
143 for (var _iterator2 = (0, _getIterator3.default)((0, _keys2.default)(object)), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
144 var key = _step2.value;
145
146 if (/[A-Z]/.test(key)) {
147 var lo_dashed_key = key.replace(/([A-Z])/g, function (match, group_1) {
148 return '_' + group_1.toLowerCase();
149 });
150
151 if (!exists(object[lo_dashed_key])) {
152 object[lo_dashed_key] = object[key];
153 delete object[key];
154 }
155 }
156 }
157 } catch (err) {
158 _didIteratorError2 = true;
159 _iteratorError2 = err;
160 } finally {
161 try {
162 if (!_iteratorNormalCompletion2 && _iterator2.return) {
163 _iterator2.return();
164 }
165 } finally {
166 if (_didIteratorError2) {
167 throw _iteratorError2;
168 }
169 }
170 }
171
172 return object;
173}
174
175function escape_regexp(string) {
176 var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", 'g');
177 return string.replace(specials, "\\$&");
178}
179
180function replace_all(where, what, with_what) {
181 var regexp = new RegExp(escape_regexp(what), 'g');
182 return where.replace(regexp, with_what);
183}
184
185function starts_with(string, substring) {
186 var j = substring.length;
187
188 if (j > string.length) {
189 return false;
190 }
191
192 while (j > 0) {
193 j--;
194
195 if (string[j] !== substring[j]) {
196 return false;
197 }
198 }
199
200 return true;
201}
202
203function ends_with(string, substring) {
204 var i = string.length;
205 var j = substring.length;
206
207 if (j > i) {
208 return false;
209 }
210
211 while (j > 0) {
212 i--;
213 j--;
214
215 if (string[i] !== substring[j]) {
216 return false;
217 }
218 }
219
220 return true;
221
222 // const index = string.lastIndexOf(substring)
223 // return index >= 0 && index === string.length - substring.length
224}
225
226function is_empty(array) {
227 return array.length === 0;
228}
229
230function not_empty(array) {
231 return array.length > 0;
232}
233
234// repeat string N times
235function repeat(what, times) {
236 var result = '';
237 while (times > 0) {
238 result += what;
239 times--;
240 }
241 return result;
242}
243
244// if the text is blank
245function is_blank(text) {
246 return !exists(text) || !text.replace(/\s/g, '');
247}
248
249// zips two arrays
250function zip(a, b) {
251 return a.map(function (_, index) {
252 return [a[index], b[index]];
253 });
254}
255
256// last element of an array
257function last(array) {
258 return array[array.length - 1];
259}
260
261/**
262 * Returns a camel case variant of the string, unless it's in TitleCase.
263 * @param {string} string
264 */
265function camel_case(string) {
266 var nameParts = string.split('_');
267 return nameParts.slice(1).reduce(function (reduced, current) {
268 return reduced + current.charAt(0).toUpperCase() + current.slice(1);
269 }, nameParts[0]);
270}
271
272// detects "private" object properties (just in case there are any)
273var is_private_property = function is_private_property(name) {
274 return starts_with(name, '__');
275};
276
277/**
278 * Creates camel case variants of the attributes on the object
279 * @param {object} object
280 */
281function alias_properties_with_camel_case(object) {
282 var _iteratorNormalCompletion3 = true;
283 var _didIteratorError3 = false;
284 var _iteratorError3 = undefined;
285
286 try {
287 for (var _iterator3 = (0, _getIterator3.default)((0, _keys2.default)(object).filter(function (key) {
288 return !is_private_property(key);
289 })), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
290 var key = _step3.value;
291
292 object[camel_case(key)] = object[key];
293 }
294 } catch (err) {
295 _didIteratorError3 = true;
296 _iteratorError3 = err;
297 } finally {
298 try {
299 if (!_iteratorNormalCompletion3 && _iterator3.return) {
300 _iterator3.return();
301 }
302 } finally {
303 if (_didIteratorError3) {
304 throw _iteratorError3;
305 }
306 }
307 }
308}
309//# sourceMappingURL=helpers.js.map
\No newline at end of file