@monorepolint/utils 0.5.0-alpha.84 → 0.5.0-alpha.85
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/lib/AggregateTiming.d.ts +15 -0
- package/lib/AggregateTiming.d.ts.map +1 -0
- package/lib/AggregateTiming.js +70 -0
- package/lib/AggregateTiming.js.map +1 -0
- package/lib/CachingHost.d.ts +39 -0
- package/lib/CachingHost.d.ts.map +1 -0
- package/lib/CachingHost.js +366 -0
- package/lib/CachingHost.js.map +1 -0
- package/lib/Host.d.ts +38 -0
- package/lib/Host.d.ts.map +1 -0
- package/lib/Host.js +9 -0
- package/lib/Host.js.map +1 -0
- package/lib/SimpleHost.d.ts +35 -0
- package/lib/SimpleHost.d.ts.map +1 -0
- package/lib/SimpleHost.js +56 -0
- package/lib/SimpleHost.js.map +1 -0
- package/lib/Table.d.ts +53 -0
- package/lib/Table.d.ts.map +1 -0
- package/lib/Table.js +234 -0
- package/lib/Table.js.map +1 -0
- package/lib/Timing.d.ts +9 -0
- package/lib/Timing.d.ts.map +1 -0
- package/lib/Timing.js +57 -0
- package/lib/Timing.js.map +1 -0
- package/lib/__tests__/CachingHost.spec.d.ts +8 -0
- package/lib/__tests__/CachingHost.spec.d.ts.map +1 -0
- package/lib/__tests__/CachingHost.spec.js +178 -0
- package/lib/__tests__/CachingHost.spec.js.map +1 -0
- package/lib/findWorkspaceDir.d.ts +2 -1
- package/lib/findWorkspaceDir.d.ts.map +1 -1
- package/lib/findWorkspaceDir.js +4 -6
- package/lib/findWorkspaceDir.js.map +1 -1
- package/lib/getPackageNameToDir.d.ts +2 -1
- package/lib/getPackageNameToDir.d.ts.map +1 -1
- package/lib/getPackageNameToDir.js +4 -5
- package/lib/getPackageNameToDir.js.map +1 -1
- package/lib/getWorkspacePackageDirs.d.ts +2 -1
- package/lib/getWorkspacePackageDirs.d.ts.map +1 -1
- package/lib/getWorkspacePackageDirs.js +5 -6
- package/lib/getWorkspacePackageDirs.js.map +1 -1
- package/lib/index.d.ts +8 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +15 -4
- package/lib/index.js.map +1 -1
- package/lib/matchesAnyGlob.d.ts +17 -0
- package/lib/matchesAnyGlob.d.ts.map +1 -0
- package/lib/matchesAnyGlob.js +131 -0
- package/lib/matchesAnyGlob.js.map +1 -0
- package/lib/mutateJson.d.ts +2 -1
- package/lib/mutateJson.d.ts.map +1 -1
- package/lib/mutateJson.js +3 -5
- package/lib/mutateJson.js.map +1 -1
- package/lib/nanosecondsToSanity.d.ts +8 -0
- package/lib/nanosecondsToSanity.d.ts.map +1 -0
- package/lib/nanosecondsToSanity.js +14 -0
- package/lib/nanosecondsToSanity.js.map +1 -0
- package/package.json +8 -6
- package/src/AggregateTiming.ts +71 -0
- package/src/CachingHost.ts +489 -0
- package/src/Host.ts +34 -0
- package/src/SimpleHost.ts +57 -0
- package/src/Table.ts +319 -0
- package/src/Timing.ts +55 -0
- package/src/__tests__/CachingHost.spec.ts +244 -0
- package/src/findWorkspaceDir.ts +5 -6
- package/src/getPackageNameToDir.ts +4 -4
- package/src/getWorkspacePackageDirs.ts +7 -3
- package/src/index.ts +8 -2
- package/src/matchesAnyGlob.ts +145 -0
- package/src/mutateJson.ts +4 -5
- package/src/nanosecondsToSanity.ts +10 -0
- package/tsconfig.tsbuildinfo +1 -4046
- package/lib/readJson.d.ts +0 -8
- package/lib/readJson.d.ts.map +0 -1
- package/lib/readJson.js +0 -16
- package/lib/readJson.js.map +0 -1
- package/lib/writeJson.d.ts +0 -8
- package/lib/writeJson.d.ts.map +0 -1
- package/lib/writeJson.js +0 -15
- package/lib/writeJson.js.map +0 -1
- package/src/readJson.ts +0 -13
- package/src/writeJson.ts +0 -12
package/src/index.ts
CHANGED
|
@@ -6,9 +6,15 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
export { getWorkspacePackageDirs } from "./getWorkspacePackageDirs";
|
|
9
|
-
export { readJson } from "./readJson";
|
|
10
|
-
export { writeJson } from "./writeJson";
|
|
11
9
|
export { mutateJson } from "./mutateJson";
|
|
12
10
|
export { PackageJson } from "./PackageJson";
|
|
13
11
|
export { findWorkspaceDir } from "./findWorkspaceDir";
|
|
14
12
|
export { getPackageNameToDir } from "./getPackageNameToDir";
|
|
13
|
+
export { Host } from "./Host";
|
|
14
|
+
export { SimpleHost } from "./SimpleHost";
|
|
15
|
+
export { CachingHost } from "./CachingHost";
|
|
16
|
+
export { matchesAnyGlob } from "./matchesAnyGlob";
|
|
17
|
+
export { nanosecondsToSanity } from "./nanosecondsToSanity";
|
|
18
|
+
export { AggregateTiming } from "./AggregateTiming";
|
|
19
|
+
export { Timing } from "./Timing";
|
|
20
|
+
export { Table } from "./Table";
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright 2022 Palantir Technologies, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the MIT license. See LICENSE file in the project root for details.
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { makeRe } from "micromatch";
|
|
9
|
+
import { nanosecondsToSanity } from "./nanosecondsToSanity";
|
|
10
|
+
import { Table } from "./Table";
|
|
11
|
+
|
|
12
|
+
// This file requires a LOT of caching to be performant. We have three layers to avoid work.
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Multimap cache of whether a needle was found in the glob haystack. Short circuits many
|
|
16
|
+
* individual checks against the globs.
|
|
17
|
+
*/
|
|
18
|
+
const cache = new Map</* haystack */ readonly string[], Map</* needle */ string, /* result */ boolean>>();
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Multimap cache of whether a needle matches a glob. Allows us to avoid regexp's.
|
|
22
|
+
*/
|
|
23
|
+
const singleMatcherCache = new Map</* glob */ string, Map</* needle */ string, /* result*/ boolean>>();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Cache of glob to regular expression. Compiling the regular expression is expensive.
|
|
27
|
+
*/
|
|
28
|
+
const compiledGlobCache = new Map</* glob */ string, RegExp>();
|
|
29
|
+
|
|
30
|
+
let haystackMiss = 0;
|
|
31
|
+
let haystackHit = 0;
|
|
32
|
+
|
|
33
|
+
let matchTime = BigInt(0);
|
|
34
|
+
|
|
35
|
+
let singleMatcherHits = 0;
|
|
36
|
+
let singleMatcherMisses = 0;
|
|
37
|
+
let singleMatcherSaves = 0; // hits + hits you would have had if the haystack didn't save you
|
|
38
|
+
|
|
39
|
+
interface MatchesAnyGlob {
|
|
40
|
+
(needle: string, haystack: readonly string[]): boolean | undefined;
|
|
41
|
+
printStats?: () => void;
|
|
42
|
+
}
|
|
43
|
+
export const matchesAnyGlob: MatchesAnyGlob = function matchesAnyGlobFunc(needle: string, haystack: readonly string[]) {
|
|
44
|
+
matchTime -= process.hrtime.bigint();
|
|
45
|
+
|
|
46
|
+
let cacheForHaystack = cache.get(haystack);
|
|
47
|
+
if (cacheForHaystack === undefined) {
|
|
48
|
+
cacheForHaystack = new Map<string, boolean>();
|
|
49
|
+
cache.set(haystack, cacheForHaystack);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let result = cacheForHaystack!.get(needle);
|
|
53
|
+
if (result === undefined) {
|
|
54
|
+
haystackMiss++;
|
|
55
|
+
result = false;
|
|
56
|
+
for (const pattern of haystack) {
|
|
57
|
+
// result = needleInPattern(needle, pattern); // commented out as a reminder to update both
|
|
58
|
+
// BEGIN INLINE of needleInPattern
|
|
59
|
+
let patternCache = singleMatcherCache.get(pattern);
|
|
60
|
+
if (patternCache === undefined) {
|
|
61
|
+
patternCache = new Map<string, boolean>();
|
|
62
|
+
singleMatcherCache.set(pattern, patternCache);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// N.B. true/false/undefined
|
|
66
|
+
result = patternCache.get(needle); // only thing different from the inline is we need to reuse `result`
|
|
67
|
+
if (result === undefined) {
|
|
68
|
+
let regexp = compiledGlobCache.get(pattern);
|
|
69
|
+
if (regexp === undefined) {
|
|
70
|
+
regexp = makeRe(pattern);
|
|
71
|
+
compiledGlobCache.set(pattern, regexp);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
singleMatcherMisses++;
|
|
75
|
+
result = regexp.test(needle);
|
|
76
|
+
patternCache.set(needle, result);
|
|
77
|
+
} else {
|
|
78
|
+
singleMatcherHits++;
|
|
79
|
+
singleMatcherSaves++;
|
|
80
|
+
}
|
|
81
|
+
// END INLINE of needleInPattern
|
|
82
|
+
if (result) break;
|
|
83
|
+
}
|
|
84
|
+
cacheForHaystack!.set(needle, result);
|
|
85
|
+
} else {
|
|
86
|
+
singleMatcherSaves += haystack.length;
|
|
87
|
+
haystackHit++;
|
|
88
|
+
}
|
|
89
|
+
matchTime += process.hrtime.bigint();
|
|
90
|
+
return result;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
matchesAnyGlob.printStats = () => {
|
|
94
|
+
const table = new Table<[string, string]>({
|
|
95
|
+
title: "matchesAnyGlob stats",
|
|
96
|
+
showHeader: true,
|
|
97
|
+
showFooter: false,
|
|
98
|
+
columns: [
|
|
99
|
+
{ header: "Stat", type: "string" },
|
|
100
|
+
{ header: "Value", type: "string" },
|
|
101
|
+
],
|
|
102
|
+
});
|
|
103
|
+
table.addRow("Haystack Miss", "" + haystackMiss);
|
|
104
|
+
table.addRow("Haystack Hit", "" + haystackHit);
|
|
105
|
+
table.addRow("Single Glob Hits", "" + singleMatcherHits);
|
|
106
|
+
table.addRow("Single Glob Misses", "" + singleMatcherMisses);
|
|
107
|
+
table.addRow("Single Glob Saves", "" + singleMatcherSaves);
|
|
108
|
+
table.addRow("Total Time", nanosecondsToSanity(matchTime, 6));
|
|
109
|
+
|
|
110
|
+
table.print();
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @deprecated Don't use this directly. We manually inline it above
|
|
115
|
+
*/
|
|
116
|
+
export function needleInPattern(needle: string, pattern: string) {
|
|
117
|
+
// benchmark says the uncommented version is best
|
|
118
|
+
// https://jsben.ch/Y8TWs
|
|
119
|
+
|
|
120
|
+
// option 2
|
|
121
|
+
let patternCache = singleMatcherCache.get(pattern);
|
|
122
|
+
if (patternCache === undefined) {
|
|
123
|
+
patternCache = new Map<string, boolean>();
|
|
124
|
+
singleMatcherCache.set(pattern, patternCache);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// N.B. true/false/undefined
|
|
128
|
+
let result = patternCache.get(needle);
|
|
129
|
+
if (result === undefined) {
|
|
130
|
+
let regexp = compiledGlobCache.get(pattern);
|
|
131
|
+
if (regexp === undefined) {
|
|
132
|
+
regexp = makeRe(pattern);
|
|
133
|
+
compiledGlobCache.set(pattern, regexp);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
singleMatcherMisses++;
|
|
137
|
+
result = regexp.test(needle);
|
|
138
|
+
patternCache.set(needle, result);
|
|
139
|
+
} else {
|
|
140
|
+
singleMatcherHits++;
|
|
141
|
+
singleMatcherSaves++;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return result;
|
|
145
|
+
}
|
package/src/mutateJson.ts
CHANGED
|
@@ -5,11 +5,10 @@
|
|
|
5
5
|
*
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
import { writeJson } from "./writeJson";
|
|
8
|
+
import { Host } from "./Host";
|
|
10
9
|
|
|
11
|
-
export function mutateJson<T extends object>(path: string, mutator: (f: T) => T) {
|
|
12
|
-
let file: T = readJson(path);
|
|
10
|
+
export function mutateJson<T extends object>(path: string, host: Host, mutator: (f: T) => T) {
|
|
11
|
+
let file: T = host.readJson(path);
|
|
13
12
|
file = mutator(file);
|
|
14
|
-
writeJson(path, file);
|
|
13
|
+
host.writeJson(path, file);
|
|
15
14
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright 2022 Palantir Technologies, Inc.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the MIT license. See LICENSE file in the project root for details.
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export function nanosecondsToSanity(n: bigint, precision: number = 9) {
|
|
9
|
+
return n / BigInt(1000000000) + "." + ("" + (n % BigInt(1000000000))).padStart(9, "0").substring(0, precision) + "s";
|
|
10
|
+
}
|