@ztimson/utils 0.25.7 → 0.25.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/database.d.ts +17 -6
- package/dist/index.cjs +75 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +76 -22
- package/dist/index.mjs.map +1 -1
- package/dist/string.d.ts +5 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -395,46 +395,88 @@ function makeArray(value) {
|
|
|
395
395
|
class Database {
|
|
396
396
|
constructor(database, tables, version) {
|
|
397
397
|
__publicField(this, "connection");
|
|
398
|
+
__publicField(this, "ready", false);
|
|
398
399
|
__publicField(this, "tables");
|
|
399
400
|
this.database = database;
|
|
400
401
|
this.version = version;
|
|
401
402
|
this.connection = new Promise((resolve, reject) => {
|
|
402
403
|
const req = indexedDB.open(this.database, this.version);
|
|
403
|
-
this.tables = tables.map((t) => {
|
|
404
|
+
this.tables = !tables ? [] : tables.map((t) => {
|
|
404
405
|
t = typeof t == "object" ? t : { name: t };
|
|
405
406
|
return { ...t, name: t.name.toString() };
|
|
406
407
|
});
|
|
407
|
-
const tableNames = new ASet(this.tables.map((t) => t.name));
|
|
408
408
|
req.onerror = () => reject(req.error);
|
|
409
409
|
req.onsuccess = () => {
|
|
410
410
|
const db = req.result;
|
|
411
|
-
|
|
411
|
+
const existing = Array.from(db.objectStoreNames);
|
|
412
|
+
if (!tables) this.tables = existing.map((t) => {
|
|
413
|
+
const tx = db.transaction(t, "readonly");
|
|
414
|
+
const store = tx.objectStore(t);
|
|
415
|
+
return { name: t, key: store.keyPath };
|
|
416
|
+
});
|
|
417
|
+
const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
|
|
418
|
+
if (tables && desired.symmetricDifference(new ASet(existing)).length) {
|
|
412
419
|
db.close();
|
|
413
420
|
Object.assign(this, new Database(this.database, this.tables, db.version + 1));
|
|
421
|
+
this.connection.then(resolve);
|
|
414
422
|
} else {
|
|
415
423
|
this.version = db.version;
|
|
416
424
|
resolve(db);
|
|
417
425
|
}
|
|
426
|
+
this.ready = true;
|
|
418
427
|
};
|
|
419
428
|
req.onupgradeneeded = () => {
|
|
420
429
|
const db = req.result;
|
|
421
430
|
const existingTables = new ASet(Array.from(db.objectStoreNames));
|
|
422
|
-
|
|
423
|
-
|
|
431
|
+
if (tables) {
|
|
432
|
+
const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
|
|
433
|
+
existingTables.difference(desired).forEach((name) => db.deleteObjectStore(name));
|
|
434
|
+
desired.difference(existingTables).forEach((name) => {
|
|
435
|
+
const t = this.tables.find(findByProp("name", name));
|
|
436
|
+
db.createObjectStore(name, {
|
|
437
|
+
keyPath: t == null ? void 0 : t.key,
|
|
438
|
+
autoIncrement: (t == null ? void 0 : t.autoIncrement) || !(t == null ? void 0 : t.key)
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
}
|
|
424
442
|
};
|
|
425
443
|
});
|
|
426
444
|
}
|
|
445
|
+
async createTable(table) {
|
|
446
|
+
if (typeof table == "string") table = { name: table };
|
|
447
|
+
const conn = await this.connection;
|
|
448
|
+
if (!this.includes(table.name)) {
|
|
449
|
+
conn.close();
|
|
450
|
+
Object.assign(this, new Database(this.database, [...this.tables, table], (this.version ?? 0) + 1));
|
|
451
|
+
}
|
|
452
|
+
return this.table(table.name);
|
|
453
|
+
}
|
|
454
|
+
async deleteTable(table) {
|
|
455
|
+
if (typeof table == "string") table = { name: table };
|
|
456
|
+
if (!this.includes(table.name)) return;
|
|
457
|
+
const conn = await this.connection;
|
|
458
|
+
conn.close();
|
|
459
|
+
Object.assign(this, new Database(this.database, this.tables.filter((t) => t.name != table.name), (this.version ?? 0) + 1));
|
|
460
|
+
}
|
|
427
461
|
includes(name) {
|
|
428
|
-
return !!this.tables.find((t) => t.name == name.toString());
|
|
462
|
+
return !!this.tables.find((t) => t.name == (typeof name == "object" ? name.name : name.toString()));
|
|
429
463
|
}
|
|
430
464
|
table(name) {
|
|
431
465
|
return new Table(this, name.toString());
|
|
432
466
|
}
|
|
433
467
|
}
|
|
434
468
|
class Table {
|
|
435
|
-
constructor(database, name) {
|
|
469
|
+
constructor(database, name, key = "id") {
|
|
470
|
+
__publicField(this, "all", this.getAll);
|
|
471
|
+
__publicField(this, "create", this.add);
|
|
472
|
+
__publicField(this, "update", this.set);
|
|
436
473
|
this.database = database;
|
|
437
474
|
this.name = name;
|
|
475
|
+
this.key = key;
|
|
476
|
+
this.database.connection.then(() => {
|
|
477
|
+
const exists = !!this.database.tables.find(findByProp("name", this.name));
|
|
478
|
+
if (!exists) this.database.createTable(this.name);
|
|
479
|
+
});
|
|
438
480
|
}
|
|
439
481
|
async tx(table, fn2, readonly = false) {
|
|
440
482
|
const db = await this.database.connection;
|
|
@@ -449,11 +491,17 @@ class Table {
|
|
|
449
491
|
add(value, key) {
|
|
450
492
|
return this.tx(this.name, (store) => store.add(value, key));
|
|
451
493
|
}
|
|
494
|
+
clear() {
|
|
495
|
+
return this.tx(this.name, (store) => store.clear());
|
|
496
|
+
}
|
|
452
497
|
count() {
|
|
453
498
|
return this.tx(this.name, (store) => store.count(), true);
|
|
454
499
|
}
|
|
455
|
-
|
|
456
|
-
return this.tx(this.name, (store) => store.
|
|
500
|
+
delete(key) {
|
|
501
|
+
return this.tx(this.name, (store) => store.delete(key));
|
|
502
|
+
}
|
|
503
|
+
get(key) {
|
|
504
|
+
return this.tx(this.name, (store) => store.get(key), true);
|
|
457
505
|
}
|
|
458
506
|
getAll() {
|
|
459
507
|
return this.tx(this.name, (store) => store.getAll(), true);
|
|
@@ -461,14 +509,15 @@ class Table {
|
|
|
461
509
|
getAllKeys() {
|
|
462
510
|
return this.tx(this.name, (store) => store.getAllKeys(), true);
|
|
463
511
|
}
|
|
464
|
-
|
|
465
|
-
return this.tx(this.name, (store) => store.
|
|
512
|
+
put(key, value) {
|
|
513
|
+
return this.tx(this.name, (store) => store.put(value, key));
|
|
466
514
|
}
|
|
467
|
-
|
|
468
|
-
return this.
|
|
515
|
+
read(key) {
|
|
516
|
+
return key ? this.get(key) : this.getAll();
|
|
469
517
|
}
|
|
470
|
-
|
|
471
|
-
|
|
518
|
+
set(value, key) {
|
|
519
|
+
if (!key && !value[this.key]) return this.add(value);
|
|
520
|
+
return this.put(key || value[this.key], value);
|
|
472
521
|
}
|
|
473
522
|
}
|
|
474
523
|
class Cache {
|
|
@@ -657,8 +706,9 @@ const NUMBER_LIST = "0123456789";
|
|
|
657
706
|
const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
|
|
658
707
|
const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST + SYMBOL_LIST;
|
|
659
708
|
function camelCase(str) {
|
|
660
|
-
|
|
661
|
-
|
|
709
|
+
if (!str) return "";
|
|
710
|
+
const pascal = pascalCase(str);
|
|
711
|
+
return pascal.charAt(0).toLowerCase() + pascal.slice(1);
|
|
662
712
|
}
|
|
663
713
|
function formatBytes(bytes, decimals = 2) {
|
|
664
714
|
if (bytes === 0) return "0 Bytes";
|
|
@@ -677,16 +727,15 @@ function insertAt(target, str, index) {
|
|
|
677
727
|
}
|
|
678
728
|
function kebabCase(str) {
|
|
679
729
|
if (!str) return "";
|
|
680
|
-
return str
|
|
730
|
+
return wordSegments(str).map((w) => w.toLowerCase()).join("-");
|
|
681
731
|
}
|
|
682
732
|
function pad(text, length, char = " ", start = true) {
|
|
683
733
|
if (start) return text.toString().padStart(length, char);
|
|
684
734
|
return text.toString().padEnd(length, char);
|
|
685
735
|
}
|
|
686
736
|
function pascalCase(str) {
|
|
687
|
-
var _a;
|
|
688
737
|
if (!str) return "";
|
|
689
|
-
return (
|
|
738
|
+
return wordSegments(str).map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join("");
|
|
690
739
|
}
|
|
691
740
|
function randomHex(length) {
|
|
692
741
|
return Array(length).fill(null).map(() => Math.round(Math.random() * 15).toString(16)).join("");
|
|
@@ -716,7 +765,7 @@ function randomStringBuilder(length, letters = false, numbers = false, symbols =
|
|
|
716
765
|
}
|
|
717
766
|
function snakeCase(str) {
|
|
718
767
|
if (!str) return "";
|
|
719
|
-
return str
|
|
768
|
+
return wordSegments(str).map((w) => w.toLowerCase()).join("_");
|
|
720
769
|
}
|
|
721
770
|
function strSplice(str, start, deleteCount, insert = "") {
|
|
722
771
|
const before = str.slice(0, start);
|
|
@@ -806,6 +855,10 @@ function safe_add(d, _) {
|
|
|
806
855
|
function bit_rol(d, _) {
|
|
807
856
|
return d << _ | d >>> 32 - _;
|
|
808
857
|
}
|
|
858
|
+
function wordSegments(str) {
|
|
859
|
+
if (!str) return [];
|
|
860
|
+
return str.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/([0-9]+)([a-zA-Z])/g, "$1 $2").replace(/([a-zA-Z])([0-9]+)/g, "$1 $2").replace(/[_\-\s]+/g, " ").trim().split(/\s+/).filter(Boolean);
|
|
861
|
+
}
|
|
809
862
|
function validateEmail(email) {
|
|
810
863
|
return /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email);
|
|
811
864
|
}
|
|
@@ -2117,6 +2170,7 @@ export {
|
|
|
2117
2170
|
timestampFilename,
|
|
2118
2171
|
toCsv,
|
|
2119
2172
|
uploadWithProgress,
|
|
2120
|
-
validateEmail
|
|
2173
|
+
validateEmail,
|
|
2174
|
+
wordSegments
|
|
2121
2175
|
};
|
|
2122
2176
|
//# sourceMappingURL=index.mjs.map
|