« get me outta code hell

upd8-util.js - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/upd8-util.js
blob: e188ed4c4ad00ea46ed68eba49b4c39769eafd4c (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// This is used by upd8.js! It's part of the 8ackend. Read the notes there if
// you're curious.
//
// Friendly(!) disclaimer: these utility functions haven't 8een tested all that
// much. Do not assume it will do exactly what you want it to do in all cases.
// It will likely only do exactly what I want it to, and only in the cases I
// decided were relevant enough to 8other handling.

'use strict';

// Apparently JavaScript doesn't come with a function to split an array into
// chunks! Weird. Anyway, this is an awesome place to use a generator, even
// though we don't really make use of the 8enefits of generators any time we
// actually use this. 8ut it's still awesome, 8ecause I say so.
module.exports.splitArray = function*(array, fn) {
    let lastIndex = 0;
    while (lastIndex < array.length) {
        let nextIndex = array.findIndex((item, index) => index >= lastIndex && fn(item));
        if (nextIndex === -1) {
            nextIndex = array.length;
        }
        yield array.slice(lastIndex, nextIndex);
        // Plus one because we don't want to include the dividing line in the
        // next array we yield.
        lastIndex = nextIndex + 1;
    }
};

// This function's name is a joke. Jokes! Hahahahahahahaha. Funny.
module.exports.joinNoOxford = function(array, plural = 'and') {
    array = array.filter(Boolean);

    if (array.length === 0) {
        // ????????
        return '';
    }

    if (array.length === 1) {
        return array[0];
    }

    if (array.length === 2) {
        return `${array[0]} ${plural} ${array[1]}`;
    }

    return `${array.slice(0, -1).join(', ')} ${plural} ${array[array.length - 1]}`;
};

module.exports.progressPromiseAll = function (msg, array) {
    if (!array.length) {
        return Promise.resolve([]);
    }

    let done = 0, total = array.length;
    process.stdout.write(`\r${msg} [0/${total}]`);
    const start = Date.now();
    return Promise.all(array.map(promise => promise.then(val => {
        done++;
        // const pc = `${done}/${total}`;
        const pc = (Math.round(done / total * 1000) / 10 + '%').padEnd('99.9%'.length, ' ');
        if (done === total) {
            const time = Date.now() - start;
            process.stdout.write(`\r\x1b[2m${msg} [${pc}] \x1b[0;32mDone! \x1b[0;2m(${time} ms) \x1b[0m\n`)
        } else {
            process.stdout.write(`\r${msg} [${pc}] `);
        }
        return val;
    })));
};

module.exports.queue = function (array, max = 50) {
    if (max === 0) {
        return array.map(fn => fn());
    }

    const begin = [];
    let current = 0;
    const ret = array.map(fn => new Promise((resolve, reject) => {
        begin.push(() => {
            current++;
            Promise.resolve(fn()).then(value => {
                current--;
                if (current < max && begin.length) {
                    begin.shift()();
                }
                resolve(value);
            }, reject);
        });
    }));

    for (let i = 0; i < max && begin.length; i++) {
        begin.shift()();
    }

    return ret;
};

module.exports.th = function (n) {
    if (n % 10 === 1 && n !== 11) {
        return n + 'st';
    } else if (n % 10 === 2 && n !== 12) {
        return n + 'nd';
    } else if (n % 10 === 3 && n !== 13) {
        return n + 'rd';
    } else {
        return n + 'th';
    }
};

// My function names just keep getting 8etter.
module.exports.s = function (n, word) {
    return `${n} ${word}` + (n === 1 ? '' : 's');
};

// Hey, did you know I apparently put a space 8efore the parameters in function
// names? 8ut only in function expressions, not declar8tions? I mean, I guess
// you did. You're pro8a8ly more familiar with my code than I am 8y this
// point. I haven't messed with any of this code in ages. Yay!!!!!!!!
//
// This function only does anything on o8jects you're going to 8e reusing.
// Argua8ly I could use a WeakMap here, 8ut since the o8ject needs to 8e
// reused to 8e useful anyway, I just store the result with a symbol.
// Sorry if it's 8een frozen I guess??
module.exports.cacheOneArg = function (fn) {
    const symbol = Symbol('Cache');
    return arg => {
        if (!arg[symbol]) {
            arg[symbol] = fn(arg);
        }
        return arg[symbol];
    };
};

const decorateTime = function (functionToBeWrapped) {
    const fn = function(...args) {
        const start = Date.now();
        const ret = functionToBeWrapped(...args);
        const end = Date.now();
        fn.timeSpent += end - start;
        fn.timesCalled++;
        return ret;
    };

    fn.wrappedName = functionToBeWrapped.name;
    fn.timeSpent = 0;
    fn.timesCalled = 0;
    fn.displayTime = function() {
        const averageTime = fn.timeSpent / fn.timesCalled;
        console.log(`\x1b[1m${fn.wrappedName}(...):\x1b[0m ${fn.timeSpent} ms / ${fn.timesCalled} calls \x1b[2m(avg: ${averageTime} ms)\x1b[0m`);
    };

    decorateTime.decoratedFunctions.push(fn);

    return fn;
};

decorateTime.decoratedFunctions = [];
decorateTime.displayTime = function() {
    if (decorateTime.decoratedFunctions.length) {
        console.log(`\x1b[1mdecorateTime results: ` + '-'.repeat(40) + '\x1b[0m');
        for (const fn of decorateTime.decoratedFunctions) {
            fn.displayTime();
        }
    }
};

module.exports.decorateTime = decorateTime;

// Stolen as #@CK from mtui!
const parseOptions = async function(options, optionDescriptorMap) {
    // This function is sorely lacking in comments, but the basic usage is
    // as such:
    //
    // options is the array of options you want to process;
    // optionDescriptorMap is a mapping of option names to objects that describe
    // the expected value for their corresponding options.
    // Returned is a mapping of any specified option names to their values, or
    // a process.exit(1) and error message if there were any issues.
    //
    // Here are examples of optionDescriptorMap to cover all the things you can
    // do with it:
    //
    // optionDescriptorMap: {
    //   'telnet-server': {type: 'flag'},
    //   't': {alias: 'telnet-server'}
    // }
    //
    // options: ['t'] -> result: {'telnet-server': true}
    //
    // optionDescriptorMap: {
    //   'directory': {
    //     type: 'value',
    //     validate(name) {
    //       // const whitelistedDirectories = ['apple', 'banana']
    //       if (whitelistedDirectories.includes(name)) {
    //         return true
    //       } else {
    //         return 'a whitelisted directory'
    //       }
    //     }
    //   },
    //   'files': {type: 'series'}
    // }
    //
    // ['--directory', 'apple'] -> {'directory': 'apple'}
    // ['--directory', 'artichoke'] -> (error)
    // ['--files', 'a', 'b', 'c', ';'] -> {'files': ['a', 'b', 'c']}
    //
    // TODO: Be able to validate the values in a series option.

    const handleDashless = optionDescriptorMap[parseOptions.handleDashless];
    const handleUnknown = optionDescriptorMap[parseOptions.handleUnknown];
    const result = Object.create(null);
    for (let i = 0; i < options.length; i++) {
        const option = options[i];
        if (option.startsWith('--')) {
            // --x can be a flag or expect a value or series of values
            let name = option.slice(2).split('=')[0]; // '--x'.split('=') = ['--x']
            let descriptor = optionDescriptorMap[name];
            if (!descriptor) {
                if (handleUnknown) {
                    handleUnknown(option);
                } else {
                    console.error(`Unknown option name: ${name}`);
                    process.exit(1);
                }
                continue;
            }
            if (descriptor.alias) {
                name = descriptor.alias;
                descriptor = optionDescriptorMap[name];
            }
            if (descriptor.type === 'flag') {
                result[name] = true;
            } else if (descriptor.type === 'value') {
                let value = option.slice(2).split('=')[1];
                if (!value) {
                    value = options[++i];
                    if (!value || value.startsWith('-')) {
                        value = null;
                    }
                }
                if (!value) {
                    console.error(`Expected a value for --${name}`);
                    process.exit(1);
                }
                result[name] = value;
            } else if (descriptor.type === 'series') {
                if (!options.slice(i).includes(';')) {
                    console.error(`Expected a series of values concluding with ; (\\;) for --${name}`);
                    process.exit(1);
                }
                const endIndex = i + options.slice(i).indexOf(';');
                result[name] = options.slice(i + 1, endIndex);
                i = endIndex;
            }
            if (descriptor.validate) {
                const validation = await descriptor.validate(result[name]);
                if (validation !== true) {
                    console.error(`Expected ${validation} for --${name}`);
                    process.exit(1);
                }
            }
        } else if (option.startsWith('-')) {
            // mtui doesn't use any -x=y or -x y format optionuments
            // -x will always just be a flag
            let name = option.slice(1);
            let descriptor = optionDescriptorMap[name];
            if (!descriptor) {
                if (handleUnknown) {
                    handleUnknown(option);
                } else {
                    console.error(`Unknown option name: ${name}`);
                    process.exit(1);
                }
                continue;
            }
            if (descriptor.alias) {
                name = descriptor.alias;
                descriptor = optionDescriptorMap[name];
            }
            if (descriptor.type === 'flag') {
                result[name] = true;
            } else {
                console.error(`Use --${name} (value) to specify ${name}`);
                process.exit(1);
            }
        } else if (handleDashless) {
            handleDashless(option);
        }
    }
    return result;
}

parseOptions.handleDashless = Symbol();
parseOptions.handleUnknown = Symbol();

module.exports.parseOptions = parseOptions;

// Cheap FP for a cheap dyke!
// I have no idea if this is what curry actually means.
module.exports.curry = f => x => (...args) => f(x, ...args);

module.exports.mapInPlace = (array, fn) => array.splice(0, array.length, ...array.map(fn));

module.exports.filterEmptyLines = string => string.split('\n').filter(line => line.trim()).join('\n');

module.exports.unique = arr => Array.from(new Set(arr));

const logColor = color => (literals, ...values) => {
    const w = s => process.stdout.write(s);
    w(`\x1b[${color}m`);
    for (let i = 0; i < literals.length; i++) {
        w(literals[i]);
        if (values[i] !== undefined) {
            w(`\x1b[1m`);
            w(String(values[i]));
            w(`\x1b[0;${color}m`);
        }
    }
    w(`\x1b[0m\n`);
};

module.exports.logWarn = logColor(33);
module.exports.logError = logColor(31);

module.exports.sortByName = (a, b) => {
    let an = a.name.toLowerCase();
    let bn = b.name.toLowerCase();
    if (an.startsWith('the ')) an = an.slice(4);
    if (bn.startsWith('the ')) bn = bn.slice(4);
    return an < bn ? -1 : an > bn ? 1 : 0;
};

module.exports.chunkByConditions = function(array, conditions) {
    if (array.length === 0) {
        return [];
    } else if (conditions.length === 0) {
        return [array];
    }

    const out = [];
    let cur = [array[0]];
    for (let i = 1; i < array.length; i++) {
        const item = array[i];
        const prev = array[i - 1];
        let chunk = false;
        for (const condition of conditions) {
            if (condition(item, prev)) {
                chunk = true;
                break;
            }
        }
        if (chunk) {
            out.push(cur);
            cur = [item];
        } else {
            cur.push(item);
        }
    }
    out.push(cur);
    return out;
};

module.exports.chunkByProperties = function(array, properties) {
    return module.exports.chunkByConditions(array, properties.map(p => (a, b) => a[p] !== b[p] || a[p] != b[p]))
        .map(chunk => ({
            ...Object.fromEntries(properties.map(p => [p, chunk[0][p]])),
            chunk
        }));
};