Skip to content

Commit 3ec6768

Browse files
authored
Merge pull request #130 from ethdebug/packages
Use @ethdebug/format as home package for TypeScript types and type predicates
2 parents 316ecd1 + 59a5c38 commit 3ec6768

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1273
-47
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"devDependencies": {
1616
"@hyperjump/json-schema": "1.6.7",
1717
"@types/jest": "^29.5.14",
18-
"chalk": "^5.4.1",
1918
"concurrently": "^8.2.2",
2019
"jest": "^29.7.0",
2120
"lerna": "^8.0.2",

packages/format/jest.config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Config } from "jest";
2+
3+
const config: Config = {
4+
displayName: "@ethdebug/format",
5+
preset: "ts-jest",
6+
testEnvironment: "node",
7+
moduleFileExtensions: ["ts", "js"],
8+
moduleNameMapper: {
9+
'^(\\.{1,2}/.*)\\.js$': '$1',
10+
},
11+
modulePathIgnorePatterns: ["<rootDir>/dist/"],
12+
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
13+
transform: {
14+
'^.+\\.tsx?$': "ts-jest"
15+
},
16+
};
17+
18+
export default config;
File renamed without changes.

packages/format/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"prepare:yamls": "node ./bin/generate-schema-yamls.js > yamls.ts",
1313
"prepare": "yarn prepare:yamls && tsc",
1414
"clean": "rm -rf dist && rm yamls.ts",
15+
"test": "node --experimental-vm-modules $(yarn bin jest)",
1516
"watch:typescript": "tsc --watch",
1617
"watch:schemas": "nodemon --watch ../../schemas -e 'yaml' --exec 'yarn prepare:yamls'",
1718
"watch": "concurrently --names=tsc,schemas \"yarn watch:typescript\" \"yarn watch:schemas\""
@@ -21,8 +22,14 @@
2122
"yaml": "^2.3.4"
2223
},
2324
"devDependencies": {
25+
"@jest/globals": "^29.7.0",
26+
"chalk": "^4.1.0",
2427
"concurrently": "^8.2.2",
25-
"nodemon": "^3.0.2"
28+
"jest": "^29.7.0",
29+
"nodemon": "^3.0.2",
30+
"ts-jest": "^29.1.1",
31+
"ts-node": "^10.9.2",
32+
"typescript": "^5.3.3"
2633
},
2734
"publishConfig": {
2835
"access": "public"

packages/format/src/describe.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ export function describeSchema({
3535
}
3636

3737
return referencesId(schema)
38-
? describeSchemaById({ schema, pointer })
38+
? describeSchemaById({ schema, ...(pointer ? { pointer } : {}) })
3939
: referencesYaml(schema)
40-
? describeSchemaByYaml({ schema, pointer })
41-
: describeSchemaByObject({ schema, pointer });
40+
? describeSchemaByYaml({ schema, ...(pointer ? { pointer } : {}) })
41+
: describeSchemaByObject({ schema, ...(pointer ? { pointer } : {}) });
4242
}
4343

4444
function describeSchemaById({
@@ -65,7 +65,7 @@ function describeSchemaById({
6565

6666
return {
6767
id,
68-
pointer,
68+
...(pointer ? { pointer } : {}),
6969
yaml,
7070
schema,
7171
rootSchema
@@ -86,14 +86,14 @@ function describeSchemaByYaml({
8686
if (id) {
8787
return {
8888
id,
89-
pointer,
89+
...(pointer ? { pointer } : {}),
9090
yaml,
9191
schema,
9292
rootSchema
9393
}
9494
} else {
9595
return {
96-
pointer,
96+
...(pointer ? { pointer } : {}),
9797
yaml,
9898
schema,
9999
rootSchema
@@ -116,14 +116,14 @@ function describeSchemaByObject({
116116
if (id) {
117117
return {
118118
id,
119-
pointer,
119+
...(pointer ? { pointer } : {}),
120120
yaml,
121121
schema,
122122
rootSchema
123123
}
124124
} else {
125125
return {
126-
pointer,
126+
...(pointer ? { pointer } : {}),
127127
yaml,
128128
schema,
129129
rootSchema

packages/format/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from "./describe";
22
export { schemas, schemaIds, type Schema } from "./schemas";
3+
4+
export * from "./types";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect, describe, it } from "@jest/globals";
2+
3+
import { describeSchema } from "../../describe";
4+
5+
import { Data } from "./index.js";
6+
7+
describe("type guards", () => {
8+
const schemaGuards = [
9+
{
10+
schema: {
11+
id: "schema:ethdebug/format/data/value"
12+
},
13+
guard: Data.isValue
14+
},
15+
{
16+
schema: {
17+
id: "schema:ethdebug/format/data/unsigned"
18+
},
19+
guard: Data.isUnsigned
20+
},
21+
{
22+
schema: {
23+
id: "schema:ethdebug/format/data/hex"
24+
},
25+
guard: Data.isHex
26+
},
27+
] as const;
28+
29+
it.each(schemaGuards)("matches its examples", ({
30+
guard,
31+
...describeSchemaOptions
32+
}) => {
33+
const { schema: { examples = [] } } = describeSchema(describeSchemaOptions);
34+
35+
expect(guard).toSatisfyAll(examples);
36+
});
37+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export namespace Data {
2+
export type Value =
3+
| Unsigned
4+
| Hex;
5+
6+
export const isValue = (value: unknown): value is Value =>
7+
[isUnsigned, isHex].some(guard => guard(value));
8+
9+
export type Unsigned = number;
10+
11+
export const isUnsigned = (value: unknown): value is Unsigned =>
12+
typeof value === "number" && value >= 0;
13+
14+
export type Hex = string;
15+
16+
const hexPattern = new RegExp(/^0x[0-9a-fA-F]{1,}$/);
17+
18+
export const isHex = (value: unknown): value is Hex =>
19+
typeof value === "string" && hexPattern.test(value);
20+
}

packages/format/src/types/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from "./data";
2+
export * from "./materials";
3+
export * from "./type";
4+
export * from "./pointer";
5+
export * from "./program";

0 commit comments

Comments
 (0)