Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tansongchen committed Jan 18, 2024
1 parent 7ec39ce commit a008ffc
Show file tree
Hide file tree
Showing 71 changed files with 305 additions and 297 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions spec/component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it } from "vitest";
import { listToObject } from "~/lib/utils";
import { listToObject } from "~/lib";
import rawrepertoire from "../public/cache/repertoire.json";
import type { PrimitveCharacter, PrimitiveRepertoire } from "~/lib/data";
import { determine } from "~/lib/repertoire";
import type { PrimitiveCharacter, PrimitiveRepertoire } from "~/lib";
import { determine } from "~/lib";

describe("recursive render component", () => {
const repertoire: PrimitiveRepertoire = listToObject(rawrepertoire);
Expand Down
20 changes: 10 additions & 10 deletions spec/degenerator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import _degenerate, {
import {
degenerate as _degenerate,
indicesToBinary,
binaryToIndices,
generateSliceBinaries,
defaultDegenerator,
} from "~/lib/degenerator";
} from "~/lib";
import { describe, it, expect } from "vitest";
import { create, all } from "mathjs";
import { computedGlyphs2 as computedGlyphs } from "./mock";
import { RenderedGlyph } from "~/lib/topology";
import { RenderedGlyph } from "~/lib";

const { randomInt } = create(all, {
const { randomInt } = create(all!, {
randomSeed: "a",
});

Expand Down Expand Up @@ -58,7 +58,7 @@ const hasroot = (a: RenderedGlyph, indices: number[], root: RenderedGlyph) => {

describe("degenerate cross tests", () => {
const {,,,,,,,,,,,,,,} =
computedGlyphs;
computedGlyphs as any;
it("says 天 has 大", () => {
expect(degenerate()).toEqual(degenerate(slice(, [1, 2, 3])));
});
Expand Down Expand Up @@ -88,8 +88,8 @@ describe("degenerate cross tests", () => {
});

describe("degenerate cross tests 2", () => {
const {,,,,} = computedGlyphs;
const 蒙下 = computedGlyphs["\ue0b3"];
const {,,,,} = computedGlyphs as any;
const 蒙下 = computedGlyphs["\ue0b3"]!;
const base = slice(, [1, 2, 3, 4, 5, 6]);
it("says 彖 has 豕下", () => {
hasroot(, [3, 4, 5, 6, 7, 8], base);
Expand All @@ -109,7 +109,7 @@ describe("degenerate cross tests 2", () => {
});

describe("degenerate cross tests 3", () => {
const {,,} = computedGlyphs;
const {,,} = computedGlyphs as any;
const base = slice(, [5, 6, 7]);
it("says 承 has 水", () => {
hasroot(, [5, 6, 7], base);
Expand All @@ -123,7 +123,7 @@ describe("degenerate cross tests 3", () => {
});

describe("degenerate cross tests 4", () => {
const {,} = computedGlyphs;
const {,} = computedGlyphs as any;
it("says 承 has 水", () => {
hasroot(, [3, 4, 5, 6, 7], );
});
Expand Down
2 changes: 1 addition & 1 deletion spec/form.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { generateSchemes } from "~/lib/component";
import { generateSchemes } from "~/lib";

describe("generate schemes", () => {
it("works for a simple case", () => {
Expand Down
10 changes: 5 additions & 5 deletions spec/mock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { listToObject } from "~/lib/utils";
import { listToObject } from "~/lib";
import rawrepertoire from "../public/cache/repertoire.json";
import type {
PrimitveCharacter,
PrimitiveCharacter,
BasicComponent,
PrimitiveRepertoire,
} from "~/lib/data";
import { determine } from "~/lib/repertoire";
import { computeComponent } from "~/lib/component";
} from "~/lib";
import { determine } from "~/lib";
import { computeComponent } from "~/lib";

export const repertoire = determine(listToObject(rawrepertoire));
export const computedGlyphs = Object.fromEntries(
Expand Down
31 changes: 11 additions & 20 deletions spec/selector.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { describe, expect, it } from "vitest";
import {
length,
bias,
order,
crossing,
attaching,
Scheme,
} from "~/lib/selector";
import select from "~/lib/selector";
import { length, bias, order, crossing, attaching, Scheme } from "~/lib";
import { select } from "~/lib";
import { computedGlyphs, repertoire } from "./mock";
import { defaultKeyboard } from "~/lib/templates";
import { Config } from "~/lib/config";
import { createConfig, defaultKeyboard } from "~/lib";
import { Config } from "~/lib";

const config: Config = {
source: null,
form: defaultKeyboard,
encoder: {
sources: {},
conditions: {},
},
};
const config: Config = createConfig({
name: "",
data: "国标五分类",
keyboard: "米十五笔字根",
encoder: "形音码",
});

const {} = computedGlyphs;
const {} = computedGlyphs as any;

const rootMap = new Map<number, string>();

Expand Down
47 changes: 36 additions & 11 deletions spec/topology.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect, describe, it } from "vitest";
import type { StrokeRelation } from "~/lib/topology";
import findTopology, { curveRelation, renderSVGGlyph } from "~/lib/topology";
import { CubicCurve, LinearCurve, area, render } from "~/lib/bezier";
import type { Draw, Point } from "~/lib/data";
import type { StrokeRelation } from "~/lib";
import { findTopology, curveRelation, renderSVGGlyph } from "~/lib";
import { CubicCurve, LinearCurve, area, render } from "~/lib";
import type { Draw, Point } from "~/lib";
import { computedGlyphs2 as computedGlyphs } from "./mock";
import { getIntervalPosition, makeCurve } from "~/lib/bezier";
import { getIntervalPosition, makeCurve } from "~/lib";

describe("interval position", () => {
it("works for easy cases", () => {
Expand All @@ -24,7 +24,14 @@ describe("interval position", () => {

describe("linear relation", () => {
const {} = computedGlyphs;
const [l, t, r, h, v, b] = .map((x) => x.curveList).flat() as LinearCurve[];
const [l, t, r, h, v, b] = !.map((x) => x.curveList).flat() as [
LinearCurve,
LinearCurve,
LinearCurve,
LinearCurve,
LinearCurve,
LinearCurve,
];
it("figures out all relations in 田", () => {
expect(curveRelation(l, t)).toEqual({
type: "连",
Expand Down Expand Up @@ -62,7 +69,11 @@ describe("linear relation", () => {

describe("linear relation 2", () => {
const {} = computedGlyphs;
const [_, s1, s2] = .map((x) => x.curveList).flat() as LinearCurve[];
const [_, s1, s2] = !.map((x) => x.curveList).flat() as [
LinearCurve,
LinearCurve,
LinearCurve,
];
it("figures out all relations in 艹", () => {
expect(curveRelation(s1, s2)).toEqual({
type: "平行",
Expand All @@ -75,7 +86,12 @@ describe("linear relation 2", () => {
describe("curve relation", () => {
it("figures out all relations in 天", () => {
const {} = computedGlyphs;
const [c1, c2, c3, c4] = .map((x) => x.curveList).flat();
const [c1, c2, c3, c4] = !.map((x) => x.curveList).flat() as [
LinearCurve,
LinearCurve,
CubicCurve,
CubicCurve,
];
expect(curveRelation(c1, c3)).toEqual({
type: "连",
first: "中",
Expand All @@ -96,7 +112,11 @@ describe("curve relation", () => {
});
it("figures out all relations in 义", () => {
const {} = computedGlyphs;
const [c1, c2, c3] = .map((x) => x.curveList).flat();
const [c1, c2, c3] = !.map((x) => x.curveList).flat() as [
CubicCurve,
CubicCurve,
CubicCurve,
];
expect(curveRelation(c1, c2)).toEqual({
type: "平行",
crossAxis: 0,
Expand All @@ -111,7 +131,12 @@ describe("curve relation", () => {
});
it("figures out all relations in 升", () => {
const {} = computedGlyphs;
const [c1, c2, c3, c4] = .map((x) => x.curveList).flat();
const [c1, c2, c3, c4] = !.map((x) => x.curveList).flat() as [
CubicCurve,
LinearCurve,
CubicCurve,
LinearCurve,
];
expect(curveRelation(c1, c2)).toEqual({
type: "平行",
mainAxis: 0,
Expand Down Expand Up @@ -183,7 +208,7 @@ describe("find topology interface", () => {
[],
],
];
expect(findTopology()).toEqual({
expect(findTopology(!)).toEqual({
matrix: array,
orientedPairs: [[2, 0]],
});
Expand Down
7 changes: 0 additions & 7 deletions spec/tsconfig.json

This file was deleted.

14 changes: 7 additions & 7 deletions src/lib/api.ts → src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PrimitveCharacter } from "./data";
import type { PrimitiveCharacter } from "./lib/data";

export const endpoint = "https://api.chaifen.app/";

Expand Down Expand Up @@ -28,7 +28,7 @@ const template =
};

export const list = async () =>
await template("GET")<PrimitveCharacter[], undefined>("repertoire/all");
await template("GET")<PrimitiveCharacter[], undefined>("repertoire/all");

export const post = template("POST");

Expand All @@ -45,20 +45,20 @@ interface Mutation {
export const remoteCreateWithoutUnicode = (payload: PUA) =>
template("POST")<number, PUA>(`repertoire`, payload);

export const remoteCreate = (payload: PrimitveCharacter) =>
template("POST")<number, PrimitveCharacter>(
export const remoteCreate = (payload: PrimitiveCharacter) =>
template("POST")<number, PrimitiveCharacter>(
`repertoire/${payload.unicode}`,
payload,
);

export const remoteUpdate = (payload: PrimitveCharacter) =>
template("PUT")<boolean, PrimitveCharacter>(
export const remoteUpdate = (payload: PrimitiveCharacter) =>
template("PUT")<boolean, PrimitiveCharacter>(
`repertoire/${payload.unicode}`,
payload,
);

export const remoteRemove = (unicode: number) =>
template("DELETE")<boolean, PrimitveCharacter>(`repertoire/${unicode}`);
template("DELETE")<boolean, PrimitiveCharacter>(`repertoire/${unicode}`);

export const remoteMutate = (payload: Mutation) =>
template("PUT")<boolean, Mutation>(`repertoire`, payload);
6 changes: 3 additions & 3 deletions src/atoms/analysis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Analysis, Degenerator, Selector, SieveName } from "~/lib/config";
import type { Analysis, Degenerator, Selector, SieveName } from "~/lib";
import { focusAtom } from "jotai-optics";
import { analysisAtom } from "./config";
import classifier, { Feature } from "~/lib/classifier";
import { Feature, mergeClassifier } from "~/lib";
import { atom } from "jotai";

export const degeneratorAtom = focusAtom(analysisAtom, (o) =>
Expand Down Expand Up @@ -38,5 +38,5 @@ export const classifierCustomizationAtom = focusAtom(analysisAtom, (o) =>

export const customClassifierAtom = atom((get) => {
const customization = get(classifierCustomizationAtom);
return { ...classifier, ...customization };
return mergeClassifier(customization);
});
4 changes: 2 additions & 2 deletions src/atoms/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { atom } from "jotai";
import { AssemblyResult } from "~/lib/assembly";
import { AnalysisResult } from "~/lib/repertoire";
import { AssemblyResult } from "~/lib";
import { AnalysisResult } from "~/lib";

export const analysisResultAtom = atom<AnalysisResult | null>(null);

Expand Down
2 changes: 1 addition & 1 deletion src/atoms/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type Info,
Analysis,
UserData,
} from "~/lib/config";
} from "~/lib";
import { focusAtom } from "jotai-optics";

/** 需要在根组件里提前修改它 */
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { atom } from "jotai";
import type { PrimitiveRepertoire } from "~/lib/data";
import type { PrimitiveRepertoire } from "~/lib";
import { produce } from "immer";
import { Equivalence, Frequency } from "~/components/Optimizer";
import {
Expand Down
12 changes: 6 additions & 6 deletions src/atoms/data.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { atom, useAtomValue } from "jotai";
import { primitiveRepertoireAtom } from "./constants";
import { isPUA } from "~/lib/utils";
import { recursiveRenderCompound } from "~/lib/component";
import { isPUA } from "~/lib";
import { recursiveRenderCompound } from "~/lib";
import { dataAtom } from ".";
import { focusAtom } from "jotai-optics";
import { PrimitiveRepertoire, SVGGlyph } from "~/lib/data";
import { CustomGlyph } from "~/lib/config";
import { determine } from "~/lib/repertoire";
import classifier from "~/lib/classifier";
import { PrimitiveRepertoire, SVGGlyph } from "~/lib";
import { CustomGlyph } from "~/lib";
import { determine } from "~/lib";
import { classifier } from "~/lib";

export const userRepertoireAtom = focusAtom(dataAtom, (o) =>
o.prop("repertoire").valueOr({} as PrimitiveRepertoire),
Expand Down
2 changes: 1 addition & 1 deletion src/atoms/encoder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { focusAtom } from "jotai-optics";
import { encoderAtom } from ".";
import { ShortCodeScheme, WordRule } from "~/lib/config";
import { ShortCodeScheme, WordRule } from "~/lib";

export const maxLengthAtom = focusAtom(encoderAtom, (o) =>
o.prop("max_length"),
Expand Down
Loading

0 comments on commit a008ffc

Please sign in to comment.