From 26cc18ffb36857de71f90758cbef6e9d1aa4fe3e Mon Sep 17 00:00:00 2001 From: ztimson Date: Sun, 11 May 2025 11:46:03 -0400 Subject: [PATCH] Fixed path event, renamed testCondition to logicTest & fixed some tests --- index.html | 104 ------------------------------------------- package.json | 2 +- src/path-events.ts | 10 ++--- src/search.ts | 18 +++++--- tests/misc.spec.ts | 40 +++++------------ tests/search.spec.ts | 9 ++++ tests/string.spec.ts | 27 ++++++++++- tests/time.spec.ts | 12 +++++ 8 files changed, 76 insertions(+), 146 deletions(-) delete mode 100644 index.html create mode 100644 tests/search.spec.ts create mode 100644 tests/time.spec.ts diff --git a/index.html b/index.html deleted file mode 100644 index ec93f1e..0000000 --- a/index.html +++ /dev/null @@ -1,104 +0,0 @@ - - - - - - About Us | OurTrainingRoom - - - -
-

About Us

-

Empowering Learning Through Innovation

-
- -
-
-

- E-learning has evolved significantly since its inception. Today, there's a shift towards - blended learning services, integrating online activities with practical, real-world applications. -

-
- -
-

What We Do

-

At OurTrainingRoom.com, we specialize in content management and professional development training tailored for:

-
    -
  • School Boards
  • -
  • Municipalities
  • -
  • Hospitals
  • -
  • Large Corporations
  • -
-
- -
-

Our Roots

-

- Our parent company, The Auxilium Group, is a leader in online data management. - The formation of OurTrainingRoom.com was a natural progression to deliver state-of-the-art front-end e-learning programs. -

-
- -
-

Our Approach

-

- Built on principles of quality and continuous improvement, our diverse delivery range continues to grow. - We set new trends by enhancing our existing products and attentively listening to our clients and their employees. - This unique approach has solidified our position in the industry, making a substantial impact for our clients. -

-
- -
-

Have a Question?

-

- We value your inquiries and are here to assist you. Please reach out with any questions or feedback. -

-
-
- - - - diff --git a/package.json b/package.json index 88442a6..941599e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.24.2", + "version": "0.24.3", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/path-events.ts b/src/path-events.ts index 2e08873..b383455 100644 --- a/src/path-events.ts +++ b/src/path-events.ts @@ -31,7 +31,7 @@ export function PE(str: TemplateStringsArray, ...args: any[]) { if(str[i]) combined.push(str[i]); if(args[i]) combined.push(args[i]); } - return new PathEvent(combined.join('')); + return new PathEvent(combined.join('/')); } /** @@ -79,16 +79,16 @@ export class PathEvent { set none(v: boolean) { v ? this.methods = new ASet(['n']) : this.methods.delete('n'); } /** Create method specified */ get create(): boolean { return !this.methods.has('n') && (this.methods.has('*') || this.methods.has('c')) } - set create(v: boolean) { v ? this.methods.delete('n').add('c') : this.methods.delete('c'); } + set create(v: boolean) { v ? this.methods.delete('n').delete('*').add('c') : this.methods.delete('c'); } /** Read method specified */ get read(): boolean { return !this.methods.has('n') && (this.methods.has('*') || this.methods.has('r')) } - set read(v: boolean) { v ? this.methods.delete('n').add('r') : this.methods.delete('r'); } + set read(v: boolean) { v ? this.methods.delete('n').delete('*').add('r') : this.methods.delete('r'); } /** Update method specified */ get update(): boolean { return !this.methods.has('n') && (this.methods.has('*') || this.methods.has('u')) } - set update(v: boolean) { v ? this.methods.delete('n').add('u') : this.methods.delete('u'); } + set update(v: boolean) { v ? this.methods.delete('n').delete('*').add('u') : this.methods.delete('u'); } /** Delete method specified */ get delete(): boolean { return !this.methods.has('n') && (this.methods.has('*') || this.methods.has('d')) } - set delete(v: boolean) { v ? this.methods.delete('n').add('d') : this.methods.delete('d'); } + set delete(v: boolean) { v ? this.methods.delete('n').delete('*').add('d') : this.methods.delete('d'); } constructor(Event: string | PathEvent) { if(typeof Event == 'object') return Object.assign(this, Event); diff --git a/src/search.ts b/src/search.ts index 3f604c6..ebb3866 100644 --- a/src/search.ts +++ b/src/search.ts @@ -1,4 +1,4 @@ -import {dotNotation, JSONAttemptParse} from './objects.ts'; +import {dotNotation, JSONAttemptParse, JSONSerialize} from './objects.ts'; export function search(rows: any[], search: string, regex?: boolean, transform: Function = (r: any) => r) { if(!rows) return []; @@ -13,12 +13,18 @@ export function search(rows: any[], search: string, regex?: boolean, transform: catch { return false; } }).length } else { - return testCondition(search, r); + return logicTest(r, search); } }); } -export function testCondition(condition: string, row: any) { +/** + * Test an object against a logic condition. By default values are checked + * @param {string} condition + * @param {object} target + * @return {boolean} + */ +export function logicTest(target: object, condition: string): boolean { const evalBoolean = (a: any, op: string, b: any): boolean => { switch(op) { case '=': @@ -40,11 +46,11 @@ export function testCondition(condition: string, row: any) { // 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])); + const key = Object.keys(target).find(k => k.toLowerCase() == prop[1].toLowerCase()); + return evalBoolean(dotNotation(target, 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(''); + const v = Object.values(target).map(JSONSerialize).join(''); if(/[A-Z]/g.test(condition)) return v.includes(p); // Case-insensitive return v.toLowerCase().includes(p); diff --git a/tests/misc.spec.ts b/tests/misc.spec.ts index 7a832ba..f52014b 100644 --- a/tests/misc.spec.ts +++ b/tests/misc.spec.ts @@ -1,36 +1,18 @@ -import {sleep, parseUrl} from '../src'; +import {fn} from '../src'; describe('Miscellanies Utilities', () => { - describe('sleep', () => { - test('wait until', async () => { - const wait = ~~(Math.random() * 500); - const time = new Date().getTime(); - await sleep(wait); - expect(new Date().getTime()).toBeGreaterThanOrEqual(time + wait); - }); - }); - - describe('urlParser', () => { - test('localhost w/ port', () => { - const parsed = parseUrl('http://localhost:4200/some/path?q1=test1&q2=test2#frag'); - expect(parsed.protocol).toStrictEqual('http'); - expect(parsed.host).toStrictEqual('localhost:4200'); - expect(parsed.domain).toStrictEqual('localhost'); - expect(parsed.port).toStrictEqual(4200); - expect(parsed.path).toStrictEqual('/some/path'); - expect(parsed.query).toStrictEqual({q1: 'test1', q2: 'test2'}); - expect(parsed.fragment).toStrictEqual('frag'); + describe('fn', () => { + test('async', async () => { + const test = {a: Math.random()}; + const resp = fn(test, 'return a;', true); + expect(resp instanceof Promise).toBeTruthy(); + expect(await resp).toEqual(test['a']); }); - test('advanced URL', () => { - const parsed = parseUrl('https://sub.domain.example.com/some/path?q1=test1&q2=test2#frag'); - expect(parsed.protocol).toStrictEqual('https'); - expect(parsed.host).toStrictEqual('sub.domain.example.com'); - expect(parsed.domain).toStrictEqual('example.com'); - expect(parsed.subdomain).toStrictEqual('sub.domain'); - expect(parsed.path).toStrictEqual('/some/path'); - expect(parsed.query).toStrictEqual({q1: 'test1', q2: 'test2'}); - expect(parsed.fragment).toStrictEqual('frag'); + test('sync', async () => { + const test = {a: Math.random()}; + const resp = fn(test, 'return a;3'); + expect(resp).toEqual(test['a']); }); }); }); diff --git a/tests/search.spec.ts b/tests/search.spec.ts new file mode 100644 index 0000000..17fc78a --- /dev/null +++ b/tests/search.spec.ts @@ -0,0 +1,9 @@ +import {logicTest} from '../src'; + +describe('Search Utilities', () => { + describe('testCondition', () => { + test('=', () => { + expect(logicTest('')) + }); + }); +}); diff --git a/tests/string.spec.ts b/tests/string.spec.ts index 1f67830..0ce0c86 100644 --- a/tests/string.spec.ts +++ b/tests/string.spec.ts @@ -1,4 +1,5 @@ -import {matchAll, randomString, randomStringBuilder} from "../src"; +import {matchAll, parseUrl, randomString, randomStringBuilder} from "../src"; + describe('String Utilities', () => { describe('randomString', () => { @@ -47,4 +48,28 @@ describe('String Utilities', () => { test('using regex', () => expect(matchAll('fooBar fooBar FooBar', /fooBar/g).length).toBe(2)); test('using malformed regex', () => expect(() => matchAll('fooBar fooBar FooBar', /fooBar/)).toThrow()); }); + + describe('urlParser', () => { + test('localhost', () => { + const parsed = parseUrl('http://localhost:4200/some/path?q1=test1&q2=test2#frag'); + expect(parsed.protocol).toStrictEqual('http'); + expect(parsed.host).toStrictEqual('localhost:4200'); + expect(parsed.domain).toStrictEqual('localhost'); + expect(parsed.port).toStrictEqual(4200); + expect(parsed.path).toStrictEqual('/some/path'); + expect(parsed.query).toStrictEqual({q1: 'test1', q2: 'test2'}); + expect(parsed.fragment).toStrictEqual('frag'); + }); + + test('subdomains', () => { + const parsed = parseUrl('https://sub.domain.example.com/some/path?q1=test1&q2=test2#frag'); + expect(parsed.protocol).toStrictEqual('https'); + expect(parsed.host).toStrictEqual('sub.domain.example.com'); + expect(parsed.domain).toStrictEqual('example.com'); + expect(parsed.subdomain).toStrictEqual('sub.domain'); + expect(parsed.path).toStrictEqual('/some/path'); + expect(parsed.query).toStrictEqual({q1: 'test1', q2: 'test2'}); + expect(parsed.fragment).toStrictEqual('frag'); + }); + }); }); diff --git a/tests/time.spec.ts b/tests/time.spec.ts new file mode 100644 index 0000000..b9e2311 --- /dev/null +++ b/tests/time.spec.ts @@ -0,0 +1,12 @@ +import {sleep} from '../src'; + +describe('Time Utilities', () => { + describe('sleep', () => { + test('wait until', async () => { + const wait = ~~(Math.random() * 500); + const time = new Date().getTime(); + await sleep(wait); + expect(new Date().getTime()).toBeGreaterThanOrEqual(time + wait); + }); + }); +});