Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b7df327cd5 | |||
| aa652bd7e0 | |||
| e815126807 | |||
| 6319f810b5 | |||
| 91f4abf1f1 | |||
| f9971d7ce1 | |||
| fbe46ddb64 | |||
| d29d0d5121 | |||
| 5b3bbdf02c | |||
| 6b379270a9 | |||
| 28716c7b5a | |||
| d530f6abdf | |||
| cbee6a4509 | |||
| e8f81bb584 | |||
| 4179b4010a | |||
| 15ac52b6a0 | |||
| c778f3d280 | |||
| d0af3a63bc | |||
| bf73d2670b | |||
| 361613f507 | |||
| 681c89d5af | |||
| 0142904414 | |||
| ddaf5c9957 | |||
| 18261dc5da | |||
| e32e5d6f71 | |||
| 41bc5e7eb5 | |||
| fda92e46d5 | |||
| 1be2c1118f | |||
| ead8fcffc0 | |||
| 367b026cea | |||
| 1b5e16ae5f | |||
| 1b0061b714 | |||
| 3048b74b2f | |||
| 49959f3060 | |||
| cabfc93773 | |||
| 38207eb618 | |||
| 1352e69895 | |||
| 1b05af09fb | |||
| 32c61fff42 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,6 +10,7 @@ uploads
|
||||
public/momentum*js
|
||||
junit.xml
|
||||
/docs/
|
||||
main.mjs
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
6927
package-lock.json
generated
Normal file
6927
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.27.17",
|
||||
"version": "0.30.4",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -122,6 +122,8 @@ export function sortByProp(prop: string, reverse = false) {
|
||||
return function (a: any, b: any) {
|
||||
const aVal = dotNotation<any>(a, prop);
|
||||
const bVal = dotNotation<any>(b, prop);
|
||||
if(aVal === undefined) return 1;
|
||||
if(bVal === undefined) return -1;
|
||||
if(typeof aVal == 'number' && typeof bVal == 'number')
|
||||
return (reverse ? -1 : 1) * (aVal - bVal);
|
||||
if(aVal > bVal) return reverse ? -1 : 1;
|
||||
|
||||
@@ -31,6 +31,8 @@ export class Cache<K extends string | number | symbol, T> {
|
||||
/** Await initial loading */
|
||||
loading = new Promise<void>(r => this._loading = r);
|
||||
|
||||
get size() { return this.store.keys().toArray().length }
|
||||
|
||||
/**
|
||||
* Create new cache
|
||||
* @param {keyof T} key Default property to use as primary key
|
||||
|
||||
67
src/color.ts
67
src/color.ts
@@ -1,12 +1,73 @@
|
||||
import {dec2Hex} from './math.ts';
|
||||
|
||||
/**
|
||||
* Determine if either black or white provides more contrast to the provided color
|
||||
* @param {string} background Color to compare against
|
||||
* @param {string} color Color to compare against
|
||||
* @return {"white" | "black"} Color with the most contrast
|
||||
*/
|
||||
export function contrast(background: string): 'white' | 'black' {
|
||||
const exploded = background?.match(background.length >= 6 ? /[0-9a-fA-F]{2}/g : /[0-9a-fA-F]/g);
|
||||
export function contrast(color: string): 'white' | 'black' {
|
||||
const exploded = color?.match(color.length >= 6 ? /[0-9a-fA-F]{2}/g : /[0-9a-fA-F]/g);
|
||||
if(!exploded || exploded?.length < 3) return 'black';
|
||||
const [r, g, b] = exploded.map(hex => parseInt(hex.length == 1 ? `${hex}${hex}` : hex, 16));
|
||||
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
||||
return luminance > 0.5 ? 'black' : 'white';
|
||||
}
|
||||
|
||||
export function hex2Int(hex: string): {r: number, g: number, b: number} {
|
||||
let r = 0, g = 0, b = 0;
|
||||
if (hex.length === 4) {
|
||||
r = parseInt(hex[1] + hex[1], 16);
|
||||
g = parseInt(hex[2] + hex[2], 16);
|
||||
b = parseInt(hex[3] + hex[3], 16);
|
||||
} else {
|
||||
r = parseInt(hex.slice(1, 3), 16);
|
||||
g = parseInt(hex.slice(3, 5), 16);
|
||||
b = parseInt(hex.slice(5, 7), 16);
|
||||
}
|
||||
return {r, g, b};
|
||||
}
|
||||
|
||||
export function hue2rgb(p: number, q: number, t: number): number {
|
||||
if(t < 0) t += 1;
|
||||
if(t > 1) t -= 1;
|
||||
if(t < 1 / 6) return p + (q - p) * 6 * t;
|
||||
if(t < 1 / 2) return q;
|
||||
if(t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||
return p;
|
||||
}
|
||||
|
||||
export function int2Hex(r: number, g: number, b: number) {
|
||||
return '#' + dec2Hex(r) + dec2Hex(g) + dec2Hex(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjusts the darkness of a hex color.
|
||||
* @param {string} hex - The hex color (e.g., '#ff0000').
|
||||
* @param {number} amount - A value between -1 (black) and 1 (white)
|
||||
*/
|
||||
export function shadeColor(hex: string, amount: number) {
|
||||
let {r, g, b} = hex2Int(hex);
|
||||
r /= 255; g /= 255; b /= 255;
|
||||
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
||||
let h, s, l = (max + min) / 2;
|
||||
|
||||
if (max === min) {
|
||||
h = s = 0;
|
||||
} else {
|
||||
const d = max - min;
|
||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||
switch (max) {
|
||||
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
|
||||
case g: h = (b - r) / d + 2; break;
|
||||
case b: h = (r - g) / d + 4; break;
|
||||
default: h = 0; break;
|
||||
}
|
||||
h /= 6;
|
||||
}
|
||||
|
||||
// Adjust Lightness
|
||||
l = Math.max(0, Math.min(1, l + amount));
|
||||
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||
const p = 2 * l - q;
|
||||
return int2Hex(hue2rgb(p, q, h + 1/3), hue2rgb(p, q, h), hue2rgb(p, q, h - 1/3));
|
||||
};
|
||||
|
||||
47
src/csv.ts
47
src/csv.ts
@@ -14,41 +14,55 @@ import {LETTER_LIST} from './string.ts';
|
||||
export function fromCsv<T = any>(csv: string, hasHeaders = true): T[] {
|
||||
function parseLine(line: string): (string | null)[] {
|
||||
const columns: string[] = [];
|
||||
let current = '', inQuotes = false;
|
||||
let current = '', inQuotes = false, quoteChar: string | null = null;
|
||||
for (let i = 0; i < line.length; i++) {
|
||||
const char = line[i];
|
||||
const nextChar = line[i + 1];
|
||||
if (char === '"') {
|
||||
if (inQuotes && nextChar === '"') {
|
||||
current += '"'; // Handle escaped quotes
|
||||
if ((char === '"' || char === "'") && !inQuotes) {
|
||||
inQuotes = true;
|
||||
quoteChar = char;
|
||||
} else if (char === quoteChar && inQuotes) {
|
||||
if (nextChar === quoteChar) {
|
||||
current += quoteChar; // Handle escaped quotes
|
||||
i++;
|
||||
} else inQuotes = !inQuotes;
|
||||
} else {
|
||||
inQuotes = false;
|
||||
quoteChar = null;
|
||||
}
|
||||
} else if (char === ',' && !inQuotes) {
|
||||
columns.push(current.trim()); // Trim column values
|
||||
columns.push(current.trim());
|
||||
current = '';
|
||||
} else current += char;
|
||||
}
|
||||
columns.push(current.trim()); // Trim last column value
|
||||
return columns.map(col => col.replace(/^"|"$/g, '').replace(/""/g, '"'));
|
||||
columns.push(current.trim());
|
||||
return columns.map(col => {
|
||||
// Remove surrounding quotes (both " and ')
|
||||
col = col.replace(/^["']|["']$/g, '');
|
||||
// Unescape doubled quotes
|
||||
return col.replace(/""/g, '"').replace(/''/g, "'");
|
||||
});
|
||||
}
|
||||
|
||||
// Normalize line endings and split rows
|
||||
const rows = [];
|
||||
let currentRow = '', inQuotes = false;
|
||||
for (const char of csv.replace(/\r\n/g, '\n')) { // Normalize \r\n to \n
|
||||
if (char === '"') inQuotes = !inQuotes;
|
||||
let currentRow = '', inQuotes = false, quoteChar: string | null = null;
|
||||
for (const char of csv.replace(/\r\n/g, '\n')) {
|
||||
if ((char === '"' || char === "'") && !inQuotes) {
|
||||
inQuotes = true;
|
||||
quoteChar = char;
|
||||
} else if (char === quoteChar && inQuotes) {
|
||||
inQuotes = false;
|
||||
quoteChar = null;
|
||||
}
|
||||
if (char === '\n' && !inQuotes) {
|
||||
rows.push(currentRow.trim()); // Trim row
|
||||
rows.push(currentRow.trim());
|
||||
currentRow = '';
|
||||
} else currentRow += char;
|
||||
}
|
||||
if (currentRow) rows.push(currentRow.trim()); // Trim last row
|
||||
if (currentRow) rows.push(currentRow.trim());
|
||||
|
||||
// Extract headers
|
||||
let headers: any = hasHeaders ? rows.splice(0, 1)[0] : null;
|
||||
if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g)?.map((h: any) => h.trim());
|
||||
|
||||
// Parse rows
|
||||
return <T[]>rows.map(r => {
|
||||
const props = parseLine(r);
|
||||
const h = headers || (Array(props.length).fill(null).map((_, i) => {
|
||||
@@ -65,7 +79,6 @@ export function fromCsv<T = any>(csv: string, hasHeaders = true): T[] {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert an array of objects to a CSV string
|
||||
*
|
||||
|
||||
@@ -113,6 +113,18 @@ export class NotAcceptableError extends CustomError {
|
||||
}
|
||||
}
|
||||
|
||||
export class TooManyRequestsError extends CustomError {
|
||||
static code = 429;
|
||||
|
||||
constructor(message: string = 'Rate Limit Reached') {
|
||||
super(message);
|
||||
}
|
||||
|
||||
static instanceof(err: Error) {
|
||||
return (<any>err).constructor.code == this.code;
|
||||
}
|
||||
}
|
||||
|
||||
export class InternalServerError extends CustomError {
|
||||
static code = 500;
|
||||
|
||||
|
||||
24
src/files.ts
24
src/files.ts
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
65
src/html.ts
Normal file
65
src/html.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Decode HTML escaped characters
|
||||
* @param html HTML to clean up
|
||||
* @returns {any}
|
||||
*/
|
||||
export function decodeHtml(html: string) {
|
||||
return html
|
||||
.replace(/ /g, '\u00A0')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, "'")
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/¢/g, '¢')
|
||||
.replace(/£/g, '£')
|
||||
.replace(/¥/g, '¥')
|
||||
.replace(/€/g, '€')
|
||||
.replace(/©/g, '©')
|
||||
.replace(/®/g, '®')
|
||||
.replace(/™/g, '™')
|
||||
.replace(/×/g, '×')
|
||||
.replace(/÷/g, '÷')
|
||||
.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec))
|
||||
.replace(/&#x([0-9a-fA-F]+);/g, (match, hex) => String.fromCharCode(parseInt(hex, 16)))
|
||||
.replace(/&/g, '&'); // Always last!
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse markdown headers
|
||||
*
|
||||
* **NOTE: frontmatter parsing only works 2 layers deep**
|
||||
*
|
||||
* @param {string} content
|
||||
* @returns {{meta: any, content: string} | {meta: {}, content: string}}
|
||||
*/
|
||||
export function parseMarkdown(content: string) {
|
||||
const match = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
|
||||
if (!match) return {meta: {}, content};
|
||||
|
||||
const meta: any = {};
|
||||
let currentParent: string | null = null;
|
||||
|
||||
for (const rawLine of match[1].split('\n')) {
|
||||
if (!rawLine.trim()) continue;
|
||||
const indented = /^\s+/.test(rawLine);
|
||||
const line = rawLine.trim();
|
||||
const colonIdx = line.indexOf(':');
|
||||
if (colonIdx === -1) continue;
|
||||
|
||||
const key = line.slice(0, colonIdx).trim();
|
||||
const value = line.slice(colonIdx + 1).trim();
|
||||
|
||||
let parsed: any = value;
|
||||
try { parsed = JSON.parse(value); } catch {}
|
||||
|
||||
if (!indented) {
|
||||
currentParent = value === '' ? key : null;
|
||||
if (value === '') meta[key] = {};
|
||||
else meta[key] = parsed;
|
||||
} else if (currentParent) {
|
||||
meta[currentParent][key] = parsed;
|
||||
}
|
||||
}
|
||||
|
||||
return {meta, content: match[2].trim()};
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
export * from 'var-persist';
|
||||
|
||||
export * from './arg-parser';
|
||||
export * from './array';
|
||||
export * from './aset';
|
||||
@@ -5,9 +7,10 @@ export * from './cache';
|
||||
export * from './color';
|
||||
export * from './csv';
|
||||
export * from './database';
|
||||
export * from './files';
|
||||
export * from './emitter';
|
||||
export * from './errors';
|
||||
export * from './files';
|
||||
export * from './html';
|
||||
export * from './http';
|
||||
export * from './json';
|
||||
export * from './jwt';
|
||||
@@ -21,5 +24,6 @@ export * from './search';
|
||||
export * from './string';
|
||||
export * from './template';
|
||||
export * from './time';
|
||||
export * from './tts';
|
||||
export * from './types';
|
||||
export * from 'var-persist';
|
||||
export * from './xml';
|
||||
|
||||
@@ -30,6 +30,10 @@ export function dec2Frac(num: number, maxDen=1000): string {
|
||||
(numerator ? numerator + '/' + closest.d : '');
|
||||
}
|
||||
|
||||
export function dec2Hex(num: number): string {
|
||||
const hex = Math.round(num * 255).toString(16);
|
||||
return hex.length === 1 ? '0' + hex : hex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert fraction to decimal number
|
||||
@@ -42,7 +46,7 @@ export function dec2Frac(num: number, maxDen=1000): string {
|
||||
* @param {string} frac Fraction to convert
|
||||
* @return {number} Faction as a decimal
|
||||
*/
|
||||
export function fracToDec(frac: string) {
|
||||
export function frac2Dec(frac: string) {
|
||||
let split = frac.split(' ');
|
||||
const whole = split.length == 2 ? Number(split[0]) : 0;
|
||||
split = (<string>split.pop()).split('/');
|
||||
|
||||
17
src/misc.ts
17
src/misc.ts
@@ -76,6 +76,23 @@ export function gravatar(email: string, def='mp') {
|
||||
return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if IP address falls within CIDR range
|
||||
* @param {string} ip IPV4 to check (192.168.0.12)
|
||||
* @param {string} cidr IP range to check against (example: 192.168.0.0/24)
|
||||
* @returns {boolean} Whether IP address is within range
|
||||
*/
|
||||
export function matchesCidr(ip: string, cidr: string): boolean {
|
||||
if(!cidr) return true;
|
||||
if(!ip) return false;
|
||||
if(!cidr?.includes('/')) return ip === cidr; // Single IP
|
||||
const [range, bits] = cidr.split('/');
|
||||
const mask = ~(2 ** (32 - parseInt(bits)) - 1);
|
||||
const ipToInt = (str: string) => str.split('.')
|
||||
.reduce((int, octet) => (int << 8) + parseInt(octet), 0) >>> 0;
|
||||
return (ipToInt(ip) & mask) === (ipToInt(range) & mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert IPv6 to v4 because who uses that, NAT4Life
|
||||
* @param {string} ip IPv6 address, e.g. 2001:0db8:85a3:0000:0000:8a2e:0370:7334
|
||||
|
||||
@@ -65,6 +65,8 @@ export class PathEvent {
|
||||
module!: string;
|
||||
/** Entire path, including the module & name */
|
||||
fullPath!: string;
|
||||
/** Parent directory, excludes module & name */
|
||||
dir!: string;
|
||||
/** Path including the name, excluding the module */
|
||||
path!: string;
|
||||
/** Last segment of path */
|
||||
@@ -74,8 +76,18 @@ export class PathEvent {
|
||||
/** Whether this path contains glob patterns */
|
||||
hasGlob!: boolean;
|
||||
|
||||
/** Internal cache for PathEvent instances to avoid redundant parsing */
|
||||
private static pathEventCache: Map<string, PathEvent> = new Map();
|
||||
/** Internal cache for parsed path data (plain objects, not instances) */
|
||||
private static pathEventCache: Map<string, {
|
||||
module: string;
|
||||
fullPath: string;
|
||||
dir: string;
|
||||
path: string;
|
||||
name: string;
|
||||
methods: Method[];
|
||||
hasGlob: boolean;
|
||||
}> = new Map();
|
||||
/** Max size for path cache before LRU eviction */
|
||||
private static readonly MAX_PATH_CACHE_SIZE = 1000;
|
||||
/** Cache for compiled permissions (path + required permissions → result) */
|
||||
private static permissionCache: Map<string, PathEvent> = new Map();
|
||||
/** Max size for permission cache before LRU eviction */
|
||||
@@ -109,8 +121,20 @@ export class PathEvent {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check cache and reconstruct from plain object
|
||||
if(PathEvent.pathEventCache.has(e)) {
|
||||
Object.assign(this, PathEvent.pathEventCache.get(e)!);
|
||||
const cached = PathEvent.pathEventCache.get(e)!;
|
||||
// Move to end (LRU - most recently used)
|
||||
PathEvent.pathEventCache.delete(e);
|
||||
PathEvent.pathEventCache.set(e, cached);
|
||||
|
||||
this.module = cached.module;
|
||||
this.fullPath = cached.fullPath;
|
||||
this.dir = cached.dir;
|
||||
this.path = cached.path;
|
||||
this.name = cached.name;
|
||||
this.methods = new ASet(cached.methods);
|
||||
this.hasGlob = cached.hasGlob;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -121,22 +145,54 @@ export class PathEvent {
|
||||
if(p === '' || p === undefined || p === '*') {
|
||||
this.module = '';
|
||||
this.path = '';
|
||||
this.dir = '';
|
||||
this.fullPath = '**';
|
||||
this.name = '';
|
||||
this.methods = new ASet<Method>(p === '*' ? ['*'] : <any>method.split(''));
|
||||
this.hasGlob = true;
|
||||
PathEvent.pathEventCache.set(e, this);
|
||||
|
||||
// LRU eviction
|
||||
if(PathEvent.pathEventCache.size >= PathEvent.MAX_PATH_CACHE_SIZE) {
|
||||
const firstKey = PathEvent.pathEventCache.keys().next().value;
|
||||
if(firstKey) PathEvent.pathEventCache.delete(firstKey);
|
||||
}
|
||||
|
||||
PathEvent.pathEventCache.set(e, {
|
||||
module: this.module,
|
||||
fullPath: this.fullPath,
|
||||
dir: this.dir,
|
||||
path: this.path,
|
||||
name: this.name,
|
||||
methods: [...this.methods],
|
||||
hasGlob: this.hasGlob
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
let temp = p.split('/').filter(p => !!p);
|
||||
this.module = temp.splice(0, 1)[0] || '';
|
||||
this.path = temp.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('*');
|
||||
this.methods = new ASet(<any>method.split(''));
|
||||
PathEvent.pathEventCache.set(e, this);
|
||||
|
||||
// LRU eviction
|
||||
if(PathEvent.pathEventCache.size >= PathEvent.MAX_PATH_CACHE_SIZE) {
|
||||
const firstKey = PathEvent.pathEventCache.keys().next().value;
|
||||
if(firstKey) PathEvent.pathEventCache.delete(firstKey);
|
||||
}
|
||||
|
||||
PathEvent.pathEventCache.set(e, {
|
||||
module: this.module,
|
||||
fullPath: this.fullPath,
|
||||
dir: this.dir,
|
||||
path: this.path,
|
||||
name: this.name,
|
||||
methods: [...this.methods],
|
||||
hasGlob: this.hasGlob
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
175
src/search.ts
175
src/search.ts
@@ -1,44 +1,122 @@
|
||||
import {JSONAttemptParse, JSONSerialize} from './json.ts';
|
||||
import {dotNotation} from './objects.ts';
|
||||
|
||||
/**
|
||||
* Filters an array of objects based on a search term and optional regex checking.
|
||||
*
|
||||
* @param {Array} rows Array of objects to filter
|
||||
* @param {string} search The logic string or regext to filter on
|
||||
* @param {boolean} [regex=false] Treat search expression as regex
|
||||
* @param {Function} [transform=(r) => r] - Transform rows before filtering
|
||||
* @return {Array} The filtered array of objects that matched search
|
||||
*/
|
||||
export function search(rows: any[], search: string, regex?: boolean, transform: Function = (r: any) => r) {
|
||||
if(!rows) return [];
|
||||
return rows.filter(r => {
|
||||
// Empty search
|
||||
if(!search) return true;
|
||||
const value = transform(r);
|
||||
// Regex search
|
||||
if(regex) {
|
||||
return !!Object.values(value).filter((v: any) => {
|
||||
try { return RegExp(search, 'gm').test(v.toString()); }
|
||||
catch { return false; }
|
||||
}).length
|
||||
} else {
|
||||
return logicTest(value, search);
|
||||
}
|
||||
});
|
||||
const VALID_FLAGS = new Set([...'dgimsuvy']);
|
||||
|
||||
function toRegex(pattern: string, defaultFlags = 'gm'): RegExp | null {
|
||||
const lit = /^\/(.+)\/([a-zA-Z]*)$/.exec(pattern);
|
||||
try { return lit ? new RegExp(lit[1], lit[2]) : new RegExp(pattern, defaultFlags); }
|
||||
catch { return null; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Test an object against a logic condition. By default values are checked
|
||||
* @param {string} condition
|
||||
* @param {object} target
|
||||
* @return {boolean}
|
||||
* Filters an array of objects based on a query string.
|
||||
*
|
||||
* Supports plain text, regex, boolean/logical operators, and dataset helpers.
|
||||
*
|
||||
* **Examples**
|
||||
* ```js
|
||||
* search(rows: T[], 'alice'): T[] // Case-Insensitive
|
||||
* search(rows: T[], 'Alice'): T[] // Case-Sensitive
|
||||
*
|
||||
* search(rows: T[], 'name = Alice'): T[] // loose equality
|
||||
* search(rows: T[], 'name != Alice'): T[] // loose inequality
|
||||
* search(rows: T[], 'role += admin'): T[] // Contains
|
||||
* search(rows: T[], 'status -= archived'): T[] // Not Contain
|
||||
* search(rows: T[], 'age > 18'): T[] // Greater Than
|
||||
* search(rows: T[], 'age >= 18'): T[] // Greater Than or Equal
|
||||
* search(rows: T[], 'age < 18'): T[] // Less Than
|
||||
* search(rows: T[], 'age <= 18'): T[] // Less Than or Equal
|
||||
*
|
||||
* search(rows: T[], '/^alice/gi'): T[] // Global Regex
|
||||
* search(rows: T[], 'name =~ ^Al'): T[] // Regex Match (shorthand)
|
||||
* search(rows: T[], 'name =~ /^al/i'): T[] // Regex Match (with flags)
|
||||
* search(rows: T[], 'email !~ /@test\.com$/i'): T[] // Regex Not Match
|
||||
*
|
||||
* search(rows: T[], 'unique(email)'): T[] // Unique property values
|
||||
* search(rows: T[], 'duplicate(email)'): T[] // Duplicate property values
|
||||
* search(rows: T[], 'distinct(type)'): T[] // Distinct property values
|
||||
*
|
||||
* search(rows: T[], 'active && role != admin'): T[] // ANDs
|
||||
* search(rows: T[], 'email != null || distinct(email)'): T[] // ORs
|
||||
* ```
|
||||
*
|
||||
* @param rows - Array of objects to filter
|
||||
* @param query - Query string; see supported syntax above
|
||||
* @param transform - Optional transform applied to each row before matching
|
||||
* @returns The filtered array of rows
|
||||
*/
|
||||
export function search(rows: any[], query: string, transform: (r: any) => any = r => r): any[] {
|
||||
if (!rows || !query?.trim()) return rows ?? [];
|
||||
|
||||
const q = query.trim();
|
||||
|
||||
// Global regex: /pattern/flags — strict, whole string, at least one valid flag
|
||||
const globalRegex = /^\/(.+)\/([a-zA-Z]+)$/.exec(q);
|
||||
if (globalRegex && [...globalRegex[2]].every(f => VALID_FLAGS.has(f))) {
|
||||
const re = toRegex(q);
|
||||
return rows.filter(r => re && Object.values(transform(r)).some((v: any) => {
|
||||
try { return re.test(v?.toString() ?? ''); } catch { return false; }
|
||||
}));
|
||||
}
|
||||
|
||||
// Split top-level && into predicates and dataset helpers
|
||||
const parts = q.split('&&').map(p => p.trim());
|
||||
const helpers = parts.filter(p => /^(unique|duplicate|distinct)\(\w+\)$/.test(p));
|
||||
const predicate = parts.filter(p => !helpers.includes(p)).join(' && ');
|
||||
|
||||
let filtered = predicate
|
||||
? rows.filter(r => logicTest(transform(r), predicate))
|
||||
: [...rows];
|
||||
|
||||
for (const h of helpers) {
|
||||
const [, fn, field] = /^(\w+)\((\w+)\)$/.exec(h)!;
|
||||
if (fn === 'distinct') continue; // run last
|
||||
const freq = new Map<any, number>();
|
||||
filtered.forEach(r => { const v = dotNotation(transform(r), field); freq.set(v, (freq.get(v) ?? 0) + 1); });
|
||||
filtered = filtered.filter(r => fn === 'unique' ? freq.get(dotNotation(transform(r), field)) === 1 : (freq.get(dotNotation(transform(r), field)) ?? 0) > 1);
|
||||
}
|
||||
|
||||
for (const h of helpers.filter(h => h.startsWith('distinct'))) {
|
||||
const field = /\((\w+)\)/.exec(h)![1];
|
||||
const seen = new Set();
|
||||
filtered = filtered.filter(r => { const v = dotNotation(transform(r), field); return seen.has(v) ? false : !!seen.add(v); });
|
||||
}
|
||||
|
||||
return filtered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests object against a logic string.
|
||||
*
|
||||
* **Property operators**
|
||||
* ```
|
||||
* 'alice' // case-insensitive
|
||||
* 'Alice' // case-sensitive
|
||||
* 'name = Alice' // loose equality (==)
|
||||
* 'name == Alice' // loose equality
|
||||
* 'name != Alice' // loose inequality
|
||||
* 'role += admin' // field contains value
|
||||
* 'status -= archived' // field does not contain value
|
||||
* 'age > 21' // greater than
|
||||
* 'age >= 21' // greater than or equal
|
||||
* 'age < 21' // less than
|
||||
* 'age <= 21' // less than or equal
|
||||
* 'name =~ ^Al' // regex match (shorthand)
|
||||
* 'name =~ /^al/i' // regex match (with flags)
|
||||
* 'email !~ /@test\.com$/i' // regex not match
|
||||
* 'status = active && role != admin' // ANDs
|
||||
* 'status = active || status = pending' // ORs
|
||||
* ```
|
||||
*
|
||||
* @param target - The object to test
|
||||
* @param condition - The condition string; see supported syntax above
|
||||
* @returns Whether the object satisfies the condition
|
||||
*/
|
||||
export function logicTest(target: object, condition: string): boolean {
|
||||
const evalBoolean = (a: any, op: string, b: any): boolean => {
|
||||
switch(op) {
|
||||
case '=':
|
||||
case '==': return a == b;
|
||||
switch (op) {
|
||||
case '=': case '==': return a == b;
|
||||
case '!=': return a != b;
|
||||
case '+=': return a?.toString().includes(b);
|
||||
case '-=': return !a?.toString().includes(b);
|
||||
@@ -46,26 +124,23 @@ export function logicTest(target: object, condition: string): boolean {
|
||||
case '>=': return a >= b;
|
||||
case '<': return a < b;
|
||||
case '<=': return a <= b;
|
||||
case '~=': try { return !!toRegex(b)?.test(a?.toString() ?? ''); } catch { return false; }
|
||||
case '!~': try { return !toRegex(b)?.test(a?.toString() ?? ''); } catch { return false; }
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const or = condition.split('||').map(p => p.trim()).filter(p => !!p);
|
||||
return -1 != or.findIndex(p => {
|
||||
// Make sure all ANDs pass
|
||||
const and = p.split('&&').map(p => p.trim()).filter(p => !!p);
|
||||
return and.filter(p => {
|
||||
// Boolean operator
|
||||
const prop = /(\S+)\s*(==?|!=|\+=|-=|>=|>|<=|<)\s*(\S+)/g.exec(p);
|
||||
if(prop) {
|
||||
const key = Object.keys(target).find(k => k.toLowerCase() == prop[1].toLowerCase());
|
||||
return evalBoolean(dotNotation<any>(target, key || prop[1]), prop[2], JSONAttemptParse(prop[3]));
|
||||
}
|
||||
// Case-sensitive
|
||||
const resolve = (key: string) => dotNotation<any>(target, Object.keys(target).find(k => k.toLowerCase() === key.toLowerCase()) ?? key);
|
||||
|
||||
const evalExpr = (expr: string): boolean => {
|
||||
const e = expr.trim();
|
||||
const prop = /^(\S+)\s*(==?|!=|~=|!~|\+=|-=|>=|>|<=|<)\s*(.+)$/.exec(e);
|
||||
if (prop) return evalBoolean(resolve(prop[1]), prop[2], JSONAttemptParse(prop[3].trim()));
|
||||
const v = Object.values(target).map(JSONSerialize).join('');
|
||||
if(/[A-Z]/g.test(condition)) return v.includes(p);
|
||||
// Case-insensitive
|
||||
return v.toLowerCase().includes(p);
|
||||
}).length == and.length;
|
||||
});
|
||||
return /[A-Z]/.test(e) ? v.includes(e) : v.toLowerCase().includes(e.toLowerCase());
|
||||
};
|
||||
|
||||
return condition.split('||').map(p => p.trim()).filter(Boolean).some(group =>
|
||||
group.split('&&').map(p => p.trim()).filter(Boolean).every(evalExpr)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ export function camelCase(str?: string): string {
|
||||
return pascal.charAt(0).toLowerCase() + pascal.slice(1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert number of bytes into a human-readable size
|
||||
*
|
||||
@@ -100,7 +99,6 @@ export function kebabCase(str?: string): string {
|
||||
return wordSegments(str).map(w => w.toLowerCase()).join("-");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add padding to string
|
||||
*
|
||||
@@ -135,7 +133,15 @@ export function pascalCase(str?: string): string {
|
||||
.join('');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove all emojis from a string
|
||||
* @param {string} str Input string with emojis
|
||||
* @returns {string} Sanitized string without emojis
|
||||
*/
|
||||
export function removeEmojis(str: string): string {
|
||||
const emojiRegex = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud83c[\udde6-\uddff]|[\ud83d[\ude00-\ude4f]|[\ud83d[\ude80-\udeff]|[\ud83c[\udd00-\uddff]|[\ud83d[\ude50-\ude7f]|[\u2600-\u26ff]|[\u2700-\u27bf]|[\ud83e[\udd00-\uddff]|[\ud83c[\udf00-\uffff]|[\ud83d[\ude00-\udeff]|[\ud83c[\udde6-\uddff])/g;
|
||||
return str.replace(emojiRegex, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random hexadecimal value
|
||||
@@ -208,7 +214,6 @@ export function snakeCase(str?: string): string {
|
||||
return wordSegments(str).map(w => w.toLowerCase()).join("_");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Splice a string together (Similar to Array.splice)
|
||||
*
|
||||
@@ -224,6 +229,16 @@ export function strSplice(str: string, start: number, deleteCount: number, inser
|
||||
return before + insert + after;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts text to Title Case
|
||||
*/
|
||||
export function titleCase(str?: string): string {
|
||||
if(!str) return '';
|
||||
return wordSegments(str)
|
||||
.map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
|
||||
.join(' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all substrings that match a given pattern.
|
||||
*
|
||||
@@ -234,19 +249,16 @@ export function strSplice(str: string, start: number, deleteCount: number, inser
|
||||
* @return {RegExpExecArray[]} Found matches.
|
||||
*/
|
||||
export function matchAll(value: string, regex: RegExp | string): RegExpExecArray[] {
|
||||
if(typeof regex === 'string') {
|
||||
regex = new RegExp(regex, 'g');
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/a/60290199
|
||||
if(!regex.global) {
|
||||
throw new TypeError('Regular expression must be global.');
|
||||
}
|
||||
if(typeof regex === 'string') regex = new RegExp(regex, 'g');
|
||||
if(!regex.global) throw new TypeError('Regular expression must be global.');
|
||||
|
||||
let ret: RegExpExecArray[] = [];
|
||||
let match: RegExpExecArray | null;
|
||||
while((match = regex.exec(value)) !== null) {
|
||||
ret.push(match);
|
||||
if(match[0].length === 0) {
|
||||
regex.lastIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
264
src/template.ts
264
src/template.ts
@@ -5,9 +5,86 @@ import {formatDate} from './time.ts';
|
||||
|
||||
export class TemplateError extends BadRequestError { }
|
||||
|
||||
export function findTemplateVars(html: string): Record<string, any> {
|
||||
const variables = new Set<string>();
|
||||
const arrays = new Set<string>();
|
||||
const excluded = new Set([
|
||||
'let', 'const', 'var', 'function', 'if', 'while', 'do', 'this', 'typeof',
|
||||
'new', 'instanceof', 'in', 'for', 'else', 'case', 'break', 'continue',
|
||||
'switch', 'default', 'with', 'eval', 'arguments', 'void', 'delete', 'null',
|
||||
'undefined', 'true', 'false', 'async', 'await', 'try', 'catch', 'finally',
|
||||
'throw', 'return', 'yield', 'debugger', 'extends', 'import', 'export',
|
||||
'class', 'super', 'static', 'get', 'set', 'constructor', 'enum', 'implements',
|
||||
'interface', 'package', 'private', 'protected', 'public', 'abstract', 'final',
|
||||
'native', 'synchronized', 'throws', 'transient', 'volatile'
|
||||
]);
|
||||
|
||||
// Extract & exclude loop variables, mark arrays
|
||||
for (const loop of matchAll(html, /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}/g)) {
|
||||
const [element, index = 'index'] = loop[1].replaceAll(/[()\s]/g, '').split(',');
|
||||
excluded.add(element);
|
||||
excluded.add(index);
|
||||
const arrayVar = loop[2].trim();
|
||||
const root = arrayVar.split('.')[0];
|
||||
if(!excluded.has(root)) {
|
||||
variables.add(arrayVar);
|
||||
arrays.add(arrayVar);
|
||||
}
|
||||
}
|
||||
|
||||
// Extract variables from if/else-if conditions
|
||||
for (const ifStmt of matchAll(html, /\{\{\s*?[!]?\?\s*?([^}]+?)\s*?}}/g)) {
|
||||
const code = ifStmt[1].replace(/["'`][^"'`]*["'`]/g, '');
|
||||
const cleaned = code.replace(/([a-zA-Z_$][a-zA-Z0-9_$.]*)\s*\(/g, (_, v) => {
|
||||
const parts = v.split('.');
|
||||
return parts.length > 1 ? parts.slice(0, -1).join('.') + ' ' : '';
|
||||
});
|
||||
const vars = cleaned.match(/[a-zA-Z_$][a-zA-Z0-9_$.]+/g) || [];
|
||||
for (const v of vars) {
|
||||
const root = v.split('.')[0];
|
||||
if(!excluded.has(root)) variables.add(v);
|
||||
}
|
||||
}
|
||||
|
||||
// Extract from if block content & regular interpolations
|
||||
const regex = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g;
|
||||
let match;
|
||||
while ((match = regex.exec(html)) !== null) {
|
||||
const code = match[1].trim().replace(/["'`][^"'`]*["'`]/g, '');
|
||||
const cleaned = code.replace(/([a-zA-Z_$][a-zA-Z0-9_$.]*)\s*\(/g, (_, v) => {
|
||||
const parts = v.split('.');
|
||||
return parts.length > 1 ? parts.slice(0, -1).join('.') + ' ' : '';
|
||||
});
|
||||
const vars = cleaned.match(/[a-zA-Z_$][a-zA-Z0-9_$.]+/g) || [];
|
||||
for (const v of vars) {
|
||||
const root = v.split('.')[0];
|
||||
if(!excluded.has(root)) variables.add(v);
|
||||
}
|
||||
}
|
||||
|
||||
const result: Record<string, any> = {};
|
||||
for (const path of variables) {
|
||||
const parts = path.split('.');
|
||||
let current = result;
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const part = parts[i];
|
||||
if(i === parts.length - 1) {
|
||||
const fullPath = parts.slice(0, i + 1).join('.');
|
||||
current[part] = arrays.has(fullPath) ? [] : '';
|
||||
} else {
|
||||
current[part] = current[part] || {};
|
||||
current = current[part];
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function renderTemplate(template: string, data: any, fetch?: (file: string) => Promise<string>) {
|
||||
let content = template, found: any;
|
||||
const now = new Date(), d = {
|
||||
if(!fetch) fetch = (file) => { throw new TemplateError(`Unable to fetch template: ${file}`); }
|
||||
|
||||
const now = new Date();
|
||||
const d = {
|
||||
date: {
|
||||
day: now.getDate(),
|
||||
month: now.toLocaleString('default', { month: 'long' }),
|
||||
@@ -18,64 +95,157 @@ export async function renderTemplate(template: string, data: any, fetch?: (file:
|
||||
...(data || {}),
|
||||
};
|
||||
|
||||
if(!fetch) fetch = (file) => {
|
||||
throw new TemplateError(`Unable to fetch template: ${file}`);
|
||||
}
|
||||
|
||||
const evaluate = (code: string, data: object, fatal = true) => {
|
||||
try {
|
||||
return Function('data', `Object.assign(this, data); return ${code};`)(data);
|
||||
} catch {
|
||||
if(fatal) throw new TemplateError(`Failed to evaluate: ${code}`);
|
||||
else return false;
|
||||
return Function('data', `with(data) { return ${code}; }`)(data);
|
||||
} catch(err: any) {
|
||||
if(fatal) throw new TemplateError(`Failed to evaluate: ${code}\n${err.message || err.toString()}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// If Statements - Optimize what we render: `{{ ? javascript }} TRUE CONTENT {{ !? }} FALSE CONTENT {{ /? }}`
|
||||
while(!!(found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)(?:\{\{\s*?!\?\s*?}}([\s\S]*?))?\{\{\s*?\/\?\s*?}}/g.exec(content))) {
|
||||
const nested = matchAll(found[0], /\{\{\s*?\?.+?}}/g).slice(-1)?.[0]?.index;
|
||||
if(nested != 0)
|
||||
found = /\{\{\s*?\?\s*?(.+?)\s*?}}([\s\S]*?)(?:\{\{\s*?!\?\s*?}}([\s\S]*?))?\{\{\s*?\/\?\s*?}}/g.exec(content.slice(found.index + nested))
|
||||
content = content.replace(found[0], (evaluate(found[1], d, false) ? found[2] : found[3]) || '');
|
||||
async function process(content: string, ctx: object = d): Promise<string> {
|
||||
let result = content;
|
||||
|
||||
// Process extends first (they wrap everything)
|
||||
const extendsMatch = result.match(/\{\{\s*>\s*(.+?):(.+?)\s*}}([\s\S]*?)\{\{\s*\/>\s*}}/);
|
||||
if(extendsMatch) {
|
||||
const parentTemplate = await (<Function>fetch)(extendsMatch[1].trim());
|
||||
if(!parentTemplate) throw new TemplateError(`Unknown extended template: ${extendsMatch[1].trim()}`);
|
||||
const slotName = extendsMatch[2].trim();
|
||||
const slotContent = await process(extendsMatch[3], ctx);
|
||||
return process(parentTemplate, {...ctx, [slotName]: slotContent});
|
||||
}
|
||||
|
||||
// Imports - We render bottom up: `{{ < file.html }}`
|
||||
while(!!(found = /\{\{\s*?<\s*?(.+?)\s*?}}/g.exec(content))) {
|
||||
content = content.replace(found[0], await renderTemplate(await fetch(found[1].trim()), data, fetch));
|
||||
let changed = true;
|
||||
while(changed) {
|
||||
changed = false;
|
||||
const before = result;
|
||||
|
||||
// Process imports
|
||||
const importMatch = result.match(/\{\{\s*<\s*(.+?)\s*}}/);
|
||||
if(importMatch) {
|
||||
const t = await (<Function>fetch)(importMatch[1].trim());
|
||||
if(!t) throw new TemplateError(`Unknown imported template: ${importMatch[1].trim()}`);
|
||||
const rendered = await process(t, ctx);
|
||||
result = result.replace(importMatch[0], rendered);
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// For Loops: `{{ * (row, index) in invoice }} CONTENT {{ /* }}`
|
||||
while(!!(found = /\{\{\s*?\*\s*?(.+?)\s+in\s+(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/\*\s*?}}/g.exec(content))) {
|
||||
const split = found[1].replaceAll(/[()\s]/g, '').split(',');
|
||||
const element = split[0];
|
||||
const index = split[1] || 'index';
|
||||
const array: any[] = <any>dotNotation(data, found[2]);
|
||||
if(!array || typeof array != 'object')
|
||||
throw new TemplateError(`Cannot iterate: ${found[2]}`);
|
||||
|
||||
let compiled = [];
|
||||
for(let i = 0; i < array.length; i++) {
|
||||
compiled.push(renderTemplate(found[3], {
|
||||
...d,
|
||||
[element]: array[i],
|
||||
[index]: i
|
||||
}, fetch))
|
||||
}
|
||||
content = content.replace(found[0], compiled.join('\n'));
|
||||
// Process for-loops (innermost first)
|
||||
const forMatch = findInnermostFor(result);
|
||||
if(forMatch) {
|
||||
const { full, vars, array, body, start } = forMatch;
|
||||
const [element, index = 'index'] = vars.split(',').map(v => v.trim());
|
||||
const arr: any[] = <any>dotNotation(ctx, array);
|
||||
if(!arr || typeof arr != 'object') throw new TemplateError(`Cannot iterate: ${array}`);
|
||||
let output = [];
|
||||
for(let i = 0; i < arr.length; i++)
|
||||
output.push(await process(body, {...ctx, [element]: arr[i], [index]: i}));
|
||||
result = result.slice(0, start) + output.join('\n') + result.slice(start + full.length);
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Evaluate whatever is left - Should come last: `{{ javascript }}`
|
||||
while(!!(found = /\{\{\s*([^<>\*\?!/}\s][^}]*?)\s*}}/g.exec(content))) {
|
||||
content = content.replace(found[0], evaluate(found[1].trim(), d) || '');
|
||||
// Process if-statements (innermost first)
|
||||
const ifMatch = findInnermostIf(result);
|
||||
if(ifMatch) {
|
||||
const { full, condition, body, start } = ifMatch;
|
||||
const branches = parseIfBranches(body);
|
||||
let output = '';
|
||||
if(evaluate(condition, ctx, false)) {
|
||||
output = branches.if;
|
||||
} else {
|
||||
for(const branch of branches.elseIf) {
|
||||
if(evaluate(branch.condition, ctx, false)) {
|
||||
output = branch.body;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!output && branches.else) output = branches.else;
|
||||
}
|
||||
result = result.slice(0, start) + output + result.slice(start + full.length);
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if(before === result) changed = false;
|
||||
}
|
||||
return processVariables(result, ctx);
|
||||
}
|
||||
|
||||
// Extends: `{{ > file.html:property }} CONTENT {{ /> }}`
|
||||
while(!!(found = /\{\{\s*?>\s*?(.+?):(.+?)\s*?}}([\s\S]*?)\{\{\s*?\/>\s*?}}/g.exec(content))) {
|
||||
content = content.replace(found[0], await renderTemplate(await fetch(found[1].trim), {
|
||||
...data,
|
||||
[found[2].trim()]: found[3],
|
||||
}, fetch));
|
||||
function processVariables(content: string, data: object): string {
|
||||
return content.replace(/\{\{\s*([^<>\*\?!/}\s][^{}]*?)\s*}}/g, (match, code) => {
|
||||
return evaluate(code.trim(), data) ?? '';
|
||||
});
|
||||
}
|
||||
|
||||
return content;
|
||||
function findInnermostIf(content: string) {
|
||||
const regex = /\{\{\s*\?\s*(.+?)\s*}}/g;
|
||||
let match, lastMatch = null;
|
||||
while((match = regex.exec(content)) !== null) {
|
||||
const start = match.index;
|
||||
const condition = match[1];
|
||||
const bodyStart = match.index + match[0].length;
|
||||
const end = findMatchingClose(content, bodyStart, /\{\{\s*\?\s*/, /\{\{\s*\/\?\s*}}/);
|
||||
if(end === -1) throw new TemplateError(`Unmatched if-statement at position ${start}`);
|
||||
const closeTag = content.slice(end).match(/\{\{\s*\/\?\s*}}/);
|
||||
const full = content.slice(start, end + (<any>closeTag)[0].length);
|
||||
const body = content.slice(bodyStart, end);
|
||||
lastMatch = { full, condition, body, start };
|
||||
}
|
||||
return lastMatch;
|
||||
}
|
||||
|
||||
function findInnermostFor(content: string) {
|
||||
const regex = /\{\{\s*\*\s*(.+?)\s+in\s+(.+?)\s*}}/g;
|
||||
let match, lastMatch = null;
|
||||
while((match = regex.exec(content)) !== null) {
|
||||
const start = match.index;
|
||||
const vars = match[1].replaceAll(/[()\s]/g, '');
|
||||
const array = match[2];
|
||||
const bodyStart = match.index + match[0].length;
|
||||
const end = findMatchingClose(content, bodyStart, /\{\{\s*\*\s*/, /\{\{\s*\/\*\s*}}/);
|
||||
if(end === -1) throw new TemplateError(`Unmatched for-loop at position ${start}`);
|
||||
const closeTag = content.slice(end).match(/\{\{\s*\/\*\s*}}/);
|
||||
const full = content.slice(start, end + (<any>closeTag)[0].length);
|
||||
const body = content.slice(bodyStart, end);
|
||||
lastMatch = { full, vars, array, body, start };
|
||||
}
|
||||
return lastMatch;
|
||||
}
|
||||
|
||||
function findMatchingClose(content: string, startIndex: number, openTag: RegExp, closeTag: RegExp): number {
|
||||
let depth = 1, pos = startIndex;
|
||||
while (depth > 0 && pos < content.length) {
|
||||
const remaining = content.slice(pos);
|
||||
const nextOpen = remaining.search(openTag);
|
||||
const nextClose = remaining.search(closeTag);
|
||||
if(nextClose === -1) return -1;
|
||||
if(nextOpen !== -1 && nextOpen < nextClose) {
|
||||
depth++;
|
||||
pos += nextOpen + 1;
|
||||
} else {
|
||||
depth--;
|
||||
if(depth === 0) return pos + nextClose;
|
||||
pos += nextClose + 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function parseIfBranches(body: string) {
|
||||
const parts = body.split(/\{\{\s*!\?\s*/);
|
||||
const result = { if: parts[0], elseIf: [] as any[], else: '' };
|
||||
for(let i = 1; i < parts.length; i++) {
|
||||
const closeBrace = parts[i].indexOf('}}');
|
||||
const condition = parts[i].slice(0, closeBrace).trim();
|
||||
const branchBody = parts[i].slice(closeBrace + 2);
|
||||
if(!condition) result.else = branchBody;
|
||||
else result.elseIf.push({ condition, body: branchBody });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return process(template);
|
||||
}
|
||||
|
||||
77
src/time.ts
77
src/time.ts
@@ -78,19 +78,62 @@ export function dayOfYear(date: Date): number {
|
||||
*
|
||||
* @param {string} format How date string will be formatted, default: `YYYY-MM-DD H:mm A`
|
||||
* @param {Date | number | string} date Date or timestamp, defaults to now
|
||||
* @param tz Set timezone offset
|
||||
* @param tz Set timezone offset in: hours (-4) or minutes (430) or IANA string (America/New_York)
|
||||
* @return {string} Formated date
|
||||
*/
|
||||
export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | number | string = new Date(), tz: string | number = 'local'): string {
|
||||
if (typeof date === 'number' || typeof date === 'string') date = new Date(date);
|
||||
if (isNaN(date.getTime())) throw new Error('Invalid date input');
|
||||
const numericTz = typeof tz === 'number';
|
||||
const localTz = tz === 'local' || (!numericTz && tz.toLowerCase?.() === 'local');
|
||||
const tzName = localTz ? Intl.DateTimeFormat().resolvedOptions().timeZone : numericTz ? 'UTC' : tz;
|
||||
|
||||
const TIMEZONE_MAP = [
|
||||
{ name: 'IDLW', iana: 'Etc/GMT+12', offset: -720 },
|
||||
{ name: 'SST', iana: 'Pacific/Pago_Pago', offset: -660 },
|
||||
{ name: 'HST', iana: 'Pacific/Honolulu', offset: -600 },
|
||||
{ name: 'AKST', iana: 'America/Anchorage', offset: -540 },
|
||||
{ name: 'PST', iana: 'America/Los_Angeles', offset: -480 },
|
||||
{ name: 'MST', iana: 'America/Denver', offset: -420 },
|
||||
{ name: 'CST', iana: 'America/Chicago', offset: -360 },
|
||||
{ name: 'EST', iana: 'America/New_York', offset: -300 },
|
||||
{ name: 'AST', iana: 'America/Halifax', offset: -240 },
|
||||
{ name: 'BRT', iana: 'America/Sao_Paulo', offset: -180 },
|
||||
{ name: 'MAT', iana: 'Atlantic/South_Georgia', offset: -120 },
|
||||
{ name: 'AZOT', iana: 'Atlantic/Azores', offset: -60 },
|
||||
{ name: 'UTC', iana: 'UTC', offset: 0 },
|
||||
{ name: 'CET', iana: 'Europe/Paris', offset: 60 },
|
||||
{ name: 'EET', iana: 'Europe/Athens', offset: 120 },
|
||||
{ name: 'MSK', iana: 'Europe/Moscow', offset: 180 },
|
||||
{ name: 'GST', iana: 'Asia/Dubai', offset: 240 },
|
||||
{ name: 'PKT', iana: 'Asia/Karachi', offset: 300 },
|
||||
{ name: 'IST', iana: 'Asia/Kolkata', offset: 330 },
|
||||
{ name: 'BST', iana: 'Asia/Dhaka', offset: 360 },
|
||||
{ name: 'ICT', iana: 'Asia/Bangkok', offset: 420 },
|
||||
{ name: 'CST', iana: 'Asia/Shanghai', offset: 480 },
|
||||
{ name: 'JST', iana: 'Asia/Tokyo', offset: 540 },
|
||||
{ name: 'AEST', iana: 'Australia/Sydney', offset: 600 },
|
||||
{ name: 'SBT', iana: 'Pacific/Guadalcanal', offset: 660 },
|
||||
{ name: 'TOT', iana: 'Pacific/Tongatapu', offset: 780 },
|
||||
{ name: 'LINT', iana: 'Pacific/Kiritimati', offset: 840 },
|
||||
];
|
||||
|
||||
let numericTz = typeof tz === 'number';
|
||||
const localTz = tz === 'local' || (!numericTz && tz.toString().toLowerCase?.() === 'local');
|
||||
let tzName = localTz ? Intl.DateTimeFormat().resolvedOptions().timeZone : numericTz ? 'UTC' : tz;
|
||||
let offsetMinutes = 0;
|
||||
|
||||
if (numericTz) {
|
||||
// Convert hours to minutes if offset is small (likely hours)
|
||||
offsetMinutes = Math.abs(tz as number) < 24 ? (tz as number) * 60 : (tz as number);
|
||||
|
||||
// Find closest matching timezone
|
||||
const closest = TIMEZONE_MAP.reduce((prev, curr) =>
|
||||
Math.abs(curr.offset - offsetMinutes) < Math.abs(prev.offset - offsetMinutes) ? curr : prev
|
||||
);
|
||||
tzName = closest.iana;
|
||||
}
|
||||
|
||||
if (!numericTz && tzName !== 'UTC') {
|
||||
try {
|
||||
new Intl.DateTimeFormat('en-US', { timeZone: tzName }).format();
|
||||
new Intl.DateTimeFormat('en-US', { timeZone: <string>tzName }).format();
|
||||
} catch {
|
||||
throw new Error(`Invalid timezone: ${tzName}`);
|
||||
}
|
||||
@@ -99,9 +142,10 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
|
||||
let zonedDate = new Date(date);
|
||||
let get: (fn: 'FullYear' | 'Month' | 'Date' | 'Day' | 'Hours' | 'Minutes' | 'Seconds' | 'Milliseconds') => number;
|
||||
const partsMap: Record<string, string> = {};
|
||||
|
||||
if (!numericTz && tzName !== 'UTC') {
|
||||
const parts = new Intl.DateTimeFormat('en-US', {
|
||||
timeZone: tzName,
|
||||
timeZone: <string>tzName,
|
||||
year: 'numeric', month: '2-digit', day: '2-digit', weekday: 'long',
|
||||
hour: '2-digit', minute: '2-digit', second: '2-digit',
|
||||
hour12: false
|
||||
@@ -111,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 => {
|
||||
@@ -127,8 +171,7 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
|
||||
}
|
||||
};
|
||||
} else {
|
||||
const offset = numericTz ? tz as number : 0;
|
||||
zonedDate = new Date(date.getTime() + offset * 60 * 60 * 1000);
|
||||
zonedDate = new Date(date.getTime() + offsetMinutes * 60 * 1000);
|
||||
get = (fn: 'FullYear' | 'Month' | 'Date' | 'Day' | 'Hours' | 'Minutes' | 'Seconds' | 'Milliseconds'): number => zonedDate[`getUTC${fn}`]();
|
||||
}
|
||||
|
||||
@@ -139,14 +182,13 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
|
||||
}
|
||||
|
||||
function getTZOffset(): string {
|
||||
if (numericTz) {
|
||||
const total = (tz as number) * 60;
|
||||
const hours = Math.floor(Math.abs(total) / 60);
|
||||
const mins = Math.abs(total) % 60;
|
||||
return `${tz >= 0 ? '+' : '-'}${String(hours).padStart(2, '0')}:${String(mins).padStart(2, '0')}`;
|
||||
if(numericTz) {
|
||||
const hours = Math.floor(Math.abs(offsetMinutes) / 60);
|
||||
const mins = Math.abs(offsetMinutes) % 60;
|
||||
return `${offsetMinutes >= 0 ? '+' : '-'}${String(hours).padStart(2, '0')}:${String(mins).padStart(2, '0')}`;
|
||||
}
|
||||
try {
|
||||
const offset = new Intl.DateTimeFormat('en-US', {timeZone: tzName, timeZoneName: 'longOffset', hour: '2-digit', minute: '2-digit',})
|
||||
const offset = new Intl.DateTimeFormat('en-US', {timeZone: <string>tzName, timeZoneName: 'longOffset', hour: '2-digit', minute: '2-digit',})
|
||||
.formatToParts(<Date>date).find(p => p.type === 'timeZoneName')?.value.match(/([+-]\d{2}:\d{2})/)?.[1];
|
||||
if (offset) return offset;
|
||||
} catch {}
|
||||
@@ -154,12 +196,11 @@ export function formatDate(format: string = 'YYYY-MM-DD H:mm', date: Date | numb
|
||||
}
|
||||
|
||||
function getTZAbbr(): string {
|
||||
if (numericTz && tz === 0) return 'UTC';
|
||||
try {
|
||||
return new Intl.DateTimeFormat('en-US', { timeZone: tzName, timeZoneName: 'short' })
|
||||
return new Intl.DateTimeFormat('en-US', { timeZone: <string>tzName, timeZoneName: 'short' })
|
||||
.formatToParts(<Date>date).find(p => p.type === 'timeZoneName')?.value || '';
|
||||
} catch {
|
||||
return tzName;
|
||||
return <string>tzName;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
148
src/tts.ts
Normal file
148
src/tts.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import {removeEmojis} from './string.ts';
|
||||
|
||||
export class TTS {
|
||||
private static readonly QUALITY_PATTERNS = ['Google', 'Microsoft', 'Samantha', 'Premium', 'Natural', 'Neural'];
|
||||
private static _errorHandlerInstalled = false;
|
||||
|
||||
private _currentUtterance: SpeechSynthesisUtterance | null = null;
|
||||
private _voicesLoaded: Promise<void>;
|
||||
private _stoppedUtterances = new WeakSet<SpeechSynthesisUtterance>();
|
||||
|
||||
private _rate: number = 1.0;
|
||||
get rate(): number { return this._rate; }
|
||||
set rate(value: number) {
|
||||
this._rate = value;
|
||||
if(this._currentUtterance) this._currentUtterance.rate = value;
|
||||
}
|
||||
|
||||
private _pitch: number = 1.0;
|
||||
get pitch(): number { return this._pitch; }
|
||||
set pitch(value: number) {
|
||||
this._pitch = value;
|
||||
if(this._currentUtterance) this._currentUtterance.pitch = value;
|
||||
}
|
||||
|
||||
private _volume: number = 1.0;
|
||||
get volume(): number { return this._volume; }
|
||||
set volume(value: number) {
|
||||
this._volume = value;
|
||||
if(this._currentUtterance) this._currentUtterance.volume = value;
|
||||
}
|
||||
|
||||
private _voice: SpeechSynthesisVoice | undefined;
|
||||
get voice(): SpeechSynthesisVoice | undefined { return this._voice; }
|
||||
set voice(value: SpeechSynthesisVoice | undefined) {
|
||||
this._voice = value;
|
||||
if(this._currentUtterance && value) this._currentUtterance.voice = value;
|
||||
}
|
||||
|
||||
constructor(config?: {rate?: number; pitch?: number; volume?: number; voice?: SpeechSynthesisVoice | null}) {
|
||||
TTS.installErrorHandler();
|
||||
this._voicesLoaded = this.initializeVoices();
|
||||
if(config) {
|
||||
if(config.rate !== undefined) this._rate = config.rate;
|
||||
if(config.pitch !== undefined) this._pitch = config.pitch;
|
||||
if(config.volume !== undefined) this._volume = config.volume;
|
||||
this._voice = config.voice === null ? undefined : (config.voice || undefined);
|
||||
}
|
||||
}
|
||||
|
||||
private static installErrorHandler(): void {
|
||||
if(this._errorHandlerInstalled) return;
|
||||
window.addEventListener('unhandledrejection', (event) => {
|
||||
if(event.reason?.error === 'interrupted' && event.reason instanceof SpeechSynthesisErrorEvent) event.preventDefault();
|
||||
});
|
||||
this._errorHandlerInstalled = true;
|
||||
}
|
||||
|
||||
private initializeVoices(): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
const voices = window.speechSynthesis.getVoices();
|
||||
if(voices.length > 0) {
|
||||
if(!this._voice) this._voice = TTS.bestVoice();
|
||||
resolve();
|
||||
} else {
|
||||
const handler = () => {
|
||||
window.speechSynthesis.removeEventListener('voiceschanged', handler);
|
||||
if(!this._voice) this._voice = TTS.bestVoice();
|
||||
resolve();
|
||||
};
|
||||
window.speechSynthesis.addEventListener('voiceschanged', handler);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static bestVoice(lang = 'en'): SpeechSynthesisVoice | undefined {
|
||||
const voices = window.speechSynthesis.getVoices();
|
||||
for (const pattern of this.QUALITY_PATTERNS) {
|
||||
const voice = voices.find(v => v.name.includes(pattern) && v.lang.startsWith(lang));
|
||||
if(voice) return voice;
|
||||
}
|
||||
return voices.find(v => v.lang.startsWith(lang));
|
||||
}
|
||||
|
||||
private static cleanText(text: string): string {
|
||||
return removeEmojis(text)
|
||||
.replace(/```[\s\S]*?```/g, ' code block ')
|
||||
.replace(/[#*_~`]/g, '');
|
||||
}
|
||||
|
||||
private createUtterance(text: string): SpeechSynthesisUtterance {
|
||||
const cleanedText = TTS.cleanText(text);
|
||||
const utterance = new SpeechSynthesisUtterance(cleanedText);
|
||||
const voice = this._voice || TTS.bestVoice();
|
||||
if(voice) utterance.voice = voice;
|
||||
utterance.rate = this._rate;
|
||||
utterance.pitch = this._pitch;
|
||||
utterance.volume = this._volume;
|
||||
return utterance;
|
||||
}
|
||||
|
||||
async speak(text: string): Promise<void> {
|
||||
if(!text.trim()) return Promise.resolve();
|
||||
await this._voicesLoaded;
|
||||
return new Promise((resolve, reject) => {
|
||||
this._currentUtterance = this.createUtterance(text);
|
||||
const utterance = this._currentUtterance;
|
||||
utterance.onend = () => {
|
||||
this._currentUtterance = null;
|
||||
resolve();
|
||||
};
|
||||
utterance.onerror = (error) => {
|
||||
this._currentUtterance = null;
|
||||
if(this._stoppedUtterances.has(utterance) && error.error === 'interrupted') resolve();
|
||||
else reject(error);
|
||||
};
|
||||
window.speechSynthesis.speak(utterance);
|
||||
});
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if(this._currentUtterance) this._stoppedUtterances.add(this._currentUtterance);
|
||||
window.speechSynthesis.cancel();
|
||||
this._currentUtterance = null;
|
||||
}
|
||||
|
||||
speakStream(): {next: (text: string) => void, done: () => Promise<void>} {
|
||||
let buffer = '';
|
||||
let streamPromise: Promise<void> = Promise.resolve();
|
||||
const sentenceRegex = /[^.!?\n]+[.!?\n]+/g;
|
||||
return {
|
||||
next: (text: string): void => {
|
||||
buffer += text;
|
||||
const sentences = buffer.match(sentenceRegex);
|
||||
if(sentences) {
|
||||
sentences.forEach(sentence => streamPromise = this.speak(sentence.trim()));
|
||||
buffer = buffer.replace(sentenceRegex, '');
|
||||
}
|
||||
},
|
||||
done: async (): Promise<void> => {
|
||||
if(buffer.trim()) {
|
||||
streamPromise = this.speak(buffer.trim());
|
||||
buffer = '';
|
||||
}
|
||||
await streamPromise;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
199
src/xml.ts
Normal file
199
src/xml.ts
Normal file
@@ -0,0 +1,199 @@
|
||||
/**
|
||||
* Parses an XML string into a structured JavaScript object.
|
||||
* @param {string} xml - The XML string to parse
|
||||
* @returns {Object} An object with `tag`, `attributes`, and `children` properties
|
||||
*/
|
||||
/**
|
||||
* Parses an XML string into a structured JavaScript object (fast-xml-parser format).
|
||||
* @param {string} xml - The XML string to parse
|
||||
* @returns {Object} An object with tag names as keys and text content or nested objects as values
|
||||
*/
|
||||
export function fromXml(xml: string) {
|
||||
xml = xml.trim();
|
||||
let pos = 0;
|
||||
|
||||
function parseNode(): any {
|
||||
skipWhitespace();
|
||||
if(xml[pos] !== '<') return parseText();
|
||||
pos++; // skip <
|
||||
|
||||
if(xml[pos] === '?') {
|
||||
const declaration = parseDeclaration();
|
||||
return { ['?' + declaration]: '', ...parseNode() };
|
||||
}
|
||||
|
||||
if(xml[pos] === '!') {
|
||||
parseComment();
|
||||
return parseNode();
|
||||
}
|
||||
|
||||
const tagName = parseTagName();
|
||||
const attributes = parseAttributes();
|
||||
skipWhitespace();
|
||||
|
||||
if(xml[pos] === '/' && xml[pos + 1] === '>') {
|
||||
pos += 2; // skip />
|
||||
return { [tagName]: '' };
|
||||
}
|
||||
|
||||
pos++; // skip >
|
||||
const children: any[] = [];
|
||||
let textContent = '';
|
||||
|
||||
while(pos < xml.length) {
|
||||
skipWhitespace();
|
||||
if(xml[pos] === '<' && xml[pos + 1] === '/') {
|
||||
pos += 2; // skip </
|
||||
parseTagName(); // skip closing tag name
|
||||
skipWhitespace();
|
||||
pos++; // skip >
|
||||
break;
|
||||
}
|
||||
const startPos = pos;
|
||||
const child = parseNode();
|
||||
if(typeof child === 'string') {
|
||||
textContent += child;
|
||||
} else if(child) {
|
||||
children.push(child);
|
||||
}
|
||||
}
|
||||
|
||||
// If only text content, return simple value
|
||||
if(children.length === 0 && textContent) {
|
||||
const value = isNumeric(textContent) ? Number(textContent) : textContent;
|
||||
return { [tagName]: value };
|
||||
}
|
||||
|
||||
// If only text with no children
|
||||
if(children.length === 0) {
|
||||
return { [tagName]: '' };
|
||||
}
|
||||
|
||||
// Merge children into object
|
||||
const result: any = {};
|
||||
for(const child of children) {
|
||||
for(const [key, value] of Object.entries(child)) {
|
||||
if(result[key]) {
|
||||
// Convert to array if duplicate tags
|
||||
if(!Array.isArray(result[key])) {
|
||||
result[key] = [result[key]];
|
||||
}
|
||||
result[key].push(value);
|
||||
} else {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { [tagName]: result };
|
||||
}
|
||||
|
||||
function parseTagName() {
|
||||
let name = '';
|
||||
while (pos < xml.length && /[a-zA-Z0-9_:-]/.test(xml[pos])) name += xml[pos++];
|
||||
return name;
|
||||
}
|
||||
|
||||
function parseAttributes() {
|
||||
const attrs: any = {};
|
||||
while (pos < xml.length) {
|
||||
skipWhitespace();
|
||||
if (xml[pos] === '>' || xml[pos] === '/') break;
|
||||
const name = parseTagName();
|
||||
skipWhitespace();
|
||||
if (xml[pos] === '=') {
|
||||
pos++;
|
||||
skipWhitespace();
|
||||
const quote = xml[pos++];
|
||||
let value = '';
|
||||
while (xml[pos] !== quote) value += xml[pos++];
|
||||
pos++; // skip closing quote
|
||||
attrs[name] = escapeXml(value, true);
|
||||
}
|
||||
}
|
||||
return attrs;
|
||||
}
|
||||
|
||||
function parseText() {
|
||||
let text = '';
|
||||
while (pos < xml.length && xml[pos] !== '<') text += xml[pos++];
|
||||
text = text.trim();
|
||||
return text ? escapeXml(text, true) : null;
|
||||
}
|
||||
|
||||
function parseDeclaration() {
|
||||
pos++; // skip ?
|
||||
let name = '';
|
||||
while (pos < xml.length && xml[pos] !== ' ' && xml[pos] !== '?') {
|
||||
name += xml[pos++];
|
||||
}
|
||||
while (xml[pos] !== '>') pos++;
|
||||
pos++;
|
||||
return name;
|
||||
}
|
||||
|
||||
function parseComment() {
|
||||
while (!(xml[pos] === '-' && xml[pos + 1] === '-' && xml[pos + 2] === '>')) pos++;
|
||||
pos += 3;
|
||||
}
|
||||
|
||||
function skipWhitespace() {
|
||||
while (pos < xml.length && /\s/.test(xml[pos])) pos++;
|
||||
}
|
||||
|
||||
function isNumeric(str: string) {
|
||||
return !isNaN(Number(str)) && !isNaN(parseFloat(str)) && str.trim() !== '';
|
||||
}
|
||||
|
||||
return parseNode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a JavaScript object into an XML string.
|
||||
* @param {Object} obj - Object with `tag`, `attributes`, and `children` properties, or a string
|
||||
* @param {string} indent - Current indentation level (used internally for formatting)
|
||||
* @returns {string} The formatted XML string
|
||||
*/
|
||||
export function toXml(obj: any, indent = '') {
|
||||
if(typeof obj === 'string') return escapeXml(obj);
|
||||
const { tag, attributes = {}, children = [] } = obj;
|
||||
let xml = `${indent}<${tag}`;
|
||||
for (const [key, value] of Object.entries(attributes))
|
||||
xml += ` ${key}="${escapeXml(<any>value)}"`;
|
||||
if (children.length === 0) {
|
||||
xml += ' />';
|
||||
return xml;
|
||||
}
|
||||
xml += '>';
|
||||
const hasComplexChildren = children.some((c: any) => typeof c === 'object');
|
||||
for (const child of children) {
|
||||
if (hasComplexChildren) xml += '\n';
|
||||
xml += toXml(child, hasComplexChildren ? indent + ' ' : '');
|
||||
}
|
||||
if(hasComplexChildren) xml += `\n${indent}`;
|
||||
xml += `</${tag}>`;
|
||||
return xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes or unescapes XML special characters.
|
||||
* @param {string} str - The string to process
|
||||
* @param {boolean} decode - If true, decodes XML entities; if false, encodes special characters
|
||||
* @returns {string} The processed string
|
||||
*/
|
||||
export function escapeXml(str: string, decode = false) {
|
||||
if(decode) {
|
||||
return str
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, "'")
|
||||
.replace(/&/g, '&');
|
||||
}
|
||||
return str
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { dec2Frac, fracToDec } from '../src';
|
||||
import { dec2Frac, frac2Dec } from '../src';
|
||||
|
||||
describe('Math Utilities', () => {
|
||||
describe('dec2Frac', () => {
|
||||
@@ -27,25 +27,25 @@ describe('Math Utilities', () => {
|
||||
|
||||
describe('fracToDec', () => {
|
||||
it('should convert mixed fraction to decimal', () => {
|
||||
expect(fracToDec('1 1/4')).toBeCloseTo(1.25);
|
||||
expect(fracToDec('2 1/2')).toBeCloseTo(2.5);
|
||||
expect(fracToDec('3 3/4')).toBeCloseTo(3.75);
|
||||
expect(frac2Dec('1 1/4')).toBeCloseTo(1.25);
|
||||
expect(frac2Dec('2 1/2')).toBeCloseTo(2.5);
|
||||
expect(frac2Dec('3 3/4')).toBeCloseTo(3.75);
|
||||
});
|
||||
|
||||
it('should convert fraction without whole part to decimal', () => {
|
||||
expect(fracToDec('3/4')).toBeCloseTo(0.75);
|
||||
expect(fracToDec('1/2')).toBeCloseTo(0.5);
|
||||
expect(fracToDec('1/10')).toBeCloseTo(0.1);
|
||||
expect(frac2Dec('3/4')).toBeCloseTo(0.75);
|
||||
expect(frac2Dec('1/2')).toBeCloseTo(0.5);
|
||||
expect(frac2Dec('1/10')).toBeCloseTo(0.1);
|
||||
});
|
||||
|
||||
it('should convert whole number fraction', () => {
|
||||
expect(fracToDec('4 0/1')).toBeCloseTo(4);
|
||||
expect(fracToDec('0/1')).toBeCloseTo(0);
|
||||
expect(frac2Dec('4 0/1')).toBeCloseTo(4);
|
||||
expect(frac2Dec('0/1')).toBeCloseTo(0);
|
||||
});
|
||||
|
||||
it('should handle zero correctly', () => {
|
||||
expect(fracToDec('0/1')).toBeCloseTo(0);
|
||||
expect(fracToDec('0 0/1')).toBeCloseTo(0);
|
||||
expect(frac2Dec('0/1')).toBeCloseTo(0);
|
||||
expect(frac2Dec('0 0/1')).toBeCloseTo(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -62,6 +62,11 @@ describe('Object utilities', () => {
|
||||
dotNotation(obj, 'd.e.f', 5);
|
||||
expect(obj.d.e.f).toBe(5);
|
||||
});
|
||||
it('undefined', () => {
|
||||
const obj: any = {a: 1};
|
||||
const resp = dotNotation(obj, 'a.b');
|
||||
expect(resp).toBe(undefined);
|
||||
})
|
||||
});
|
||||
|
||||
describe('encodeQuery', () => {
|
||||
|
||||
@@ -17,56 +17,131 @@ describe('Search Utilities', () => {
|
||||
expect(search(rows, '')).toEqual(rows);
|
||||
});
|
||||
|
||||
it('filters based on a simple property string', () => {
|
||||
expect(search(rows, 'Alice')).toEqual([rows[0]]);
|
||||
});
|
||||
|
||||
it('filters using regex when regex=true', () => {
|
||||
expect(search(rows, '^B', true)).toEqual([rows[1]]);
|
||||
});
|
||||
|
||||
it('applies the transform function before filtering', () => {
|
||||
const transform = (r: any) => ({...r, name: r.name.toLowerCase()});
|
||||
expect(search(rows, 'alice', false, transform)).toEqual([rows[0]]);
|
||||
});
|
||||
|
||||
it('uses logicTest for non-regex search', () => {
|
||||
expect(search(rows, 'age == 30')).toEqual([rows[0], rows[2]]);
|
||||
expect(search(rows, 'id = 2')).toEqual([rows[1]]);
|
||||
});
|
||||
|
||||
it('returns all if search is falsy and regex enabled', () => {
|
||||
expect(search(rows, '', true)).toEqual(rows);
|
||||
});
|
||||
|
||||
it('handles regex search with special characters', () => {
|
||||
expect(search(rows, '^[AC]', true)).toEqual([rows[0], rows[2]]);
|
||||
});
|
||||
|
||||
it('ignores case when regex is applied', () => {
|
||||
expect(search(rows, 'ALICE', true)).toEqual([]);
|
||||
});
|
||||
|
||||
it('performs partial matches for properties when regex=false', () => {
|
||||
expect(search(rows, 'Da')).toEqual([rows[3]]);
|
||||
});
|
||||
|
||||
it('handles empty array input gracefully', () => {
|
||||
expect(search([], 'test')).toEqual([]);
|
||||
});
|
||||
|
||||
it('handles numeric values with comparison logic in strings', () => {
|
||||
expect(search(rows, 'age < 31')).toEqual([rows[0], rows[1], rows[2]]);
|
||||
it('applies transform before filtering', () => {
|
||||
const transform = (r: any) => ({...r, name: r.name.toLowerCase()});
|
||||
expect(search(rows, 'alice', transform)).toEqual([rows[0]]);
|
||||
});
|
||||
|
||||
// New test cases for `+` and `-` operators
|
||||
it('filters rows using the + operator', () => {
|
||||
describe('plain text', () => {
|
||||
it('matches case-insensitively when lowercase', () => {
|
||||
expect(search(rows, 'alice')).toEqual([rows[0]]);
|
||||
});
|
||||
|
||||
it('matches case-sensitively when uppercase present', () => {
|
||||
expect(search(rows, 'Alice')).toEqual([rows[0]]);
|
||||
expect(search(rows, 'ALICE')).toEqual([]);
|
||||
});
|
||||
|
||||
it('performs partial matches', () => {
|
||||
expect(search(rows, 'Da')).toEqual([rows[3]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('global regex', () => {
|
||||
it('matches with valid /pattern/flags syntax', () => {
|
||||
expect(search(rows, '/^B/g')).toEqual([rows[1]]);
|
||||
});
|
||||
|
||||
it('matches multiple rows', () => {
|
||||
expect(search(rows, '/^[AC]/g')).toEqual([rows[0], rows[2]]);
|
||||
});
|
||||
|
||||
it('respects flags', () => {
|
||||
expect(search(rows, '/alice/i')).toEqual([rows[0]]);
|
||||
expect(search(rows, '/alice/g')).toEqual([]);
|
||||
});
|
||||
|
||||
it('does not treat /pattern/ without flags as regex', () => {
|
||||
expect(search(rows, '/Alice/')).toEqual([]);
|
||||
});
|
||||
|
||||
it('does not treat paths as regex', () => {
|
||||
const pathRows = [{url: 'users/alice'}];
|
||||
expect(search(pathRows, 'users/alice')).toEqual(pathRows);
|
||||
});
|
||||
});
|
||||
|
||||
describe('property operators', () => {
|
||||
it('filters with equality', () => {
|
||||
expect(search(rows, 'age == 30')).toEqual([rows[0], rows[2]]);
|
||||
expect(search(rows, 'id = 2')).toEqual([rows[1]]);
|
||||
});
|
||||
|
||||
it('filters with inequality', () => {
|
||||
expect(search(rows, 'name != Alice')).toEqual([rows[1], rows[2], rows[3]]);
|
||||
});
|
||||
|
||||
it('filters with contains', () => {
|
||||
expect(search(rows, 'name += Al')).toEqual([rows[0]]);
|
||||
});
|
||||
|
||||
it('filters rows using the - operator', () => {
|
||||
it('filters with not-contains', () => {
|
||||
expect(search(rows, 'name -= Al')).toEqual([rows[1], rows[2], rows[3]]);
|
||||
});
|
||||
|
||||
it('filters with numeric comparisons', () => {
|
||||
expect(search(rows, 'age < 31')).toEqual([rows[0], rows[1], rows[2]]);
|
||||
expect(search(rows, 'age > 30')).toEqual([rows[3]]);
|
||||
expect(search(rows, 'age >= 30')).toEqual([rows[0], rows[2], rows[3]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('property regex', () => {
|
||||
it('matches with ~= shorthand', () => {
|
||||
expect(search(rows, 'name ~= ^Al')).toEqual([rows[0]]);
|
||||
});
|
||||
|
||||
it('matches with ~= /pattern/flags', () => {
|
||||
expect(search(rows, 'name ~= /^al/i')).toEqual([rows[0]]);
|
||||
});
|
||||
|
||||
it('excludes with !~', () => {
|
||||
expect(search(rows, 'name !~ ^[AB]')).toEqual([rows[2], rows[3]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('logical operators', () => {
|
||||
it('supports &&', () => {
|
||||
expect(search(rows, 'age == 30 && name != Alice')).toEqual([rows[2]]);
|
||||
});
|
||||
|
||||
it('supports ||', () => {
|
||||
expect(search(rows, 'name = Alice || name = Bob')).toEqual([rows[0], rows[1]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('dataset helpers', () => {
|
||||
const dupeRows = [
|
||||
{id: 1, email: 'a@a.com', type: 'admin'},
|
||||
{id: 2, email: 'b@b.com', type: 'user'},
|
||||
{id: 3, email: 'a@a.com', type: 'user'},
|
||||
{id: 4, email: 'c@c.com', type: 'admin'},
|
||||
];
|
||||
|
||||
it('unique() returns rows where field appears once', () => {
|
||||
expect(search(dupeRows, 'unique(email)')).toEqual([dupeRows[1], dupeRows[3]]);
|
||||
});
|
||||
|
||||
it('duplicate() returns rows where field appears more than once', () => {
|
||||
expect(search(dupeRows, 'duplicate(email)')).toEqual([dupeRows[0], dupeRows[2]]);
|
||||
});
|
||||
|
||||
it('distinct() returns one row per field value', () => {
|
||||
expect(search(dupeRows, 'distinct(type)')).toEqual([dupeRows[0], dupeRows[1]]);
|
||||
});
|
||||
|
||||
it('composes helpers with predicates', () => {
|
||||
expect(search(dupeRows, 'type = user && duplicate(email)')).toEqual([]);
|
||||
});
|
||||
|
||||
it('applies distinct last', () => {
|
||||
expect(search(dupeRows, 'duplicate(email) && distinct(type)')).toEqual([dupeRows[0], dupeRows[2]]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('logicTest', () => {
|
||||
@@ -83,60 +158,63 @@ describe('Search Utilities', () => {
|
||||
expect(logicTest(obj, 'x < 5')).toBe(false);
|
||||
});
|
||||
|
||||
it('supports case insensitive property search', () => {
|
||||
expect(logicTest(obj, 'alpha')).toBeTruthy();
|
||||
expect(logicTest(obj, 'ALPHA')).toBeFalsy();
|
||||
it('handles contains and not-contains', () => {
|
||||
expect(logicTest(obj, 'name += Alpha')).toBe(true);
|
||||
expect(logicTest(obj, 'name += Alp')).toBe(true);
|
||||
expect(logicTest(obj, 'name += Bet')).toBe(false);
|
||||
expect(logicTest(obj, 'name -= Alpha')).toBe(false);
|
||||
expect(logicTest(obj, 'name -= Bet')).toBe(true);
|
||||
expect(logicTest(obj, 'name += lph')).toBe(true);
|
||||
expect(logicTest(obj, 'name -= lph')).toBe(false);
|
||||
});
|
||||
|
||||
it('handles logical AND/OR expressions', () => {
|
||||
it('handles property regex ~=', () => {
|
||||
expect(logicTest(obj, 'name ~= ^Alp')).toBe(true);
|
||||
expect(logicTest(obj, 'name ~= /^alp/i')).toBe(true);
|
||||
expect(logicTest(obj, 'name ~= ^Bet')).toBe(false);
|
||||
});
|
||||
|
||||
it('handles property regex !~', () => {
|
||||
expect(logicTest(obj, 'name !~ ^Bet')).toBe(true);
|
||||
expect(logicTest(obj, 'name !~ ^Alp')).toBe(false);
|
||||
});
|
||||
|
||||
it('handles invalid regex gracefully', () => {
|
||||
expect(logicTest(obj, 'name ~= [invalid')).toBe(false);
|
||||
});
|
||||
|
||||
it('supports plain text case-insensitive search', () => {
|
||||
expect(logicTest(obj, 'alpha')).toBe(true);
|
||||
});
|
||||
|
||||
it('supports plain text case-sensitive search', () => {
|
||||
expect(logicTest(obj, 'Alpha')).toBe(true);
|
||||
expect(logicTest(obj, 'ALPHA')).toBe(false);
|
||||
});
|
||||
|
||||
it('handles logical AND/OR', () => {
|
||||
expect(logicTest(obj, 'x == 10 && y == 5')).toBe(true);
|
||||
expect(logicTest(obj, 'x == 10 || y == 100')).toBe(true);
|
||||
expect(logicTest(obj, 'x == 1 && y == 5')).toBe(false);
|
||||
});
|
||||
|
||||
it('handles numeric ranges', () => {
|
||||
expect(logicTest(obj, 'x > 5 && x < 15')).toBe(true);
|
||||
expect(logicTest(obj, 'x > 15')).toBe(false);
|
||||
});
|
||||
|
||||
it('matches keys case-insensitively', () => {
|
||||
const mixedCaseObj = {TestKey: 123};
|
||||
expect(logicTest(mixedCaseObj, 'TestKey == 123')).toBe(true);
|
||||
expect(logicTest(mixedCaseObj, 'testkey == 123')).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false for unsupported operators', () => {
|
||||
expect(logicTest(obj, 'x === 10')).toBe(false);
|
||||
});
|
||||
|
||||
it('handles invalid condition strings gracefully', () => {
|
||||
expect(logicTest(obj, 'invalid condition')).toBe(false);
|
||||
});
|
||||
|
||||
it('supports numeric operations with ranges', () => {
|
||||
expect(logicTest(obj, 'x > 5 && x < 15')).toBe(true);
|
||||
expect(logicTest(obj, 'x > 15')).toBe(false);
|
||||
});
|
||||
|
||||
it('handles mixed case keys gracefully', () => {
|
||||
const mixedCaseObj = {TestKey: 123};
|
||||
expect(logicTest(mixedCaseObj, 'TestKey == 123')).toBe(true);
|
||||
expect(logicTest(mixedCaseObj, 'testkey == 123')).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false if condition operators are missing', () => {
|
||||
it('returns false for missing operators', () => {
|
||||
expect(logicTest(obj, 'x 10')).toBe(false);
|
||||
});
|
||||
|
||||
// New test cases for `+` and `-` operators
|
||||
it('handles the + operator for inclusion', () => {
|
||||
expect(logicTest(obj, 'name += Alpha')).toBe(true);
|
||||
expect(logicTest(obj, 'name += Alp')).toBe(true);
|
||||
expect(logicTest(obj, 'name += Bet')).toBe(false);
|
||||
});
|
||||
|
||||
it('handles the - operator for exclusion', () => {
|
||||
expect(logicTest(obj, 'name -= Alpha')).toBe(false);
|
||||
expect(logicTest(obj, 'name -= Alp')).toBe(false);
|
||||
expect(logicTest(obj, 'name -= Bet')).toBe(true);
|
||||
});
|
||||
|
||||
it('includes partial matches correctly with +', () => {
|
||||
expect(logicTest(obj, 'name += lph')).toBe(true);
|
||||
});
|
||||
|
||||
it('excludes partial matches correctly with -', () => {
|
||||
expect(logicTest(obj, 'name -= lph')).toBe(false);
|
||||
expect(logicTest(obj, 'name -= xyz')).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
304
tests/templates.spec.ts
Normal file
304
tests/templates.spec.ts
Normal file
@@ -0,0 +1,304 @@
|
||||
import {findTemplateVars, renderTemplate, TemplateError} from '../src';
|
||||
|
||||
describe('findTemplateVars', () => {
|
||||
test('extracts simple variables', () => {
|
||||
const result = findTemplateVars('Hello {{ name }}!');
|
||||
expect(result).toEqual({ name: '' });
|
||||
});
|
||||
|
||||
test('extracts nested object paths', () => {
|
||||
const result = findTemplateVars('{{ user.name }} is {{ user.age }}');
|
||||
expect(result).toEqual({ user: { name: '', age: '' } });
|
||||
});
|
||||
|
||||
test('extracts variables from if statements', () => {
|
||||
const result = findTemplateVars('{{ ? active }}{{ message }}{{ /? }}');
|
||||
expect(result).toEqual({ active: '', message: '' });
|
||||
});
|
||||
|
||||
test('extracts variables from else-if conditions', () => {
|
||||
const result = findTemplateVars('{{ ? status == "paid" }}PAID{{ !? status == "pending" }}{{ value }}{{ /? }}');
|
||||
expect(result).toEqual({ status: '', value: '' });
|
||||
});
|
||||
|
||||
test('extracts array reference from loops', () => {
|
||||
const result = findTemplateVars('{{ * item in items }}{{ item }}{{ /* }}');
|
||||
expect(result).toEqual({ items: [] });
|
||||
});
|
||||
|
||||
test('excludes loop element variable', () => {
|
||||
const result = findTemplateVars('{{ * item in items }}{{ item.name }}{{ /* }}');
|
||||
expect(result).toEqual({ items: [] });
|
||||
expect(result).not.toHaveProperty('item');
|
||||
});
|
||||
|
||||
test('excludes loop index variable', () => {
|
||||
const result = findTemplateVars('{{ * (item, i) in items }}{{ i }}:{{ item }}{{ /* }}');
|
||||
expect(result).toEqual({ items: [] });
|
||||
expect(result).not.toHaveProperty('item');
|
||||
expect(result).not.toHaveProperty('i');
|
||||
});
|
||||
|
||||
test('extracts external vars used inside loops', () => {
|
||||
const result = findTemplateVars('{{ * item in items }}{{ item }}-{{ prefix }}{{ /* }}');
|
||||
expect(result).toEqual({ items: [], prefix: '' });
|
||||
});
|
||||
|
||||
test('handles nested loops', () => {
|
||||
const result = findTemplateVars('{{ * row in rows }}{{ * col in row.cols }}{{ col }}{{ /* }}{{ /* }}');
|
||||
expect(result).toEqual({ rows: [] });
|
||||
expect(result).not.toHaveProperty('row');
|
||||
expect(result).not.toHaveProperty('col');
|
||||
});
|
||||
|
||||
test('extracts from complex nested template', () => {
|
||||
const tpl = `
|
||||
{{ ? items.length > 0 }}
|
||||
{{ * (item, i) in items }}
|
||||
{{ i }}. {{ item.name }}: {{ currency }}{{ item.price }}
|
||||
{{ /* }}
|
||||
Total: {{ total }}
|
||||
{{ !? }}
|
||||
{{ emptyMessage }}
|
||||
{{ /? }}`;
|
||||
const result = findTemplateVars(tpl);
|
||||
expect(result).toEqual({
|
||||
items: [],
|
||||
currency: '',
|
||||
total: '',
|
||||
emptyMessage: ''
|
||||
});
|
||||
expect(result).not.toHaveProperty('item');
|
||||
expect(result).not.toHaveProperty('i');
|
||||
});
|
||||
|
||||
test('ignores template control syntax', () => {
|
||||
const result = findTemplateVars('{{ < header.html }}{{ > layout.html:content }}content{{ /> }}');
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
test('handles multiple variables in expressions', () => {
|
||||
const result = findTemplateVars('{{ firstName + " " + lastName }}');
|
||||
expect(result).toEqual({ firstName: '', lastName: '' });
|
||||
});
|
||||
|
||||
test('creates arrays for loop variables', () => {
|
||||
const result = findTemplateVars('{{ * item in items }}{{ item }}{{ /* }}');
|
||||
expect(result).toEqual({ items: [] });
|
||||
});
|
||||
|
||||
test('creates nested arrays', () => {
|
||||
const result = findTemplateVars('{{ * row in data.rows }}{{ row }}{{ /* }}');
|
||||
expect(result).toEqual({ data: { rows: [] } });
|
||||
});
|
||||
|
||||
test('creates multiple arrays', () => {
|
||||
const result = findTemplateVars('{{ * user in users }}{{ user }}{{ /* }}{{ * post in posts }}{{ post }}{{ /* }}');
|
||||
expect(result).toEqual({ users: [], posts: [] });
|
||||
});
|
||||
|
||||
test('excludes function calls', () => {
|
||||
const result = findTemplateVars('{{ value.toFixed(2) }}');
|
||||
expect(result).toEqual({ value: '' });
|
||||
expect(result.value).not.toBe('toFixed');
|
||||
});
|
||||
|
||||
test('excludes method chains', () => {
|
||||
const result = findTemplateVars('{{ text.replaceAll("\\n", "<br>") }}');
|
||||
expect(result).toEqual({ text: '' });
|
||||
});
|
||||
|
||||
test('handles mix of arrays and regular variables', () => {
|
||||
const result = findTemplateVars('{{ * item in cart }}{{ item.name }}{{ /* }}{{ total }}');
|
||||
expect(result).toEqual({ cart: [], total: '' });
|
||||
});
|
||||
|
||||
test('real world template', async () => {
|
||||
const wrapper = `<div> {{body}} </div>`;
|
||||
const tpl = `
|
||||
{{ > email:body }}
|
||||
<div style="text-align: center">
|
||||
{{ ? title }}<h1 style="margin: 0">{{ title }}</h1>{{ /? }}
|
||||
{{ ? subject }}<h2 style="margin: 0">{{ subject }}</h2>{{ /? }}
|
||||
{{ ? message }}<p style="margin-top: 1rem">{{ message }}</p>{{ /? }}
|
||||
{{ ? link }}
|
||||
<br>
|
||||
<div style="background: #dedede; padding: 8px 20px; border-radius: 10px; overflow-x: auto;">
|
||||
{{ ? link.startsWith('http') }}
|
||||
<a style="word-break: break-all;" target="_blank" href="{{ link }}">{{ link }}</a>
|
||||
{{ !? }}
|
||||
<p style="margin: 0; word-break: break-all;">{{ link }}</p>
|
||||
{{ /? }}
|
||||
</div>
|
||||
{{ /? }}
|
||||
{{ ? footer }}
|
||||
<br>
|
||||
<p>{{ footer }}</p>
|
||||
{{ /? }}
|
||||
</div>
|
||||
{{ /> }}`;
|
||||
console.log(await renderTemplate(tpl, {
|
||||
title: 'test',
|
||||
subject: 'test',
|
||||
message: 'test',
|
||||
link: 'test',
|
||||
footer: 'test',
|
||||
}, async () => wrapper))
|
||||
});
|
||||
|
||||
test('real world invoice template', () => {
|
||||
const tpl = `
|
||||
{{ settings.title }}
|
||||
{{ * (row, index) in transaction.cart }}
|
||||
{{ row.quantity }} x {{ row.name }} = \${{ row.cost.toFixed(2) }}
|
||||
{{ /* }}
|
||||
Total: \${{ transaction.total.toFixed(2) }}
|
||||
{{ ? transaction.discount }}
|
||||
Discount: {{ transaction.discount.value }}
|
||||
{{ /? }}
|
||||
`;
|
||||
const result = findTemplateVars(tpl);
|
||||
expect(result).toEqual({
|
||||
settings: { title: '' },
|
||||
transaction: {
|
||||
cart: [],
|
||||
total: '',
|
||||
discount: { value: '' }
|
||||
}
|
||||
});
|
||||
expect(result).not.toHaveProperty('row');
|
||||
expect(result).not.toHaveProperty('index');
|
||||
expect(result.transaction.cart).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('renderTemplate', () => {
|
||||
test('basic variable interpolation', async () => {
|
||||
const result = await renderTemplate('Hello {{ name }}!', { name: 'World' });
|
||||
expect(result).toBe('Hello World!');
|
||||
});
|
||||
|
||||
test('nested object access', async () => {
|
||||
const result = await renderTemplate('{{ user.name }} is {{ user.age }}', {
|
||||
user: { name: 'Alice', age: 25 }
|
||||
});
|
||||
expect(result).toBe('Alice is 25');
|
||||
});
|
||||
|
||||
test('method calls', async () => {
|
||||
const result = await renderTemplate('{{ price.toFixed(2) }}', { price: 9.5 });
|
||||
expect(result).toBe('9.50');
|
||||
});
|
||||
|
||||
test('if statement true', async () => {
|
||||
const result = await renderTemplate('{{ ? active }}YES{{ /? }}', { active: true });
|
||||
expect(result).toBe('YES');
|
||||
});
|
||||
|
||||
test('if statement false', async () => {
|
||||
const result = await renderTemplate('{{ ? active }}YES{{ /? }}', { active: false });
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
test('if-else', async () => {
|
||||
const result = await renderTemplate('{{ ? active }}YES{{ !? }}NO{{ /? }}', { active: false });
|
||||
expect(result).toBe('NO');
|
||||
});
|
||||
|
||||
test('if-elseif-else', async () => {
|
||||
const tpl = '{{ ? status == "paid" }}PAID{{ !? status == "pending" }}PENDING{{ !? }}OTHER{{ /? }}';
|
||||
expect(await renderTemplate(tpl, { status: 'paid' })).toBe('PAID');
|
||||
expect(await renderTemplate(tpl, { status: 'pending' })).toBe('PENDING');
|
||||
expect(await renderTemplate(tpl, { status: 'failed' })).toBe('OTHER');
|
||||
});
|
||||
|
||||
test('nested if statements', async () => {
|
||||
const tpl = '{{ ? a }}A{{ ? b }}B{{ /? }}{{ /? }}';
|
||||
expect(await renderTemplate(tpl, { a: true, b: true })).toBe('AB');
|
||||
expect(await renderTemplate(tpl, { a: true, b: false })).toBe('A');
|
||||
expect(await renderTemplate(tpl, { a: false, b: true })).toBe('');
|
||||
});
|
||||
|
||||
test('for loop', async () => {
|
||||
const tpl = '{{ * item in items }}{{ item }}{{ /* }}';
|
||||
const result = await renderTemplate(tpl, { items: ['a', 'b', 'c'] });
|
||||
expect(result).toBe('a\nb\nc');
|
||||
});
|
||||
|
||||
test('for loop with index', async () => {
|
||||
const tpl = '{{ * (item, i) in items }}{{ i }}:{{ item }}{{ /* }}';
|
||||
const result = await renderTemplate(tpl, { items: ['a', 'b'] });
|
||||
expect(result).toBe('0:a\n1:b');
|
||||
});
|
||||
|
||||
test('for loop with objects', async () => {
|
||||
const tpl = '{{ * user in users }}{{ user.name }}{{ /* }}';
|
||||
const result = await renderTemplate(tpl, {
|
||||
users: [{ name: 'Alice' }, { name: 'Bob' }]
|
||||
});
|
||||
expect(result).toBe('Alice\nBob');
|
||||
});
|
||||
|
||||
test('for loop error on non-array', async () => {
|
||||
await expect(
|
||||
renderTemplate('{{ * x in notArray }}{{ x }}{{ /* }}', { notArray: 'string' })
|
||||
).rejects.toThrow(TemplateError);
|
||||
});
|
||||
|
||||
test('import template', async () => {
|
||||
const fetch = async (file: string) => {
|
||||
if (file === 'header.html') return 'HEADER: {{ title }}';
|
||||
throw new Error('Not found');
|
||||
};
|
||||
const result = await renderTemplate('{{ < header.html }}', { title: 'Test' }, fetch);
|
||||
expect(result).toBe('HEADER: Test');
|
||||
});
|
||||
|
||||
test('import template error', async () => {
|
||||
await expect(
|
||||
renderTemplate('{{ < missing.html }}', {})
|
||||
).rejects.toThrow(TemplateError);
|
||||
});
|
||||
|
||||
test('extend template', async () => {
|
||||
const fetch = async (file: string) => {
|
||||
if (file === 'layout.html') return '<div>{{ content }}</div>';
|
||||
throw new Error('Not found');
|
||||
};
|
||||
const result = await renderTemplate('{{ > layout.html:content }}Hello{{ /> }}', {}, fetch);
|
||||
expect(result).toBe('<div>Hello</div>');
|
||||
});
|
||||
|
||||
test('date object', async () => {
|
||||
const result = await renderTemplate('{{ date.year }}', {});
|
||||
expect(result).toBe(new Date().getFullYear().toString());
|
||||
});
|
||||
|
||||
test('evaluation error', async () => {
|
||||
await expect(
|
||||
renderTemplate('{{ undefined.property }}', {})
|
||||
).rejects.toThrow(TemplateError);
|
||||
});
|
||||
|
||||
test('complex nested example', async () => {
|
||||
const tpl = `
|
||||
{{ ? items.length > 0 }}
|
||||
{{ * (item, i) in items }}
|
||||
{{ i + 1 }}. {{ item.name }}: \${{ item.price.toFixed(2) }}
|
||||
{{ /* }}
|
||||
Total: \${{ total.toFixed(2) }}
|
||||
{{ !? }}
|
||||
No items
|
||||
{{ /? }}`;
|
||||
const result = await renderTemplate(tpl, {
|
||||
items: [
|
||||
{ name: 'A', price: 10 },
|
||||
{ name: 'B', price: 20 }
|
||||
],
|
||||
total: 30
|
||||
});
|
||||
expect(result.trim()).toContain('1. A: $10.00');
|
||||
expect(result.trim()).toContain('2. B: $20.00');
|
||||
expect(result.trim()).toContain('Total: $30.00');
|
||||
});
|
||||
});
|
||||
210
tests/xml.spec.ts
Normal file
210
tests/xml.spec.ts
Normal file
@@ -0,0 +1,210 @@
|
||||
import { toXml, fromXml } from '../src';
|
||||
|
||||
describe('XML Parser', () => {
|
||||
describe('fromXml', () => {
|
||||
it('should parse simple tag', () => {
|
||||
const xml = '<root></root>';
|
||||
const result = fromXml(xml);
|
||||
expect(result).toEqual({ root: '' });
|
||||
});
|
||||
|
||||
it('should parse self-closing tag', () => {
|
||||
const xml = '<item />';
|
||||
const result = fromXml(xml);
|
||||
expect(result).toEqual({ item: '' });
|
||||
});
|
||||
|
||||
it('should parse tag with attributes (ignored in fast-xml-parser format)', () => {
|
||||
const xml = '<user id="1" name="someone" />';
|
||||
const result = fromXml(xml);
|
||||
expect(result).toEqual({ user: '' });
|
||||
});
|
||||
|
||||
it('should parse tag with text content', () => {
|
||||
const xml = '<email>someone@example.com</email>';
|
||||
const result = fromXml(xml);
|
||||
expect(result).toEqual({ email: 'someone@example.com' });
|
||||
});
|
||||
|
||||
it('should parse tag with numeric content', () => {
|
||||
const xml = '<ttl>240</ttl>';
|
||||
const result = fromXml(xml);
|
||||
expect(result).toEqual({ ttl: 240 });
|
||||
});
|
||||
|
||||
it('should parse nested tags', () => {
|
||||
const xml = '<root><child>text</child></root>';
|
||||
const result = fromXml(xml);
|
||||
expect(result).toEqual({
|
||||
root: {
|
||||
child: 'text'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse multiple children with same tag as array', () => {
|
||||
const xml = '<root><item>a</item><item>b</item><item>c</item></root>';
|
||||
const result = fromXml(xml);
|
||||
expect(result).toEqual({
|
||||
root: {
|
||||
item: ['a', 'b', 'c']
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse mixed children', () => {
|
||||
const xml = '<root><a>1</a><b>2</b><c>3</c></root>';
|
||||
const result = fromXml(xml);
|
||||
expect(result.root).toEqual({ a: 1, b: 2, c: 3 });
|
||||
});
|
||||
|
||||
it('should skip XML declaration and include as key', () => {
|
||||
const xml = '<?xml version="1.0"?><root />';
|
||||
const result = fromXml(xml);
|
||||
expect(result).toHaveProperty('?xml');
|
||||
expect(result).toHaveProperty('root');
|
||||
});
|
||||
|
||||
it('should skip comments', () => {
|
||||
const xml = '<root><!-- comment --><child>text</child></root>';
|
||||
const result = fromXml(xml);
|
||||
expect(result).toEqual({
|
||||
root: {
|
||||
child: 'text'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle escaped characters', () => {
|
||||
const xml = '<text><hello> & "world"</text>';
|
||||
const result = fromXml(xml);
|
||||
expect(result.text).toBe('<hello> & "world"');
|
||||
});
|
||||
|
||||
it('should parse complex nested structure', () => {
|
||||
const xml = `
|
||||
<root>
|
||||
<user id="1" name="someone">
|
||||
<email>someone@example.com</email>
|
||||
<active />
|
||||
</user>
|
||||
</root>
|
||||
`;
|
||||
const result = fromXml(xml);
|
||||
expect(result).toEqual({
|
||||
root: {
|
||||
user: {
|
||||
email: 'someone@example.com',
|
||||
active: ''
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse RSS-like structure with multiple items', () => {
|
||||
const xml = `
|
||||
<rss>
|
||||
<channel>
|
||||
<title>Test Feed</title>
|
||||
<item>
|
||||
<title>Item 1</title>
|
||||
<link>http://example.com/1</link>
|
||||
</item>
|
||||
<item>
|
||||
<title>Item 2</title>
|
||||
<link>http://example.com/2</link>
|
||||
</item>
|
||||
</channel>
|
||||
</rss>
|
||||
`;
|
||||
const result = fromXml(xml);
|
||||
expect(result.rss.channel.title).toBe('Test Feed');
|
||||
expect(Array.isArray(result.rss.channel.item)).toBe(true);
|
||||
expect(result.rss.channel.item.length).toBe(2);
|
||||
expect(result.rss.channel.item[0].title).toBe('Item 1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('toXml', () => {
|
||||
it('should encode simple tag', () => {
|
||||
const obj = { tag: 'root', attributes: {}, children: [] };
|
||||
expect(toXml(obj)).toBe('<root />');
|
||||
});
|
||||
|
||||
it('should encode tag with attributes', () => {
|
||||
const obj = { tag: 'user', attributes: { id: '1', name: 'someone' }, children: [] };
|
||||
const result = toXml(obj);
|
||||
expect(result).toContain('id="1"');
|
||||
expect(result).toContain('name="someone"');
|
||||
});
|
||||
|
||||
it('should encode tag with text content', () => {
|
||||
const obj = { tag: 'email', attributes: {}, children: ['someone@example.com'] };
|
||||
expect(toXml(obj)).toBe('<email>someone@example.com</email>');
|
||||
});
|
||||
|
||||
it('should encode nested tags with indentation', () => {
|
||||
const obj = {
|
||||
tag: 'root',
|
||||
attributes: {},
|
||||
children: [
|
||||
{ tag: 'child', attributes: {}, children: ['text'] }
|
||||
]
|
||||
};
|
||||
const result = toXml(obj);
|
||||
expect(result).toContain('<root>');
|
||||
expect(result).toContain(' <child>');
|
||||
expect(result).toContain('</root>');
|
||||
});
|
||||
|
||||
it('should escape special characters', () => {
|
||||
const obj = { tag: 'text', attributes: {}, children: ['<hello> & "world"'] };
|
||||
const result = toXml(obj);
|
||||
expect(result).toContain('<hello> & "world"');
|
||||
});
|
||||
|
||||
it('should escape attributes', () => {
|
||||
const obj = { tag: 'node', attributes: { attr: 'a & b' }, children: [] };
|
||||
const result = toXml(obj);
|
||||
expect(result).toContain('attr="a & b"');
|
||||
});
|
||||
|
||||
it('should handle multiple children', () => {
|
||||
const obj = {
|
||||
tag: 'root',
|
||||
attributes: {},
|
||||
children: [
|
||||
{ tag: 'a', attributes: {}, children: [] },
|
||||
{ tag: 'b', attributes: {}, children: [] }
|
||||
]
|
||||
};
|
||||
const result = toXml(obj);
|
||||
expect(result).toContain('<a />');
|
||||
expect(result).toContain('<b />');
|
||||
});
|
||||
|
||||
it('should encode string directly', () => {
|
||||
expect(toXml('hello')).toBe('hello');
|
||||
expect(toXml('a & b')).toBe('a & b');
|
||||
});
|
||||
});
|
||||
|
||||
describe('round-trip', () => {
|
||||
it('should parse toXml output back to fast-xml-parser format', () => {
|
||||
const obj = {
|
||||
tag: 'root',
|
||||
attributes: { id: '1' },
|
||||
children: [
|
||||
{ tag: 'child', attributes: {}, children: ['text'] }
|
||||
]
|
||||
};
|
||||
const xml = toXml(obj);
|
||||
const parsed = fromXml(xml);
|
||||
expect(parsed).toEqual({
|
||||
root: {
|
||||
child: 'text'
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user