Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
54a2880391 | |||
b7aeee4706 | |||
b3eab0d6c9 | |||
ad4194a981 | |||
e1f22a01a6 | |||
91c0858d9f | |||
8094b6507f | |||
e40f410830 | |||
c1043e65e2 |
10
.github/workflows/build.yaml
vendored
10
.github/workflows/build.yaml
vendored
@ -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
13
Dockerfile
Normal 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
|
@ -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
11346
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.16.7",
|
||||
"version": "0.17.1",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
28
src/cache.ts
28
src/cache.ts
@ -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
|
||||
*
|
||||
|
@ -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[]} = {};
|
||||
|
||||
|
@ -15,3 +15,4 @@ export * from './promise-progress';
|
||||
export * from './string';
|
||||
export * from './time';
|
||||
export * from './types';
|
||||
export * from 'var-persist';
|
||||
|
@ -6,10 +6,10 @@ import {JSONAttemptParse} from './objects.ts';
|
||||
* @param {string} token JWT to decode
|
||||
* @return {unknown} JWT payload
|
||||
*/
|
||||
function decodeJwt<T>(token: string): T {
|
||||
export function jwtDecode<T>(token: string): T {
|
||||
const base64 = token.split('.')[1]
|
||||
.replace(/-/g, '+').replace(/_/g, '/');
|
||||
return <T>JSONAttemptParse(decodeURIComponent(window.atob(base64).split('').map(function(c) {
|
||||
return <T>JSONAttemptParse(decodeURIComponent(atob(base64).split('').map(function(c) {
|
||||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
||||
}).join('')));
|
||||
}
|
||||
|
Reference in New Issue
Block a user