UNPKG

@bscotch/gamemaker-releases

Version:

Utility for combining GameMaker release data into a single feed.

44 lines 1.36 kB
import { assert } from '@bscotch/utility/browser'; import { decode } from 'entities'; import { z } from 'zod'; export function findMax(items, propOrFunc) { assert(items.length, 'Cannot find max of empty array'); const score = (item, idx) => propOrFunc === undefined ? item : typeof propOrFunc === 'function' ? propOrFunc(item, idx, items) : item[propOrFunc]; let maxScore = score(items[0], 0); let maxIdx = 0; for (let i = 1; i < items.length; i++) { const itemScore = score(items[i], i); if (itemScore > maxScore) { maxScore = itemScore; maxIdx = i; } } return items[maxIdx]; } export function htmlString() { return z.string().transform((s) => { if (!s || typeof s !== 'string') { return s; } // decode HTML entities s = decode(s.trim()); // remove anchor targets s = s.replace(/\btarget=[^\s>]+/g, ''); // remove excess space s = s.replace(/ +/g, ' '); s = s.replace(/\r/g, ''); s = s.replace(/[\t ]*\n[\t ]*(\n[\t ]*)+/g, '\n\n'); return s; }); } export function countNonUnique(arr) { return arr.length - new Set(arr).size; } export function isError(thing) { return thing instanceof Error; } //# sourceMappingURL=utils.js.map