This commit is contained in:
BluJ
2023-03-27 17:07:17 -06:00
parent ce0170b808
commit 72c9e5fd26
10 changed files with 73 additions and 62 deletions

View File

@@ -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

View File

@@ -1,5 +1,7 @@
import { typeFromProps } from "../../util";
export class IBuilder<A> {
protected constructor(readonly a: A) {}
protected constructor(readonly a: A) { }
public build(): A {
return this.a;

View File

@@ -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<A extends ValueSpecList> extends IBuilder<A> {
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<A extends ValueSpecList> extends IBuilder<A> {
type: "list" as const,
subtype: "string" as const,
...a,
} as ValueSpecListOf<"string">);
});
}
static number<
A extends {
@@ -143,4 +144,8 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
...value,
});
}
public validator() {
return guardAll(this.a);
}
}

View File

@@ -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<A extends ValueSpec> extends IBuilder<A> {
warning: string | null;
nullable: boolean;
default: string | null;
values: Record<string, string>;
}
values: B;
},
B extends Record<string, string>
>(a: A) {
return new Value({
type: "select" as const,
@@ -156,7 +159,10 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
});
}
static list<A extends List<ValueSpecList>>(a: A) {
static list<A extends ValueSpecList>(a: List<A>) {
return new Value(a.build());
}
public validator() {
return guardAll(this.a);
}
}

View File

@@ -10,7 +10,8 @@ testOutput<InputSpec["rpc"]["enable"], string>()(null);
testOutput<InputSpec["rpc"]["enable"], boolean>()(null);
testOutput<InputSpec["rpc"]["username"], string>()(null);
// testOutput<InputSpec["rpc"]["advanced"]["auth"], string[]>()(null);
testOutput<InputSpec["rpc"]["advanced"]["auth"], readonly string[]>()(null);
testOutput<InputSpec["rpc"]["advanced"]["serialversion"], readonly string[]>()(null);
describe("Inputs", () => {
test("test", () => {
expect(true).toEqual(true);

View File

@@ -42,21 +42,35 @@ type GuardObject<A> =
{ _error: "Invalid Spec" }
) :
unknown
// prettier-ignore
export type GuardList<A> =
A extends { readonly type: TypeList, readonly subtype: infer B, spec?: { spec?: infer C } } ? ReadonlyArray<GuardAll<Omit<A, "type" | "spec"> & ({ type: B, spec: C })>> :
A extends { readonly type: TypeList, readonly subtype: infer B, spec?: { spec?: infer C } } ? ReadonlyArray<GuardAll<Omit<A, "type" | "subtype" | "spec"> & ({ type: B, spec: C })>> :
A extends { readonly type: TypeList, readonly subtype: infer B, spec?: {} } ? ReadonlyArray<GuardAll<Omit<A, "type"> & ({ type: B })>> :
unknown
// prettier-ignore
type GuardSelect<A> =
A extends { readonly type: TypeSelect, variants: { [key in infer B & string]: string } } ? B :
unknown
// prettier-ignore
type GuardMultiselect<A> =
A extends { readonly type: TypeMultiselect, variants: { [key in infer B & string]: string } } ? B[] :
A extends { readonly type: TypeSelect, variants: infer B } ? (
B extends Record<string, string> ? 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> =
A extends { readonly type: TypeMultiselect, variants: { [key in infer B & string]: string } } ?B[] :
unknown
// prettier-ignore
type VariantValue<A> =
A extends { name: string, spec: infer B } ? { name: A['name'], spec: TypeFromProps<B> } :
@@ -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

20
package-lock.json generated
View File

@@ -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",

View File

@@ -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",

View File

@@ -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}})`;
}

View File

@@ -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"
}
}
}
}