From 3bc82fab45f33890f5e445e3abde48371a37a14b Mon Sep 17 00:00:00 2001 From: ztimson Date: Tue, 6 May 2025 15:59:08 -0400 Subject: [PATCH] - Fixed cache.addAll() - Renamed jwtDecode to decodeJWT to match conventions - Added testCondition to search --- package.json | 2 +- src/cache.ts | 10 +-------- src/jwt.ts | 2 +- src/objects.ts | 1 - src/search.ts | 59 +++++++++++++++++++++++++++++++------------------- 5 files changed, 40 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 63e6513..de8d9bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.23.23", + "version": "0.24.0", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/cache.ts b/src/cache.ts index a3ecee4..743d599 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -22,7 +22,6 @@ export class Cache { /** * Create new cache - * * @param {keyof T} key Default property to use as primary key * @param options */ @@ -56,7 +55,6 @@ export class Cache { /** * Get all cached items - * * @return {T[]} Array of items */ all(): T[] { @@ -65,7 +63,6 @@ export class Cache { /** * 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 { /** * 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 { /** * Delete an item from the cache - * * @param {K} key Item's primary key */ delete(key: K) { @@ -126,7 +122,6 @@ export class Cache { /** * Get a list of cached keys - * * @return {K[]} Array of keys */ keys(): K[] { @@ -135,7 +130,6 @@ export class Cache { /** * Get map of cached items - * * @return {Record} */ map(): Record { @@ -144,7 +138,6 @@ export class Cache { /** * 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 { /** * Get all cached items - * * @return {T[]} Array of items */ values = this.all(); diff --git a/src/jwt.ts b/src/jwt.ts index 6688add..0420167 100644 --- a/src/jwt.ts +++ b/src/jwt.ts @@ -6,7 +6,7 @@ import {JSONAttemptParse} from './objects.ts'; * @param {string} token JWT to decode * @return {unknown} JWT payload */ -export function jwtDecode(token: string): T { +export function decodeJwt(token: string): T { const base64 = token.split('.')[1] .replace(/-/g, '+').replace(/_/g, '/'); return JSONAttemptParse(decodeURIComponent(atob(base64).split('').map(function(c) { diff --git a/src/objects.ts b/src/objects.ts index bab46bf..62a0244 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -109,7 +109,6 @@ export function encodeQuery(data: any): string { ).join('&'); } - /** * Recursively flatten a nested object, while maintaining key structure * diff --git a/src/search.ts b/src/search.ts index 87cb36e..a727820 100644 --- a/src/search.ts +++ b/src/search.ts @@ -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(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(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; }); }