opusdev/vector-similarity-api
1
1import { BSONValue } from './bson_value';2import { BSON_INT32_MAX, BSON_INT32_MIN } from './constants';3import { BSONError } from './error';4import type { EJSONOptions } from './extended_json';5import { type InspectFn, defaultInspect } from './parser/utils';6import { removeLeadingZerosAndExplicitPlus } from './utils/string_utils';7 8/** @public */9export interface Int32Extended {10 $numberInt: string;11}12 13/**14 * A class representation of a BSON Int32 type.15 * @public16 * @category BSONType17 */18export class Int32 extends BSONValue {19 get _bsontype(): 'Int32' {20 return 'Int32';21 }22 23 value!: number;24 /**25 * Create an Int32 type26 *27 * @param value - the number we want to represent as an int32.28 */29 constructor(value: number | string) {30 super();31 if ((value as unknown) instanceof Number) {32 value = value.valueOf();33 }34 35 this.value = +value | 0;36 }37 38 /**39 * Attempt to create an Int32 type from string.40 *41 * This method will throw a BSONError on any string input that is not representable as an Int32.42 * Notably, this method will also throw on the following string formats:43 * - Strings in non-decimal formats (exponent notation, binary, hex, or octal digits)44 * - Strings non-numeric and non-leading sign characters (ex: '2.0', '24,000')45 * - Strings with leading and/or trailing whitespace46 *47 * Strings with leading zeros, however, are allowed.48 *49 * @param value - the string we want to represent as an int32.50 */51 static fromString(value: string): Int32 {52 const cleanedValue = removeLeadingZerosAndExplicitPlus(value);53 54 const coercedValue = Number(value);55 56 if (BSON_INT32_MAX < coercedValue) {57 throw new BSONError(`Input: '${value}' is larger than the maximum value for Int32`);58 } else if (BSON_INT32_MIN > coercedValue) {59 throw new BSONError(`Input: '${value}' is smaller than the minimum value for Int32`);60 } else if (!Number.isSafeInteger(coercedValue)) {61 throw new BSONError(`Input: '${value}' is not a safe integer`);62 } else if (coercedValue.toString() !== cleanedValue) {63 // catch all case64 throw new BSONError(`Input: '${value}' is not a valid Int32 string`);65 }66 return new Int32(coercedValue);67 }68 69 /**70 * Access the number value.71 *72 * @returns returns the wrapped int32 number.73 */74 valueOf(): number {75 return this.value;76 }77 78 toString(radix?: number): string {79 return this.value.toString(radix);80 }81 82 toJSON(): number {83 return this.value;84 }85 86 /** @internal */87 toExtendedJSON(options?: EJSONOptions): number | Int32Extended {88 if (options && (options.relaxed || options.legacy)) return this.value;89 return { $numberInt: this.value.toString() };90 }91 92 /** @internal */93 static fromExtendedJSON(doc: Int32Extended, options?: EJSONOptions): number | Int32 {94 return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);95 }96 97 inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {98 inspect ??= defaultInspect;99 return `new Int32(${inspect(this.value, options)})`;100 }101}102 