Compare commits
4 Commits
0.30.0
...
e815126807
| Author | SHA1 | Date | |
|---|---|---|---|
| e815126807 | |||
| 6319f810b5 | |||
| 91f4abf1f1 | |||
| f9971d7ce1 |
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.30.0",
|
||||
"description": "Utility library",
|
||||
"version": "0.30.3",
|
||||
"description": "Utility library",S
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
"private": false,
|
||||
|
||||
@@ -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
|
||||
|
||||
45
src/html.ts
Normal file
45
src/html.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 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
|
||||
* @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 = {};
|
||||
for (const line of match[1].split('\n')) {
|
||||
const colonIdx = line.indexOf(':');
|
||||
if (colonIdx === -1) continue;
|
||||
const key = line.slice(0, colonIdx).trim();
|
||||
const value = line.slice(colonIdx + 1).trim();
|
||||
try { meta[key] = JSON.parse(value); } catch { meta[key] = value; }
|
||||
}
|
||||
return {meta, content: match[2].trim()};
|
||||
}
|
||||
@@ -7,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';
|
||||
|
||||
@@ -27,32 +27,6 @@ export function camelCase(str?: string): string {
|
||||
return pascal.charAt(0).toLowerCase() + pascal.slice(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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!
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert number of bytes into a human-readable size
|
||||
*
|
||||
@@ -125,7 +99,6 @@ export function kebabCase(str?: string): string {
|
||||
return wordSegments(str).map(w => w.toLowerCase()).join("-");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add padding to string
|
||||
*
|
||||
@@ -256,18 +229,14 @@ export function strSplice(str: string, start: number, deleteCount: number, inser
|
||||
return before + insert + after;
|
||||
}
|
||||
|
||||
function titleCase(str: string) {
|
||||
// Normalize separators: replace underscores and hyphens with spaces
|
||||
let normalizedStr = str.replace(/(_|-)/g, ' ');
|
||||
// Handle CamelCase/PascalCase boundaries: insert a space before capital letters
|
||||
normalizedStr = normalizedStr.replace(/([a-z])([A-Z])/g, '$1 $2');
|
||||
// Lowercase the whole string, split by any whitespace, and capitalize each word
|
||||
let words = normalizedStr.toLowerCase().split(/\s+/).filter(Boolean);
|
||||
const titledWords = words.map(word => {
|
||||
if (word.length === 0) return '';
|
||||
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
||||
});
|
||||
return titledWords.join(' ');
|
||||
/**
|
||||
* 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(' ');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -280,19 +249,16 @@ function titleCase(str: string) {
|
||||
* @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;
|
||||
|
||||
Reference in New Issue
Block a user