Compare commits

..

11 Commits

Author SHA1 Message Date
c51239d12b Bump 0.18.2
All checks were successful
Build / Build NPM Project (push) Successful in 25s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 26s
2024-10-12 12:30:23 -04:00
e4229296c1 Added escapeRegex
All checks were successful
Build / Build NPM Project (push) Successful in 59s
Build / Tag Version (push) Successful in 14s
Build / Publish Documentation (push) Successful in 42s
2024-10-12 12:27:45 -04:00
b93ed45521 Renamed sleepUntil to sleepWhile, it was misleading
All checks were successful
Build / Build NPM Project (push) Successful in 1m5s
Build / Tag Version (push) Successful in 14s
Build / Publish Documentation (push) Successful in 39s
2024-10-07 15:40:14 -04:00
3e8f5cc00b Add or remove multiple elements from ASet at once
All checks were successful
Build / Build NPM Project (push) Successful in 30s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 27s
2024-10-06 19:14:36 -04:00
54a2880391 Updated docs
All checks were successful
Build / Build NPM Project (push) Successful in 27s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 25s
2024-10-01 15:44:49 -04:00
b7aeee4706 Fixing docker container
All checks were successful
Build / Build NPM Project (push) Successful in 25s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 25s
2024-10-01 15:39:24 -04:00
b3eab0d6c9 Created docs docker container
All checks were successful
Build / Build NPM Project (push) Successful in 26s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 19s
2024-10-01 15:22:48 -04:00
ad4194a981 Fixed extracting version number during build
Some checks failed
Build / Build NPM Project (push) Successful in 27s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Failing after 8s
2024-10-01 15:13:47 -04:00
e1f22a01a6 Added docs container
Some checks failed
Build / Build NPM Project (push) Successful in 31s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Failing after 5s
2024-10-01 15:08:52 -04:00
91c0858d9f Added var-persist
All checks were successful
Build / Build NPM Project (push) Successful in 28s
Build / Tag Version (push) Successful in 6s
2024-09-30 21:43:13 -04:00
8094b6507f Updated cache indexing signature
All checks were successful
Build / Build NPM Project (push) Successful in 26s
Build / Tag Version (push) Successful in 7s
2024-09-30 19:39:06 -04:00
11 changed files with 5833 additions and 5619 deletions

View File

@ -48,3 +48,13 @@ jobs:
uses: ztimson/actions/tag@develop
with:
tag: ${{env.VERSION}}
docs:
name: Publish Documentation
needs: build
uses: ztimson/actions/.github/workflows/docker.yaml@develop
with:
name: ztimson/utils
repository: ${{github.server_url}}/${{github.repository}}.git
pass: ${{secrets.DEPLOY_TOKEN}}

13
Dockerfile Normal file
View File

@ -0,0 +1,13 @@
# Build application
FROM node:alpine as build
RUN mkdir /app
WORKDIR /app
COPY . .
RUN if [ ! -d "node_modules" ]; then npm i; fi && \
if [ ! -d "dist" ]; then npm run docs; fi
# Use Nginx to serve
FROM nginx:1.23-alpine
COPY --from=build /app/docs /usr/share/nginx/html

View File

@ -20,7 +20,7 @@ Javascript/Typescript Utilities
---
<div>
<a href="https://git.zakscode.com/ztimson/utils/wiki" target="_blank">Documentation</a>
<a href="https://utils.docs.zakscode.com" target="_blank">Documentation</a>
<a href="https://git.zakscode.com/ztimson/utils/releases" target="_blank">Release Notes</a>
<a href="https://git.zakscode.com/ztimson/utils/issues/new?template=.github%2fissue_template%2fbug.md" target="_blank">Report a Bug</a>
<a href="https://git.zakscode.com/ztimson/utils/issues/new?template=.github%2fissue_template%2fenhancement.md" target="_blank">Request a Feature</a>
@ -36,7 +36,7 @@ Javascript/Typescript Utilities
- [Setup](#setup)
- [Production](#production)
- [Development](#development)
- [Documentation](https://git.zakscode.com/ztimson/utils/wiki)
- [Documentation](https://utils.docs.zakscode.com/)
- [License](#license)
## About
@ -82,7 +82,7 @@ A collection of utilities to make life a little easier
## Documentation
[Available Here](https://git.zakscode.com/ztimson/utils/wiki)
[Available Here](https://utils.docs.zakscode.com/)
## License

11346
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.16.9",
"version": "0.18.2",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",
@ -21,7 +21,7 @@
},
"scripts": {
"build": "npx tsc && npx vite build",
"docs": "typedoc --plugin typedoc-plugin-markdown --cleanOutputDir false --outputFileStrategy modules --hidePageHeader --out ./docs --entryPoints src/**/*.ts --readme none --entryFileName Home",
"docs": "typedoc --cleanOutputDir false --out ./docs --entryPoints src/**/*.ts --readme none",
"test": "npx jest",
"test:coverage": "npx jest --coverage",
"watch": "npx vite build --watch"
@ -32,12 +32,14 @@
"jest-junit": "^16.0.0",
"ts-jest": "^29.1.2",
"typedoc": "^0.26.7",
"typedoc-plugin-markdown": "^4.2.7",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vite-plugin-dts": "^3.7.2"
},
"files": [
"dist"
]
],
"dependencies": {
"var-persist": "^1.0.1"
}
}

View File

@ -19,20 +19,22 @@ export class ASet<T> extends Array {
}
/**
* Add single element to set if unique
* @param {T} el Element to add
* Add elements to set if unique
* @param items
*/
add(el: T) {
if(!this.has(el)) this.push(el);
add(...items: T[]) {
items.filter(el => !this.has(el)).forEach(el => this.push(el));
}
/**
* Delete element from set
* @param {T} el Element that will be deleted
* Delete elements from set
* @param items Elements that will be deleted
*/
delete(el: T) {
const index = this.indexOf(el);
if(index != -1) this.slice(index, 1);
delete(...items: T[]) {
items.forEach(el => {
const index = this.indexOf(el);
if(index != -1) this.slice(index, 1);
})
}
/**

View File

@ -1,9 +1,11 @@
/**
* Map of data which tracks whether it is a complete collection & offers optional expiry of cached values
*/
export class Cache<K, T> {
private store: any = {};
export class Cache<K extends string | number | symbol, T> {
private store = <Record<K, T>>{};
/** Support index lookups */
[key: string | number | symbol]: T | any;
/** Whether cache is complete */
complete = false;
@ -13,21 +15,22 @@ export class Cache<K, T> {
* @param {keyof T} key Default property to use as primary key
* @param {number} ttl Default expiry in milliseconds
*/
constructor(public readonly key: keyof T, public ttl?: number) {
constructor(public readonly key?: keyof T, public ttl?: number) {
return new Proxy(this, {
get: (target: this, prop: string | symbol) => {
if(prop in target) return (<any>target)[prop];
return target.store[prop];
if (prop in target) return (target as any)[prop];
return target.store[prop as K];
},
set: (target: any, prop: string | symbol, value: T) => {
if(prop in target) target[prop] = value;
else target.store[prop] = value;
set: (target: this, prop: string | symbol, value: any) => {
if (prop in target) (target as any)[prop] = value;
else target.store[prop as K] = value;
return true;
}
});
}
private getKey(value: T): K {
if(!this.key) throw new Error('No key defined');
return <K>value[this.key];
}
@ -101,6 +104,15 @@ export class Cache<K, T> {
return <K[]>Object.keys(this.store);
}
/**
* Get map of cached items
*
* @return {Record<K, T>}
*/
map(): Record<K, T> {
return structuredClone(this.store);
}
/**
* Add an item to the cache manually specifying the key
*

View File

@ -1,6 +1,10 @@
export type Listener = (...args: any[]) => any;
export type TypedEvents = {[k in string | symbol]: Listener} & {'*': (event: string, ...args: any[]) => any};
export type NamespaceEvents<Namespace extends string, Events extends TypedEvents> = {
[K in keyof Events as `${Namespace}:${Extract<K, string>}`]: Events[K];
};
export class TypedEmitter<T extends TypedEvents = TypedEvents> {
private static listeners: {[key: string]: Listener[]} = {};

View File

@ -15,3 +15,4 @@ export * from './promise-progress';
export * from './string';
export * from './time';
export * from './types';
export * from 'var-persist';

View File

@ -11,3 +11,13 @@ export function gravatar(email: string, def='mp') {
if(!email) return '';
return `https://www.gravatar.com/avatar/${md5(email)}?d=${def}`;
}
/**
* Escape any regex special characters to avoid misinterpretation during search
*
* @param {string} value String which should be escaped
* @return {string} New escaped sequence
*/
export function escapeRegex(value: string) {
return value.replace(/[.*+?^${}()|\[\]\\]/g, '\\$&');
}

View File

@ -36,14 +36,14 @@ export function sleep(ms: number): Promise<void> {
* ```js
* let loading = true;
* setTimeout(() => wait = false, 1000);
* await sleepUntil(() => loading); // Won't continue until loading flag is false
* await sleepWhile(() => loading); // Won't continue until loading flag is false
* ```
*
* @param {() => boolean | Promise<boolean>} fn Return true to continue
* @param {number} checkInterval Run function ever x milliseconds
* @return {Promise<void>} Callback when sleep is over
*/
export async function sleepUntil(fn : () => boolean | Promise<boolean>, checkInterval = 100): Promise<void> {
export async function sleepWhile(fn : () => boolean | Promise<boolean>, checkInterval = 100): Promise<void> {
while(await fn()) await sleep(checkInterval);
}