-
Notifications
You must be signed in to change notification settings - Fork 1
/
horizon.mjs
51 lines (44 loc) · 1.77 KB
/
horizon.mjs
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
// Object
Object.values = Object.values || (obj => Object.keys(obj).map(key => obj[key]));
// RegExp
RegExp.escape = RegExp.escape || (str => { //$& means the whole matched string
return String(str).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
// BigInt
BigInt.prototype.toJSON = BigInt.prototype.toJSON || function() {
return this.toString();
};
// Assert
import { default as asst } from 'assert';
const _assert = asst || ((val, err) => {
if (!val) { throw Error.isError(err) ? err : new Error(err); }
});
const _extErr = (er, status = 500, o = {}) => Object.assign(er, { status }, o);
if (!globalThis.assert) {
globalThis.assert = (value, message, status, options) => {
try { return _assert(value, message); }
catch (error) { throw _extErr(error, status, options); }
};
for (let i in _assert || {}) { assert[i] = _assert[i]; }
}
// Buffer
import { Buffer } from 'node:buffer';
globalThis.Buffer = globalThis.Buffer || Buffer;
// Is
const _is = (type, value) => value?.constructor === type;
const _type = (any) => typeof any === 'undefined' ? 'Undefined'
: Object.prototype.toString.call(any).replace(/^\[[^\ ]*\ (.*)\]$/, '$1');
[
ArrayBuffer, BigInt, Boolean, Error, Number, Object, Set, String, Uint8Array
].map(type => {
const name = `is${type.name}`;
type[name] = type[name] || (value => _is(type, value));
});
Date.isDate = Date.isDate || ((value, strict) => _is(Date, value) ? (
strict ? value.toTimeString().toLowerCase() !== 'invalid date' : true
) : false);
Function.isFunction = Function.isFunction
|| (value => ['Function', 'AsyncFunction'].includes(_type(value)));
// Print
globalThis.print = globalThis.print || console.log;
export default {};