跳转到内容
这是一个实验性功能。

范围

说明

导入

import { Range } from "@tai-kun/surrealdb/decodeonly-datatypes";
import { Range } from "@tai-kun/surrealdb/encodable-datatypes";
import { Range } from "@tai-kun/surrealdb/standard-datatypes";

.constructor()

创建一个新的 Range 对象。

new Range(
source: [
begin: BoundIncluded | BoundExcluded | null,
end: BoundIncluded | BoundExcluded | null,
],
);

参数

source

范围的最小值和最大值的配对。如果是无限的,则为 null

返回值

如果通过 new 调用,Range 将返回其实例。

.begin 实例 属性 >=decode-only

范围的最小值。如果没有下限,则为 null

.begin: BoundIncluded | BoundExcluded | null;

.end 实例 属性 >=decode-only

范围的最大值。如果没有上限,则为 null

.end: BoundIncluded | BoundExcluded | null;

.toString() 实例 方法 >=encodable

Range 对象转换为 SurrealQL 可用的字符串。

.toJSON().toSurql() 将返回与该方法相同的结果。

.toString(): string;

参数

无。

返回值

返回范围的字符串表示。

例子

import {
Range,
BoundIncluded,
BoundExcluded,
} from "@tai-kun/surrealdb/encodable-datatypes";
const range = new Range([new BoundIncluded(1), new BoundIncluded(3)]);
console.log(range.toString());
// -> 1..=3
const range = new Range([new BoundExcluded(1), new BoundExcluded(3)]);
console.log(range.toString());
// -> 1>..3
const range = new Range([new BoundIncluded(1), new BoundExcluded(3)]);
console.log(range.toString());
// -> 1..3
const range = new Range([new BoundIncluded(1), null]);
console.log(range.toString());
// -> 1..
const range = new Range([null, new BoundExcluded(3)]);
console.log(range.toString());
// -> ..3
const range = new Range([new BoundExcluded(1), new BoundIncluded(3)]);
console.log(range.toString());
// -> 1>..=3
const range = new Range([new BoundExcluded(1), null]);
console.log(range.toString());
// -> 1>..
const range = new Range([null, new BoundIncluded(3)]);
console.log(range.toString());
// -> ..=3
const range = new Range([null, null]);
console.log(range.toString());
// -> ..

进阶

Range 对象的通用判定

建议使用 isRange 函数来判断值是否为 Range 对象,而无需依赖预设。由于此函数不使用 instanceof,因此在验证值的预设不明确的情况下非常有用。

import { isRange } from "@tai-kun/surrealdb";