@chevrotain/utils 10.1.1 → 10.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chevrotain/utils",
3
- "version": "10.1.1",
3
+ "version": "10.1.2",
4
4
  "description": "common utilities",
5
5
  "keywords": [],
6
6
  "bugs": {
@@ -13,6 +13,7 @@
13
13
  "typings": "lib/src/api.d.ts",
14
14
  "main": "lib/src/api.js",
15
15
  "files": [
16
+ "src/**/*.ts",
16
17
  "lib/src/**/*.js",
17
18
  "lib/src/**/*.d.ts",
18
19
  "lib/src/**/*.js.map",
@@ -36,5 +37,5 @@
36
37
  "publishConfig": {
37
38
  "access": "public"
38
39
  },
39
- "gitHead": "7104f18a4cc2537487086789943e3a200e7b966f"
40
+ "gitHead": "a9669e27213c0cac9ce59d890d331dc27f20190d"
40
41
  }
package/src/api.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { PRINT_WARNING, PRINT_ERROR } from "./print"
2
+ export { timer } from "./timer"
3
+ export { toFastProperties } from "./to-fast-properties"
package/src/print.ts ADDED
@@ -0,0 +1,14 @@
1
+ export function PRINT_ERROR(msg: string) {
2
+ /* istanbul ignore else - can't override global.console in node.js */
3
+ if (console && console.error) {
4
+ console.error(`Error: ${msg}`)
5
+ }
6
+ }
7
+
8
+ export function PRINT_WARNING(msg: string) {
9
+ /* istanbul ignore else - can't override global.console in node.js*/
10
+ if (console && console.warn) {
11
+ // TODO: modify docs accordingly
12
+ console.warn(`Warning: ${msg}`)
13
+ }
14
+ }
package/src/timer.ts ADDED
@@ -0,0 +1,7 @@
1
+ export function timer<T>(func: () => T): { time: number; value: T } {
2
+ const start = new Date().getTime()
3
+ const val = func()
4
+ const end = new Date().getTime()
5
+ const total = end - start
6
+ return { time: total, value: val }
7
+ }
@@ -0,0 +1,23 @@
1
+ // based on: https://github.com/petkaantonov/bluebird/blob/b97c0d2d487e8c5076e8bd897e0dcd4622d31846/src/util.js#L201-L216
2
+ export function toFastProperties(toBecomeFast: any) {
3
+ function FakeConstructor() {}
4
+
5
+ // If our object is used as a constructor it would receive
6
+ FakeConstructor.prototype = toBecomeFast
7
+ const fakeInstance = new (FakeConstructor as any)()
8
+
9
+ function fakeAccess() {
10
+ return typeof fakeInstance.bar
11
+ }
12
+
13
+ // help V8 understand this is a "real" prototype by actually using
14
+ // the fake instance.
15
+ fakeAccess()
16
+ fakeAccess()
17
+
18
+ return toBecomeFast
19
+ // Eval prevents optimization of this method (even though this is dead code)
20
+ /* istanbul ignore next */
21
+ // tslint:disable-next-line
22
+ eval(toBecomeFast)
23
+ }