- 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", "name": "@ztimson/utils",
"version": "0.23.23", "version": "0.24.0",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@ -22,7 +22,6 @@ export class Cache<K extends string | number | symbol, T> {
/** /**
* Create new cache * Create new cache
*
* @param {keyof T} key Default property to use as primary key * @param {keyof T} key Default property to use as primary key
* @param options * @param options
*/ */
@ -56,7 +55,6 @@ export class Cache<K extends string | number | symbol, T> {
/** /**
* Get all cached items * Get all cached items
*
* @return {T[]} Array of items * @return {T[]} Array of items
*/ */
all(): T[] { 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 * Add a new item to the cache. Like set, but finds key automatically
*
* @param {T} value Item to add to cache * @param {T} value Item to add to cache
* @param {number | undefined} ttl Override default expiry * @param {number | undefined} ttl Override default expiry
* @return {this} * @return {this}
@ -78,12 +75,12 @@ export class Cache<K extends string | number | symbol, T> {
/** /**
* Add several rows to the cache * Add several rows to the cache
*
* @param {T[]} rows Several items that will be cached using the default key * @param {T[]} rows Several items that will be cached using the default key
* @param complete Mark cache as complete & reliable, defaults to true * @param complete Mark cache as complete & reliable, defaults to true
* @return {this} * @return {this}
*/ */
addAll(rows: T[], complete = true): this { addAll(rows: T[], complete = true): this {
this.clear();
rows.forEach(r => this.add(r)); rows.forEach(r => this.add(r));
this.complete = complete; this.complete = complete;
return this; return this;
@ -98,7 +95,6 @@ export class Cache<K extends string | number | symbol, T> {
/** /**
* Delete an item from the cache * Delete an item from the cache
*
* @param {K} key Item's primary key * @param {K} key Item's primary key
*/ */
delete(key: K) { delete(key: K) {
@ -126,7 +122,6 @@ export class Cache<K extends string | number | symbol, T> {
/** /**
* Get a list of cached keys * Get a list of cached keys
*
* @return {K[]} Array of keys * @return {K[]} Array of keys
*/ */
keys(): K[] { keys(): K[] {
@ -135,7 +130,6 @@ export class Cache<K extends string | number | symbol, T> {
/** /**
* Get map of cached items * Get map of cached items
*
* @return {Record<K, T>} * @return {Record<K, T>}
*/ */
map(): 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 * Add an item to the cache manually specifying the key
*
* @param {K} key Key item will be cached under * @param {K} key Key item will be cached under
* @param {T} value Item to cache * @param {T} value Item to cache
* @param {number | undefined} ttl Override default expiry in seconds * @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 * Get all cached items
*
* @return {T[]} Array of items * @return {T[]} Array of items
*/ */
values = this.all(); values = this.all();

View File

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

View File

@ -109,7 +109,6 @@ export function encodeQuery(data: any): string {
).join('&'); ).join('&');
} }
/** /**
* Recursively flatten a nested object, while maintaining key structure * 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) { export function search(rows: any[], search: string, regex?: boolean, transform: Function = (r: any) => r) {
if(!rows) return []; 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()); } try { return RegExp(search, 'gm').test(v.toString()); }
catch { return false; } catch { return false; }
}).length }).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 export function testCondition(condition: string, row: any) {
const and = p.split('&&').map(p => p.trim()).filter(p => !!p); const evalBoolean = (a: any, op: string, b: any): boolean => {
return and.filter(p => { switch(op) {
// Boolean operator case '=':
const prop = /(\w+)\s*(==?|!=|>=|>|<=|<)\s*(\w+)/g.exec(p); case '==': return a == b;
if(prop) { case '!=': return a != b;
const a = JSON.stringify(JSONAttemptParse(dotNotation<any>(value, prop[1]))); case '>': return a > b;
const operator = prop[2] == '=' ? '==' : prop[2]; case '>=': return a >= b;
const b = JSON.stringify(JSONAttemptParse(prop[3])); case '<': return a < b;
return eval(`${a} ${operator} ${b}`); case '<=': return a <= b;
} default: return false;
// Case-sensitive }
const v = Object.values(value).join(''); }
if(/[A-Z]/g.test(search)) return v.includes(p);
// Case-insensitive const or = condition.split('||').map(p => p.trim()).filter(p => !!p);
return v.toLowerCase().includes(p); return -1 != or.findIndex(p => {
}).length == and.length; // 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;
}); });
} }