UNPKG

1.63 kBPlain TextView Raw
1import { propertyPath } from '@stryker-mutator/util';
2import { schema } from '@stryker-mutator/api/core';
3import { StrykerOptions } from '@stryker-mutator/api/core';
4
5import emojiRegex from 'emoji-regex';
6
7const emojiRe = emojiRegex();
8
9const { MutantStatus } = schema;
10
11export function wrapInClosure(codeFragment: string): string {
12 return `
13 (function (window) {
14 ${codeFragment}
15 })((Function('return this'))());`;
16}
17
18export function padLeft(input: string): string {
19 return input
20 .split('\n')
21 .map((str) => '\t' + str)
22 .join('\n');
23}
24
25export function plural(items: number): string {
26 if (items > 1) {
27 return 's';
28 } else {
29 return '';
30 }
31}
32
33export function serialize(thing: unknown): string {
34 return JSON.stringify(thing);
35}
36
37export function deserialize<T>(stringified: string): T {
38 return JSON.parse(stringified);
39}
40
41export function getEmojiForStatus(status: schema.MutantStatus): string {
42 switch (status) {
43 case MutantStatus.Killed:
44 return '✅';
45 case MutantStatus.NoCoverage:
46 return '🙈';
47 case MutantStatus.Ignored:
48 return '🤥';
49 case MutantStatus.Survived:
50 return '👽';
51 case MutantStatus.Timeout:
52 return '⌛';
53 case MutantStatus.RuntimeError:
54 case MutantStatus.CompileError:
55 return '💥';
56 }
57}
58
59export function stringWidth(input: string): number {
60 let length = input.length;
61 for (const match of input.matchAll(emojiRe)) {
62 length = length - match[0].length + 2;
63 }
64 return length;
65}
66
67/**
68 * Print the name of (or path to) a stryker option
69 */
70export const optionsPath = propertyPath<StrykerOptions>();