From 6f6217b5449a9b31be0d38e868838b21bb0c5ca7 Mon Sep 17 00:00:00 2001 From: Mark Probst Date: Sat, 31 Mar 2018 14:23:12 -0700 Subject: [PATCH] Move base64/inflate to Support.ts --- src/MarkovChain.ts | 7 ++----- src/Support.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/MarkovChain.ts b/src/MarkovChain.ts index 316bfc59..6fb0083f 100644 --- a/src/MarkovChain.ts +++ b/src/MarkovChain.ts @@ -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[]] { diff --git a/src/Support.ts b/src/Support.ts index 91b4ae9b..63f9bb2d 100644 --- a/src/Support.ts +++ b/src/Support.ts @@ -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(separator: T, items: Collection): List { const acc: T[] = []; items.forEach((x: T) => { @@ -173,3 +176,8 @@ export async function mapSync( } return coll.map(_v => results[index++]); } + +export function inflateBase64(encoded: string): string { + const bytes = Base64.atob(encoded); + return pako.inflate(bytes, { to: "string" }); +}