Move base64/inflate to Support.ts

This commit is contained in:
Mark Probst 2018-03-31 14:23:12 -07:00
Родитель ad9743d895
Коммит 6f6217b544
2 изменённых файлов: 10 добавлений и 5 удалений

Просмотреть файл

@ -1,10 +1,8 @@
"use strict";
import { Base64 } from "js-base64";
import { panic, assert } from "./Support";
import { panic, assert, inflateBase64 } from "./Support";
import { encodedMarkovChain } from "./EncodedMarkovChain";
import * as pako from "pako";
// This must be null, not undefined, because we read it from JSON.
export type SubTrie = number | null | Trie;
@ -90,8 +88,7 @@ export function train(lines: string[], depth: number): MarkovChain {
}
export function load(): MarkovChain {
const bytes = Base64.atob(encodedMarkovChain);
return JSON.parse(pako.inflate(bytes, { to: "string" }));
return JSON.parse(inflateBase64(encodedMarkovChain));
}
export function evaluateFull(mc: MarkovChain, word: string): [number, number[]] {

Просмотреть файл

@ -2,6 +2,9 @@
import { Collection, List, Set, isKeyed, isIndexed } from "immutable";
import { Base64 } from "js-base64";
import * as pako from "pako";
export function intercalate<T>(separator: T, items: Collection<any, T>): List<T> {
const acc: T[] = [];
items.forEach((x: T) => {
@ -173,3 +176,8 @@ export async function mapSync<K, V, U>(
}
return coll.map(_v => results[index++]);
}
export function inflateBase64(encoded: string): string {
const bytes = Base64.atob(encoded);
return pako.inflate(bytes, { to: "string" });
}