This commit is contained in:
Seth Trowbridge 2025-07-01 09:50:54 -04:00
parent 603eaaa5bb
commit 10ff8fda46
8 changed files with 122 additions and 40 deletions

24
.vscode/launch.json vendored Normal file
View File

@ -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
}
]
}

37
app.js
View File

@ -1,4 +1,4 @@
import DB from "./db.js" import DB from "./db.js";
// build parts // build parts
const Part = {}; const Part = {};
@ -8,9 +8,7 @@ function CreatePart(id, name)
Part[id] = part; Part[id] = part;
return part; return part;
} }
CreatePart('p1', 'Part 1'); await DB(import.meta.resolve('./db_part.csv'), 2, CreatePart);
CreatePart('p2', 'Part 2');
CreatePart('p3', 'Part 3');
// then build work // then build work
@ -27,16 +25,8 @@ function CreateWork(partId, user, time, data)
Work.push(work); Work.push(work);
return work; return work;
} }
async function LoadWork() await DB(import.meta.resolve('./db_work.csv'), 3, CreateWork);
{
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'});
// then build desks // then build desks
const Desk = {}; const Desk = {};
@ -72,10 +62,15 @@ const Range =(need, make)=>
return [dirtyNeed, dirtyMake, needMax, makeMin]; 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<string, number]>, 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; Desk[id] = desk;
need.forEach(partId => Part[partId]._.need.push(desk)); need.forEach(partId => Part[partId]._.need.push(desk));
make.forEach(partId => Part[partId]._.make.push(desk)); make.forEach(partId => Part[partId]._.make.push(desk));
@ -85,8 +80,14 @@ function CreateDesk(id, name, need, make, role)
return desk; 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); console.log(Desk);

57
db.js
View File

@ -6,30 +6,47 @@ function createCSVScanner(onRow, nonJsonCols) {
const lines = buffer.split('\n'); const lines = buffer.split('\n');
buffer = lines.pop(); // Save incomplete line buffer = lines.pop(); // Save incomplete line
for (const line of lines) { if(nonJsonCols !== 0)
const fields = []; {
let start = 0; for (const line of lines)
let commaCount = 0; {
const fields = [];
let start = 0;
let commaCount = 0;
for (let i = 0; i < line.length; i++) { for (let i = 0; i < line.length; i++) {
if (line[i] === ',' && commaCount < nonJsonCols) { if (line[i] === ',' && commaCount < nonJsonCols) {
fields.push(line.slice(start, i)); fields.push(line.slice(start, i));
start = i + 1; start = i + 1;
commaCount++; 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 else
const jsonStr = line.slice(start); {
let jsonData = null; for (const line of lines)
try { {
jsonData = JSON.parse(jsonStr); const fields = [];
} catch (_e) { let start = 0;
console.error('Invalid JSON:', jsonStr); 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);
} }
onRow(...fields, jsonData);
} }
}; };
} }

1
db_desk.csv Normal file
View File

@ -0,0 +1 @@
d1,Desk One,[{"p1":1, "p2":1}, ["p3"], "all", ["r1"]]
Can't render this file because it contains an unexpected character in line 1 and column 15.

3
db_part.csv Normal file
View File

@ -0,0 +1,3 @@
p1,Part 1
p2,Part 2
p3,Part 3
1 p1 Part 1
2 p2 Part 2
3 p3 Part 3

View File

@ -1 +1 @@
seth trowbridge, seth111@gmail.com u1,seth trowbridge

1 u1 seth111@gmail.com seth trowbridge

19
test.test.ts Normal file
View File

@ -0,0 +1,19 @@
const check =<T extends object | null, Ret, Args extends [...string[], T]>(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<string, number>)
{
return [1, 2, 3]
}
type LoadArgs = Parameters<typeof load>;
function save(n:number[])
{
return ["a", "b", "c", { x: 1, y: 2 }] as LoadArgs
}
const c1 = check({load, save});

17
types.d.ts vendored Normal file
View File

@ -0,0 +1,17 @@
export {};
export type Row<
L extends number,
T extends any[] = []
> = T["length"] extends L ? T : Row<L, [string, ...T]>;
declare global {
namespace DB
{
type DB<T, Count extends number>=(path:string, flat:Count, handler:(...args:[...Row<Count>, T])=>void)=>Promise<void>
}
}