File size: 1,653 Bytes
bc20498 |
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 |
import { BIGINT, BOOLEAN, FUNCTION, NULL, NUMBER, OBJECT, STRING, SYMBOL, UNDEFINED } from './types.js';
const { isArray } = Array;
export { isArray };
export const invoke = value => /** @type {Function} */ (value)();
/**
* @template Value
* @param {string} type
* @param {Value} value
* @returns {Value}
*/
export const reviver = (type, value) => value;
/**
* @template V
* @typedef {[V]} Arr
*/
/**
* @template V
* @typedef {() => V} Ctx
*/
/**
* @template T, V
* @typedef {{t:T, v:V}} Obj
*/
/**
* @template V
* @typedef {V extends bigint ? BIGINT : V extends boolean ? BOOLEAN : V extends null ? NULL : V extends number ? NUMBER : V extends string ? STRING : V extends symbol ? SYMBOL : V extends undefined ? UNDEFINED : V extends object ? OBJECT : never} TypeOf
*/
/**
* @template T, V
* @param {T} t
* @param {V} v
* @returns {Obj<T, V>}
*/
export const obj = (t, v) => ({t, v});
/**
* @template V
* @param {V} value
* @returns {Ctx<V>}
*/
export const bound = value => Context.bind(value);
/**
* @template V, T
* @param {V} value
* @returns {V extends Ctx<T> ? ReturnType<V> : V}
*/
export const unbound = value => (
typeof value === FUNCTION ? invoke(value) : value
);
// This is needed to unlock *both* apply and construct
// traps otherwise one of these might fail.
// The 'use strict' directive is needed to allow
// also primitive types to be bound.
function Context() {
'use strict';
return this;
}
// TODO: is this really needed in here?
// const { hasOwn } = Object;
// const isConstructable = value => hasOwn(value, 'prototype');
// const isFunction = value => typeof value === FUNCTION;
|