Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
e40f410830 | |||
c1043e65e2 | |||
67d9928a61 | |||
e6636d373b | |||
811d797e1b | |||
0909c4f648 | |||
8384d6a299 | |||
19251244d2 |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.15.6",
|
"version": "0.16.9",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.15.6",
|
"version": "0.16.9",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.5.12",
|
"@types/jest": "^29.5.12",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.16.1",
|
"version": "0.16.9",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
127
src/cache.ts
Normal file
127
src/cache.ts
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
/**
|
||||||
|
* 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 = {};
|
||||||
|
|
||||||
|
/** Whether cache is complete */
|
||||||
|
complete = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create new cache
|
||||||
|
*
|
||||||
|
* @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) {
|
||||||
|
return new Proxy(this, {
|
||||||
|
get: (target: this, prop: string | symbol) => {
|
||||||
|
if(prop in target) return (<any>target)[prop];
|
||||||
|
return target.store[prop];
|
||||||
|
},
|
||||||
|
set: (target: any, prop: string | symbol, value: T) => {
|
||||||
|
if(prop in target) target[prop] = value;
|
||||||
|
else target.store[prop] = value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private getKey(value: T): K {
|
||||||
|
return <K>value[this.key];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all cached items
|
||||||
|
*
|
||||||
|
* @return {T[]} Array of items
|
||||||
|
*/
|
||||||
|
all(): T[] {
|
||||||
|
return Object.values(this.store);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new item to the cache. Like set, but finds key automatically
|
||||||
|
*
|
||||||
|
* @param {T} value Item to add to cache
|
||||||
|
* @param {number | undefined} ttl Override default expiry
|
||||||
|
* @return {this}
|
||||||
|
*/
|
||||||
|
add(value: T, ttl = this.ttl): this {
|
||||||
|
const key = this.getKey(value);
|
||||||
|
this.set(key, value, ttl);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add several rows to the cache
|
||||||
|
*
|
||||||
|
* @param {T[]} rows Several items that will be cached using the default key
|
||||||
|
* @param complete Mark cache as complete & reliable, defaults to true
|
||||||
|
* @return {this}
|
||||||
|
*/
|
||||||
|
addAll(rows: T[], complete = true): this {
|
||||||
|
rows.forEach(r => this.add(r));
|
||||||
|
this.complete = complete;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete an item from the cache
|
||||||
|
*
|
||||||
|
* @param {K} key Item's primary key
|
||||||
|
*/
|
||||||
|
delete(key: K) {
|
||||||
|
delete this.store[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return cache as an array of key-value pairs
|
||||||
|
* @return {[K, T][]} Key-value pairs array
|
||||||
|
*/
|
||||||
|
entries(): [K, T][] {
|
||||||
|
return <[K, T][]>Object.entries(this.store);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get item from the cache
|
||||||
|
* @param {K} key Key to lookup
|
||||||
|
* @return {T} Cached item
|
||||||
|
*/
|
||||||
|
get(key: K): T {
|
||||||
|
return this.store[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of cached keys
|
||||||
|
*
|
||||||
|
* @return {K[]} Array of keys
|
||||||
|
*/
|
||||||
|
keys(): K[] {
|
||||||
|
return <K[]>Object.keys(this.store);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add an item to the cache manually specifying the key
|
||||||
|
*
|
||||||
|
* @param {K} key Key item will be cached under
|
||||||
|
* @param {T} value Item to cache
|
||||||
|
* @param {number | undefined} ttl Override default expiry
|
||||||
|
* @return {this}
|
||||||
|
*/
|
||||||
|
set(key: K, value: T, ttl = this.ttl): this {
|
||||||
|
this.store[key] = value;
|
||||||
|
if(ttl) setTimeout(() => {
|
||||||
|
this.complete = false;
|
||||||
|
this.delete(key);
|
||||||
|
}, ttl);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all cached items
|
||||||
|
*
|
||||||
|
* @return {T[]} Array of items
|
||||||
|
*/
|
||||||
|
values = this.all();
|
||||||
|
}
|
@ -1,10 +1,12 @@
|
|||||||
export * from './array';
|
export * from './array';
|
||||||
export * from './aset';
|
export * from './aset';
|
||||||
|
export * from './cache';
|
||||||
export * from './csv';
|
export * from './csv';
|
||||||
export * from './files';
|
export * from './files';
|
||||||
export * from './emitter';
|
export * from './emitter';
|
||||||
export * from './errors';
|
export * from './errors';
|
||||||
export * from './http';
|
export * from './http';
|
||||||
|
export * from './jwt';
|
||||||
export * from './logger';
|
export * from './logger';
|
||||||
export * from './math';
|
export * from './math';
|
||||||
export * from './misc';
|
export * from './misc';
|
||||||
|
15
src/jwt.ts
Normal file
15
src/jwt.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import {JSONAttemptParse} from './objects.ts';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode a JWT payload, this will not check for tampering so be careful
|
||||||
|
*
|
||||||
|
* @param {string} token JWT to decode
|
||||||
|
* @return {unknown} JWT payload
|
||||||
|
*/
|
||||||
|
export function jwtDecode<T>(token: string): T {
|
||||||
|
const base64 = token.split('.')[1]
|
||||||
|
.replace(/-/g, '+').replace(/_/g, '/');
|
||||||
|
return <T>JSONAttemptParse(decodeURIComponent(atob(base64).split('').map(function(c) {
|
||||||
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
||||||
|
}).join('')));
|
||||||
|
}
|
@ -39,12 +39,12 @@ export function sleep(ms: number): Promise<void> {
|
|||||||
* await sleepUntil(() => loading); // Won't continue until loading flag is false
|
* await sleepUntil(() => loading); // Won't continue until loading flag is false
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @param {() => boolean} fn Return true to continue
|
* @param {() => boolean | Promise<boolean>} fn Return true to continue
|
||||||
* @param {number} checkInterval Run function ever x milliseconds
|
* @param {number} checkInterval Run function ever x milliseconds
|
||||||
* @return {Promise<void>} Callback when sleep is over
|
* @return {Promise<void>} Callback when sleep is over
|
||||||
*/
|
*/
|
||||||
export async function sleepUntil(fn : () => boolean, checkInterval=100): Promise<void> {
|
export async function sleepUntil(fn : () => boolean | Promise<boolean>, checkInterval = 100): Promise<void> {
|
||||||
while(fn()) await sleep(checkInterval);
|
while(await fn()) await sleep(checkInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user