Skip to content

Commit

Permalink
fix: make zip util server-only
Browse files Browse the repository at this point in the history
  • Loading branch information
diauweb committed Feb 10, 2023
1 parent 75377ae commit 0b4c1f4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
56 changes: 44 additions & 12 deletions src/lib/server/util.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,56 @@
import { find } from './db.js'
import {find} from './db.js'
import {getText} from "$lib/server/text";
import {toPlainObject} from "$lib/util";

export async function inlineLanguage(resultItem: any) {
const translate: Record<string, string> = {};
for (const k of Object.keys(resultItem)) {
if (!k.endsWith("TextMapHash")) continue;
translate[resultItem[k]] = k;
if (!k.endsWith("TextMapHash")) continue;
translate[resultItem[k]] = k;
}

const q = (await find("TextMap", {
_ver: resultItem._ver,
_ver: resultItem._ver,
hash: { $in: Object.keys(translate).map(i => parseInt(i)) }
}))
q.forEach(e => resultItem[translate[e.hash]] = e)
Object.values(translate).forEach(e => {
if (typeof resultItem[e] !== 'object') {
const hs = resultItem[e]
resultItem[e] = { hash: hs }
}
if (typeof resultItem[e] !== 'object') {
const hs = resultItem[e]
resultItem[e] = {hash: hs}
}
})

return resultItem;
}

}


export type ZippedExcel = { mapper: string[], table: any[][] };

export async function zipExcel(obj: Record<string, any>[]): ZippedExcel {
let n = 1;
const map = new Map<string, number>();
map.set("#", 0);

const table = [];
for (let ii = 0; ii < obj.length; ii++) {
const i = obj[ii];
const cur: any[] = [ii];
for (const k of Object.keys(i)) {
if (!map.has(k)) map.set(k, n++);
if (k.endsWith('MapHash')) {
cur[<number>map.get(k)] = {hash: i[k], ...toPlainObject(await getText(i[k]))};
} else {
cur[<number>map.get(k)] = i[k];
}
}
table.push(cur);
}

const mapper: string[] = [];
map.forEach((value, key) => mapper[value] = key);
return {
mapper,
table,
}
}
32 changes: 0 additions & 32 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {getText} from "$lib/server/text";

export function toPlainObject<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}
Expand All @@ -20,33 +18,3 @@ export function stripBsid(obj: any) {
}
return newobj;
}

export type ZippedExcel = { mapper: string[], table: any[][] };

export async function zipExcel(obj: Record<string, any>[]): ZippedExcel {
let n = 1;
const map = new Map<string, number>();
map.set("#", 0);

const table = [];
for (let ii = 0; ii < obj.length; ii++) {
const i = obj[ii];
const cur: any[] = [ii];
for (const k of Object.keys(i)) {
if (!map.has(k)) map.set(k, n++);
if (k.endsWith('MapHash')) {
cur[<number>map.get(k)] = {hash: i[k], ...toPlainObject(await getText(i[k]))};
} else {
cur[<number>map.get(k)] = i[k];
}
}
table.push(cur);
}

const mapper: string[] = [];
map.forEach((value, key) => mapper[value] = key);
return {
mapper,
table,
}
}
3 changes: 1 addition & 2 deletions src/routes/(app)/table/[name]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type {PageServerLoad} from "./$types";
import {error} from "@sveltejs/kit";
import LRU from 'lru-cache';
import {getFile} from "$lib/server/git";
import type {ZippedExcel} from "$lib/util";
import {zipExcel} from "$lib/util";
import {zipExcel, ZippedExcel} from "$lib/server/util";

const cache = new LRU<string, ZippedExcel>({
ttl: 60000 * 10,
Expand Down

0 comments on commit 0b4c1f4

Please sign in to comment.