Skip to content

Commit

Permalink
[minor] add getOrSet for objects to @augment-vir/common
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Jan 6, 2024
1 parent c80ff35 commit ca6bbf5
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 219 deletions.
233 changes: 118 additions & 115 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "augment-vir",
"version": "22.2.2",
"version": "22.3.0",
"private": true,
"homepage": "https://github.com/electrovir/augment-vir",
"bugs": {
Expand Down
6 changes: 3 additions & 3 deletions packages/browser-testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/browser-testing",
"version": "22.2.2",
"version": "22.3.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -23,8 +23,8 @@
"test:coverage": "npm test"
},
"dependencies": {
"@augment-vir/common": "^22.2.2",
"@augment-vir/testing": "^22.2.2",
"@augment-vir/common": "^22.3.0",
"@augment-vir/testing": "^22.3.0",
"@open-wc/testing": "^4.0.0",
"@types/mocha": "^10.0.6",
"@web/test-runner-commands": "^0.9.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/browser",
"version": "22.2.2",
"version": "22.3.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/browser",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -24,12 +24,12 @@
"test:watch": "web-test-runner --color --config configs/web-test-runner.config.mjs --watch"
},
"dependencies": {
"@augment-vir/common": "^22.2.2",
"@augment-vir/common": "^22.3.0",
"html-spec-tags": "^1.0.3",
"run-time-assertions": "^0.3.0"
},
"devDependencies": {
"@augment-vir/browser-testing": "^22.2.2",
"@augment-vir/browser-testing": "^22.3.0",
"@open-wc/testing": "^4.0.0",
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.6",
Expand Down
6 changes: 3 additions & 3 deletions packages/chai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/chai",
"version": "22.2.2",
"version": "22.3.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand All @@ -23,8 +23,8 @@
"test:coverage": "npm test"
},
"dependencies": {
"@augment-vir/common": "^22.2.2",
"@augment-vir/testing": "^22.2.2",
"@augment-vir/common": "^22.3.0",
"@augment-vir/testing": "^22.3.0",
"type-fest": "^4.9.0"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions packages/common-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/common-tests",
"version": "22.2.2",
"version": "22.3.0",
"private": true,
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common-tests",
"bugs": {
Expand All @@ -22,9 +22,9 @@
"test:types": "tsc --noEmit"
},
"devDependencies": {
"@augment-vir/chai": "^22.2.2",
"@augment-vir/common": "^22.2.2",
"@augment-vir/node-js": "^22.2.2",
"@augment-vir/chai": "^22.3.0",
"@augment-vir/common": "^22.3.0",
"@augment-vir/node-js": "^22.3.0",
"@electrovir/nyc": "^15.1.0-fix0",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@types/chai": "^4.3.11",
Expand Down
45 changes: 0 additions & 45 deletions packages/common-tests/src/tests/map.test.ts

This file was deleted.

159 changes: 159 additions & 0 deletions packages/common-tests/src/tests/object/get-or-set.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import {itCases} from '@augment-vir/chai';
import {AnyObject, getOrSet, getOrSetFromMap, randomString} from '@augment-vir/common';
import {assert} from 'chai';
import {describe} from 'mocha';
import {assertInstanceOf, assertThrows, assertTypeOf} from 'run-time-assertions';

describe(getOrSetFromMap.name, () => {
it('retrieves an existing item', () => {
const exampleKey = {};
const exampleValue = randomString();
const exampleMap = new Map([
[
exampleKey,
exampleValue,
],
]);

assert.strictEqual(
getOrSetFromMap(exampleMap, exampleKey, () => ''),
exampleValue,
);
});

it('sets a new item if it did not exist', () => {
const exampleKey = {};
const exampleValue = randomString();
const exampleMap = new Map();

assert.strictEqual(
getOrSetFromMap(exampleMap, exampleKey, () => exampleValue),
exampleValue,
);
assert.strictEqual(exampleMap.get(exampleKey), exampleValue);
});

it('works with WeakMap', () => {
const exampleKey = {};
const exampleValue = randomString();
const exampleMap = new WeakMap();

assert.strictEqual(
getOrSetFromMap(exampleMap, exampleKey, () => exampleValue),
exampleValue,
);
assert.strictEqual(exampleMap.get(exampleKey), exampleValue);
});
});

describe(getOrSet.name, () => {
function testGetOrSet(
originalObject: Record<PropertyKey, unknown>,
key: PropertyKey,
newValue: unknown,
) {
const got = getOrSet(originalObject, key, () => newValue);

return {
got,
object: originalObject,
};
}

itCases(testGetOrSet, [
{
it: 'adds a missing value',
inputs: [
{},
'hi',
'new value',
],
expect: {
got: 'new value',
object: {hi: 'new value'},
},
},
{
it: 'does not modify a value that already exists',
inputs: [
{myKey: 'hello'},
'myKey',
randomString(),
],
expect: {
got: 'hello',
object: {myKey: 'hello'},
},
},
{
it: 'keeps undefined values',
inputs: [
{myKey: undefined},
'myKey',
randomString(),
],
expect: {
got: undefined,
object: {myKey: undefined},
},
},
]);

it('does not call the callback unless needed', () => {
let callCount = 0;
const originalObject: AnyObject = {};
const key = 'myKey';
const newValue = randomString();

assert.strictEqual(
getOrSet(originalObject, key, () => {
callCount++;
return newValue;
}),
newValue,
);
assert.strictEqual(callCount, 1);

assert.strictEqual(
getOrSet(originalObject, key, () => {
callCount++;
return newValue;
}),
newValue,
);
assert.strictEqual(callCount, 1);
});
it('handles an async callback', async () => {
const newValue = randomString();
const result = getOrSet({} as AnyObject, 'myKey', async () => {
return await Promise.resolve(newValue);
});

assertInstanceOf(result, Promise);

assert.strictEqual(await result, newValue);
});
it('requires proper types', () => {
const myObject: {a: string; b?: number} = {a: 'hello'};
assertTypeOf(getOrSet(myObject, 'b', () => 5)).toEqualTypeOf<number>();
// @ts-expect-error the "a" key requires a string type
getOrSet(myObject, 'a', () => 5);
assertTypeOf(getOrSet(myObject, 'b', async () => 5)).toEqualTypeOf<Promise<number>>();
});
it('re-throws callback errors', async () => {
await assertThrows(
async () => {
await getOrSet({} as AnyObject, 'myKey', async () => {
throw new Error('test error');
});
},
{matchMessage: 'test error'},
);
});
it('works with an indexed key type', () => {
const myObject: Record<string, number> = {a: 4};
assertTypeOf(getOrSet(myObject, 'a', () => 5)).toEqualTypeOf<number>();
assertTypeOf(getOrSet(myObject, 'b', () => 2)).toEqualTypeOf<number>();
assert.deepStrictEqual(myObject, {a: 4, b: 2});
});
});
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@augment-vir/common",
"version": "22.2.2",
"version": "22.3.0",
"homepage": "https://github.com/electrovir/augment-vir/tree/main/packages/common",
"bugs": {
"url": "https://github.com/electrovir/augment-vir/issues"
Expand Down
29 changes: 0 additions & 29 deletions packages/common/src/augments/map.ts

This file was deleted.

Loading

0 comments on commit ca6bbf5

Please sign in to comment.