smartobject/test/test.both.ts

70 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

2023-07-12 17:40:41 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
import * as smartobject from '../ts/index.js';
2020-07-24 11:37:11 +00:00
tap.test('first test', async () => {
const result = smartobject.compareObjects(
{ thisIsEq: 'wow', thisIsDeepEq: { deeper: 'sodeep' } },
2021-03-22 02:33:23 +00:00
{ thisIsEq: 'wow', thisIsDeepEq: { deeper: 'sodeep' }, hey: 'there' }
2020-07-24 11:37:11 +00:00
);
console.log(result);
});
2021-03-22 02:33:01 +00:00
tap.test('should fast deep equal objects', async () => {
2022-03-06 10:09:53 +00:00
expect(smartobject.fastDeepEqual({ hello: 'yes' }, { hello: 'yes' })).toBeTrue();
expect(smartobject.fastDeepEqual({ hello: 'yes' }, { hello: 3 })).toBeFalse();
});
let parentObject: any = {};
let childObject: any = {};
tap.test('childObject should not yet be in parentObject', async () => {
expect(smartobject.exists(parentObject, 'childObject')).toBeFalse();
parentObject.childObject = childObject;
});
tap.test('childObject should now be in parentObject', async () => {
expect(smartobject.exists(parentObject, 'childObject')).toBeTrue();
});
tap.test('should be able to deepAdd an childParam', async () => {
const parentObject = {
2023-07-12 17:40:41 +00:00
hello: 'there',
2022-03-06 10:09:53 +00:00
};
const parentObject2 = smartobject.smartAdd(parentObject, 'wow.za', 'yes');
console.log(parentObject2);
expect(smartobject.exists(parentObject2.wow, 'za')).toBeTrue();
});
tap.test('should be able to deep get an item', async () => {
const testObject = {
hey: {
there: {
2023-07-12 17:40:41 +00:00
so: 'cool',
},
},
2022-03-06 10:09:53 +00:00
};
const item = smartobject.smartGet(testObject, 'hey.there.so');
expect(item).toEqual('cool');
});
tap.test('should call properties for minimatched properties', async () => {
let testObject = {
matchedOne: 'Hey!',
matchedTwo: 'this works!',
2023-07-12 17:40:41 +00:00
notmatched: 'NOT!',
2022-03-06 10:09:53 +00:00
};
const matchedStrings: string[] = [];
2023-07-12 17:40:41 +00:00
await smartobject.forEachMinimatch(testObject, 'matched*', (matchedProperty) => {
2022-03-06 10:09:53 +00:00
matchedStrings.push(matchedProperty);
console.log(matchedProperty);
});
expect(matchedStrings).toContain('Hey!');
expect(matchedStrings).toContain('this works!');
expect(matchedStrings).not.toContain('NOT!');
2021-03-22 02:33:01 +00:00
});
2020-07-24 11:37:11 +00:00
tap.start();