@mongosh/logging 3.0.0 → 3.4.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.
@@ -0,0 +1,26 @@
1
+ import { expect } from 'chai';
2
+ import { toSnakeCase } from './helpers';
3
+
4
+ describe('logging helpers', function () {
5
+ describe('toSnakeCase', function () {
6
+ const useCases = [
7
+ { input: 'MongoDB REPL', output: 'mongo_db_repl' },
8
+ {
9
+ input: 'Node.js REPL Instantiation',
10
+ output: 'node_js_repl_instantiation',
11
+ },
12
+ { input: 'A', output: 'a' },
13
+ {
14
+ input: 'OneLongThingInPascalCase',
15
+ output: 'one_long_thing_in_pascal_case',
16
+ },
17
+ { input: 'Removes .Dots in Node.js', output: 'removes_dots_in_node_js' },
18
+ ];
19
+
20
+ for (const { input, output } of useCases) {
21
+ it(`should convert ${input} to ${output}`, function () {
22
+ expect(toSnakeCase(input)).to.equal(output);
23
+ });
24
+ }
25
+ });
26
+ });
package/src/helpers.ts ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * A helper class for keeping track of how often specific events occurred.
3
+ */
4
+ export class MultiSet<T extends Record<string, unknown>> {
5
+ _entries: Map<string, number> = new Map();
6
+
7
+ add(entry: T): void {
8
+ const key = JSON.stringify(Object.entries(entry).sort());
9
+ this._entries.set(key, (this._entries.get(key) ?? 0) + 1);
10
+ }
11
+
12
+ clear(): void {
13
+ this._entries.clear();
14
+ }
15
+
16
+ *[Symbol.iterator](): Iterator<[T, number]> {
17
+ for (const [key, count] of this._entries) {
18
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
19
+ yield [Object.fromEntries(JSON.parse(key)) as T, count];
20
+ }
21
+ }
22
+ }
23
+
24
+ /**
25
+ * It transforms a random string into snake case. Snake case is completely
26
+ * lowercase and uses '_' to separate words. For example:
27
+ *
28
+ * This function defines a "word" as a sequence of characters until the next `.` or capital letter.
29
+ *
30
+ * 'Random String' => 'random_string'
31
+ *
32
+ * It will also remove any non alphanumeric characters to ensure the string
33
+ * is compatible with Segment. For example:
34
+ *
35
+ * 'Node.js REPL Instantiation' => 'node_js_repl_instantiation'
36
+ *
37
+ * @param str Any non snake-case formatted string
38
+ * @returns The snake-case formatted string
39
+ */
40
+ export function toSnakeCase(str: string): string {
41
+ const matches = str.match(
42
+ /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g
43
+ );
44
+ if (!matches) {
45
+ return str;
46
+ }
47
+
48
+ return matches.map((x) => x.toLowerCase()).join('_');
49
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,3 @@
1
- export { setupLoggerAndTelemetry } from './setup-logger-and-telemetry';
2
1
  export {
3
2
  MongoshAnalytics,
4
3
  ToggleableAnalytics,
@@ -6,3 +5,5 @@ export {
6
5
  NoopAnalytics,
7
6
  ThrottledAnalytics,
8
7
  } from './analytics-helpers';
8
+ export { MongoshLoggingAndTelemetry } from './types';
9
+ export { setupLoggingAndTelemetry } from './logging-and-telemetry';