From 826aa261984cbc044d4718fa45a992a30b1e0905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Sat, 11 Nov 2023 10:59:52 +0100 Subject: [PATCH] refactor(jslib): specific constant for any instead of undefined --- tools/upend_js/query.ts | 17 +++++++++-------- tools/upend_js/test.ts | 15 ++++++--------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/tools/upend_js/query.ts b/tools/upend_js/query.ts index dd1ae05..ba8e9cf 100644 --- a/tools/upend_js/query.ts +++ b/tools/upend_js/query.ts @@ -1,27 +1,28 @@ import type { Address } from "./types"; import { isAddress } from "./types"; -type OneOrMany = T | T[]; +export const Any = "?"; +type QueryPart = T | T[] | typeof Any; export class Query { private _query: string | undefined; - public matches( - entity: OneOrMany | undefined, - attribute: OneOrMany | undefined, - value: OneOrMany | undefined + public static matches( + entity: QueryPart, + attribute: QueryPart, + value: QueryPart ): 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]) diff --git a/tools/upend_js/test.ts b/tools/upend_js/test.ts index 60c0109..8bc05c3 100644 --- a/tools/upend_js/test.ts +++ b/tools/upend_js/test.ts @@ -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))'); });