-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmin.ts
41 lines (32 loc) · 937 Bytes
/
min.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 Min<T> {
min(): T | undefined;
}
export const min: PrototypeStruct = {
label: 'min',
fn: function arrayMin<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 minTemp = Number.MAX_SAFE_INTEGER;
let minValue: 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 < minTemp) {
minTemp = pt;
minValue = n;
continue;
}
} else if ('number' === nType && (n as unknown as number) < minTemp) {
minTemp = minValue = n as unknown as number;
}
}
return minValue as T;
},
};
declare global {
interface Array<T> extends Min<T> {}
}