Compare commits

..

4 Commits

Author SHA1 Message Date
5b3bbdf02c Added node support for uploadWithProgress
All checks were successful
Build / Publish Docs (push) Successful in 54s
Build / Build NPM Project (push) Successful in 38s
Build / Tag Version (push) Successful in 9s
2026-07-13 22:16:58 -04:00
6b379270a9 Fixed path event dir property
All checks were successful
Build / Publish Docs (push) Successful in 1m3s
Build / Build NPM Project (push) Successful in 36s
Build / Tag Version (push) Successful in 9s
2026-06-15 09:55:38 -04:00
28716c7b5a Added titleCase helper
All checks were successful
Build / Publish Docs (push) Successful in 2m20s
Build / Build NPM Project (push) Successful in 2m29s
Build / Tag Version (push) Successful in 9s
2026-04-15 21:59:42 -04:00
d530f6abdf Cross timezone day of week fix
All checks were successful
Build / Build NPM Project (push) Successful in 53s
Build / Tag Version (push) Successful in 11s
Build / Publish Docs (push) Successful in 40s
2026-04-11 23:19:10 -04:00
7 changed files with 6979 additions and 16 deletions

1
.gitignore vendored
View File

@@ -10,6 +10,7 @@ uploads
public/momentum*js
junit.xml
/docs/
main.mjs
# Logs
*.log

6927
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

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

View File

@@ -83,6 +83,7 @@ export function timestampFilename(name?: string, date: Date | number | string =
/**
* Upload file to URL with progress callback using PromiseProgress
* Works in both browser (with progress) and Node.js (fallback without progress)
*
* @param {{url: string, files: File[], headers?: {[p: string]: string}, withCredentials?: boolean}} options
* @return {PromiseProgress<T>} Promise of request with `onProgress` callback
@@ -93,10 +94,12 @@ export function uploadWithProgress<T>(options: {
headers?: {[key: string]: string};
withCredentials?: boolean;
}): PromiseProgress<T> {
// Browser environment - use XMLHttpRequest for progress
if (typeof XMLHttpRequest !== 'undefined') {
return new PromiseProgress<T>((res, rej, prog) => {
const xhr = new XMLHttpRequest();
const formData = new FormData();
options.files.forEach(f => formData.append('file', f));
options.files.forEach(f => formData.append('files', f));
xhr.withCredentials = !!options.withCredentials;
xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
@@ -108,4 +111,23 @@ export function uploadWithProgress<T>(options: {
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
xhr.send(formData);
});
}
// Node.js environment - fallback to fetch without progress
return new PromiseProgress<T>(async (res, rej) => {
try {
const formData = new FormData();
options.files.forEach(f => formData.append('files', f));
const response = await fetch(options.url, {method: 'POST', headers: options.headers || {}, body: formData});
if(!response.ok) {
const error = await response.text();
rej(JSONAttemptParse(error));
} else {
const result = await response.text();
res(<T>JSONAttemptParse(result));
}
} catch (error) {
rej(error);
}
});
}

View File

@@ -135,7 +135,7 @@ export class PathEvent {
let temp = p.split('/').filter(p => !!p);
this.module = temp.splice(0, 1)[0] || '';
this.path = temp.join('/');
this.dir = temp.length > 2 ? temp.slice(0, -1).join('/') : '';
this.dir = temp.length > 1 ? temp.slice(0, -1).join('/') : '';
this.fullPath = `${this.module}${this.module && this.path ? '/' : ''}${this.path}`;
this.name = temp.pop() || '';
this.hasGlob = this.fullPath.includes('*');

View File

@@ -241,7 +241,6 @@ export function snakeCase(str?: string): string {
return wordSegments(str).map(w => w.toLowerCase()).join("_");
}
/**
* Splice a string together (Similar to Array.splice)
*
@@ -257,6 +256,20 @@ export function strSplice(str: string, start: number, deleteCount: number, inser
return before + insert + after;
}
function titleCase(str: string) {
// Normalize separators: replace underscores and hyphens with spaces
let normalizedStr = str.replace(/(_|-)/g, ' ');
// Handle CamelCase/PascalCase boundaries: insert a space before capital letters
normalizedStr = normalizedStr.replace(/([a-z])([A-Z])/g, '$1 $2');
// Lowercase the whole string, split by any whitespace, and capitalize each word
let words = normalizedStr.toLowerCase().split(/\s+/).filter(Boolean);
const titledWords = words.map(word => {
if (word.length === 0) return '';
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
});
return titledWords.join(' ');
}
/**
* Find all substrings that match a given pattern.
*

View File

@@ -155,7 +155,7 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
});
const monthValue = parseInt(partsMap.month) - 1;
const dayOfWeekValue = new Date(`${partsMap.year}-${partsMap.month}-${partsMap.day}`).getDay();
const dayOfWeekValue = new Date(Date.UTC(parseInt(partsMap.year), parseInt(partsMap.month) - 1, parseInt(partsMap.day))).getUTCDay();
const hourValue = parseInt(partsMap.hour);
get = (fn: 'FullYear' | 'Month' | 'Date' | 'Day' | 'Hours' | 'Minutes' | 'Seconds' | 'Milliseconds'): number => {