From f189f8a890894aab2b1652e7788b9c282beed23f Mon Sep 17 00:00:00 2001 From: Kyran Mendoza Date: Sun, 6 Jun 2021 19:24:38 +0800 Subject: [PATCH 01/26] feat: add 'ensure' method Inspired by `[Enmap](https://enmap.evie.dev)#ensure`, `Collection#ensure` gets an element with the specified key if it exists, otherwise sets it to the provided `defaultValue` and returns the `defaultValue`. Useful for things like per-guild settings where you want to either get it or set it to a default value. --- src/index.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/index.ts b/src/index.ts index 7298809..061fa83 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,6 +25,18 @@ export interface Collection extends Map { export class Collection extends Map { public static readonly default: typeof Collection = Collection; + /** + * Gets an element if the key exists, otherwise sets it to {@param defaultValue} and returns the {@param defaultValue}. + * @param {*} key - Key to get from/set to the collection. + * @param {*} defaultValue - Default value to be set and returned if the key doesn't exist. + * @returns {*} + */ + public ensure(key: K, defaultValue: V): V { + if (this.has(key)) return this.get(key)!; + this.set(key, defaultValue); + return defaultValue; + } + /** * Checks if all of the elements exist in the collection. * From b91583141c8480e7d0e8c36692307eb6d05d35bb Mon Sep 17 00:00:00 2001 From: Kyran Mendoza Date: Sun, 6 Jun 2021 20:39:05 +0800 Subject: [PATCH 02/26] test: add 'ensure' method tests --- __tests__/collection.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/__tests__/collection.test.ts b/__tests__/collection.test.ts index 353980e..a3d9caf 100644 --- a/__tests__/collection.test.ts +++ b/__tests__/collection.test.ts @@ -435,3 +435,28 @@ describe('random thisArg tests', () => { }, array); }); }); + +describe('ensure() tests', () => { + const coll = new Collection(); + coll.set('a', 1); + coll.set('b', 2); + + test('set new value if key does not exist', () => { + coll.ensure('c', 3); + expect(coll.size).toStrictEqual(3); + expect(coll.has('c')).toBeTruthy(); + }); + + test('return existing value if key exists', () => { + const ensureB = coll.ensure('b', 3); + const getB = coll.get('b'); + expect(ensureB).toBe(getB); + expect(coll.size).toStrictEqual(3); + }); + + test('ensure when key exists should not change the collection', () => { + coll.ensure('a', 4); + expect(coll.size).toStrictEqual(3); + expect(coll.get('a')).toStrictEqual(1); + }); +}); From a7a8b2995dddb0775242533959bd51a2e3da4b82 Mon Sep 17 00:00:00 2001 From: kxmndz <71072915+kxmndz@users.noreply.github.com> Date: Wed, 9 Jun 2021 14:39:54 +0800 Subject: [PATCH 03/26] Update src/index.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 061fa83..dee152c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,7 +32,8 @@ export class Collection extends Map { * @returns {*} */ public ensure(key: K, defaultValue: V): V { - if (this.has(key)) return this.get(key)!; + const value = this.get(key); + if (typeof value !== 'undefined') return value; this.set(key, defaultValue); return defaultValue; } From b5ec6ad34b3db859b968f6387702bf04a949e5c1 Mon Sep 17 00:00:00 2001 From: kxmndz <71072915+kxmndz@users.noreply.github.com> Date: Thu, 10 Jun 2021 11:30:38 +0800 Subject: [PATCH 04/26] update wording MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index dee152c..3bb7517 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,7 +26,7 @@ export class Collection extends Map { public static readonly default: typeof Collection = Collection; /** - * Gets an element if the key exists, otherwise sets it to {@param defaultValue} and returns the {@param defaultValue}. + * Gets an element if the key exists, otherwise sets and returns {@param defaultValue}. * @param {*} key - Key to get from/set to the collection. * @param {*} defaultValue - Default value to be set and returned if the key doesn't exist. * @returns {*} From af4501d1b1edbcfc0f2374404b79f1ef3f14add0 Mon Sep 17 00:00:00 2001 From: kxmndz <71072915+kxmndz@users.noreply.github.com> Date: Thu, 10 Jun 2021 11:55:59 +0800 Subject: [PATCH 05/26] style: update wording on some stuff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- __tests__/collection.test.ts | 2 +- src/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/__tests__/collection.test.ts b/__tests__/collection.test.ts index a3d9caf..cd80f05 100644 --- a/__tests__/collection.test.ts +++ b/__tests__/collection.test.ts @@ -454,7 +454,7 @@ describe('ensure() tests', () => { expect(coll.size).toStrictEqual(3); }); - test('ensure when key exists should not change the collection', () => { + test('ensure with existing key should not change the collection', () => { coll.ensure('a', 4); expect(coll.size).toStrictEqual(3); expect(coll.get('a')).toStrictEqual(1); diff --git a/src/index.ts b/src/index.ts index 3bb7517..4644a0e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,7 +29,7 @@ export class Collection extends Map { * Gets an element if the key exists, otherwise sets and returns {@param defaultValue}. * @param {*} key - Key to get from/set to the collection. * @param {*} defaultValue - Default value to be set and returned if the key doesn't exist. - * @returns {*} + * @returns {*} The existing value if any, {@param defaultValue} otherwise. */ public ensure(key: K, defaultValue: V): V { const value = this.get(key); From d9636c61092f193e37fd8a9bce19e2b4a77ef0d4 Mon Sep 17 00:00:00 2001 From: Kyran Mendoza Date: Sun, 13 Jun 2021 22:29:51 +0800 Subject: [PATCH 06/26] refactor(ensure): lazily evaluate result --- src/index.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4644a0e..adc595b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,13 +29,35 @@ export class Collection extends Map { * Gets an element if the key exists, otherwise sets and returns {@param defaultValue}. * @param {*} key - Key to get from/set to the collection. * @param {*} defaultValue - Default value to be set and returned if the key doesn't exist. - * @returns {*} The existing value if any, {@param defaultValue} otherwise. - */ - public ensure(key: K, defaultValue: V): V { + * @returns {function} Function that lazily evaluates to the existing value if any, {@param defaultValue} otherwise. + * @example Example use case: per-guild settings + * // should be customized for what your bot needs + * const defaultSettings = { + * welcomeChannelID: null, + * reactionRoleMessageID: null, + * adminRoleID: null, + * muteRoleID: null + * } + * + * client.on('guildMemberAdd', member => { + * const getGuildSettings = client.settings.ensure(message.guild.id, defaultSettings); + * // ... + * + * // evaluate the data when you need it + * const guildSettings = getGuildSettings(); + * + * // use the data + * if (guildSettings.welcomeChannelID) { + * const channel = client.channels.fetch(guildSettings.welcomeChannelID); + * channel.send(`Hello <@${member.id}>, welcome to ${member.guild.name}! Enjoy your stay!`); + * } + * }) + */ + public ensure(key: K, defaultValue: V): () => V { const value = this.get(key); - if (typeof value !== 'undefined') return value; + if (typeof value !== 'undefined') return () => value; this.set(key, defaultValue); - return defaultValue; + return () => defaultValue; } /** From 8af3b0de16879d2e7c6ce9dba0f41a1c8b28e6c7 Mon Sep 17 00:00:00 2001 From: Kyran Mendoza Date: Sun, 13 Jun 2021 22:33:00 +0800 Subject: [PATCH 07/26] test(ensure): update tests for lazy evaluation --- __tests__/collection.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/__tests__/collection.test.ts b/__tests__/collection.test.ts index cd80f05..5e984ae 100644 --- a/__tests__/collection.test.ts +++ b/__tests__/collection.test.ts @@ -449,8 +449,9 @@ describe('ensure() tests', () => { test('return existing value if key exists', () => { const ensureB = coll.ensure('b', 3); + const ensuredB = ensureB(); const getB = coll.get('b'); - expect(ensureB).toBe(getB); + expect(ensuredB).toBe(getB); expect(coll.size).toStrictEqual(3); }); From 7f6f1613ed8ed8b023f32449476c716a24b33f22 Mon Sep 17 00:00:00 2001 From: Kyran Mendoza Date: Mon, 14 Jun 2021 23:54:27 +0800 Subject: [PATCH 08/26] refactor(ensure): lazily evaluate defaultValue --- src/index.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index adc595b..cc14af3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,8 +28,8 @@ export class Collection extends Map { /** * Gets an element if the key exists, otherwise sets and returns {@param defaultValue}. * @param {*} key - Key to get from/set to the collection. - * @param {*} defaultValue - Default value to be set and returned if the key doesn't exist. - * @returns {function} Function that lazily evaluates to the existing value if any, {@param defaultValue} otherwise. + * @param {*} getDefaultValue - Function that returns the default value to be set and returned if the key doesn't exist. + * @returns {*} The existing value if any, {@param getDefaultValue}'s return value otherwise. * @example Example use case: per-guild settings * // should be customized for what your bot needs * const defaultSettings = { @@ -40,11 +40,7 @@ export class Collection extends Map { * } * * client.on('guildMemberAdd', member => { - * const getGuildSettings = client.settings.ensure(message.guild.id, defaultSettings); - * // ... - * - * // evaluate the data when you need it - * const guildSettings = getGuildSettings(); + * const getGuildSettings = client.settings.ensure(message.guild.id, () => defaultSettings); * * // use the data * if (guildSettings.welcomeChannelID) { @@ -53,11 +49,12 @@ export class Collection extends Map { * } * }) */ - public ensure(key: K, defaultValue: V): () => V { + public ensure(key: K, getDefaultValue: () => V): V { const value = this.get(key); - if (typeof value !== 'undefined') return () => value; + if (typeof value !== 'undefined') return value; + const defaultValue = getDefaultValue(); this.set(key, defaultValue); - return () => defaultValue; + return defaultValue; } /** From 469fefa7cdad3f473ffaddd543fd0e6847a663f7 Mon Sep 17 00:00:00 2001 From: Kyran Mendoza Date: Mon, 14 Jun 2021 23:56:28 +0800 Subject: [PATCH 09/26] test(ensure): update tests for lazily evaluated defaultValue --- __tests__/collection.test.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/__tests__/collection.test.ts b/__tests__/collection.test.ts index 5e984ae..291370c 100644 --- a/__tests__/collection.test.ts +++ b/__tests__/collection.test.ts @@ -442,21 +442,20 @@ describe('ensure() tests', () => { coll.set('b', 2); test('set new value if key does not exist', () => { - coll.ensure('c', 3); + coll.ensure('c', () => 3); expect(coll.size).toStrictEqual(3); expect(coll.has('c')).toBeTruthy(); }); test('return existing value if key exists', () => { - const ensureB = coll.ensure('b', 3); - const ensuredB = ensureB(); + const ensureB = coll.ensure('b', () => 3); const getB = coll.get('b'); - expect(ensuredB).toBe(getB); + expect(ensureB).toBe(getB); expect(coll.size).toStrictEqual(3); }); test('ensure with existing key should not change the collection', () => { - coll.ensure('a', 4); + coll.ensure('a', () => 4); expect(coll.size).toStrictEqual(3); expect(coll.get('a')).toStrictEqual(1); }); From 8b9b4cf0b4bb504b59bf6d5b70765db8d4f0b564 Mon Sep 17 00:00:00 2001 From: Kyran Mendoza Date: Tue, 15 Jun 2021 16:47:56 +0800 Subject: [PATCH 10/26] refactor(ensure): revert back to has and get --- src/index.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index cc14af3..7f8f407 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,9 +26,9 @@ export class Collection extends Map { public static readonly default: typeof Collection = Collection; /** - * Gets an element if the key exists, otherwise sets and returns {@param defaultValue}. + * Gets an element if the key exists, otherwise sets and returns {@param getDefaultValue}'s return value. * @param {*} key - Key to get from/set to the collection. - * @param {*} getDefaultValue - Function that returns the default value to be set and returned if the key doesn't exist. + * @param {function} getDefaultValue - Function that returns the default value to be set and returned if the key doesn't exist. * @returns {*} The existing value if any, {@param getDefaultValue}'s return value otherwise. * @example Example use case: per-guild settings * // should be customized for what your bot needs @@ -50,8 +50,7 @@ export class Collection extends Map { * }) */ public ensure(key: K, getDefaultValue: () => V): V { - const value = this.get(key); - if (typeof value !== 'undefined') return value; + if (this.has(key)) return this.get(key)!; const defaultValue = getDefaultValue(); this.set(key, defaultValue); return defaultValue; From a6f516995c528eab18c49e05fd415561f31384a2 Mon Sep 17 00:00:00 2001 From: kxmndz <71072915+kxmndz@users.noreply.github.com> Date: Wed, 16 Jun 2021 12:54:17 +0800 Subject: [PATCH 11/26] docs(ensure): remove jsdoc example Signed-off-by: Kyran Mendoza --- src/index.ts | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7f8f407..9f78530 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,24 +30,6 @@ export class Collection extends Map { * @param {*} key - Key to get from/set to the collection. * @param {function} getDefaultValue - Function that returns the default value to be set and returned if the key doesn't exist. * @returns {*} The existing value if any, {@param getDefaultValue}'s return value otherwise. - * @example Example use case: per-guild settings - * // should be customized for what your bot needs - * const defaultSettings = { - * welcomeChannelID: null, - * reactionRoleMessageID: null, - * adminRoleID: null, - * muteRoleID: null - * } - * - * client.on('guildMemberAdd', member => { - * const getGuildSettings = client.settings.ensure(message.guild.id, () => defaultSettings); - * - * // use the data - * if (guildSettings.welcomeChannelID) { - * const channel = client.channels.fetch(guildSettings.welcomeChannelID); - * channel.send(`Hello <@${member.id}>, welcome to ${member.guild.name}! Enjoy your stay!`); - * } - * }) */ public ensure(key: K, getDefaultValue: () => V): V { if (this.has(key)) return this.get(key)!; From 3a8b8353d48d5a3f461403e960eaacdfd175e29d Mon Sep 17 00:00:00 2001 From: kxmndz <71072915+kxmndz@users.noreply.github.com> Date: Wed, 16 Jun 2021 18:52:11 +0800 Subject: [PATCH 12/26] docs(ensure): remove @param links in docstring --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9f78530..d349ef2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,10 +26,10 @@ export class Collection extends Map { public static readonly default: typeof Collection = Collection; /** - * Gets an element if the key exists, otherwise sets and returns {@param getDefaultValue}'s return value. + * Gets an element if the key exists, otherwise sets and returns the provided default value. * @param {*} key - Key to get from/set to the collection. * @param {function} getDefaultValue - Function that returns the default value to be set and returned if the key doesn't exist. - * @returns {*} The existing value if any, {@param getDefaultValue}'s return value otherwise. + * @returns {*} The existing value if any, the provided return value otherwise. */ public ensure(key: K, getDefaultValue: () => V): V { if (this.has(key)) return this.get(key)!; From 6af4e64704df355a927f46b91588150f95c9bb7d Mon Sep 17 00:00:00 2001 From: kxmndz <71072915+kxmndz@users.noreply.github.com> Date: Fri, 18 Jun 2021 00:10:52 +0800 Subject: [PATCH 13/26] docs(ensure): update docstring wording MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index d349ef2..8bb5991 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,7 +27,7 @@ export class Collection extends Map { /** * Gets an element if the key exists, otherwise sets and returns the provided default value. - * @param {*} key - Key to get from/set to the collection. + * @param {*} key - Key to ensure from the collection. * @param {function} getDefaultValue - Function that returns the default value to be set and returned if the key doesn't exist. * @returns {*} The existing value if any, the provided return value otherwise. */ From 6e671a24867d98dc96e57f494d65d34a3aadbff9 Mon Sep 17 00:00:00 2001 From: MidSpike Date: Wed, 24 Nov 2021 12:49:30 -0500 Subject: [PATCH 14/26] refactor(#40): plus more --- src/index.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8bb5991..a559bbe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,14 +26,19 @@ export class Collection extends Map { public static readonly default: typeof Collection = Collection; /** - * Gets an element if the key exists, otherwise sets and returns the provided default value. - * @param {*} key - Key to ensure from the collection. - * @param {function} getDefaultValue - Function that returns the default value to be set and returned if the key doesn't exist. - * @returns {*} The existing value if any, the provided return value otherwise. - */ - public ensure(key: K, getDefaultValue: () => V): V { - if (this.has(key)) return this.get(key)!; - const defaultValue = getDefaultValue(); + * Obtains an element if the key exists, otherwise sets and returns the value provided by the default value generator. + * + * @param {*} key - Key to ensure (if unset) or return from the collection (if set) + * @param {function} defaultValueGenerator - Function that returns a value to be set (if unset) and returned afterwards + * + * @returns The existing value if any, otherwise the value provided by the default value generator. + * + * @example + * collection.ensure(guildId, () => defaultGuildConfig); + */ + public ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V | undefined { + if (this.has(key)) return this.get(key); + const defaultValue = defaultValueGenerator(key, this); this.set(key, defaultValue); return defaultValue; } From f8eb7b4c038cb52902b93d26e612c8b133e16e52 Mon Sep 17 00:00:00 2001 From: Tyler Resch Date: Wed, 24 Nov 2021 14:14:32 -0500 Subject: [PATCH 15/26] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- __tests__/collection.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/__tests__/collection.test.ts b/__tests__/collection.test.ts index 291370c..ce2fb8f 100644 --- a/__tests__/collection.test.ts +++ b/__tests__/collection.test.ts @@ -444,13 +444,14 @@ describe('ensure() tests', () => { test('set new value if key does not exist', () => { coll.ensure('c', () => 3); expect(coll.size).toStrictEqual(3); - expect(coll.has('c')).toBeTruthy(); + expect(coll.get('c')).toStrictEqual(3); }); test('return existing value if key exists', () => { const ensureB = coll.ensure('b', () => 3); const getB = coll.get('b'); - expect(ensureB).toBe(getB); + expect(ensureB).toStrictEqual(2); + expect(getB).toStrictEqual(2); expect(coll.size).toStrictEqual(3); }); From 6a439d236e5bafc9e41da5359f89f0e202a48823 Mon Sep 17 00:00:00 2001 From: Tyler Resch Date: Wed, 24 Nov 2021 14:16:35 -0500 Subject: [PATCH 16/26] Update src/index.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index a559bbe..ed54394 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,7 @@ export class Collection extends Map { /** * Obtains an element if the key exists, otherwise sets and returns the value provided by the default value generator. * - * @param {*} key - Key to ensure (if unset) or return from the collection (if set) + * @param {*} key - The key to get from if it exists or set otherwise. * @param {function} defaultValueGenerator - Function that returns a value to be set (if unset) and returned afterwards * * @returns The existing value if any, otherwise the value provided by the default value generator. From 69cad081d50a8bc514753846eef74fa4469c7b68 Mon Sep 17 00:00:00 2001 From: MidSpike Date: Wed, 24 Nov 2021 14:23:39 -0500 Subject: [PATCH 17/26] fix(Tests): ensured tests used scoped varaiables --- __tests__/collection.test.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/__tests__/collection.test.ts b/__tests__/collection.test.ts index ce2fb8f..ba03d4f 100644 --- a/__tests__/collection.test.ts +++ b/__tests__/collection.test.ts @@ -437,27 +437,32 @@ describe('random thisArg tests', () => { }); describe('ensure() tests', () => { - const coll = new Collection(); - coll.set('a', 1); - coll.set('b', 2); - test('set new value if key does not exist', () => { + const coll = new Collection(); + coll.set('a', 1); + coll.set('b', 2); coll.ensure('c', () => 3); expect(coll.size).toStrictEqual(3); expect(coll.get('c')).toStrictEqual(3); }); test('return existing value if key exists', () => { + const coll = new Collection(); + coll.set('a', 1); + coll.set('b', 2); const ensureB = coll.ensure('b', () => 3); const getB = coll.get('b'); expect(ensureB).toStrictEqual(2); expect(getB).toStrictEqual(2); - expect(coll.size).toStrictEqual(3); + expect(coll.size).toStrictEqual(2); }); test('ensure with existing key should not change the collection', () => { + const coll = new Collection(); + coll.set('a', 1); + coll.set('b', 2); coll.ensure('a', () => 4); - expect(coll.size).toStrictEqual(3); + expect(coll.size).toStrictEqual(2); expect(coll.get('a')).toStrictEqual(1); }); }); From de2e8dba52cfd884692d844380ccbaae3e6c72ff Mon Sep 17 00:00:00 2001 From: Tyler Resch Date: Wed, 24 Nov 2021 14:41:46 -0500 Subject: [PATCH 18/26] Update src/index.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index ed54394..f3be632 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,8 +28,8 @@ export class Collection extends Map { /** * Obtains an element if the key exists, otherwise sets and returns the value provided by the default value generator. * - * @param {*} key - The key to get from if it exists or set otherwise. - * @param {function} defaultValueGenerator - Function that returns a value to be set (if unset) and returned afterwards + * @param {*} key - The key to get from if it exists or set otherwise + * @param {Function} defaultValueGenerator - A function that returns the ensured value * * @returns The existing value if any, otherwise the value provided by the default value generator. * From 65c16079b8d54a076e4de388e4886927d3490cc5 Mon Sep 17 00:00:00 2001 From: MidSpike Date: Wed, 24 Nov 2021 14:45:44 -0500 Subject: [PATCH 19/26] fix(Tests): implemented requested changes --- __tests__/collection.test.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/__tests__/collection.test.ts b/__tests__/collection.test.ts index ba03d4f..4302e5b 100644 --- a/__tests__/collection.test.ts +++ b/__tests__/collection.test.ts @@ -437,19 +437,22 @@ describe('random thisArg tests', () => { }); describe('ensure() tests', () => { + function createTestCollection() { + return new Collection([ + ['a', 1], + ['b', 2], + ]); + } + test('set new value if key does not exist', () => { - const coll = new Collection(); - coll.set('a', 1); - coll.set('b', 2); + 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 = new Collection(); - coll.set('a', 1); - coll.set('b', 2); + const coll = createTestCollection(); const ensureB = coll.ensure('b', () => 3); const getB = coll.get('b'); expect(ensureB).toStrictEqual(2); @@ -458,9 +461,7 @@ describe('ensure() tests', () => { }); test('ensure with existing key should not change the collection', () => { - const coll = new Collection(); - coll.set('a', 1); - coll.set('b', 2); + const coll = createTestCollection(); coll.ensure('a', () => 4); expect(coll.size).toStrictEqual(2); expect(coll.get('a')).toStrictEqual(1); From 9edfaaa80e6bb60b768c701d0b406bc3ac7eb97c Mon Sep 17 00:00:00 2001 From: MidSpike Date: Wed, 24 Nov 2021 17:33:58 -0500 Subject: [PATCH 20/26] fix(Docs): fixes formatting --- src/index.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index f3be632..0a2f31c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,10 +28,8 @@ export class Collection extends Map { /** * Obtains an element if the key exists, otherwise sets and returns the value provided by the default value generator. * - * @param {*} key - The key to get from if it exists or set otherwise - * @param {Function} defaultValueGenerator - A function that returns the ensured value - * - * @returns The existing value if any, otherwise the value provided by the default value generator. + * @param key The key to get from if it exists or set otherwise + * @param defaultValueGenerator A function that returns the ensured value * * @example * collection.ensure(guildId, () => defaultGuildConfig); From 58077d19ebd916e1fe587976fe58503808767de3 Mon Sep 17 00:00:00 2001 From: Tyler Resch Date: Wed, 24 Nov 2021 18:10:00 -0500 Subject: [PATCH 21/26] Update src/index.ts implemented suggested changes by kyranet Co-authored-by: muchnameless <12682826+muchnameless@users.noreply.github.com> --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 0a2f31c..8b5f412 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,7 +34,7 @@ export class Collection extends Map { * @example * collection.ensure(guildId, () => defaultGuildConfig); */ - public ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V | undefined { + public ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V { if (this.has(key)) return this.get(key); const defaultValue = defaultValueGenerator(key, this); this.set(key, defaultValue); From 3046aafed32a59184918d6470f7f3d1e458e76c6 Mon Sep 17 00:00:00 2001 From: Tyler Resch Date: Wed, 24 Nov 2021 18:14:18 -0500 Subject: [PATCH 22/26] Update src/index.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 8b5f412..1f91e1e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,7 +35,7 @@ export class Collection extends Map { * collection.ensure(guildId, () => defaultGuildConfig); */ public ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V { - if (this.has(key)) return this.get(key); + if (this.has(key)) return this.get(key)!; const defaultValue = defaultValueGenerator(key, this); this.set(key, defaultValue); return defaultValue; From 86b3983fb179156944b77aa7780973c4c68635ec Mon Sep 17 00:00:00 2001 From: Tyler Resch Date: Wed, 24 Nov 2021 19:17:14 -0500 Subject: [PATCH 23/26] Update src/index.ts Co-authored-by: Rodry <38259440+ImRodry@users.noreply.github.com> --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 1f91e1e..797be48 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,7 +26,7 @@ export class Collection extends Map { 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. + * Obtains the value of the given key if set, otherwise sets the value provided by the default value generator and returns it. * * @param key The key to get from if it exists or set otherwise * @param defaultValueGenerator A function that returns the ensured value From ace3fbdc72a701f0b52df377b7addd6555dddc5f Mon Sep 17 00:00:00 2001 From: Tyler Resch Date: Wed, 24 Nov 2021 19:18:10 -0500 Subject: [PATCH 24/26] Update src/index.ts Co-authored-by: Rodry <38259440+ImRodry@users.noreply.github.com> --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 797be48..a953705 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,8 +28,8 @@ export class Collection extends Map { /** * Obtains the value of the given key if set, otherwise sets the value provided by the default value generator and returns it. * - * @param key The key to get from if it exists or set otherwise - * @param defaultValueGenerator A function that returns the ensured value + * @param key The key to get if it exists, or set otherwise + * @param defaultValueGenerator A function that generates the value to be set if it's missing * * @example * collection.ensure(guildId, () => defaultGuildConfig); From 330ed962cf836b625c4f398a14fda492ca7a3fa6 Mon Sep 17 00:00:00 2001 From: Tyler Resch Date: Wed, 24 Nov 2021 19:20:05 -0500 Subject: [PATCH 25/26] Update __tests__/collection.test.ts Co-authored-by: Rodry <38259440+ImRodry@users.noreply.github.com> --- __tests__/collection.test.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/__tests__/collection.test.ts b/__tests__/collection.test.ts index 4302e5b..0de9655 100644 --- a/__tests__/collection.test.ts +++ b/__tests__/collection.test.ts @@ -459,11 +459,4 @@ describe('ensure() tests', () => { 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); - }); }); From 52e011c3a24506f3aa0629fe3471d6c554b44dca Mon Sep 17 00:00:00 2001 From: Tyler Resch Date: Thu, 25 Nov 2021 10:40:30 -0500 Subject: [PATCH 26/26] Update src/index.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit implemented suggested change Co-authored-by: Antonio Román --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index a953705..20d9fdf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,10 +26,10 @@ export class Collection extends Map { public static readonly default: typeof Collection = Collection; /** - * Obtains the value of the given key if set, otherwise sets the value provided by the default value generator and returns it. + * Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator. * * @param key The key to get if it exists, or set otherwise - * @param defaultValueGenerator A function that generates the value to be set if it's missing + * @param defaultValueGenerator A function that generates the default value * * @example * collection.ensure(guildId, () => defaultGuildConfig);