-
Notifications
You must be signed in to change notification settings - Fork 1
/
max.ts
41 lines (32 loc) · 938 Bytes
/
max.ts
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
import type { PrototypeStruct } from '../index.js';
interface Max<T> {
max(): T | undefined;
}
export const max: PrototypeStruct = {
label: 'max',
fn: function arrayMax<T>(): T | undefined {
const ctx = this as unknown as T[];
const len = ctx.length;
if (!len) return void 0;
if (1 === len) return ctx[0];
let maxTemp = Number.NEGATIVE_INFINITY;
let maxValue: any;
for (const n of ctx) {
const nType = typeof(n);
if ('string' === nType && 1 === (n as unknown as string).length) {
const pt = (n as unknown as string).codePointAt(0)!;
if (pt > maxTemp) {
maxTemp = pt;
maxValue = n;
continue;
}
} else if ('number' === nType && (n as unknown as number) > maxTemp) {
maxTemp = maxValue = n as unknown as number;
}
}
return maxValue as T;
},
};
declare global {
interface Array<T> extends Max<T> {}
}