From 10ff8fda4661bcb7a56068305f31b7de302088b6 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Tue, 1 Jul 2025 09:50:54 -0400 Subject: [PATCH] misc --- .vscode/launch.json | 24 ++++++++++++++++++ app.js | 37 ++++++++++++++-------------- db.js | 59 +++++++++++++++++++++++++++++---------------- db_desk.csv | 1 + db_part.csv | 3 +++ db_user.csv | 2 +- test.test.ts | 19 +++++++++++++++ types.d.ts | 17 +++++++++++++ 8 files changed, 122 insertions(+), 40 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 db_desk.csv create mode 100644 db_part.csv create mode 100644 test.test.ts create mode 100644 types.d.ts diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..bd52258 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "request": "launch", + "name": "Launch Program", + "type": "node", + "cwd": "${workspaceFolder}", + "env": {}, + "runtimeExecutable": "deno", + "runtimeArgs": [ + "-Ar", + "--unstable", + "--inspect-brk", + "app.js" + ], + "attachSimplePort": 9229 + } + ] +} \ No newline at end of file diff --git a/app.js b/app.js index a9e77cb..01fb5bc 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,4 @@ -import DB from "./db.js" +import DB from "./db.js"; // build parts const Part = {}; @@ -8,9 +8,7 @@ function CreatePart(id, name) Part[id] = part; return part; } -CreatePart('p1', 'Part 1'); -CreatePart('p2', 'Part 2'); -CreatePart('p3', 'Part 3'); +await DB(import.meta.resolve('./db_part.csv'), 2, CreatePart); // then build work @@ -27,16 +25,8 @@ function CreateWork(partId, user, time, data) Work.push(work); return work; } -async function LoadWork() -{ - return await DB(import.meta.resolve('./db_work.csv'), 3, CreateWork); -} -await LoadWork(); -// CreateWork('p1', 'u1', 1, {key: 'value1'}); -// CreateWork('p1', 'u1', 2, {key: 'value2'}); -// CreateWork('p2', 'u2', 3, {key: 'value3'}); -// CreateWork('p2', 'u2', 4, {key: 'value4'}); -// CreateWork('p3', 'u2', 3, {key: 'valueDONE'}); +await DB(import.meta.resolve('./db_work.csv'), 3, CreateWork); + // then build desks const Desk = {}; @@ -72,10 +62,15 @@ const Range =(need, make)=> return [dirtyNeed, dirtyMake, needMax, makeMin]; }; -/** @type {(id:string, name:string, need:string[], make:string[], role:string[])=>void} */ -function CreateDesk(id, name, need, make, role) + +/** @typedef {[need:Record, make:string[], mode:"one"|"all", role:string[]} StoredDesk */ + +/** @type {(id:string, name:string, data:StoredDesk)=>void} */ +function CreateDesk(id, name, data, role, need, time, make, mode="one") { - const desk = {name, need, make, role, _:{id}}; + const need = Object.keys(data[0]) + const time = Object.values(data[0]); + const desk = {name, need, time, make, role, _:{id}}; Desk[id] = desk; need.forEach(partId => Part[partId]._.need.push(desk)); make.forEach(partId => Part[partId]._.make.push(desk)); @@ -85,8 +80,14 @@ function CreateDesk(id, name, need, make, role) return desk; } -CreateDesk('d1', 'Desk 1', ['p1', 'p2'], ['p3'], ['role1']); + +await DB(import.meta.resolve('./db_work.csv'), 3, (id, name, /**@type{StoredDesk}*/data)=>{ + + const need = Object.keys(data[0]) + CreateDesk(id, name, need, data[1], ); +}); +console.log(Part); console.log(Desk); \ No newline at end of file diff --git a/db.js b/db.js index cb0309d..adf7e3a 100644 --- a/db.js +++ b/db.js @@ -6,30 +6,47 @@ function createCSVScanner(onRow, nonJsonCols) { const lines = buffer.split('\n'); buffer = lines.pop(); // Save incomplete line - for (const line of lines) { - const fields = []; - let start = 0; - let commaCount = 0; + if(nonJsonCols !== 0) + { + for (const line of lines) + { + const fields = []; + let start = 0; + let commaCount = 0; - for (let i = 0; i < line.length; i++) { - if (line[i] === ',' && commaCount < nonJsonCols) { - fields.push(line.slice(start, i)); - start = i + 1; - commaCount++; + for (let i = 0; i < line.length; i++) { + if (line[i] === ',' && commaCount < nonJsonCols) { + fields.push(line.slice(start, i)); + start = i + 1; + commaCount++; + } } + + const jsonStr = line.slice(start); + let jsonData = null; + try { + jsonData = JSON.parse(jsonStr); + } catch (_e) { + console.error('Invalid JSON:', jsonStr); + } + onRow(...fields, jsonData); } - - // Push the remaining part as JSON string - const jsonStr = line.slice(start); - let jsonData = null; - try { - jsonData = JSON.parse(jsonStr); - } catch (_e) { - console.error('Invalid JSON:', jsonStr); - } - - onRow(...fields, jsonData); - + } + else + { + for (const line of lines) + { + const fields = []; + let start = 0; + for (let i = 0; i < line.length; i++) { + if (line[i] === ',') { + fields.push(line.slice(start, i)); + start = i + 1; + } + } + fields.push(line.slice(start, line.length-1)) + onRow(...fields); + } } }; } diff --git a/db_desk.csv b/db_desk.csv new file mode 100644 index 0000000..9ac4d9e --- /dev/null +++ b/db_desk.csv @@ -0,0 +1 @@ +d1,Desk One,[{"p1":1, "p2":1}, ["p3"], "all", ["r1"]] diff --git a/db_part.csv b/db_part.csv new file mode 100644 index 0000000..16b3d6b --- /dev/null +++ b/db_part.csv @@ -0,0 +1,3 @@ +p1,Part 1 +p2,Part 2 +p3,Part 3 diff --git a/db_user.csv b/db_user.csv index 07168a2..81ca231 100644 --- a/db_user.csv +++ b/db_user.csv @@ -1 +1 @@ -seth trowbridge, seth111@gmail.com \ No newline at end of file +u1,seth trowbridge diff --git a/test.test.ts b/test.test.ts new file mode 100644 index 0000000..ed60edb --- /dev/null +++ b/test.test.ts @@ -0,0 +1,19 @@ + +const check =(settings:{load:(...args:Args)=>Ret, save:(internal:Ret)=>Args})=> +{ + return {} as [never, ...Args][Args["length"]]; +} + +function load(a1:string, a2:string, a3:string, a4:Record) +{ + return [1, 2, 3] +} + +type LoadArgs = Parameters; + +function save(n:number[]) +{ + return ["a", "b", "c", { x: 1, y: 2 }] as LoadArgs +} + +const c1 = check({load, save}); diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..e3853e8 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,17 @@ +export {}; + +export type Row< + L extends number, + T extends any[] = [] +> = T["length"] extends L ? T : Row; + +declare global { + namespace DB + { + type DB=(path:string, flat:Count, handler:(...args:[...Row, T])=>void)=>Promise + } +} + + + +