- Fixed cache.addAll()
Some checks failed
Build / Build NPM Project (push) Failing after 34s
Build / Publish Documentation (push) Has been skipped
Build / Tag Version (push) Has been skipped

- Renamed jwtDecode to decodeJWT to match conventions
- Added testCondition to search
This commit is contained in:
Zakary Timson 2025-05-06 15:59:08 -04:00
parent 6b15848896
commit 3bc82fab45
5 changed files with 40 additions and 34 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.23.23",
"version": "0.24.0",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -22,7 +22,6 @@ export class Cache<K extends string | number | symbol, T> {
/**
* Create new cache
*
* @param {keyof T} key Default property to use as primary key
* @param options
*/
@ -56,7 +55,6 @@ export class Cache<K extends string | number | symbol, T> {
/**
* Get all cached items
*
* @return {T[]} Array of items
*/
all(): T[] {
@ -65,7 +63,6 @@ export class Cache<K extends string | number | symbol, T> {
/**
* 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}
@ -78,12 +75,12 @@ export class Cache<K extends string | number | symbol, T> {
/**
* 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 {
this.clear();
rows.forEach(r => this.add(r));
this.complete = complete;
return this;
@ -98,7 +95,6 @@ export class Cache<K extends string | number | symbol, T> {
/**
* Delete an item from the cache
*
* @param {K} key Item's primary key
*/
delete(key: K) {
@ -126,7 +122,6 @@ export class Cache<K extends string | number | symbol, T> {
/**
* Get a list of cached keys
*
* @return {K[]} Array of keys
*/
keys(): K[] {
@ -135,7 +130,6 @@ export class Cache<K extends string | number | symbol, T> {
/**
* Get map of cached items
*
* @return {Record<K, T>}
*/
map(): Record<K, T> {
@ -144,7 +138,6 @@ export class Cache<K extends string | number | symbol, T> {
/**
* 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 in seconds
@ -163,7 +156,6 @@ export class Cache<K extends string | number | symbol, T> {
/**
* Get all cached items
*
* @return {T[]} Array of items
*/
values = this.all();

View File

@ -6,7 +6,7 @@ import {JSONAttemptParse} from './objects.ts';
* @param {string} token JWT to decode
* @return {unknown} JWT payload
*/
export function jwtDecode<T>(token: string): T {
export function decodeJwt<T>(token: string): T {
const base64 = token.split('.')[1]
.replace(/-/g, '+').replace(/_/g, '/');
return <T>JSONAttemptParse(decodeURIComponent(atob(base64).split('').map(function(c) {

View File

@ -109,7 +109,6 @@ export function encodeQuery(data: any): string {
).join('&');
}
/**
* Recursively flatten a nested object, while maintaining key structure
*

View File

@ -1,4 +1,4 @@
import {dotNotation, JSONAttemptParse} from './objects';
import {dotNotation, JSONAttemptParse} from '@ztimson/utils';
export function search(rows: any[], search: string, regex?: boolean, transform: Function = (r: any) => r) {
if(!rows) return [];
@ -12,27 +12,42 @@ export function search(rows: any[], search: string, regex?: boolean, transform:
try { return RegExp(search, 'gm').test(v.toString()); }
catch { return false; }
}).length
} else {
return testCondition(search, r);
}
// Make sure at least one OR passes
const or = search.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 = /(\w+)\s*(==?|!=|>=|>|<=|<)\s*(\w+)/g.exec(p);
if(prop) {
const a = JSON.stringify(JSONAttemptParse(dotNotation<any>(value, prop[1])));
const operator = prop[2] == '=' ? '==' : prop[2];
const b = JSON.stringify(JSONAttemptParse(prop[3]));
return eval(`${a} ${operator} ${b}`);
}
// Case-sensitive
const v = Object.values(value).join('');
if(/[A-Z]/g.test(search)) return v.includes(p);
// Case-insensitive
return v.toLowerCase().includes(p);
}).length == and.length;
})
});
}
export function testCondition(condition: string, row: any) {
const evalBoolean = (a: any, op: string, b: any): boolean => {
switch(op) {
case '=':
case '==': return a == b;
case '!=': return a != b;
case '>': return a > b;
case '>=': return a >= b;
case '<': return a < b;
case '<=': return a <= b;
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(row).find(k => k.toLowerCase() == prop[1].toLowerCase());
return evalBoolean(dotNotation<any>(row, key || prop[1]), prop[2], JSONAttemptParse(prop[3]));
}
// Case-sensitive
const v = Object.values(row).map(v => typeof v == 'object' && v != null ? JSON.stringify(v) : v).join('');
if(/[A-Z]/g.test(condition)) return v.includes(p);
// Case-insensitive
return v.toLowerCase().includes(p);
}).length == and.length;
});
}