@chevrotain/utils 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,74 @@
1
+ export declare function isEmpty(arr: any[]): boolean;
2
+ export declare function keys(obj: any): string[];
3
+ export declare function values(obj: any): any[];
4
+ export declare function mapValues<I, O>(obj: Object, callback: (value: I, key?: string) => O): O[];
5
+ export declare function map<I, O>(arr: I[], callback: (I: any, idx?: number) => O): O[];
6
+ export declare function flatten<T>(arr: any[]): T[];
7
+ export declare function first<T>(arr: T[]): T;
8
+ export declare function last<T>(arr: T[]): T;
9
+ export declare function forEach(collection: any, iteratorCallback: Function): void;
10
+ export declare function isString(item: any): boolean;
11
+ export declare function isUndefined(item: any): boolean;
12
+ export declare function isFunction(item: any): boolean;
13
+ export declare function drop<T>(arr: T[], howMuch?: number): T[];
14
+ export declare function dropRight<T>(arr: T[], howMuch?: number): T[];
15
+ export declare function filter<T>(arr: T[], predicate: (T: any) => boolean): T[];
16
+ export declare function reject<T>(arr: T[], predicate: (T: any) => boolean): T[];
17
+ export declare function pick(obj: Object, predicate: (item: any) => boolean): {};
18
+ export declare function has(obj: any, prop: string): boolean;
19
+ export declare function contains<T>(arr: T[], item: any): boolean;
20
+ /**
21
+ * shallow clone
22
+ */
23
+ export declare function cloneArr<T>(arr: T[]): T[];
24
+ /**
25
+ * shallow clone
26
+ */
27
+ export declare function cloneObj(obj: Object): any;
28
+ export declare function find<T>(arr: T[], predicate: (item: T) => boolean): T;
29
+ export declare function findAll<T>(arr: T[], predicate: (item: T) => boolean): T[];
30
+ export declare function reduce<T, A>(arrOrObj: Array<T> | Object, iterator: (result: A, item: any, idx?: any) => A, initial: A): A;
31
+ export declare function compact<T>(arr: T[]): T[];
32
+ export declare function uniq<T>(arr: T[], identity?: (item: T) => any): T[];
33
+ export declare function partial(func: Function, ...restArgs: any[]): Function;
34
+ export declare function isArray(obj: any): obj is any[];
35
+ export declare function isRegExp(obj: any): obj is RegExp;
36
+ export declare function isObject(obj: any): obj is Object;
37
+ export declare function every<T>(arr: T[], predicate: (item: T, idx?: any) => boolean): boolean;
38
+ export declare function difference<T>(arr: T[], values: T[]): T[];
39
+ export declare function some<T>(arr: T[], predicate: (item: T) => boolean): boolean;
40
+ export declare function indexOf<T>(arr: T[], value: T): number;
41
+ export declare function sortBy<T>(arr: T[], orderFunc: (item: T) => number): T[];
42
+ export declare function zipObject(keys: any[], values: any[]): Object;
43
+ /**
44
+ * mutates! (and returns) target
45
+ */
46
+ export declare function assign(target: Object, ...sources: Object[]): Object;
47
+ /**
48
+ * mutates! (and returns) target
49
+ */
50
+ export declare function assignNoOverwrite(target: Object, ...sources: Object[]): Object;
51
+ export declare function defaults(...sources: any[]): any;
52
+ export declare function groupBy<T>(arr: T[], groupKeyFunc: (item: T) => string): {
53
+ [groupKey: string]: T[];
54
+ };
55
+ /**
56
+ * Merge obj2 into obj1.
57
+ * Will overwrite existing properties with the same name
58
+ */
59
+ export declare function merge(obj1: Object, obj2: Object): any;
60
+ export declare function NOOP(): void;
61
+ export declare function IDENTITY(item: any): any;
62
+ /**
63
+ * Will return a new packed array with same values.
64
+ */
65
+ export declare function packArray<T>(holeyArr: T[]): T[];
66
+ export declare function PRINT_ERROR(msg: any): void;
67
+ export declare function PRINT_WARNING(msg: any): void;
68
+ export declare function isES2015MapSupported(): boolean;
69
+ export declare function peek<T>(arr: T[]): T;
70
+ export declare function timer<T>(func: () => T): {
71
+ time: number;
72
+ value: T;
73
+ };
74
+ export declare function toFastProperties(toBecomeFast: any): any;
package/lib/src/api.js ADDED
@@ -0,0 +1,456 @@
1
+ "use strict";
2
+ /*
3
+ Utils using lodash style API. (not necessarily 100% compliant) for functional and other utils.
4
+ These utils should replace usage of lodash in the production code base. not because they are any better...
5
+ but for the purpose of being a dependency free library.
6
+
7
+ The hotspots in the code are already written in imperative style for performance reasons.
8
+ so writing several dozen utils which may be slower than the original lodash, does not matter as much
9
+ considering they will not be invoked in hotspots...
10
+ */
11
+ var __spreadArray = (this && this.__spreadArray) || function (to, from) {
12
+ for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
13
+ to[j] = from[i];
14
+ return to;
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.toFastProperties = exports.timer = exports.peek = exports.isES2015MapSupported = exports.PRINT_WARNING = exports.PRINT_ERROR = exports.packArray = exports.IDENTITY = exports.NOOP = exports.merge = exports.groupBy = exports.defaults = exports.assignNoOverwrite = exports.assign = exports.zipObject = exports.sortBy = exports.indexOf = exports.some = exports.difference = exports.every = exports.isObject = exports.isRegExp = exports.isArray = exports.partial = exports.uniq = exports.compact = exports.reduce = exports.findAll = exports.find = exports.cloneObj = exports.cloneArr = exports.contains = exports.has = exports.pick = exports.reject = exports.filter = exports.dropRight = exports.drop = exports.isFunction = exports.isUndefined = exports.isString = exports.forEach = exports.last = exports.first = exports.flatten = exports.map = exports.mapValues = exports.values = exports.keys = exports.isEmpty = void 0;
18
+ function isEmpty(arr) {
19
+ return arr && arr.length === 0;
20
+ }
21
+ exports.isEmpty = isEmpty;
22
+ function keys(obj) {
23
+ if (obj === undefined || obj === null) {
24
+ return [];
25
+ }
26
+ return Object.keys(obj);
27
+ }
28
+ exports.keys = keys;
29
+ function values(obj) {
30
+ var vals = [];
31
+ var keys = Object.keys(obj);
32
+ for (var i = 0; i < keys.length; i++) {
33
+ vals.push(obj[keys[i]]);
34
+ }
35
+ return vals;
36
+ }
37
+ exports.values = values;
38
+ function mapValues(obj, callback) {
39
+ var result = [];
40
+ var objKeys = keys(obj);
41
+ for (var idx = 0; idx < objKeys.length; idx++) {
42
+ var currKey = objKeys[idx];
43
+ result.push(callback.call(null, obj[currKey], currKey));
44
+ }
45
+ return result;
46
+ }
47
+ exports.mapValues = mapValues;
48
+ function map(arr, callback) {
49
+ var result = [];
50
+ for (var idx = 0; idx < arr.length; idx++) {
51
+ result.push(callback.call(null, arr[idx], idx));
52
+ }
53
+ return result;
54
+ }
55
+ exports.map = map;
56
+ function flatten(arr) {
57
+ var result = [];
58
+ for (var idx = 0; idx < arr.length; idx++) {
59
+ var currItem = arr[idx];
60
+ if (Array.isArray(currItem)) {
61
+ result = result.concat(flatten(currItem));
62
+ }
63
+ else {
64
+ result.push(currItem);
65
+ }
66
+ }
67
+ return result;
68
+ }
69
+ exports.flatten = flatten;
70
+ function first(arr) {
71
+ return isEmpty(arr) ? undefined : arr[0];
72
+ }
73
+ exports.first = first;
74
+ function last(arr) {
75
+ var len = arr && arr.length;
76
+ return len ? arr[len - 1] : undefined;
77
+ }
78
+ exports.last = last;
79
+ function forEach(collection, iteratorCallback) {
80
+ /* istanbul ignore else */
81
+ if (Array.isArray(collection)) {
82
+ for (var i = 0; i < collection.length; i++) {
83
+ iteratorCallback.call(null, collection[i], i);
84
+ }
85
+ }
86
+ else if (isObject(collection)) {
87
+ var colKeys = keys(collection);
88
+ for (var i = 0; i < colKeys.length; i++) {
89
+ var key = colKeys[i];
90
+ var value = collection[key];
91
+ iteratorCallback.call(null, value, key);
92
+ }
93
+ }
94
+ else {
95
+ throw Error("non exhaustive match");
96
+ }
97
+ }
98
+ exports.forEach = forEach;
99
+ function isString(item) {
100
+ return typeof item === "string";
101
+ }
102
+ exports.isString = isString;
103
+ function isUndefined(item) {
104
+ return item === undefined;
105
+ }
106
+ exports.isUndefined = isUndefined;
107
+ function isFunction(item) {
108
+ return item instanceof Function;
109
+ }
110
+ exports.isFunction = isFunction;
111
+ function drop(arr, howMuch) {
112
+ if (howMuch === void 0) { howMuch = 1; }
113
+ return arr.slice(howMuch, arr.length);
114
+ }
115
+ exports.drop = drop;
116
+ function dropRight(arr, howMuch) {
117
+ if (howMuch === void 0) { howMuch = 1; }
118
+ return arr.slice(0, arr.length - howMuch);
119
+ }
120
+ exports.dropRight = dropRight;
121
+ function filter(arr, predicate) {
122
+ var result = [];
123
+ if (Array.isArray(arr)) {
124
+ for (var i = 0; i < arr.length; i++) {
125
+ var item = arr[i];
126
+ if (predicate.call(null, item)) {
127
+ result.push(item);
128
+ }
129
+ }
130
+ }
131
+ return result;
132
+ }
133
+ exports.filter = filter;
134
+ function reject(arr, predicate) {
135
+ return filter(arr, function (item) { return !predicate(item); });
136
+ }
137
+ exports.reject = reject;
138
+ function pick(obj, predicate) {
139
+ var keys = Object.keys(obj);
140
+ var result = {};
141
+ for (var i = 0; i < keys.length; i++) {
142
+ var currKey = keys[i];
143
+ var currItem = obj[currKey];
144
+ if (predicate(currItem)) {
145
+ result[currKey] = currItem;
146
+ }
147
+ }
148
+ return result;
149
+ }
150
+ exports.pick = pick;
151
+ function has(obj, prop) {
152
+ if (isObject(obj)) {
153
+ return obj.hasOwnProperty(prop);
154
+ }
155
+ return false;
156
+ }
157
+ exports.has = has;
158
+ function contains(arr, item) {
159
+ return find(arr, function (currItem) { return currItem === item; }) !== undefined ? true : false;
160
+ }
161
+ exports.contains = contains;
162
+ /**
163
+ * shallow clone
164
+ */
165
+ function cloneArr(arr) {
166
+ var newArr = [];
167
+ for (var i = 0; i < arr.length; i++) {
168
+ newArr.push(arr[i]);
169
+ }
170
+ return newArr;
171
+ }
172
+ exports.cloneArr = cloneArr;
173
+ /**
174
+ * shallow clone
175
+ */
176
+ function cloneObj(obj) {
177
+ var clonedObj = {};
178
+ for (var key in obj) {
179
+ /* istanbul ignore else */
180
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
181
+ clonedObj[key] = obj[key];
182
+ }
183
+ }
184
+ return clonedObj;
185
+ }
186
+ exports.cloneObj = cloneObj;
187
+ function find(arr, predicate) {
188
+ for (var i = 0; i < arr.length; i++) {
189
+ var item = arr[i];
190
+ if (predicate.call(null, item)) {
191
+ return item;
192
+ }
193
+ }
194
+ return undefined;
195
+ }
196
+ exports.find = find;
197
+ function findAll(arr, predicate) {
198
+ var found = [];
199
+ for (var i = 0; i < arr.length; i++) {
200
+ var item = arr[i];
201
+ if (predicate.call(null, item)) {
202
+ found.push(item);
203
+ }
204
+ }
205
+ return found;
206
+ }
207
+ exports.findAll = findAll;
208
+ function reduce(arrOrObj, iterator, initial) {
209
+ var isArr = Array.isArray(arrOrObj);
210
+ var vals = isArr ? arrOrObj : values(arrOrObj);
211
+ var objKeys = isArr ? [] : keys(arrOrObj);
212
+ var accumulator = initial;
213
+ for (var i = 0; i < vals.length; i++) {
214
+ accumulator = iterator.call(null, accumulator, vals[i], isArr ? i : objKeys[i]);
215
+ }
216
+ return accumulator;
217
+ }
218
+ exports.reduce = reduce;
219
+ function compact(arr) {
220
+ return reject(arr, function (item) { return item === null || item === undefined; });
221
+ }
222
+ exports.compact = compact;
223
+ function uniq(arr, identity) {
224
+ if (identity === void 0) { identity = function (item) { return item; }; }
225
+ var identities = [];
226
+ return reduce(arr, function (result, currItem) {
227
+ var currIdentity = identity(currItem);
228
+ if (contains(identities, currIdentity)) {
229
+ return result;
230
+ }
231
+ else {
232
+ identities.push(currIdentity);
233
+ return result.concat(currItem);
234
+ }
235
+ }, []);
236
+ }
237
+ exports.uniq = uniq;
238
+ function partial(func) {
239
+ var restArgs = [];
240
+ for (var _i = 1; _i < arguments.length; _i++) {
241
+ restArgs[_i - 1] = arguments[_i];
242
+ }
243
+ var firstArg = [null];
244
+ var allArgs = firstArg.concat(restArgs);
245
+ return Function.bind.apply(func, allArgs);
246
+ }
247
+ exports.partial = partial;
248
+ function isArray(obj) {
249
+ return Array.isArray(obj);
250
+ }
251
+ exports.isArray = isArray;
252
+ function isRegExp(obj) {
253
+ return obj instanceof RegExp;
254
+ }
255
+ exports.isRegExp = isRegExp;
256
+ function isObject(obj) {
257
+ return obj instanceof Object;
258
+ }
259
+ exports.isObject = isObject;
260
+ function every(arr, predicate) {
261
+ for (var i = 0; i < arr.length; i++) {
262
+ if (!predicate(arr[i], i)) {
263
+ return false;
264
+ }
265
+ }
266
+ return true;
267
+ }
268
+ exports.every = every;
269
+ function difference(arr, values) {
270
+ return reject(arr, function (item) { return contains(values, item); });
271
+ }
272
+ exports.difference = difference;
273
+ function some(arr, predicate) {
274
+ for (var i = 0; i < arr.length; i++) {
275
+ if (predicate(arr[i])) {
276
+ return true;
277
+ }
278
+ }
279
+ return false;
280
+ }
281
+ exports.some = some;
282
+ function indexOf(arr, value) {
283
+ for (var i = 0; i < arr.length; i++) {
284
+ if (arr[i] === value) {
285
+ return i;
286
+ }
287
+ }
288
+ return -1;
289
+ }
290
+ exports.indexOf = indexOf;
291
+ function sortBy(arr, orderFunc) {
292
+ var result = cloneArr(arr);
293
+ result.sort(function (a, b) { return orderFunc(a) - orderFunc(b); });
294
+ return result;
295
+ }
296
+ exports.sortBy = sortBy;
297
+ function zipObject(keys, values) {
298
+ if (keys.length !== values.length) {
299
+ throw Error("can't zipObject with different number of keys and values!");
300
+ }
301
+ var result = {};
302
+ for (var i = 0; i < keys.length; i++) {
303
+ result[keys[i]] = values[i];
304
+ }
305
+ return result;
306
+ }
307
+ exports.zipObject = zipObject;
308
+ /**
309
+ * mutates! (and returns) target
310
+ */
311
+ function assign(target) {
312
+ var sources = [];
313
+ for (var _i = 1; _i < arguments.length; _i++) {
314
+ sources[_i - 1] = arguments[_i];
315
+ }
316
+ for (var i = 0; i < sources.length; i++) {
317
+ var curSource = sources[i];
318
+ var currSourceKeys = keys(curSource);
319
+ for (var j = 0; j < currSourceKeys.length; j++) {
320
+ var currKey = currSourceKeys[j];
321
+ target[currKey] = curSource[currKey];
322
+ }
323
+ }
324
+ return target;
325
+ }
326
+ exports.assign = assign;
327
+ /**
328
+ * mutates! (and returns) target
329
+ */
330
+ function assignNoOverwrite(target) {
331
+ var sources = [];
332
+ for (var _i = 1; _i < arguments.length; _i++) {
333
+ sources[_i - 1] = arguments[_i];
334
+ }
335
+ for (var i = 0; i < sources.length; i++) {
336
+ var curSource = sources[i];
337
+ var currSourceKeys = keys(curSource);
338
+ for (var j = 0; j < currSourceKeys.length; j++) {
339
+ var currKey = currSourceKeys[j];
340
+ if (!has(target, currKey)) {
341
+ target[currKey] = curSource[currKey];
342
+ }
343
+ }
344
+ }
345
+ return target;
346
+ }
347
+ exports.assignNoOverwrite = assignNoOverwrite;
348
+ function defaults() {
349
+ var sources = [];
350
+ for (var _i = 0; _i < arguments.length; _i++) {
351
+ sources[_i] = arguments[_i];
352
+ }
353
+ return assignNoOverwrite.apply(void 0, __spreadArray([{}], sources));
354
+ }
355
+ exports.defaults = defaults;
356
+ function groupBy(arr, groupKeyFunc) {
357
+ var result = {};
358
+ forEach(arr, function (item) {
359
+ var currGroupKey = groupKeyFunc(item);
360
+ var currGroupArr = result[currGroupKey];
361
+ if (currGroupArr) {
362
+ currGroupArr.push(item);
363
+ }
364
+ else {
365
+ result[currGroupKey] = [item];
366
+ }
367
+ });
368
+ return result;
369
+ }
370
+ exports.groupBy = groupBy;
371
+ /**
372
+ * Merge obj2 into obj1.
373
+ * Will overwrite existing properties with the same name
374
+ */
375
+ function merge(obj1, obj2) {
376
+ var result = cloneObj(obj1);
377
+ var keys2 = keys(obj2);
378
+ for (var i = 0; i < keys2.length; i++) {
379
+ var key = keys2[i];
380
+ var value = obj2[key];
381
+ result[key] = value;
382
+ }
383
+ return result;
384
+ }
385
+ exports.merge = merge;
386
+ function NOOP() { }
387
+ exports.NOOP = NOOP;
388
+ function IDENTITY(item) {
389
+ return item;
390
+ }
391
+ exports.IDENTITY = IDENTITY;
392
+ /**
393
+ * Will return a new packed array with same values.
394
+ */
395
+ function packArray(holeyArr) {
396
+ var result = [];
397
+ for (var i = 0; i < holeyArr.length; i++) {
398
+ var orgValue = holeyArr[i];
399
+ result.push(orgValue !== undefined ? orgValue : undefined);
400
+ }
401
+ return result;
402
+ }
403
+ exports.packArray = packArray;
404
+ function PRINT_ERROR(msg) {
405
+ /* istanbul ignore else - can't override global.console in node.js */
406
+ if (console && console.error) {
407
+ console.error("Error: " + msg);
408
+ }
409
+ }
410
+ exports.PRINT_ERROR = PRINT_ERROR;
411
+ function PRINT_WARNING(msg) {
412
+ /* istanbul ignore else - can't override global.console in node.js*/
413
+ if (console && console.warn) {
414
+ // TODO: modify docs accordingly
415
+ console.warn("Warning: " + msg);
416
+ }
417
+ }
418
+ exports.PRINT_WARNING = PRINT_WARNING;
419
+ function isES2015MapSupported() {
420
+ return typeof Map === "function";
421
+ }
422
+ exports.isES2015MapSupported = isES2015MapSupported;
423
+ function peek(arr) {
424
+ return arr[arr.length - 1];
425
+ }
426
+ exports.peek = peek;
427
+ /* istanbul ignore next - for performance tracing*/
428
+ function timer(func) {
429
+ var start = new Date().getTime();
430
+ var val = func();
431
+ var end = new Date().getTime();
432
+ var total = end - start;
433
+ return { time: total, value: val };
434
+ }
435
+ exports.timer = timer;
436
+ // based on: https://github.com/petkaantonov/bluebird/blob/b97c0d2d487e8c5076e8bd897e0dcd4622d31846/src/util.js#L201-L216
437
+ function toFastProperties(toBecomeFast) {
438
+ function FakeConstructor() { }
439
+ // If our object is used as a constructor it would receive
440
+ FakeConstructor.prototype = toBecomeFast;
441
+ var fakeInstance = new FakeConstructor();
442
+ function fakeAccess() {
443
+ return typeof fakeInstance.bar;
444
+ }
445
+ // help V8 understand this is a "real" prototype by actually using
446
+ // the fake instance.
447
+ fakeAccess();
448
+ fakeAccess();
449
+ return toBecomeFast;
450
+ // Eval prevents optimization of this method (even though this is dead code)
451
+ /* istanbul ignore next */
452
+ // tslint:disable-next-line
453
+ eval(toBecomeFast);
454
+ }
455
+ exports.toFastProperties = toFastProperties;
456
+ //# sourceMappingURL=api.js.map
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@chevrotain/utils",
3
+ "version": "9.0.0",
4
+ "description": "common utilities",
5
+ "keywords": [],
6
+ "bugs": {
7
+ "url": "https://github.com/Chevrotain/chevrotain/issues"
8
+ },
9
+ "license": "Apache-2.0",
10
+ "author": {
11
+ "name": "Shahar Soel"
12
+ },
13
+ "typings": "api.d.ts",
14
+ "main": "lib/src/api.js",
15
+ "files": [
16
+ "lib/src/api.js",
17
+ "lib/src/api.ts",
18
+ "lib/src/api.d.ts",
19
+ "README.md",
20
+ "LICENSE.TXT"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git://github.com/Chevrotain/chevrotain.git"
25
+ },
26
+ "scripts": {
27
+ "---------- CI FLOWS --------": "",
28
+ "build": "npm-run-all clean compile",
29
+ "test": "npm-run-all coverage",
30
+ "---------- BUILD STEPS --------": "",
31
+ "clean": "shx rm -rf dev",
32
+ "compile:watch": "tsc -w",
33
+ "compile": "tsc",
34
+ "coverage": "nyc mocha"
35
+ },
36
+ "devDependencies": {
37
+ "@types/chai": "^4.2.15"
38
+ },
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "gitHead": "ca48f1b64ea7768c0cf0248572e5270f593b18c8"
43
+ }