From 72c9e5fd267d6770d03087e9d66c779852cdec69 Mon Sep 17 00:00:00 2001 From: BluJ Date: Mon, 27 Mar 2023 17:07:17 -0600 Subject: [PATCH] wip --- Makefile | 7 +++---- lib/config/builder/builder.ts | 4 +++- lib/config/builder/list.ts | 9 +++++++-- lib/config/builder/value.ts | 18 ++++++++++++------ lib/test/output.test.ts | 3 ++- lib/util/propertiesMatcher.ts | 36 ++++++++++++++++++++++++----------- package-lock.json | 20 ++----------------- package.json | 8 +++----- scripts/oldSpecToBuilder.ts | 17 ++++++++--------- tsconfig.json | 13 ++++++++----- 10 files changed, 73 insertions(+), 62 deletions(-) diff --git a/Makefile b/Makefile index c467290..12428f9 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,8 @@ make clean: lib/test/output.ts: lib/test/makeOutput.ts scripts/oldSpecToBuilder.ts npm run buildOutput -bundle: fmt $(TS_FILES) .FORCE node_modules - npx tsc-multi - npx tsc --emitDeclarationOnly +bundle: fmt $(TS_FILES) package.json .FORCE node_modules + npx tsc check: npm run check @@ -22,7 +21,7 @@ fmt: node_modules node_modules: package.json npm install -publish: bundle +publish: bundle package.json README.md LICENSE cp package.json dist/package.json cp README.md dist/README.md cp LICENSE dist/LICENSE diff --git a/lib/config/builder/builder.ts b/lib/config/builder/builder.ts index dbc844c..8843660 100644 --- a/lib/config/builder/builder.ts +++ b/lib/config/builder/builder.ts @@ -1,5 +1,7 @@ +import { typeFromProps } from "../../util"; + export class IBuilder { - protected constructor(readonly a: A) {} + protected constructor(readonly a: A) { } public build(): A { return this.a; diff --git a/lib/config/builder/list.ts b/lib/config/builder/list.ts index 37fccad..7803d66 100644 --- a/lib/config/builder/list.ts +++ b/lib/config/builder/list.ts @@ -7,6 +7,7 @@ import { ValueSpecList, ValueSpecListOf, } from "../config-types"; +import { guardAll, typeFromProps } from "../../util"; /** * Used as a subtype of Value.list @@ -39,7 +40,7 @@ export class List extends IBuilder { default: string[]; range: string; spec: { - masked: boolean | null; + masked: boolean; placeholder: string | null; pattern: string | null; patternDescription: string | null; @@ -51,7 +52,7 @@ export class List extends IBuilder { type: "list" as const, subtype: "string" as const, ...a, - } as ValueSpecListOf<"string">); + }); } static number< A extends { @@ -143,4 +144,8 @@ export class List extends IBuilder { ...value, }); } + + public validator() { + return guardAll(this.a); + } } diff --git a/lib/config/builder/value.ts b/lib/config/builder/value.ts index 9f62f86..763c425 100644 --- a/lib/config/builder/value.ts +++ b/lib/config/builder/value.ts @@ -7,15 +7,17 @@ import { ValueSpec, ValueSpecList, ValueSpecNumber, + ValueSpecOf, ValueSpecString, } from "../config-types"; +import { guardAll } from "../../util"; export type DefaultString = | string | { - charset: string | null | undefined; - len: number; - }; + charset: string | null | undefined; + len: number; + }; /** * A value is going to be part of the form in the FE of the OS. @@ -98,8 +100,9 @@ export class Value extends IBuilder { warning: string | null; nullable: boolean; default: string | null; - values: Record; - } + values: B; + }, + B extends Record >(a: A) { return new Value({ type: "select" as const, @@ -156,7 +159,10 @@ export class Value extends IBuilder { }); } - static list>(a: A) { + static list(a: List) { return new Value(a.build()); } + public validator() { + return guardAll(this.a); + } } diff --git a/lib/test/output.test.ts b/lib/test/output.test.ts index 4705099..8805ced 100644 --- a/lib/test/output.test.ts +++ b/lib/test/output.test.ts @@ -10,7 +10,8 @@ testOutput()(null); testOutput()(null); testOutput()(null); -// testOutput()(null); +testOutput()(null); +testOutput()(null); describe("Inputs", () => { test("test", () => { expect(true).toEqual(true); diff --git a/lib/util/propertiesMatcher.ts b/lib/util/propertiesMatcher.ts index a505dfc..2c13c02 100644 --- a/lib/util/propertiesMatcher.ts +++ b/lib/util/propertiesMatcher.ts @@ -42,21 +42,35 @@ type GuardObject = { _error: "Invalid Spec" } ) : unknown - // prettier-ignore export type GuardList = - A extends { readonly type: TypeList, readonly subtype: infer B, spec?: { spec?: infer C } } ? ReadonlyArray & ({ type: B, spec: C })>> : + A extends { readonly type: TypeList, readonly subtype: infer B, spec?: { spec?: infer C } } ? ReadonlyArray & ({ type: B, spec: C })>> : A extends { readonly type: TypeList, readonly subtype: infer B, spec?: {} } ? ReadonlyArray & ({ type: B })>> : unknown // prettier-ignore type GuardSelect = - A extends { readonly type: TypeSelect, variants: { [key in infer B & string]: string } } ? B : - unknown -// prettier-ignore -type GuardMultiselect = - A extends { readonly type: TypeMultiselect, variants: { [key in infer B & string]: string } } ? B[] : + A extends { readonly type: TypeSelect, variants: infer B } ? ( + B extends Record ? keyof B : never + ) : unknown +const bluj: GuardSelect<{ + type: "select"; +} & { + readonly name: "Serialization Version"; + readonly description: "Return raw transaction or block hex with Segwit or non-SegWit serialization."; + readonly warning: null; + readonly default: "segwit"; + readonly nullable: false; + readonly values: { + ...; + }; +}> + // prettier-ignore + type GuardMultiselect = + A extends { readonly type: TypeMultiselect, variants: { [key in infer B & string]: string } } ?B[] : +unknown + // prettier-ignore type VariantValue = A extends { name: string, spec: infer B } ? { name: A['name'], spec: TypeFromProps } : @@ -158,13 +172,13 @@ export function matchNumberWithRange(range: string) { leftValue === "*" ? (_) => true : left === "[" - ? (x) => x >= Number(leftValue) - : (x) => x > Number(leftValue), + ? (x) => x >= Number(leftValue) + : (x) => x > Number(leftValue), leftValue === "*" ? "any" : left === "[" - ? `greaterThanOrEqualTo${leftValue}` - : `greaterThan${leftValue}` + ? `greaterThanOrEqualTo${leftValue}` + : `greaterThan${leftValue}` ) .validate( // prettier-ignore diff --git a/package-lock.json b/package-lock.json index 981ae38..1a5dd9c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "start-sdk", - "version": "0.4.0-alpha10", + "version": "0.4.0-lib0.beta1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "start-sdk", - "version": "0.4.0-alpha10", + "version": "0.4.0-lib0.beta1", "license": "MIT", "dependencies": { "@iarna/toml": "^2.2.5", @@ -20,7 +20,6 @@ "jest": "^29.4.3", "ts-jest": "^29.0.5", "ts-node": "^10.9.1", - "tsc": "^2.0.4", "tsc-multi": "^0.6.1", "tsconfig-paths": "^3.14.2", "typescript": "^4.9.5", @@ -5042,15 +5041,6 @@ "node": ">=0.3.1" } }, - "node_modules/tsc": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/tsc/-/tsc-2.0.4.tgz", - "integrity": "sha512-fzoSieZI5KKJVBYGvwbVZs/J5za84f2lSTLPYf6AGiIf43tZ3GNrI1QzTLcjtyDDP4aLxd46RTZq1nQxe7+k5Q==", - "dev": true, - "bin": { - "tsc": "bin/tsc" - } - }, "node_modules/tsc-multi": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/tsc-multi/-/tsc-multi-0.6.1.tgz", @@ -9399,12 +9389,6 @@ } } }, - "tsc": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/tsc/-/tsc-2.0.4.tgz", - "integrity": "sha512-fzoSieZI5KKJVBYGvwbVZs/J5za84f2lSTLPYf6AGiIf43tZ3GNrI1QzTLcjtyDDP4aLxd46RTZq1nQxe7+k5Q==", - "dev": true - }, "tsc-multi": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/tsc-multi/-/tsc-multi-0.6.1.tgz", diff --git a/package.json b/package.json index 4f020d5..519b12d 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,9 @@ { "name": "start-sdk", - "version": "0.4.0-alpha10", + "version": "0.4.0-lib0.beta1", "description": "For making the patterns that are wanted in making services for the startOS.", - "main": "./index.cjs", - "types": "./index.d.ts", - "module": "./index.mjs", + "main": "./lib/index.js", + "types": "./lib/index.d.ts", "scripts": { "test": "jest -c ./jest.config.js", "buildOutput": "ts-node --esm ./lib/test/makeOutput.ts", @@ -32,7 +31,6 @@ "jest": "^29.4.3", "ts-jest": "^29.0.5", "ts-node": "^10.9.1", - "tsc": "^2.0.4", "tsc-multi": "^0.6.1", "tsconfig-paths": "^3.14.2", "typescript": "^4.9.5", diff --git a/scripts/oldSpecToBuilder.ts b/scripts/oldSpecToBuilder.ts index 6817b3f..dd7325e 100644 --- a/scripts/oldSpecToBuilder.ts +++ b/scripts/oldSpecToBuilder.ts @@ -98,9 +98,9 @@ export default async function makeFileContent( )})`; } case "enum": { - const allValueNames = new Set( - ...(value?.spec?.["values"] || []), - ...Object.keys(value?.spec?.["value-names"] || {}) + const allValueNames = new Set([ + ...(value?.["values"] || []), + ...Object.keys(value?.["value-names"] || {})] ); const values = Object.fromEntries( Array.from(allValueNames) @@ -118,7 +118,7 @@ export default async function makeFileContent( }, null, 2 - )})`; + )} as const)`; } case "object": { const specName = newConst( @@ -255,8 +255,8 @@ export default async function makeFileContent( spec: { variants: ${variants}, displayAs: ${JSON.stringify( - value?.spec?.["display-as"] || null - )}, + value?.spec?.["display-as"] || null + )}, uniqueBy: ${JSON.stringify(value?.spec?.["unique-by"] || null)}, default: ${JSON.stringify(value?.spec?.default || null)}, }, @@ -277,9 +277,8 @@ export default async function makeFileContent( let answer = "Variants.of({"; for (const [key, value] of Object.entries(variants)) { const variantSpec = newConst(key, convertInputSpec(value)); - answer += `"${key}": {name: "${ - variantNames[key] || key - }", spec: ${variantSpec}},`; + answer += `"${key}": {name: "${variantNames[key] || key + }", spec: ${variantSpec}},`; } return `${answer}})`; } diff --git a/tsconfig.json b/tsconfig.json index 36082f8..ac07a10 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,11 @@ { - "include": ["./lib/**/*.ts", "scripts/oldSpecToBuilder.ts"], - "inputs": ["./lib/index.ts"], + "include": [ + "./lib/**/*.ts", + "scripts/oldSpecToBuilder.ts" + ], + "inputs": [ + "./lib/index.ts" + ], "compilerOptions": { "target": "es2022", "module": "es2022", @@ -9,9 +14,7 @@ "outDir": "./dist/", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, - "strict": true, - "skipLibCheck": true }, "ts-node": { @@ -19,4 +22,4 @@ "module": "commonjs" } } -} +} \ No newline at end of file