Compare commits

..

3 Commits

Author SHA1 Message Date
1c2c18b65d Added camelCase function
All checks were successful
Build / Build NPM Project (push) Successful in 1m12s
Build / Tag Version (push) Successful in 12s
Build / Publish Documentation (push) Successful in 50s
2025-03-10 09:50:16 -04:00
cd5741d6ab Simplified timestampedFilename to use formatDate
All checks were successful
Build / Build NPM Project (push) Successful in 43s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 45s
2025-03-02 21:37:48 -05:00
5b9e0714ce Cache returns deep copies to prevent deletion mid-use
All checks were successful
Build / Build NPM Project (push) Successful in 1m13s
Build / Tag Version (push) Successful in 17s
Build / Publish Documentation (push) Successful in 54s
2025-02-27 08:36:30 -05:00
4 changed files with 22 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.23.12",
"version": "0.23.15",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -1,3 +1,5 @@
import {deepCopy} from './objects.ts';
export type CacheOptions = {
/** Delete keys automatically after x amount of seconds */
ttl?: number;
@ -36,12 +38,12 @@ export class Cache<K extends string | number | symbol, T> {
}
return new Proxy(this, {
get: (target: this, prop: string | symbol) => {
if (prop in target) return (target as any)[prop];
return target.store[prop as K];
if(prop in target) return (target as any)[prop];
return deepCopy(target.store[prop as K]);
},
set: (target: this, prop: string | symbol, value: any) => {
if (prop in target) (target as any)[prop] = value;
else target.store[prop as K] = value;
if(prop in target) (target as any)[prop] = value;
else this.set(prop as K, value);
return true;
}
});
@ -58,7 +60,7 @@ export class Cache<K extends string | number | symbol, T> {
* @return {T[]} Array of items
*/
all(): T[] {
return Object.values(this.store);
return deepCopy(Object.values(this.store));
}
/**
@ -119,7 +121,7 @@ export class Cache<K extends string | number | symbol, T> {
* @return {T} Cached item
*/
get(key: K): T {
return this.store[key];
return deepCopy(this.store[key]);
}
/**
@ -137,7 +139,7 @@ export class Cache<K extends string | number | symbol, T> {
* @return {Record<K, T>}
*/
map(): Record<K, T> {
return structuredClone(this.store);
return deepCopy(this.store);
}
/**

View File

@ -1,6 +1,7 @@
import {makeArray} from './array.ts';
import {JSONAttemptParse} from './objects.ts';
import {PromiseProgress} from './promise-progress';
import {formatDate} from './time.ts';
/**
* Download blob as a file
@ -76,7 +77,7 @@ export function fileText(file: any): Promise<string | null> {
*/
export function timestampFilename(name?: string, date: Date | number | string = new Date()) {
if(typeof date == 'number' || typeof date == 'string') date = new Date(date);
const timestamp = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}_${date.getHours().toString().padStart(2, '0')}-${date.getMinutes().toString().padStart(2, '0')}-${date.getSeconds().toString().padStart(2, '0')}`;
const timestamp = formatDate('YYYY-MM-DD_HH:mm:ss', date);
return name ? name.replace('{{TIMESTAMP}}', timestamp) : timestamp;
}

View File

@ -18,6 +18,16 @@ export const SYMBOL_LIST = '~`!@#$%^&*()_-+={[}]|\\:;"\'<,>.?/';
*/
export const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST + SYMBOL_LIST;
/**
* Converts text to camel case
*/
export function camelCase(text?: string) {
if(!text) return '';
text = text.replaceAll(/^[0-9]+/g, '')
.replaceAll(/[^a-zA-Z0-9]+(\w?)/g, (...args) => args[1]?.toUpperCase() || '');
return text[0].toLowerCase() + text.slice(1);
}
/**
* Convert number of bytes into a human-readable size
*