UNPKG

reflected-ffi

Version:

A remotely reflected Foreign Function Interface

38 lines (34 loc) 934 B
/** @type {Map<symbol, string>} */ const symbols = new Map( Reflect.ownKeys(Symbol).map( key => [Symbol[key], `@${String(key)}`] ) ); /** * @param {symbol} value * @param {string} description * @returns {string} */ const asSymbol = (value, description) => ( description === void 0 ? '?' : (Symbol.keyFor(value) === void 0 ? `!${description}` : `#${description}`) ); /** * Extract the value from a pair of type and value. * @param {string} name * @returns {symbol} */ export const fromSymbol = name => { switch (name[0]) { case '@': return Symbol[name.slice(1)]; case '#': return Symbol.for(name.slice(1)); case '!': return Symbol(name.slice(1)); default: return Symbol(); } }; /** * Create the name of a symbol. * @param {symbol} value * @returns {string} */ export const toSymbol = value => symbols.get(value) || asSymbol(value, value.description);