Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 10x 10x 10x 10x 20x 20x 12x 12x 8x 4x 4x 4x 4x 2x 10x 5x 18x 7x 7x 7x 7x 7x 7x 23x 23x 7x | /** * A export function class that helps with arrays. */ import * as mathf from '../mathf/mathf'; /** * A deep copy that works with objects or arrays. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function deepCopy( // eslint-disable-next-line @typescript-eslint/no-explicit-any input: Array<any> | [key: string] // eslint-disable-next-line @typescript-eslint/no-explicit-any ): Array<any> | Object { let value, key; if (typeof input !== 'object' || input === null) { return input; } // eslint-disable-next-line @typescript-eslint/no-explicit-any const output: Record<string, any> = Array.isArray(input) ? [] : {}; for (key in input) { value = input[key]; output[key] = typeof value === 'object' && value !== null ? deepCopy(value) : value; } return output; } /** * Return value from array that generates the highest return value when * passed as a parameter to the score function. * * Multiple score functions can be passed with later functions used only to * break ties for values returned from the previous score function. * * Example: * ``` * const values = [{'a': 2, 'b': 3}, {'a': 1, 'b': 4}]; * arrayf.max(values, (x) => x.a); // Returns {'a': 2, 'b': 3} * arrayf.max(values, (x) => x.b); // Returns {'a': 1, 'b': 4} * ``` */ export function max<T>(values: T[], ...scoreFns: Array<(v: T) => number>): T { let maxValue: T | null = null; let maxScore = Number.NEGATIVE_INFINITY; const scoreFn = scoreFns[0]; values.forEach((value) => { const score = scoreFn(value); if (maxScore < score) { maxValue = value; maxScore = score; } else if (maxScore === score && scoreFns.length > 1) { let i = 1; let tieBreaker = scoreFns[i]; while ( i < scoreFns.length && tieBreaker(<T>maxValue) === tieBreaker(value) ) { tieBreaker = scoreFns[i++]; } if (tieBreaker(<T>maxValue) < tieBreaker(value)) { maxValue = value; } } }); return maxValue!; } /** * Return value from array that generates the lowest return value when * passed as a parameter to the score function. * * Multiple score functions can be passed with later functions used only to * break ties for values returned from the previous score function. * * Example: * ``` * const values = [{'a': 2, 'b': 3}, {'a': 1, 'b': 4}]; * arrayf.min(values, (x) => x.a); // Returns {'a': 1, 'b': 4} * arrayf.min(values, (x) => x.b); // Returns {'a': 2, 'b': 3} * ``` */ export function min<T>(values: T[], ...scoreFns: Array<(v: T) => number>): T { return max( values, ...scoreFns.map((scoreFn) => { return (v: T) => -1 * scoreFn(v); }) ); } /** * Operates as per the built in slice with the option to wrap around from * the end of the array to the start and vice versa. Can also slice in the * reverse direction if given a negative value for `direction`. * @param values * @param startIndex * @param rawEndIndex * @param direction */ export function loopSlice<T>( values: T[], startIndex: number, rawEndIndex: number, direction = 1 ): T[] { const result: T[] = []; const length = values.length; const increment = Math.sign(direction); const endIndex = mathf.wrap(rawEndIndex, 0, length); let index = mathf.wrap(startIndex, 0, length); while (index !== endIndex) { result.push(values[index]); index = mathf.wrap(index + increment, 0, length); } return result; } |