import type { Document } from "./bson.ts";export interface CodeExtended { $code: string | Function; $scope?: Document;}
export class Code { _bsontype = "Code"; code!: string | Function; scope?: Document; constructor(code: string | Function, scope?: Document) { this.code = code; this.scope = scope; }
toJSON(): { code: string | Function; scope?: Document } { return { code: this.code, scope: this.scope }; }
toExtendedJSON(): CodeExtended { if (this.scope) { return { $code: this.code, $scope: this.scope }; }
return { $code: this.code }; }
static fromExtendedJSON(doc: CodeExtended): Code { return new Code(doc.$code, doc.$scope); }
[Symbol.for("Deno.customInspect")](): string { const codeJson = this.toJSON(); return `new Code("${codeJson.code}"${ codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : "" })`; }}