CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
index.js69 linesDownload Raw Back to side-channel-map
1'use strict';2 3var GetIntrinsic = require('get-intrinsic');4var callBound = require('call-bound');5var inspect = require('object-inspect');6 7var $TypeError = require('es-errors/type');8var $Map = GetIntrinsic('%Map%', true);9 10/** @type {<K, V>(thisArg: Map<K, V>, key: K) => V} */11var $mapGet = callBound('Map.prototype.get', true);12/** @type {<K, V>(thisArg: Map<K, V>, key: K, value: V) => void} */13var $mapSet = callBound('Map.prototype.set', true);14/** @type {<K, V>(thisArg: Map<K, V>, key: K) => boolean} */15var $mapHas = callBound('Map.prototype.has', true);16/** @type {<K, V>(thisArg: Map<K, V>, key: K) => boolean} */17var $mapDelete = callBound('Map.prototype.delete', true);18/** @type {<K, V>(thisArg: Map<K, V>) => number} */19var $mapSize = callBound('Map.prototype.size', true);20 21/** @type {import('.')} */22module.exports = !!$Map && /** @type {Exclude<import('.'), false>} */ function getSideChannelMap() {23	/** @typedef {ReturnType<typeof getSideChannelMap>} Channel */24	/** @typedef {Parameters<Channel['get']>[0]} K */25	/** @typedef {Parameters<Channel['set']>[1]} V */26 27	/** @type {Map<K, V> | undefined} */ var $m;28 29	/** @type {Channel} */30	var channel = {31		assert: function (key) {32			if (!channel.has(key)) {33				throw new $TypeError('Side channel does not contain ' + inspect(key));34			}35		},36		'delete': function (key) {37			if ($m) {38				var result = $mapDelete($m, key);39				if ($mapSize($m) === 0) {40					$m = void undefined;41				}42				return result;43			}44			return false;45		},46		get: function (key) { // eslint-disable-line consistent-return47			if ($m) {48				return $mapGet($m, key);49			}50		},51		has: function (key) {52			if ($m) {53				return $mapHas($m, key);54			}55			return false;56		},57		set: function (key, value) {58			if (!$m) {59				// @ts-expect-error TS can't handle narrowing a variable inside a closure60				$m = new $Map();61			}62			$mapSet($m, key, value);63		}64	};65 66	// @ts-expect-error TODO: figure out why TS is erroring here67	return channel;68};69