misc
This commit is contained in:
parent
603eaaa5bb
commit
10ff8fda46
24
.vscode/launch.json
vendored
Normal file
24
.vscode/launch.json
vendored
Normal 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
37
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<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;
|
||||
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);
|
||||
59
db.js
59
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
1
db_desk.csv
Normal file
1
db_desk.csv
Normal 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
3
db_part.csv
Normal file
@ -0,0 +1,3 @@
|
||||
p1,Part 1
|
||||
p2,Part 2
|
||||
p3,Part 3
|
||||
|
@ -1 +1 @@
|
||||
seth trowbridge, seth111@gmail.com
|
||||
u1,seth trowbridge
|
||||
|
||||
|
19
test.test.ts
Normal file
19
test.test.ts
Normal 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
17
types.d.ts
vendored
Normal 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>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user