-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtransform.ts
More file actions
171 lines (144 loc) · 4.59 KB
/
transform.ts
File metadata and controls
171 lines (144 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { platform } from 'node:os';
export function toArray<T>(val: T | T[]): T[] {
return Array.isArray(val) ? val : [val];
}
export function objectToKeys<T extends object>(obj: T) {
return Object.keys(obj) as (keyof T)[];
}
export function objectToEntries<T extends object>(obj: T) {
return Object.entries(obj) as [keyof T, T[keyof T]][];
}
export function objectFromEntries<K extends PropertyKey, V>(entries: [K, V][]) {
return Object.fromEntries(entries) as Record<K, V>;
}
export function countOccurrences<T extends PropertyKey>(
values: T[],
): Partial<Record<T, number>> {
return values.reduce<Partial<Record<T, number>>>(
(acc, value) => ({ ...acc, [value]: (acc[value] ?? 0) + 1 }),
{},
);
}
export function distinct<T extends string | number | boolean>(array: T[]): T[] {
return [...new Set(array)];
}
export function deepClone<T>(obj: T): T {
return obj == null || typeof obj !== 'object' ? obj : structuredClone(obj);
}
export function factorOf<T>(items: T[], filterFn: (i: T) => boolean): number {
const itemCount = items.length;
// early exit for empty rows
if (!itemCount) {
return 1;
}
const filterCount = items.filter(filterFn).length;
// if no rows result from the filter fn we forward return 1 as factor
return filterCount === 0 ? 1 : (itemCount - filterCount) / itemCount;
}
type ArgumentValue = number | string | boolean | string[];
export type CliArgsObject<T extends object = Record<string, ArgumentValue>> =
T extends never
? Record<string, ArgumentValue | undefined> | { _: string }
: T;
/**
* Converts an object with different types of values into an array of command-line arguments.
*
* @example
* const args = objectToCliArgs({
* _: ['node', 'index.js'], // node index.js
* name: 'Juanita', // --name=Juanita
* formats: ['json', 'md'] // --format=json --format=md
* });
*/
export function objectToCliArgs<
T extends object = Record<string, ArgumentValue>,
>(params?: CliArgsObject<T>): string[] {
if (!params) {
return [];
}
return Object.entries(params).flatMap(([key, value]) => {
// process/file/script
if (key === '_') {
return Array.isArray(value) ? value : [`${value}`];
}
const prefix = key.length === 1 ? '-' : '--';
// "-*" arguments (shorthands)
if (Array.isArray(value)) {
return value.map(v => `${prefix}${key}="${v}"`);
}
// "--*" arguments ==========
if (Array.isArray(value)) {
return value.map(v => `${prefix}${key}="${v}"`);
}
if (typeof value === 'object') {
return Object.entries(value as Record<string, ArgumentValue>).flatMap(
// transform nested objects to the dot notation `key.subkey`
([k, v]) => objectToCliArgs({ [`${key}.${k}`]: v }),
);
}
if (typeof value === 'string') {
return [`${prefix}${key}="${value}"`];
}
if (typeof value === 'number') {
return [`${prefix}${key}=${value}`];
}
if (typeof value === 'boolean') {
return [`${prefix}${value ? '' : 'no-'}${key}`];
}
throw new Error(`Unsupported type ${typeof value} for key ${key}`);
});
}
export function toUnixPath(path: string): string {
return path.replace(/\\/g, '/');
}
export function toUnixNewlines(text: string): string {
return platform() === 'win32' ? text.replace(/\r\n/g, '\n') : text;
}
export function fromJsonLines<T extends unknown[]>(jsonLines: string) {
const unifiedNewLines = toUnixNewlines(jsonLines).trim();
const invalid = Symbol('invalid json');
return unifiedNewLines
.split('\n')
.map(line => {
try {
return JSON.parse(line);
} catch {
return invalid;
}
})
.filter(line => line !== invalid) as T;
}
export function toJsonLines<T>(json: T[]) {
return json.map(item => JSON.stringify(item)).join('\n');
}
export function toNumberPrecision(
value: number,
decimalPlaces: number,
): number {
return Number(
`${Math.round(
Number.parseFloat(`${value}e${decimalPlaces}`),
)}e-${decimalPlaces}`,
);
}
export function toOrdinal(value: number): string {
/* eslint-disable @typescript-eslint/no-magic-numbers */
if (value % 10 === 1 && value % 100 !== 11) {
return `${value}st`;
}
if (value % 10 === 2 && value % 100 !== 12) {
return `${value}nd`;
}
if (value % 10 === 3 && value % 100 !== 13) {
return `${value}rd`;
}
/* eslint-enable @typescript-eslint/no-magic-numbers */
return `${value}th`;
}
export function removeUndefinedAndEmptyProps<T extends object>(obj: T): T {
return Object.fromEntries(
Object.entries(obj).filter(
([, value]) => value !== undefined && value !== '',
),
) as T;
}