File size: 1,821 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 75 76 77 78 79 80 81 82 83 84 85 86 |
'use strict';
const {
OBJECT,
FUNCTION,
BOOLEAN,
NUMBER,
STRING,
UNDEFINED,
BIGINT,
SYMBOL,
NULL
} = require('./types.js');
const {
defineProperty,
getOwnPropertyDescriptor,
getPrototypeOf,
isExtensible,
ownKeys,
preventExtensions,
set,
setPrototypeOf
} = Reflect;
const TypedArray = getPrototypeOf(Int8Array);
exports.TypedArray = TypedArray;
exports.defineProperty = defineProperty;
exports.getOwnPropertyDescriptor = getOwnPropertyDescriptor;
exports.getPrototypeOf = getPrototypeOf;
exports.isExtensible = isExtensible;
exports.ownKeys = ownKeys;
exports.preventExtensions = preventExtensions;
exports.set = set;
exports.setPrototypeOf = setPrototypeOf;
const augment = (descriptor, how) => {
const {get, set, value} = descriptor;
if (get) descriptor.get = how(get);
if (set) descriptor.set = how(set);
if (value) descriptor.value = how(value);
return descriptor;
};
exports.augment = augment;
const entry = (type, value) => [type, value];
exports.entry = entry;
const asEntry = transform => value => {
const type = typeof value;
switch (type) {
case OBJECT:
if (value == null)
return entry(NULL, value);
case FUNCTION:
return transform(type, value);
case BOOLEAN:
case NUMBER:
case STRING:
case UNDEFINED:
case BIGINT:
return entry(type, value);
case SYMBOL: {
if (symbols.has(value))
return entry(type, symbols.get(value));
}
}
throw new Error(`Unable to handle this ${type} type`);
};
exports.asEntry = asEntry;
const symbols = new Map(
ownKeys(Symbol)
.filter(s => typeof Symbol[s] === SYMBOL)
.map(s => [Symbol[s], s])
);
const symbol = value => {
for (const [symbol, name] of symbols) {
if (name === value)
return symbol;
}
};
exports.symbol = symbol;
|