@d-es-ign/stryker-js-util 9.6.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.
Files changed (93) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +13 -0
  3. package/dist/src/child-process-as-promised.d.ts +5 -0
  4. package/dist/src/child-process-as-promised.d.ts.map +1 -0
  5. package/dist/src/child-process-as-promised.js +6 -0
  6. package/dist/src/child-process-as-promised.js.map +1 -0
  7. package/dist/src/deep-merge.d.ts +10 -0
  8. package/dist/src/deep-merge.d.ts.map +1 -0
  9. package/dist/src/deep-merge.js +25 -0
  10. package/dist/src/deep-merge.js.map +1 -0
  11. package/dist/src/errors.d.ts +6 -0
  12. package/dist/src/errors.d.ts.map +1 -0
  13. package/dist/src/errors.js +27 -0
  14. package/dist/src/errors.js.map +1 -0
  15. package/dist/src/find-unserializables.d.ts +6 -0
  16. package/dist/src/find-unserializables.d.ts.map +1 -0
  17. package/dist/src/find-unserializables.js +60 -0
  18. package/dist/src/find-unserializables.js.map +1 -0
  19. package/dist/src/i.d.ts +8 -0
  20. package/dist/src/i.d.ts.map +1 -0
  21. package/dist/src/i.js +2 -0
  22. package/dist/src/i.js.map +1 -0
  23. package/dist/src/immutable.d.ts +12 -0
  24. package/dist/src/immutable.d.ts.map +1 -0
  25. package/dist/src/immutable.js +32 -0
  26. package/dist/src/immutable.js.map +1 -0
  27. package/dist/src/index.d.ts +17 -0
  28. package/dist/src/index.d.ts.map +1 -0
  29. package/dist/src/index.js +17 -0
  30. package/dist/src/index.js.map +1 -0
  31. package/dist/src/known-keys.d.ts +8 -0
  32. package/dist/src/known-keys.d.ts.map +1 -0
  33. package/dist/src/known-keys.js +2 -0
  34. package/dist/src/known-keys.js.map +1 -0
  35. package/dist/src/noop-logger.d.ts +15 -0
  36. package/dist/src/noop-logger.d.ts.map +1 -0
  37. package/dist/src/noop-logger.js +39 -0
  38. package/dist/src/noop-logger.js.map +1 -0
  39. package/dist/src/not-empty.d.ts +2 -0
  40. package/dist/src/not-empty.d.ts.map +1 -0
  41. package/dist/src/not-empty.js +4 -0
  42. package/dist/src/not-empty.js.map +1 -0
  43. package/dist/src/platform.d.ts +9 -0
  44. package/dist/src/platform.d.ts.map +1 -0
  45. package/dist/src/platform.js +11 -0
  46. package/dist/src/platform.js.map +1 -0
  47. package/dist/src/primitive.d.ts +2 -0
  48. package/dist/src/primitive.d.ts.map +1 -0
  49. package/dist/src/primitive.js +2 -0
  50. package/dist/src/primitive.js.map +1 -0
  51. package/dist/src/require-resolve.d.ts +11 -0
  52. package/dist/src/require-resolve.d.ts.map +1 -0
  53. package/dist/src/require-resolve.js +18 -0
  54. package/dist/src/require-resolve.js.map +1 -0
  55. package/dist/src/split.d.ts +2 -0
  56. package/dist/src/split.d.ts.map +1 -0
  57. package/dist/src/split.js +15 -0
  58. package/dist/src/split.js.map +1 -0
  59. package/dist/src/string-utils.d.ts +41 -0
  60. package/dist/src/string-utils.d.ts.map +1 -0
  61. package/dist/src/string-utils.js +47 -0
  62. package/dist/src/string-utils.js.map +1 -0
  63. package/dist/src/stryker-error.d.ts +5 -0
  64. package/dist/src/stryker-error.d.ts.map +1 -0
  65. package/dist/src/stryker-error.js +9 -0
  66. package/dist/src/stryker-error.js.map +1 -0
  67. package/dist/src/task.d.ts +23 -0
  68. package/dist/src/task.d.ts.map +1 -0
  69. package/dist/src/task.js +56 -0
  70. package/dist/src/task.js.map +1 -0
  71. package/dist/src/test-files-provided.d.ts +4 -0
  72. package/dist/src/test-files-provided.d.ts.map +1 -0
  73. package/dist/src/test-files-provided.js +4 -0
  74. package/dist/src/test-files-provided.js.map +1 -0
  75. package/package.json +43 -0
  76. package/src/child-process-as-promised.ts +6 -0
  77. package/src/deep-merge.ts +29 -0
  78. package/src/errors.ts +31 -0
  79. package/src/find-unserializables.ts +71 -0
  80. package/src/i.ts +7 -0
  81. package/src/immutable.ts +58 -0
  82. package/src/index.ts +16 -0
  83. package/src/known-keys.ts +11 -0
  84. package/src/noop-logger.ts +38 -0
  85. package/src/not-empty.ts +3 -0
  86. package/src/platform.ts +10 -0
  87. package/src/primitive.ts +1 -0
  88. package/src/require-resolve.ts +19 -0
  89. package/src/split.ts +16 -0
  90. package/src/string-utils.ts +76 -0
  91. package/src/stryker-error.ts +12 -0
  92. package/src/task.ts +71 -0
  93. package/src/test-files-provided.ts +5 -0
@@ -0,0 +1,38 @@
1
+ export const noopLogger = {
2
+ isTraceEnabled(): boolean {
3
+ return false;
4
+ },
5
+ isDebugEnabled(): boolean {
6
+ return false;
7
+ },
8
+ isInfoEnabled(): boolean {
9
+ return false;
10
+ },
11
+ isWarnEnabled(): boolean {
12
+ return false;
13
+ },
14
+ isErrorEnabled(): boolean {
15
+ return false;
16
+ },
17
+ isFatalEnabled(): boolean {
18
+ return false;
19
+ },
20
+ trace(): void {
21
+ // noop
22
+ },
23
+ debug(): void {
24
+ // noop
25
+ },
26
+ info(): void {
27
+ // noop
28
+ },
29
+ warn(): void {
30
+ // noop
31
+ },
32
+ error(): void {
33
+ // noop
34
+ },
35
+ fatal(): void {
36
+ // noop
37
+ },
38
+ };
@@ -0,0 +1,3 @@
1
+ export function notEmpty<T>(item: T | null | undefined): item is T {
2
+ return item !== undefined && item !== null;
3
+ }
@@ -0,0 +1,10 @@
1
+ export const platform = {
2
+ /**
3
+ * Tells whether the filesystem is case sensitive.
4
+ *
5
+ * @returns false on Win32, true elsewhere
6
+ */
7
+ caseSensitiveFs(): boolean {
8
+ return process.platform != 'win32';
9
+ },
10
+ };
@@ -0,0 +1 @@
1
+ export type Primitive = boolean | number | string | null | undefined;
@@ -0,0 +1,19 @@
1
+ import { createRequire } from 'module';
2
+
3
+ /**
4
+ * Require a module from the current working directory (or a different base dir)
5
+ * @see https://nodejs.org/api/modules.html#modules_require_resolve_paths_request
6
+ */
7
+ export function requireResolve(id: string, from = process.cwd()): unknown {
8
+ const require = createRequire(import.meta.url);
9
+ return require(resolveFromCwd(id, from));
10
+ }
11
+
12
+ /**
13
+ * Resolves a module from the current working directory (or a different base dir)
14
+ * @see https://nodejs.org/api/modules.html#modules_require_resolve_paths_request
15
+ */
16
+ export function resolveFromCwd(id: string, cwd = process.cwd()): string {
17
+ const require = createRequire(import.meta.url);
18
+ return require.resolve(id, { paths: [cwd] });
19
+ }
package/src/split.ts ADDED
@@ -0,0 +1,16 @@
1
+ export function split<T>(
2
+ values: Iterable<T>,
3
+ predicate: (value: T, index: number) => boolean,
4
+ ): [T[], T[]] {
5
+ const left: T[] = [];
6
+ const right: T[] = [];
7
+ let index = 0;
8
+ for (const value of values) {
9
+ if (predicate(value, index++)) {
10
+ left.push(value);
11
+ } else {
12
+ right.push(value);
13
+ }
14
+ }
15
+ return [left, right];
16
+ }
@@ -0,0 +1,76 @@
1
+ import { KnownKeys } from './known-keys.js';
2
+ import { Primitive } from './primitive.js';
3
+
4
+ type OnlyObject<T> = Exclude<T, Primitive>;
5
+
6
+ /**
7
+ * Consolidates multiple consecutive white spaces into a single space.
8
+ * @param str The string to be normalized
9
+ */
10
+ export function normalizeWhitespaces(str: string): string {
11
+ return str.replace(/\s+/g, ' ').trim();
12
+ }
13
+
14
+ /**
15
+ * Normalizes line endings as unix style.
16
+ */
17
+ export function normalizeLineEndings(text: string): string {
18
+ return text.replace(/\r\n/g, '\n');
19
+ }
20
+
21
+ export interface PropertyPathOverloads<T> {
22
+ (key: KnownKeys<T>): string;
23
+ <TProp1 extends KnownKeys<T>>(
24
+ key: TProp1,
25
+ key2: KnownKeys<OnlyObject<T[TProp1]>>,
26
+ ): string;
27
+ <
28
+ TProp1 extends KnownKeys<T>,
29
+ TProp2 extends KnownKeys<OnlyObject<T[TProp1]>>,
30
+ >(
31
+ key: TProp1,
32
+ key2: TProp2,
33
+ key3: KnownKeys<OnlyObject<OnlyObject<T[TProp1]>[TProp2]>>,
34
+ ): string;
35
+ }
36
+
37
+ /**
38
+ * Given a base type, allows type safe access to the name of a property.
39
+ * @param prop The property name
40
+ */
41
+ export function propertyPath<T>(): PropertyPathOverloads<T> {
42
+ const fn: PropertyPathOverloads<T> = ((...args: string[]) =>
43
+ args.join('.')) as unknown as PropertyPathOverloads<T>;
44
+ return fn;
45
+ }
46
+
47
+ /**
48
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
49
+ */
50
+ export function escapeRegExpLiteral(input: string): string {
51
+ return input.replace(/[.*+\-?^${}()|[\]\\/]/g, '\\$&'); // $& means the whole matched string
52
+ }
53
+
54
+ /**
55
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
56
+ */
57
+ export function escapeRegExp(input: string): string {
58
+ return input.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
59
+ }
60
+
61
+ /**
62
+ * Normalizes relative or absolute file names to be in posix format (forward slashes '/')
63
+ */
64
+ export function normalizeFileName(fileName: string): string {
65
+ return fileName.replace(/\\/g, '/');
66
+ }
67
+
68
+ /**
69
+ * Creates a URL to the page where you can report a bug.
70
+ * @param titleSuggestion The title to be prefilled in.
71
+ */
72
+ export function strykerReportBugUrl(titleSuggestion: string): string {
73
+ return `https://github.com/stryker-mutator/stryker-js/issues/new?assignees=&labels=%F0%9F%90%9B+Bug&template=bug_report.md&title=${encodeURIComponent(
74
+ titleSuggestion,
75
+ )}`;
76
+ }
@@ -0,0 +1,12 @@
1
+ import { errorToString } from './errors.js';
2
+
3
+ export class StrykerError extends Error {
4
+ constructor(
5
+ message: string,
6
+ public readonly innerError?: unknown,
7
+ ) {
8
+ super(
9
+ `${message}${innerError ? `. Inner error: ${errorToString(innerError)}` : ''}`,
10
+ );
11
+ }
12
+ }
package/src/task.ts ADDED
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Wraps a promise in a Task api for convenience.
3
+ */
4
+ export class Task<T = void> {
5
+ protected _promise: Promise<T>;
6
+ private resolveFn!: (value: PromiseLike<T> | T) => void;
7
+ private rejectFn!: (reason: any) => void;
8
+ private _isCompleted = false;
9
+
10
+ constructor() {
11
+ this._promise = new Promise<T>((resolve, reject) => {
12
+ this.resolveFn = resolve;
13
+ this.rejectFn = reject;
14
+ });
15
+ }
16
+
17
+ public get promise(): Promise<T> {
18
+ return this._promise;
19
+ }
20
+
21
+ public get isCompleted(): boolean {
22
+ return this._isCompleted;
23
+ }
24
+
25
+ public resolve = (result: PromiseLike<T> | T): void => {
26
+ this._isCompleted = true;
27
+ this.resolveFn(result);
28
+ };
29
+
30
+ public reject: (reason: any) => void = (reason: any): void => {
31
+ this._isCompleted = true;
32
+ this.rejectFn(reason);
33
+ };
34
+ }
35
+
36
+ /**
37
+ * A task that can expire after the given time.
38
+ */
39
+ export class ExpirableTask<T = void> extends Task<
40
+ T | typeof ExpirableTask.TimeoutExpired
41
+ > {
42
+ public static readonly TimeoutExpired: unique symbol =
43
+ Symbol('TimeoutExpired');
44
+
45
+ constructor(timeoutMS: number) {
46
+ super();
47
+ this._promise = ExpirableTask.timeout(this._promise, timeoutMS);
48
+ }
49
+
50
+ public static timeout<K>(
51
+ promise: Promise<K>,
52
+ ms: number,
53
+ ): Promise<K | typeof ExpirableTask.TimeoutExpired> {
54
+ const sleep = new Promise<K | typeof ExpirableTask.TimeoutExpired>(
55
+ (res, rej) => {
56
+ const timer = setTimeout(() => res(ExpirableTask.TimeoutExpired), ms);
57
+ promise
58
+ .then((result) => {
59
+ clearTimeout(timer);
60
+ res(result);
61
+ })
62
+ .catch((error: unknown) => {
63
+ clearTimeout(timer);
64
+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
65
+ rej(error);
66
+ });
67
+ },
68
+ );
69
+ return sleep;
70
+ }
71
+ }
@@ -0,0 +1,5 @@
1
+ export function testFilesProvided(options: {
2
+ testFiles?: readonly string[];
3
+ }): boolean {
4
+ return !!options.testFiles && options.testFiles.length > 0;
5
+ }