refactor(jslib): specific constant for any instead of undefined
parent
58b90e1650
commit
826aa26198
|
@ -1,27 +1,28 @@
|
|||
import type { Address } from "./types";
|
||||
import { isAddress } from "./types";
|
||||
|
||||
type OneOrMany<T> = T | T[];
|
||||
export const Any = "?";
|
||||
type QueryPart<T> = T | T[] | typeof Any;
|
||||
|
||||
export class Query {
|
||||
private _query: string | undefined;
|
||||
|
||||
public matches(
|
||||
entity: OneOrMany<string> | undefined,
|
||||
attribute: OneOrMany<string> | undefined,
|
||||
value: OneOrMany<string | number | Address> | undefined
|
||||
public static matches(
|
||||
entity: QueryPart<string>,
|
||||
attribute: QueryPart<string>,
|
||||
value: QueryPart<string | number | Address>
|
||||
): Query {
|
||||
const query = new Query();
|
||||
|
||||
let entityStr;
|
||||
if (entity === undefined) {
|
||||
if (entity === Any) {
|
||||
entityStr = "?";
|
||||
} else {
|
||||
entityStr = Array.isArray(entity) ? `(in ${entity.join(" ")})` : entity;
|
||||
}
|
||||
|
||||
let attributeStr;
|
||||
if (attribute === undefined) {
|
||||
if (attribute === Any) {
|
||||
attributeStr = "?";
|
||||
} else {
|
||||
attributeStr = Array.isArray(attribute)
|
||||
|
@ -30,7 +31,7 @@ export class Query {
|
|||
}
|
||||
|
||||
let valueStr;
|
||||
if (value === undefined) {
|
||||
if (value === Any) {
|
||||
valueStr = "?";
|
||||
} else {
|
||||
valueStr = (Array.isArray(value) ? value : [value])
|
||||
|
|
|
@ -1,26 +1,23 @@
|
|||
import test from "ava";
|
||||
import { Query } from "./query";
|
||||
import { Any, Query } from "./query";
|
||||
|
||||
test("query matches simple", (t) => {
|
||||
const query = new Query().matches("entity", "attribute", "value");
|
||||
const query = Query.matches("entity", "attribute", "value");
|
||||
t.is(query.toString(), '(matches entity "attribute" "value")');
|
||||
});
|
||||
|
||||
test("query matches anything", (t) => {
|
||||
const query = new Query().matches(undefined, undefined, undefined);
|
||||
const query = Query.matches(Any, Any, Any);
|
||||
t.is(query.toString(), "(matches ? ? ?)");
|
||||
});
|
||||
|
||||
test("query matches array", (t) => {
|
||||
const query = new Query().matches("entity", "attribute", [
|
||||
"value1",
|
||||
"value2",
|
||||
]);
|
||||
const query = Query.matches("entity", "attribute", ["value1", "value2"]);
|
||||
t.is(query.toString(), '(matches entity "attribute" (in "value1" "value2"))');
|
||||
});
|
||||
|
||||
test("query matches addresses", (t) => {
|
||||
const query = new Query().matches("entity", "attribute", [
|
||||
const query = Query.matches("entity", "attribute", [
|
||||
"@address1",
|
||||
"@address2",
|
||||
]);
|
||||
|
@ -31,6 +28,6 @@ test("query matches addresses", (t) => {
|
|||
});
|
||||
|
||||
test("query matches numbers", (t) => {
|
||||
const query = new Query().matches("entity", "attribute", [1, 2]);
|
||||
const query = Query.matches("entity", "attribute", [1, 2]);
|
||||
t.is(query.toString(), '(matches entity "attribute" (in 1 2))');
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue