UNPKG

3.94 kBJavaScriptView Raw
1// // if the variable is defined
2'use strict';
3
4var _Object$keys = require('babel-runtime/core-js/object/keys')['default'];
5
6var _getIterator = require('babel-runtime/core-js/get-iterator')['default'];
7
8exports.__esModule = true;
9exports.is_object = is_object;
10exports.extend = extend;
11exports.merge = merge;
12exports.clone = clone;
13exports.convert_from_camel_case = convert_from_camel_case;
14exports.replace_all = replace_all;
15exports.starts_with = starts_with;
16exports.ends_with = ends_with;
17var exists = function exists(what) {
18 return typeof what !== 'undefined';
19};
20
21exports.exists = exists;
22// used for JSON object type checking
23var object_constructor = ({}).constructor;
24
25// detects a JSON object
26
27function is_object(object) {
28 return exists(object) && object !== null && object.constructor === object_constructor;
29}
30
31// extends the first object with
32/* istanbul ignore next: some weird transpiled code, not testable */
33
34function extend() {
35 var _this = this,
36 _arguments = arguments;
37
38 var _again = true;
39
40 _function: while (_again) {
41 _again = false;
42
43 for (var _len = _arguments.length, objects = Array(_len), _key = 0; _key < _len; _key++) {
44 objects[_key] = _arguments[_key];
45 }
46
47 var to = objects[0];
48 var from = objects[1];
49
50 if (objects.length > 2) {
51 var last = objects.pop();
52 var intermediary_result = extend.apply(_this, objects);
53 _this = undefined;
54 _arguments = [intermediary_result, last];
55 _again = true;
56 _len = objects = _key = to = from = last = intermediary_result = undefined;
57 continue _function;
58 }
59
60 for (var _iterator = _Object$keys(from), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _getIterator(_iterator);;) {
61 var _ref;
62
63 if (_isArray) {
64 if (_i >= _iterator.length) break;
65 _ref = _iterator[_i++];
66 } else {
67 _i = _iterator.next();
68 if (_i.done) break;
69 _ref = _i.value;
70 }
71
72 var key = _ref;
73
74 if (is_object(from[key])) {
75 if (!is_object(to[key])) {
76 to[key] = {};
77 }
78
79 extend(to[key], from[key]);
80 } else {
81 to[key] = from[key];
82 }
83 }
84
85 return to;
86 }
87}
88
89function merge() {
90 var parameters = Array.prototype.slice.call(arguments, 0);
91 parameters.unshift({});
92 return extend.apply(this, parameters);
93}
94
95function clone(object) {
96 return merge({}, object);
97}
98
99// creates camelCased aliases for all the keys of an object
100
101function convert_from_camel_case(object) {
102 for (var _iterator2 = _Object$keys(object), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _getIterator(_iterator2);;) {
103 var _ref2;
104
105 if (_isArray2) {
106 if (_i2 >= _iterator2.length) break;
107 _ref2 = _iterator2[_i2++];
108 } else {
109 _i2 = _iterator2.next();
110 if (_i2.done) break;
111 _ref2 = _i2.value;
112 }
113
114 var key = _ref2;
115
116 if (/[A-Z]/.test(key))
117 // if (key.indexOf('_') >= 0)
118 {
119 // const camel_cased_key = key.replace(/_(.)/g, function(match, group_1)
120 // {
121 // return group_1.toUpperCase()
122 // })
123
124 // if (!exists(object[camel_cased_key]))
125 // {
126 // object[camel_cased_key] = object[key]
127 // delete object[key]
128 // }
129
130 var lo_dashed_key = key.replace(/([A-Z])/g, function (match, group_1) {
131 return '_' + group_1.toLowerCase();
132 });
133
134 if (!exists(object[lo_dashed_key])) {
135 object[lo_dashed_key] = object[key];
136 delete object[key];
137 }
138 }
139 }
140
141 return object;
142}
143
144function escape_regexp(string) {
145 var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", 'g');
146 return string.replace(specials, "\\$&");
147}
148
149function replace_all(where, what, with_what) {
150 var regexp = new RegExp(escape_regexp(what), 'g');
151 return where.replace(regexp, with_what);
152}
153
154function starts_with(string, substring) {
155 return string.indexOf(substring) === 0;
156}
157
158function ends_with(string, substring) {
159 var index = string.lastIndexOf(substring);
160 return index >= 0 && index === string.length - substring.length;
161}
162//# sourceMappingURL=helpers.js.map
\No newline at end of file