CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
code.ts70 linesDownload Raw Back to src
1import type { Document } from './bson';2import { BSONValue } from './bson_value';3import { type InspectFn, defaultInspect } from './parser/utils';4 5/** @public */6export interface CodeExtended {7  $code: string;8  $scope?: Document;9}10 11/**12 * A class representation of the BSON Code type.13 * @public14 * @category BSONType15 */16export class Code extends BSONValue {17  get _bsontype(): 'Code' {18    return 'Code';19  }20 21  code: string;22 23  // a code instance having a null scope is what determines whether24  // it is BSONType 0x0D (just code) / 0x0F (code with scope)25  scope: Document | null;26 27  /**28   * @param code - a string or function.29   * @param scope - an optional scope for the function.30   */31  constructor(code: string | Function, scope?: Document | null) {32    super();33    this.code = code.toString();34    this.scope = scope ?? null;35  }36 37  toJSON(): { code: string; scope?: Document } {38    if (this.scope != null) {39      return { code: this.code, scope: this.scope };40    }41 42    return { code: this.code };43  }44 45  /** @internal */46  toExtendedJSON(): CodeExtended {47    if (this.scope) {48      return { $code: this.code, $scope: this.scope };49    }50 51    return { $code: this.code };52  }53 54  /** @internal */55  static fromExtendedJSON(doc: CodeExtended): Code {56    return new Code(doc.$code, doc.$scope);57  }58 59  inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {60    inspect ??= defaultInspect;61    let parametersString = inspect(this.code, options);62    const multiLineFn = parametersString.includes('\n');63    if (this.scope != null) {64      parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;65    }66    const endingNewline = multiLineFn && this.scope === null;67    return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;68  }69}70