-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencode.ts
More file actions
74 lines (68 loc) · 2.16 KB
/
encode.ts
File metadata and controls
74 lines (68 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { pruneConfig } from "@classmodel/class/config_utils";
import { unwrap } from "solid-js/store";
import { parseAnalysis } from "./analysis_type";
import type { Analysis } from "./analysis_type";
import {
type ExperimentConfig,
type PartialExperimentConfig,
parseExperimentConfig,
} from "./experiment_config";
import { findPresetByName } from "./presets";
import type { Experiment } from "./store";
export function decodeAppState(encoded: string): [Experiment[], Analysis[]] {
const decoded = decodeURI(encoded);
const parsed = JSON.parse(decoded);
const experiments: Experiment[] = [];
if (typeof parsed === "object" && Array.isArray(parsed.experiments)) {
for (const exp of parsed.experiments) {
const config = parseExperimentConfig(exp);
const experiment: Experiment = {
config,
output: {
running: false,
permutations: [],
},
};
experiments.push(experiment);
}
} else {
console.error("No experiments found in ", encoded);
}
const analyses: Analysis[] = [];
if (typeof parsed === "object" && Array.isArray(parsed.analyses)) {
for (const analysisRaw of parsed.analyses) {
const analysis = parseAnalysis(analysisRaw);
analyses.push(analysis);
}
}
return [experiments, analyses];
}
export function encodeAppState(
experiments: Experiment[],
analyses: Analysis[],
) {
const rawExperiments = unwrap(experiments);
const rawAnalyses = unwrap(analyses);
const minimizedState = {
experiments: rawExperiments.map((exp) => toPartial(exp.config)),
analyses: rawAnalyses,
};
return encodeURI(JSON.stringify(minimizedState, undefined, 0));
}
export function toPartial(config: ExperimentConfig): PartialExperimentConfig {
const preset = findPresetByName(config.preset);
const reference = pruneConfig(config.reference, preset.config);
return {
reference,
preset: config.preset,
permutations: config.permutations.map((perm) =>
pruneConfig(perm, config.reference),
),
observations: config.observations,
};
}
export function fromPartial(
partial: PartialExperimentConfig,
): ExperimentConfig {
return parseExperimentConfig(partial);
}