Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

feat: add Collection#ensure #52

Merged
merged 26 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f189f8a
feat: add 'ensure' method
kxmndz Jun 6, 2021
b915831
test: add 'ensure' method tests
kxmndz Jun 6, 2021
a7a8b29
Update src/index.ts
kxmndz Jun 9, 2021
b5ec6ad
update wording
kxmndz Jun 10, 2021
af4501d
style: update wording on some stuff
kxmndz Jun 10, 2021
d9636c6
refactor(ensure): lazily evaluate result
kxmndz Jun 13, 2021
8af3b0d
test(ensure): update tests for lazy evaluation
kxmndz Jun 13, 2021
7f6f161
refactor(ensure): lazily evaluate defaultValue
kxmndz Jun 14, 2021
469fefa
test(ensure): update tests for lazily evaluated defaultValue
kxmndz Jun 14, 2021
8b9b4cf
refactor(ensure): revert back to has and get
kxmndz Jun 15, 2021
a6f5169
docs(ensure): remove jsdoc example
kxmndz Jun 16, 2021
3a8b835
docs(ensure): remove @param links in docstring
kxmndz Jun 16, 2021
6af4e64
docs(ensure): update docstring wording
kxmndz Jun 17, 2021
6e671a2
refactor(#40): plus more
MidSpike Nov 24, 2021
f8eb7b4
Apply suggestions from code review
MidSpike Nov 24, 2021
6a439d2
Update src/index.ts
MidSpike Nov 24, 2021
69cad08
fix(Tests): ensured tests used scoped varaiables
MidSpike Nov 24, 2021
de2e8db
Update src/index.ts
MidSpike Nov 24, 2021
65c1607
fix(Tests): implemented requested changes
MidSpike Nov 24, 2021
9edfaaa
fix(Docs): fixes formatting
MidSpike Nov 24, 2021
58077d1
Update src/index.ts
MidSpike Nov 24, 2021
3046aaf
Update src/index.ts
MidSpike Nov 24, 2021
86b3983
Update src/index.ts
MidSpike Nov 25, 2021
ace3fbd
Update src/index.ts
MidSpike Nov 25, 2021
330ed96
Update __tests__/collection.test.ts
MidSpike Nov 25, 2021
52e011c
Update src/index.ts
MidSpike Nov 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions __tests__/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,35 @@ describe('random thisArg tests', () => {
}, array);
});
});

describe('ensure() tests', () => {
function createTestCollection() {
return new Collection([
['a', 1],
['b', 2],
]);
}

test('set new value if key does not exist', () => {
const coll = createTestCollection();
coll.ensure('c', () => 3);
expect(coll.size).toStrictEqual(3);
expect(coll.get('c')).toStrictEqual(3);
});

test('return existing value if key exists', () => {
const coll = createTestCollection();
const ensureB = coll.ensure('b', () => 3);
const getB = coll.get('b');
expect(ensureB).toStrictEqual(2);
expect(getB).toStrictEqual(2);
expect(coll.size).toStrictEqual(2);
});

test('ensure with existing key should not change the collection', () => {
const coll = createTestCollection();
coll.ensure('a', () => 4);
expect(coll.size).toStrictEqual(2);
expect(coll.get('a')).toStrictEqual(1);
});
MidSpike marked this conversation as resolved.
Show resolved Hide resolved
});
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ export interface Collection<K, V> extends Map<K, V> {
export class Collection<K, V> extends Map<K, V> {
public static readonly default: typeof Collection = Collection;

/**
* Obtains an element if the key exists, otherwise sets and returns the value provided by the default value generator.
MidSpike marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {*} key - The key to get from if it exists or set otherwise
* @param {Function} defaultValueGenerator - A function that returns the ensured value
MidSpike marked this conversation as resolved.
Show resolved Hide resolved
*
* @returns The existing value if any, otherwise the value provided by the default value generator.
*
MidSpike marked this conversation as resolved.
Show resolved Hide resolved
* @example
* collection.ensure(guildId, () => defaultGuildConfig);
*/
public ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V | undefined {
MidSpike marked this conversation as resolved.
Show resolved Hide resolved
if (this.has(key)) return this.get(key);
MidSpike marked this conversation as resolved.
Show resolved Hide resolved
const defaultValue = defaultValueGenerator(key, this);
this.set(key, defaultValue);
return defaultValue;
kyranet marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Checks if all of the elements exist in the collection.
*
Expand Down