opusdev/vector-similarity-api
1
1const TypedArrayPrototypeGetSymbolToStringTag = (() => {2 // Type check system lovingly referenced from:3 // https://github.com/nodejs/node/blob/7450332339ed40481f470df2a3014e2ec355d8d8/lib/internal/util/types.js#L13-L154 // eslint-disable-next-line @typescript-eslint/unbound-method -- the intention is to call this method with a bound value5 const g = Object.getOwnPropertyDescriptor(6 Object.getPrototypeOf(Uint8Array.prototype),7 Symbol.toStringTag8 )!.get!;9 10 return (value: unknown) => g.call(value);11})();12 13export function isUint8Array(value: unknown): value is Uint8Array {14 return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';15}16 17export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {18 return (19 typeof value === 'object' &&20 value != null &&21 Symbol.toStringTag in value &&22 (value[Symbol.toStringTag] === 'ArrayBuffer' ||23 value[Symbol.toStringTag] === 'SharedArrayBuffer')24 );25}26 27export function isRegExp(regexp: unknown): regexp is RegExp {28 return regexp instanceof RegExp || Object.prototype.toString.call(regexp) === '[object RegExp]';29}30 31export function isMap(value: unknown): value is Map<unknown, unknown> {32 return (33 typeof value === 'object' &&34 value != null &&35 Symbol.toStringTag in value &&36 value[Symbol.toStringTag] === 'Map'37 );38}39 40export function isDate(date: unknown): date is Date {41 return date instanceof Date || Object.prototype.toString.call(date) === '[object Date]';42}43 44export type InspectFn = (x: unknown, options?: unknown) => string;45export function defaultInspect(x: unknown, _options?: unknown): string {46 return JSON.stringify(x, (k: string, v: unknown) => {47 if (typeof v === 'bigint') {48 return { $numberLong: `${v}` };49 } else if (isMap(v)) {50 return Object.fromEntries(v);51 }52 return v;53 });54}55 56/** @internal */57type StylizeFunction = (x: string, style: string) => string;58/** @internal */59export function getStylizeFunction(options?: unknown): StylizeFunction | undefined {60 const stylizeExists =61 options != null &&62 typeof options === 'object' &&63 'stylize' in options &&64 typeof options.stylize === 'function';65 66 if (stylizeExists) {67 return options.stylize as StylizeFunction;68 }69}70 