seyfert 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +190 -0
- package/README.md +57 -0
- package/lib/api/CDN.d.ts +220 -0
- package/lib/api/CDN.js +236 -0
- package/lib/api/REST.d.ts +127 -0
- package/lib/api/REST.js +424 -0
- package/lib/api/Router.d.ts +17 -0
- package/lib/api/Router.js +63 -0
- package/lib/api/Routes/applications.d.ts +46 -0
- package/lib/api/Routes/applications.js +2 -0
- package/lib/api/Routes/cdn.d.ts +66 -0
- package/lib/api/Routes/cdn.js +2 -0
- package/lib/api/Routes/channels.d.ts +109 -0
- package/lib/api/Routes/channels.js +2 -0
- package/lib/api/Routes/gateway.d.ts +11 -0
- package/lib/api/Routes/gateway.js +2 -0
- package/lib/api/Routes/guilds.d.ts +171 -0
- package/lib/api/Routes/guilds.js +2 -0
- package/lib/api/Routes/index.d.ts +13 -0
- package/lib/api/Routes/index.js +17 -0
- package/lib/api/Routes/interactions.d.ts +14 -0
- package/lib/api/Routes/interactions.js +2 -0
- package/lib/api/Routes/invites.d.ts +9 -0
- package/lib/api/Routes/invites.js +2 -0
- package/lib/api/Routes/stage-instances.d.ts +13 -0
- package/lib/api/Routes/stage-instances.js +2 -0
- package/lib/api/Routes/stickers.d.ts +11 -0
- package/lib/api/Routes/stickers.js +2 -0
- package/lib/api/Routes/users.d.ts +35 -0
- package/lib/api/Routes/users.js +2 -0
- package/lib/api/Routes/voice.d.ts +10 -0
- package/lib/api/Routes/voice.js +2 -0
- package/lib/api/Routes/webhooks.d.ts +29 -0
- package/lib/api/Routes/webhooks.js +2 -0
- package/lib/api/errors/DiscordAPIError.d.ts +51 -0
- package/lib/api/errors/DiscordAPIError.js +81 -0
- package/lib/api/errors/HTTPError.d.ts +20 -0
- package/lib/api/errors/HTTPError.js +28 -0
- package/lib/api/errors/RateLimitError.d.ts +19 -0
- package/lib/api/errors/RateLimitError.js +37 -0
- package/lib/api/handlers/BurstHandler.d.ts +51 -0
- package/lib/api/handlers/BurstHandler.js +124 -0
- package/lib/api/handlers/SequentialHandler.d.ts +81 -0
- package/lib/api/handlers/SequentialHandler.js +365 -0
- package/lib/api/handlers/Shared.d.ts +14 -0
- package/lib/api/handlers/Shared.js +125 -0
- package/lib/api/index.d.ts +5 -0
- package/lib/api/index.js +21 -0
- package/lib/api/interfaces/Handler.d.ts +21 -0
- package/lib/api/interfaces/Handler.js +2 -0
- package/lib/api/shared.d.ts +7 -0
- package/lib/api/shared.js +27 -0
- package/lib/api/utils/constants.d.ts +39 -0
- package/lib/api/utils/constants.js +50 -0
- package/lib/api/utils/types.d.ts +335 -0
- package/lib/api/utils/types.js +14 -0
- package/lib/api/utils/utils.d.ts +52 -0
- package/lib/api/utils/utils.js +159 -0
- package/lib/builders/ActionRow.d.ts +40 -0
- package/lib/builders/ActionRow.js +58 -0
- package/lib/builders/Attachment.d.ts +106 -0
- package/lib/builders/Attachment.js +196 -0
- package/lib/builders/Base.d.ts +10 -0
- package/lib/builders/Base.js +13 -0
- package/lib/builders/Button.d.ts +59 -0
- package/lib/builders/Button.js +94 -0
- package/lib/builders/MessageEmbed.d.ts +116 -0
- package/lib/builders/MessageEmbed.js +159 -0
- package/lib/builders/Modal.d.ts +111 -0
- package/lib/builders/Modal.js +162 -0
- package/lib/builders/SelectMenu.d.ts +201 -0
- package/lib/builders/SelectMenu.js +293 -0
- package/lib/builders/index.d.ts +12 -0
- package/lib/builders/index.js +54 -0
- package/lib/builders/types.d.ts +21 -0
- package/lib/builders/types.js +2 -0
- package/lib/cache/adapters/default.d.ts +24 -0
- package/lib/cache/adapters/default.js +118 -0
- package/lib/cache/adapters/index.d.ts +4 -0
- package/lib/cache/adapters/index.js +20 -0
- package/lib/cache/adapters/redis.d.ts +35 -0
- package/lib/cache/adapters/redis.js +180 -0
- package/lib/cache/adapters/types.d.ts +25 -0
- package/lib/cache/adapters/types.js +2 -0
- package/lib/cache/adapters/workeradapter.d.ts +24 -0
- package/lib/cache/adapters/workeradapter.js +76 -0
- package/lib/cache/index.d.ts +97 -0
- package/lib/cache/index.js +394 -0
- package/lib/cache/resources/channels.d.ts +7 -0
- package/lib/cache/resources/channels.js +20 -0
- package/lib/cache/resources/default/base.d.ts +25 -0
- package/lib/cache/resources/default/base.js +75 -0
- package/lib/cache/resources/default/guild-based.d.ts +33 -0
- package/lib/cache/resources/default/guild-based.js +91 -0
- package/lib/cache/resources/default/guild-related.d.ts +32 -0
- package/lib/cache/resources/default/guild-related.js +102 -0
- package/lib/cache/resources/emojis.d.ts +7 -0
- package/lib/cache/resources/emojis.js +17 -0
- package/lib/cache/resources/guilds.d.ts +10 -0
- package/lib/cache/resources/guilds.js +117 -0
- package/lib/cache/resources/members.d.ts +10 -0
- package/lib/cache/resources/members.js +41 -0
- package/lib/cache/resources/presence.d.ts +9 -0
- package/lib/cache/resources/presence.js +14 -0
- package/lib/cache/resources/roles.d.ts +7 -0
- package/lib/cache/resources/roles.js +17 -0
- package/lib/cache/resources/stage-instances.d.ts +5 -0
- package/lib/cache/resources/stage-instances.js +8 -0
- package/lib/cache/resources/stickers.d.ts +7 -0
- package/lib/cache/resources/stickers.js +17 -0
- package/lib/cache/resources/threads.d.ts +7 -0
- package/lib/cache/resources/threads.js +17 -0
- package/lib/cache/resources/users.d.ts +7 -0
- package/lib/cache/resources/users.js +17 -0
- package/lib/cache/resources/voice-states.d.ts +9 -0
- package/lib/cache/resources/voice-states.js +13 -0
- package/lib/client/base.d.ts +244 -0
- package/lib/client/base.js +178 -0
- package/lib/client/client.d.ts +32 -0
- package/lib/client/client.js +156 -0
- package/lib/client/httpclient.d.ts +21 -0
- package/lib/client/httpclient.js +143 -0
- package/lib/client/index.d.ts +4 -0
- package/lib/client/index.js +19 -0
- package/lib/client/oninteraction.d.ts +4 -0
- package/lib/client/oninteraction.js +180 -0
- package/lib/client/workerclient.d.ts +28 -0
- package/lib/client/workerclient.js +213 -0
- package/lib/collection.d.ts +233 -0
- package/lib/collection.js +371 -0
- package/lib/commands/applications/chat.d.ts +139 -0
- package/lib/commands/applications/chat.js +173 -0
- package/lib/commands/applications/chatcontext.d.ts +28 -0
- package/lib/commands/applications/chatcontext.js +53 -0
- package/lib/commands/applications/menu.d.ts +39 -0
- package/lib/commands/applications/menu.js +87 -0
- package/lib/commands/applications/menucontext.d.ts +25 -0
- package/lib/commands/applications/menucontext.js +64 -0
- package/lib/commands/applications/options.d.ts +67 -0
- package/lib/commands/applications/options.js +44 -0
- package/lib/commands/applications/shared.d.ts +35 -0
- package/lib/commands/applications/shared.js +2 -0
- package/lib/commands/decorators.d.ts +107 -0
- package/lib/commands/decorators.js +85 -0
- package/lib/commands/handler.d.ts +15 -0
- package/lib/commands/handler.js +140 -0
- package/lib/commands/index.d.ts +8 -0
- package/lib/commands/index.js +24 -0
- package/lib/commands/optionresolver.d.ts +49 -0
- package/lib/commands/optionresolver.js +146 -0
- package/lib/common/bot/watcher.d.ts +53 -0
- package/lib/common/bot/watcher.js +104 -0
- package/lib/common/index.d.ts +13 -0
- package/lib/common/index.js +30 -0
- package/lib/common/it/colors.d.ts +274 -0
- package/lib/common/it/colors.js +453 -0
- package/lib/common/it/constants.d.ts +36 -0
- package/lib/common/it/constants.js +40 -0
- package/lib/common/it/logger.d.ts +114 -0
- package/lib/common/it/logger.js +189 -0
- package/lib/common/it/utils.d.ts +107 -0
- package/lib/common/it/utils.js +275 -0
- package/lib/common/shorters/base.d.ts +5 -0
- package/lib/common/shorters/base.js +10 -0
- package/lib/common/shorters/channels.d.ts +90 -0
- package/lib/common/shorters/channels.js +95 -0
- package/lib/common/shorters/guilds.d.ts +389 -0
- package/lib/common/shorters/guilds.js +350 -0
- package/lib/common/shorters/members.d.ts +115 -0
- package/lib/common/shorters/members.js +179 -0
- package/lib/common/shorters/messages.d.ts +27 -0
- package/lib/common/shorters/messages.js +117 -0
- package/lib/common/shorters/roles.d.ts +12 -0
- package/lib/common/shorters/roles.js +51 -0
- package/lib/common/shorters/templates.d.ts +12 -0
- package/lib/common/shorters/templates.js +29 -0
- package/lib/common/shorters/users.d.ts +11 -0
- package/lib/common/shorters/users.js +42 -0
- package/lib/common/shorters/webhook.d.ts +33 -0
- package/lib/common/shorters/webhook.js +69 -0
- package/lib/common/types/options.d.ts +10 -0
- package/lib/common/types/options.js +2 -0
- package/lib/common/types/resolvables.d.ts +10 -0
- package/lib/common/types/resolvables.js +2 -0
- package/lib/common/types/util.d.ts +90 -0
- package/lib/common/types/util.js +2 -0
- package/lib/common/types/write.d.ts +21 -0
- package/lib/common/types/write.js +2 -0
- package/lib/components/ActionRow.d.ts +10 -0
- package/lib/components/ActionRow.js +13 -0
- package/lib/components/ButtonComponent.d.ts +19 -0
- package/lib/components/ButtonComponent.js +34 -0
- package/lib/components/ChannelSelectMenuComponent.d.ts +6 -0
- package/lib/components/ChannelSelectMenuComponent.js +12 -0
- package/lib/components/MentionableSelectMenuComponent.d.ts +4 -0
- package/lib/components/MentionableSelectMenuComponent.js +7 -0
- package/lib/components/RoleSelectMenuComponent.d.ts +4 -0
- package/lib/components/RoleSelectMenuComponent.js +7 -0
- package/lib/components/StringSelectMenuComponent.d.ts +6 -0
- package/lib/components/StringSelectMenuComponent.js +12 -0
- package/lib/components/TextInputComponent.d.ts +7 -0
- package/lib/components/TextInputComponent.js +14 -0
- package/lib/components/UserSelectMenuComponent.d.ts +4 -0
- package/lib/components/UserSelectMenuComponent.js +7 -0
- package/lib/components/command.d.ts +23 -0
- package/lib/components/command.js +15 -0
- package/lib/components/handler.d.ts +48 -0
- package/lib/components/handler.js +231 -0
- package/lib/components/index.d.ts +20 -0
- package/lib/components/index.js +55 -0
- package/lib/components/listener.d.ts +11 -0
- package/lib/components/listener.js +18 -0
- package/lib/events/event.d.ts +23 -0
- package/lib/events/event.js +2 -0
- package/lib/events/handler.d.ts +19 -0
- package/lib/events/handler.js +112 -0
- package/lib/events/hooks/application_command.d.ts +12 -0
- package/lib/events/hooks/application_command.js +8 -0
- package/lib/events/hooks/auto_moderation.d.ts +22 -0
- package/lib/events/hooks/auto_moderation.js +21 -0
- package/lib/events/hooks/channel.d.ts +11 -0
- package/lib/events/hooks/channel.js +24 -0
- package/lib/events/hooks/custom.d.ts +4 -0
- package/lib/events/hooks/custom.js +11 -0
- package/lib/events/hooks/dispatch.d.ts +5 -0
- package/lib/events/hooks/dispatch.js +12 -0
- package/lib/events/hooks/entitlement.d.ts +35 -0
- package/lib/events/hooks/entitlement.js +16 -0
- package/lib/events/hooks/guild.d.ts +228 -0
- package/lib/events/hooks/guild.js +101 -0
- package/lib/events/hooks/index.d.ts +23 -0
- package/lib/events/hooks/index.js +34 -0
- package/lib/events/hooks/integration.d.ts +94 -0
- package/lib/events/hooks/integration.js +27 -0
- package/lib/events/hooks/interactions.d.ts +4 -0
- package/lib/events/hooks/interactions.js +8 -0
- package/lib/events/hooks/invite.d.ts +21 -0
- package/lib/events/hooks/invite.js +12 -0
- package/lib/events/hooks/message.d.ts +69 -0
- package/lib/events/hooks/message.js +37 -0
- package/lib/events/hooks/presence.d.ts +27 -0
- package/lib/events/hooks/presence.js +8 -0
- package/lib/events/hooks/stage.d.ts +29 -0
- package/lib/events/hooks/stage.js +16 -0
- package/lib/events/hooks/thread.d.ts +198 -0
- package/lib/events/hooks/thread.js +29 -0
- package/lib/events/hooks/typing.d.ts +16 -0
- package/lib/events/hooks/typing.js +14 -0
- package/lib/events/hooks/user.d.ts +4 -0
- package/lib/events/hooks/user.js +8 -0
- package/lib/events/hooks/voice.d.ts +37 -0
- package/lib/events/hooks/voice.js +18 -0
- package/lib/events/hooks/webhook.d.ts +6 -0
- package/lib/events/hooks/webhook.js +8 -0
- package/lib/events/index.d.ts +2 -0
- package/lib/events/index.js +18 -0
- package/lib/index.d.ts +79 -0
- package/lib/index.js +104 -0
- package/lib/langs/handler.d.ts +11 -0
- package/lib/langs/handler.js +34 -0
- package/lib/langs/index.d.ts +2 -0
- package/lib/langs/index.js +18 -0
- package/lib/langs/router.d.ts +18 -0
- package/lib/langs/router.js +42 -0
- package/lib/structures/AnonymousGuild.d.ts +7 -0
- package/lib/structures/AnonymousGuild.js +11 -0
- package/lib/structures/AutoModerationRule.d.ts +22 -0
- package/lib/structures/AutoModerationRule.js +34 -0
- package/lib/structures/ClientUser.d.ts +25 -0
- package/lib/structures/ClientUser.js +52 -0
- package/lib/structures/Guild.d.ts +86 -0
- package/lib/structures/Guild.js +66 -0
- package/lib/structures/GuildEmoji.d.ts +29 -0
- package/lib/structures/GuildEmoji.js +47 -0
- package/lib/structures/GuildMember.d.ts +92 -0
- package/lib/structures/GuildMember.js +137 -0
- package/lib/structures/GuildPreview.d.ts +10 -0
- package/lib/structures/GuildPreview.js +14 -0
- package/lib/structures/GuildRole.d.ts +21 -0
- package/lib/structures/GuildRole.js +32 -0
- package/lib/structures/GuildTemplate.d.ts +23 -0
- package/lib/structures/GuildTemplate.js +36 -0
- package/lib/structures/Interaction.d.ts +152 -0
- package/lib/structures/Interaction.js +409 -0
- package/lib/structures/Message.d.ts +53 -0
- package/lib/structures/Message.js +123 -0
- package/lib/structures/Sticker.d.ts +27 -0
- package/lib/structures/Sticker.js +44 -0
- package/lib/structures/User.d.ts +20 -0
- package/lib/structures/User.js +37 -0
- package/lib/structures/Webhook.d.ts +42 -0
- package/lib/structures/Webhook.js +63 -0
- package/lib/structures/channels.d.ts +232 -0
- package/lib/structures/channels.js +366 -0
- package/lib/structures/extra/Base.d.ts +7 -0
- package/lib/structures/extra/Base.js +39 -0
- package/lib/structures/extra/BaseComponent.d.ts +9 -0
- package/lib/structures/extra/BaseComponent.js +12 -0
- package/lib/structures/extra/BaseGuild.d.ts +40 -0
- package/lib/structures/extra/BaseGuild.js +71 -0
- package/lib/structures/extra/BaseSelectMenuComponent.d.ts +9 -0
- package/lib/structures/extra/BaseSelectMenuComponent.js +13 -0
- package/lib/structures/extra/BitField.d.ts +14 -0
- package/lib/structures/extra/BitField.js +56 -0
- package/lib/structures/extra/DiscordBase.d.ts +18 -0
- package/lib/structures/extra/DiscordBase.js +28 -0
- package/lib/structures/extra/Permissions.d.ts +54 -0
- package/lib/structures/extra/Permissions.js +9 -0
- package/lib/structures/extra/functions.d.ts +11 -0
- package/lib/structures/extra/functions.js +69 -0
- package/lib/structures/index.d.ts +15 -0
- package/lib/structures/index.js +31 -0
- package/lib/websocket/SharedTypes.d.ts +128 -0
- package/lib/websocket/SharedTypes.js +2 -0
- package/lib/websocket/constants/index.d.ts +26 -0
- package/lib/websocket/constants/index.js +31 -0
- package/lib/websocket/discord/basesocket.d.ts +12 -0
- package/lib/websocket/discord/basesocket.js +37 -0
- package/lib/websocket/discord/events/memberUpdate.d.ts +16 -0
- package/lib/websocket/discord/events/memberUpdate.js +47 -0
- package/lib/websocket/discord/events/presenceUpdate.d.ts +24 -0
- package/lib/websocket/discord/events/presenceUpdate.js +46 -0
- package/lib/websocket/discord/handlemessage.d.ts +0 -0
- package/lib/websocket/discord/handlemessage.js +1 -0
- package/lib/websocket/discord/index.d.ts +4 -0
- package/lib/websocket/discord/index.js +20 -0
- package/lib/websocket/discord/memberUpdate.d.ts +16 -0
- package/lib/websocket/discord/memberUpdate.js +47 -0
- package/lib/websocket/discord/shard.d.ts +46 -0
- package/lib/websocket/discord/shard.js +278 -0
- package/lib/websocket/discord/sharder.d.ts +25 -0
- package/lib/websocket/discord/sharder.js +153 -0
- package/lib/websocket/discord/shared.d.ts +99 -0
- package/lib/websocket/discord/shared.js +8 -0
- package/lib/websocket/discord/worker.d.ts +43 -0
- package/lib/websocket/discord/worker.js +2 -0
- package/lib/websocket/discord/workermanager.d.ts +69 -0
- package/lib/websocket/discord/workermanager.js +273 -0
- package/lib/websocket/index.d.ts +3 -0
- package/lib/websocket/index.js +19 -0
- package/lib/websocket/structures/index.d.ts +117 -0
- package/lib/websocket/structures/index.js +261 -0
- package/lib/websocket/structures/timeout.d.ts +20 -0
- package/lib/websocket/structures/timeout.js +75 -0
- package/package.json +70 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a collection that extends the built-in Map class.
|
|
3
|
+
* @template K The type of the keys in the collection.
|
|
4
|
+
* @template V The type of the values in the collection.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Collection<K, V> extends Map<K, V> {
|
|
7
|
+
/**
|
|
8
|
+
* Removes elements from the collection based on a filter function.
|
|
9
|
+
* @param fn The filter function that determines which elements to remove.
|
|
10
|
+
* @param thisArg The value to use as `this` when executing the filter function.
|
|
11
|
+
* @returns The number of elements removed from the collection.
|
|
12
|
+
* @example
|
|
13
|
+
* const collection = new Collection<number, string>();
|
|
14
|
+
* collection.set(1, 'one');
|
|
15
|
+
* collection.set(2, 'two');
|
|
16
|
+
* collection.set(3, 'three');
|
|
17
|
+
* const removedCount = collection.sweep((value, key) => key % 2 === 0);
|
|
18
|
+
* console.log(removedCount); // Output: 1
|
|
19
|
+
* console.log(collection.size); // Output: 2
|
|
20
|
+
*/
|
|
21
|
+
sweep(fn: (value: V, key: K, collection: this) => unknown): number;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new array with the results of calling a provided function on every element in the collection.
|
|
24
|
+
* @param fn The function that produces an element of the new array.
|
|
25
|
+
* @param thisArg The value to use as `this` when executing the map function.
|
|
26
|
+
* @returns A new array with the results of calling the provided function on every element in the collection.
|
|
27
|
+
* @example
|
|
28
|
+
* const collection = new Collection<number, string>();
|
|
29
|
+
* collection.set(1, 'one');
|
|
30
|
+
* collection.set(2, 'two');
|
|
31
|
+
* collection.set(3, 'three');
|
|
32
|
+
* const mappedArray = collection.map((value, key) => `${key}: ${value}`);
|
|
33
|
+
* console.log(mappedArray); // Output: ['1: one', '2: two', '3: three']
|
|
34
|
+
*/
|
|
35
|
+
map<T = any>(fn: (value: V, key: K, collection: this) => T): T[];
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new array with all elements that pass the test implemented by the provided function.
|
|
38
|
+
* @param fn The function to test each element of the collection.
|
|
39
|
+
* @param thisArg The value to use as `this` when executing the filter function.
|
|
40
|
+
* @returns A new array with the elements that pass the test.
|
|
41
|
+
* @example
|
|
42
|
+
* const collection = new Collection<number, string>();
|
|
43
|
+
* collection.set(1, 'one');
|
|
44
|
+
* collection.set(2, 'two');
|
|
45
|
+
* collection.set(3, 'three');
|
|
46
|
+
* const filteredArray = collection.filter((value, key) => key % 2 === 0);
|
|
47
|
+
* console.log(filteredArray); // Output: ['two']
|
|
48
|
+
*/
|
|
49
|
+
filter(fn: (value: V, key: K, collection: this) => boolean): V[];
|
|
50
|
+
/**
|
|
51
|
+
* Apply a function against an accumulator and each element in the collection (from left to right) to reduce it to a single value.
|
|
52
|
+
* @param fn The function to execute on each element in the collection.
|
|
53
|
+
* @param initialValue The initial value of the accumulator.
|
|
54
|
+
* @returns The value that results from the reduction.
|
|
55
|
+
* @example
|
|
56
|
+
* const collection = new Collection<number, number>();
|
|
57
|
+
* collection.set(1, 1);
|
|
58
|
+
* collection.set(2, 2);
|
|
59
|
+
* collection.set(3, 3);
|
|
60
|
+
* const sum = collection.reduce((acc, value) => acc + value, 0);
|
|
61
|
+
* console.log(sum); // Output: 6
|
|
62
|
+
*/
|
|
63
|
+
reduce<T = any>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T;
|
|
64
|
+
/**
|
|
65
|
+
* Checks if all elements in the collection pass a test implemented by the provided function.
|
|
66
|
+
* @param fn The function to test each element of the collection.
|
|
67
|
+
* @returns `true` if all elements pass the test, otherwise `false`.
|
|
68
|
+
* @example
|
|
69
|
+
* const collection = new Collection<number, number>();
|
|
70
|
+
* collection.set(1, 1);
|
|
71
|
+
* collection.set(2, 2);
|
|
72
|
+
* collection.set(3, 3);
|
|
73
|
+
* const allGreaterThanZero = collection.every(value => value > 0);
|
|
74
|
+
* console.log(allGreaterThanZero); // Output: true
|
|
75
|
+
*/
|
|
76
|
+
every(fn: (value: V, key: K, collection: this) => boolean): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Checks if at least one element in the collection passes a test implemented by the provided function.
|
|
79
|
+
* @param fn The function to test each element of the collection.
|
|
80
|
+
* @returns `true` if at least one element passes the test, otherwise `false`.
|
|
81
|
+
* @example
|
|
82
|
+
* const collection = new Collection<number, number>();
|
|
83
|
+
* collection.set(1, 1);
|
|
84
|
+
* collection.set(2, 2);
|
|
85
|
+
* collection.set(3, 3);
|
|
86
|
+
* const hasEvenValue = collection.some(value => value % 2 === 0);
|
|
87
|
+
* console.log(hasEvenValue); // Output: true
|
|
88
|
+
*/
|
|
89
|
+
some(fn: (value: V, key: K, collection: this) => boolean): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Returns the value of the first element in the collection that satisfies the provided testing function.
|
|
92
|
+
* @param fn The function to test each element of the collection.
|
|
93
|
+
* @returns The value of the first element that passes the test. `undefined` if no element passes the test.
|
|
94
|
+
* @example
|
|
95
|
+
* const collection = new Collection<number, number>();
|
|
96
|
+
* collection.set(1, 1);
|
|
97
|
+
* collection.set(2, 2);
|
|
98
|
+
* collection.set(3, 3);
|
|
99
|
+
* const firstEvenValue = collection.find(value => value % 2 === 0);
|
|
100
|
+
* console.log(firstEvenValue); // Output: 2
|
|
101
|
+
*/
|
|
102
|
+
find(fn: (value: V, key: K, collection: this) => boolean): V | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* Returns the first key in the collection that satisfies the provided testing function.
|
|
105
|
+
* @param fn The function to test each element of the collection.
|
|
106
|
+
* @returns The first key that passes the test. `undefined` if no element passes the test.
|
|
107
|
+
* @example
|
|
108
|
+
* const collection = new Collection<number, number>();
|
|
109
|
+
* collection.set(1, 1);
|
|
110
|
+
* collection.set(2, 2);
|
|
111
|
+
* collection.set(3, 3);
|
|
112
|
+
* const firstEvenKey = collection.findKey(value => value % 2 === 0);
|
|
113
|
+
* console.log(firstEvenKey); // Output: 2
|
|
114
|
+
*/
|
|
115
|
+
findKey(fn: (value: V, key: K, collection: this) => boolean): K | undefined;
|
|
116
|
+
}
|
|
117
|
+
type LimitedCollectionData<V> = {
|
|
118
|
+
expire: number;
|
|
119
|
+
expireOn: number;
|
|
120
|
+
value: V;
|
|
121
|
+
};
|
|
122
|
+
export interface LimitedCollectionOptions {
|
|
123
|
+
limit: number;
|
|
124
|
+
expire: number;
|
|
125
|
+
resetOnDemand: boolean;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Creates a new array with the results of calling a provided function on every element in the collection.
|
|
129
|
+
* @param fn The function that produces an element of the new array.
|
|
130
|
+
* @param thisArg The value to use as `this` when executing the map function.
|
|
131
|
+
* @returns A new array with the results of calling the provided function on every element in the collection.
|
|
132
|
+
* @example
|
|
133
|
+
* const collection = new Collection<number, string>();
|
|
134
|
+
* collection.set(1, 'one');
|
|
135
|
+
* collection.set(2, 'two');
|
|
136
|
+
* collection.set(3, 'three');
|
|
137
|
+
* const mappedArray = collection.map((value, key) => `${key}: ${value}`);
|
|
138
|
+
* console.log(mappedArray); // Output: ['1: one', '2: two', '3: three']
|
|
139
|
+
*/
|
|
140
|
+
export declare class LimitedCollection<K, V> {
|
|
141
|
+
static readonly default: LimitedCollectionOptions;
|
|
142
|
+
private readonly data;
|
|
143
|
+
private readonly options;
|
|
144
|
+
private timeout;
|
|
145
|
+
constructor(options: Partial<LimitedCollectionOptions>);
|
|
146
|
+
/**
|
|
147
|
+
* Adds an element to the limited collection.
|
|
148
|
+
* @param key The key of the element.
|
|
149
|
+
* @param value The value of the element.
|
|
150
|
+
* @param customExpire The custom expiration time for the element.
|
|
151
|
+
* @example
|
|
152
|
+
* const collection = new LimitedCollection<number, string>({ limit: 3 });
|
|
153
|
+
* collection.set(1, 'one');
|
|
154
|
+
* collection.set(2, 'two');
|
|
155
|
+
* collection.set(3, 'three');
|
|
156
|
+
* console.log(collection.size); // Output: 3
|
|
157
|
+
* collection.set(4, 'four');
|
|
158
|
+
* console.log(collection.size); // Output: 3
|
|
159
|
+
* console.log(collection.get(1)); // Output: undefined
|
|
160
|
+
*/
|
|
161
|
+
set(key: K, value: V, customExpire?: number): void;
|
|
162
|
+
/**
|
|
163
|
+
* Returns the raw data of an element in the limited collection.
|
|
164
|
+
* @param key The key of the element.
|
|
165
|
+
* @returns The raw data of the element, or `undefined` if the element does not exist.
|
|
166
|
+
* @example
|
|
167
|
+
* const collection = new LimitedCollection<number, string>();
|
|
168
|
+
* collection.set(1, 'one');
|
|
169
|
+
* const rawData = collection.raw(1);
|
|
170
|
+
* console.log(rawData); // Output: { value: 'one', expire: -1, expireOn: -1 }
|
|
171
|
+
*/
|
|
172
|
+
raw(key: K): LimitedCollectionData<V> | undefined;
|
|
173
|
+
/**
|
|
174
|
+
* Returns the value of an element in the limited collection.
|
|
175
|
+
* @param key The key of the element.
|
|
176
|
+
* @returns The value of the element, or `undefined` if the element does not exist.
|
|
177
|
+
* @example
|
|
178
|
+
* const collection = new LimitedCollection<number, string>();
|
|
179
|
+
* collection.set(1, 'one');
|
|
180
|
+
* const value = collection.get(1);
|
|
181
|
+
* console.log(value); // Output: 'one'
|
|
182
|
+
*/
|
|
183
|
+
get(key: K): V | undefined;
|
|
184
|
+
/**
|
|
185
|
+
* Checks if an element exists in the limited collection.
|
|
186
|
+
* @param key The key of the element.
|
|
187
|
+
* @returns `true` if the element exists, `false` otherwise.
|
|
188
|
+
* @example
|
|
189
|
+
* const collection = new LimitedCollection<number, string>();
|
|
190
|
+
* collection.set(1, 'one');
|
|
191
|
+
* console.log(collection.has(1)); // Output: true
|
|
192
|
+
* console.log(collection.has(2)); // Output: false
|
|
193
|
+
*/
|
|
194
|
+
has(key: K): boolean;
|
|
195
|
+
/**
|
|
196
|
+
* Removes an element from the limited collection.
|
|
197
|
+
* @param key The key of the element to remove.
|
|
198
|
+
* @returns `true` if the element was removed, `false` otherwise.
|
|
199
|
+
* @example
|
|
200
|
+
* const collection = new LimitedCollection<number, string>();
|
|
201
|
+
* collection.set(1, 'one');
|
|
202
|
+
* console.log(collection.delete(1)); // Output: true
|
|
203
|
+
* console.log(collection.delete(2)); // Output: false
|
|
204
|
+
*/
|
|
205
|
+
delete(key: K): boolean;
|
|
206
|
+
/**
|
|
207
|
+
* Returns the element in the limited collection that is closest to expiration.
|
|
208
|
+
* @returns The element that is closest to expiration, or `undefined` if the collection is empty.
|
|
209
|
+
* @example
|
|
210
|
+
* const collection = new LimitedCollection<number, string>();
|
|
211
|
+
* collection.set(1, 'one', 1000);
|
|
212
|
+
* collection.set(2, 'two', 2000);
|
|
213
|
+
* collection.set(3, 'three', 500);
|
|
214
|
+
* const closestElement = collection.closer;
|
|
215
|
+
* console.log(closestElement); // Output: { value: 'three', expire: 500, expireOn: [current timestamp + 500] }
|
|
216
|
+
*/
|
|
217
|
+
get closer(): LimitedCollectionData<V> | undefined;
|
|
218
|
+
/**
|
|
219
|
+
* Returns the number of elements in the limited collection.
|
|
220
|
+
* @returns The number of elements in the collection.
|
|
221
|
+
* @example
|
|
222
|
+
* const collection = new LimitedCollection<number, string>();
|
|
223
|
+
* collection.set(1, 'one');
|
|
224
|
+
* collection.set(2, 'two');
|
|
225
|
+
* console.log(collection.size); // Output: 2
|
|
226
|
+
*/
|
|
227
|
+
get size(): number;
|
|
228
|
+
private resetTimeout;
|
|
229
|
+
private stopTimeout;
|
|
230
|
+
private startTimeout;
|
|
231
|
+
private clearExpired;
|
|
232
|
+
}
|
|
233
|
+
export {};
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LimitedCollection = exports.Collection = void 0;
|
|
4
|
+
const common_1 = require("./common");
|
|
5
|
+
/**
|
|
6
|
+
* Represents a collection that extends the built-in Map class.
|
|
7
|
+
* @template K The type of the keys in the collection.
|
|
8
|
+
* @template V The type of the values in the collection.
|
|
9
|
+
*/
|
|
10
|
+
class Collection extends Map {
|
|
11
|
+
/**
|
|
12
|
+
* Removes elements from the collection based on a filter function.
|
|
13
|
+
* @param fn The filter function that determines which elements to remove.
|
|
14
|
+
* @param thisArg The value to use as `this` when executing the filter function.
|
|
15
|
+
* @returns The number of elements removed from the collection.
|
|
16
|
+
* @example
|
|
17
|
+
* const collection = new Collection<number, string>();
|
|
18
|
+
* collection.set(1, 'one');
|
|
19
|
+
* collection.set(2, 'two');
|
|
20
|
+
* collection.set(3, 'three');
|
|
21
|
+
* const removedCount = collection.sweep((value, key) => key % 2 === 0);
|
|
22
|
+
* console.log(removedCount); // Output: 1
|
|
23
|
+
* console.log(collection.size); // Output: 2
|
|
24
|
+
*/
|
|
25
|
+
sweep(fn) {
|
|
26
|
+
const previous = this.size;
|
|
27
|
+
for (const [key, val] of this) {
|
|
28
|
+
if (fn(val, key, this))
|
|
29
|
+
this.delete(key);
|
|
30
|
+
}
|
|
31
|
+
return previous - this.size;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Creates a new array with the results of calling a provided function on every element in the collection.
|
|
35
|
+
* @param fn The function that produces an element of the new array.
|
|
36
|
+
* @param thisArg The value to use as `this` when executing the map function.
|
|
37
|
+
* @returns A new array with the results of calling the provided function on every element in the collection.
|
|
38
|
+
* @example
|
|
39
|
+
* const collection = new Collection<number, string>();
|
|
40
|
+
* collection.set(1, 'one');
|
|
41
|
+
* collection.set(2, 'two');
|
|
42
|
+
* collection.set(3, 'three');
|
|
43
|
+
* const mappedArray = collection.map((value, key) => `${key}: ${value}`);
|
|
44
|
+
* console.log(mappedArray); // Output: ['1: one', '2: two', '3: three']
|
|
45
|
+
*/
|
|
46
|
+
map(fn) {
|
|
47
|
+
const result = [];
|
|
48
|
+
for (const [key, value] of this.entries()) {
|
|
49
|
+
result.push(fn(value, key, this));
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates a new array with all elements that pass the test implemented by the provided function.
|
|
55
|
+
* @param fn The function to test each element of the collection.
|
|
56
|
+
* @param thisArg The value to use as `this` when executing the filter function.
|
|
57
|
+
* @returns A new array with the elements that pass the test.
|
|
58
|
+
* @example
|
|
59
|
+
* const collection = new Collection<number, string>();
|
|
60
|
+
* collection.set(1, 'one');
|
|
61
|
+
* collection.set(2, 'two');
|
|
62
|
+
* collection.set(3, 'three');
|
|
63
|
+
* const filteredArray = collection.filter((value, key) => key % 2 === 0);
|
|
64
|
+
* console.log(filteredArray); // Output: ['two']
|
|
65
|
+
*/
|
|
66
|
+
filter(fn) {
|
|
67
|
+
const result = [];
|
|
68
|
+
for (const [key, value] of this.entries()) {
|
|
69
|
+
if (fn(value, key, this))
|
|
70
|
+
result.push(value);
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Apply a function against an accumulator and each element in the collection (from left to right) to reduce it to a single value.
|
|
76
|
+
* @param fn The function to execute on each element in the collection.
|
|
77
|
+
* @param initialValue The initial value of the accumulator.
|
|
78
|
+
* @returns The value that results from the reduction.
|
|
79
|
+
* @example
|
|
80
|
+
* const collection = new Collection<number, number>();
|
|
81
|
+
* collection.set(1, 1);
|
|
82
|
+
* collection.set(2, 2);
|
|
83
|
+
* collection.set(3, 3);
|
|
84
|
+
* const sum = collection.reduce((acc, value) => acc + value, 0);
|
|
85
|
+
* console.log(sum); // Output: 6
|
|
86
|
+
*/
|
|
87
|
+
reduce(fn, initialValue) {
|
|
88
|
+
const entries = this.entries();
|
|
89
|
+
const first = entries.next().value;
|
|
90
|
+
let result = initialValue;
|
|
91
|
+
if (result !== undefined) {
|
|
92
|
+
result = fn(result, first[1], first[0], this);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
result = first[1];
|
|
96
|
+
}
|
|
97
|
+
for (const [key, value] of entries) {
|
|
98
|
+
result = fn(result, value, key, this);
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Checks if all elements in the collection pass a test implemented by the provided function.
|
|
104
|
+
* @param fn The function to test each element of the collection.
|
|
105
|
+
* @returns `true` if all elements pass the test, otherwise `false`.
|
|
106
|
+
* @example
|
|
107
|
+
* const collection = new Collection<number, number>();
|
|
108
|
+
* collection.set(1, 1);
|
|
109
|
+
* collection.set(2, 2);
|
|
110
|
+
* collection.set(3, 3);
|
|
111
|
+
* const allGreaterThanZero = collection.every(value => value > 0);
|
|
112
|
+
* console.log(allGreaterThanZero); // Output: true
|
|
113
|
+
*/
|
|
114
|
+
every(fn) {
|
|
115
|
+
for (const [key, value] of this.entries()) {
|
|
116
|
+
if (!fn(value, key, this)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Checks if at least one element in the collection passes a test implemented by the provided function.
|
|
124
|
+
* @param fn The function to test each element of the collection.
|
|
125
|
+
* @returns `true` if at least one element passes the test, otherwise `false`.
|
|
126
|
+
* @example
|
|
127
|
+
* const collection = new Collection<number, number>();
|
|
128
|
+
* collection.set(1, 1);
|
|
129
|
+
* collection.set(2, 2);
|
|
130
|
+
* collection.set(3, 3);
|
|
131
|
+
* const hasEvenValue = collection.some(value => value % 2 === 0);
|
|
132
|
+
* console.log(hasEvenValue); // Output: true
|
|
133
|
+
*/
|
|
134
|
+
some(fn) {
|
|
135
|
+
for (const [key, value] of this.entries()) {
|
|
136
|
+
if (fn(value, key, this)) {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Returns the value of the first element in the collection that satisfies the provided testing function.
|
|
144
|
+
* @param fn The function to test each element of the collection.
|
|
145
|
+
* @returns The value of the first element that passes the test. `undefined` if no element passes the test.
|
|
146
|
+
* @example
|
|
147
|
+
* const collection = new Collection<number, number>();
|
|
148
|
+
* collection.set(1, 1);
|
|
149
|
+
* collection.set(2, 2);
|
|
150
|
+
* collection.set(3, 3);
|
|
151
|
+
* const firstEvenValue = collection.find(value => value % 2 === 0);
|
|
152
|
+
* console.log(firstEvenValue); // Output: 2
|
|
153
|
+
*/
|
|
154
|
+
find(fn) {
|
|
155
|
+
for (const [key, value] of this.entries()) {
|
|
156
|
+
if (fn(value, key, this)) {
|
|
157
|
+
return value;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Returns the first key in the collection that satisfies the provided testing function.
|
|
164
|
+
* @param fn The function to test each element of the collection.
|
|
165
|
+
* @returns The first key that passes the test. `undefined` if no element passes the test.
|
|
166
|
+
* @example
|
|
167
|
+
* const collection = new Collection<number, number>();
|
|
168
|
+
* collection.set(1, 1);
|
|
169
|
+
* collection.set(2, 2);
|
|
170
|
+
* collection.set(3, 3);
|
|
171
|
+
* const firstEvenKey = collection.findKey(value => value % 2 === 0);
|
|
172
|
+
* console.log(firstEvenKey); // Output: 2
|
|
173
|
+
*/
|
|
174
|
+
findKey(fn) {
|
|
175
|
+
for (const [key, value] of this.entries()) {
|
|
176
|
+
if (fn(value, key, this)) {
|
|
177
|
+
return key;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
exports.Collection = Collection;
|
|
184
|
+
/**
|
|
185
|
+
* Creates a new array with the results of calling a provided function on every element in the collection.
|
|
186
|
+
* @param fn The function that produces an element of the new array.
|
|
187
|
+
* @param thisArg The value to use as `this` when executing the map function.
|
|
188
|
+
* @returns A new array with the results of calling the provided function on every element in the collection.
|
|
189
|
+
* @example
|
|
190
|
+
* const collection = new Collection<number, string>();
|
|
191
|
+
* collection.set(1, 'one');
|
|
192
|
+
* collection.set(2, 'two');
|
|
193
|
+
* collection.set(3, 'three');
|
|
194
|
+
* const mappedArray = collection.map((value, key) => `${key}: ${value}`);
|
|
195
|
+
* console.log(mappedArray); // Output: ['1: one', '2: two', '3: three']
|
|
196
|
+
*/
|
|
197
|
+
class LimitedCollection {
|
|
198
|
+
static default = {
|
|
199
|
+
resetOnDemand: false,
|
|
200
|
+
limit: Infinity,
|
|
201
|
+
expire: 0,
|
|
202
|
+
};
|
|
203
|
+
data = new Map();
|
|
204
|
+
options;
|
|
205
|
+
timeout = undefined;
|
|
206
|
+
constructor(options) {
|
|
207
|
+
this.options = (0, common_1.MergeOptions)(LimitedCollection.default, options);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Adds an element to the limited collection.
|
|
211
|
+
* @param key The key of the element.
|
|
212
|
+
* @param value The value of the element.
|
|
213
|
+
* @param customExpire The custom expiration time for the element.
|
|
214
|
+
* @example
|
|
215
|
+
* const collection = new LimitedCollection<number, string>({ limit: 3 });
|
|
216
|
+
* collection.set(1, 'one');
|
|
217
|
+
* collection.set(2, 'two');
|
|
218
|
+
* collection.set(3, 'three');
|
|
219
|
+
* console.log(collection.size); // Output: 3
|
|
220
|
+
* collection.set(4, 'four');
|
|
221
|
+
* console.log(collection.size); // Output: 3
|
|
222
|
+
* console.log(collection.get(1)); // Output: undefined
|
|
223
|
+
*/
|
|
224
|
+
set(key, value, customExpire = this.options.expire) {
|
|
225
|
+
if (this.options.limit <= 0) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const expireOn = Date.now() + customExpire;
|
|
229
|
+
this.data.set(key, customExpire > 0 ? { value, expire: customExpire, expireOn } : { value, expire: -1, expireOn: -1 });
|
|
230
|
+
if (this.size > this.options.limit) {
|
|
231
|
+
const iter = this.data.keys();
|
|
232
|
+
while (this.size > this.options.limit) {
|
|
233
|
+
this.delete(iter.next().value);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (this.closer.expireOn >= expireOn) {
|
|
237
|
+
this.resetTimeout();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Returns the raw data of an element in the limited collection.
|
|
242
|
+
* @param key The key of the element.
|
|
243
|
+
* @returns The raw data of the element, or `undefined` if the element does not exist.
|
|
244
|
+
* @example
|
|
245
|
+
* const collection = new LimitedCollection<number, string>();
|
|
246
|
+
* collection.set(1, 'one');
|
|
247
|
+
* const rawData = collection.raw(1);
|
|
248
|
+
* console.log(rawData); // Output: { value: 'one', expire: -1, expireOn: -1 }
|
|
249
|
+
*/
|
|
250
|
+
raw(key) {
|
|
251
|
+
return this.data.get(key);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Returns the value of an element in the limited collection.
|
|
255
|
+
* @param key The key of the element.
|
|
256
|
+
* @returns The value of the element, or `undefined` if the element does not exist.
|
|
257
|
+
* @example
|
|
258
|
+
* const collection = new LimitedCollection<number, string>();
|
|
259
|
+
* collection.set(1, 'one');
|
|
260
|
+
* const value = collection.get(1);
|
|
261
|
+
* console.log(value); // Output: 'one'
|
|
262
|
+
*/
|
|
263
|
+
get(key) {
|
|
264
|
+
const data = this.data.get(key);
|
|
265
|
+
if (this.options.resetOnDemand && data && data.expire !== -1) {
|
|
266
|
+
if (this.closer?.expireOn === data.expireOn) {
|
|
267
|
+
setImmediate(() => this.resetTimeout());
|
|
268
|
+
}
|
|
269
|
+
data.expireOn = Date.now() + data.expire;
|
|
270
|
+
}
|
|
271
|
+
return data?.value;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Checks if an element exists in the limited collection.
|
|
275
|
+
* @param key The key of the element.
|
|
276
|
+
* @returns `true` if the element exists, `false` otherwise.
|
|
277
|
+
* @example
|
|
278
|
+
* const collection = new LimitedCollection<number, string>();
|
|
279
|
+
* collection.set(1, 'one');
|
|
280
|
+
* console.log(collection.has(1)); // Output: true
|
|
281
|
+
* console.log(collection.has(2)); // Output: false
|
|
282
|
+
*/
|
|
283
|
+
has(key) {
|
|
284
|
+
return this.data.has(key);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Removes an element from the limited collection.
|
|
288
|
+
* @param key The key of the element to remove.
|
|
289
|
+
* @returns `true` if the element was removed, `false` otherwise.
|
|
290
|
+
* @example
|
|
291
|
+
* const collection = new LimitedCollection<number, string>();
|
|
292
|
+
* collection.set(1, 'one');
|
|
293
|
+
* console.log(collection.delete(1)); // Output: true
|
|
294
|
+
* console.log(collection.delete(2)); // Output: false
|
|
295
|
+
*/
|
|
296
|
+
delete(key) {
|
|
297
|
+
setImmediate(() => this.resetTimeout());
|
|
298
|
+
return this.data.delete(key);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Returns the element in the limited collection that is closest to expiration.
|
|
302
|
+
* @returns The element that is closest to expiration, or `undefined` if the collection is empty.
|
|
303
|
+
* @example
|
|
304
|
+
* const collection = new LimitedCollection<number, string>();
|
|
305
|
+
* collection.set(1, 'one', 1000);
|
|
306
|
+
* collection.set(2, 'two', 2000);
|
|
307
|
+
* collection.set(3, 'three', 500);
|
|
308
|
+
* const closestElement = collection.closer;
|
|
309
|
+
* console.log(closestElement); // Output: { value: 'three', expire: 500, expireOn: [current timestamp + 500] }
|
|
310
|
+
*/
|
|
311
|
+
get closer() {
|
|
312
|
+
let d;
|
|
313
|
+
for (const value of this.data.values()) {
|
|
314
|
+
if (value.expire === -1) {
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (!d) {
|
|
318
|
+
d = value;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (d.expireOn > value.expireOn) {
|
|
322
|
+
d = value;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return d;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Returns the number of elements in the limited collection.
|
|
329
|
+
* @returns The number of elements in the collection.
|
|
330
|
+
* @example
|
|
331
|
+
* const collection = new LimitedCollection<number, string>();
|
|
332
|
+
* collection.set(1, 'one');
|
|
333
|
+
* collection.set(2, 'two');
|
|
334
|
+
* console.log(collection.size); // Output: 2
|
|
335
|
+
*/
|
|
336
|
+
get size() {
|
|
337
|
+
return this.data.size;
|
|
338
|
+
}
|
|
339
|
+
resetTimeout() {
|
|
340
|
+
this.stopTimeout();
|
|
341
|
+
this.startTimeout();
|
|
342
|
+
}
|
|
343
|
+
stopTimeout() {
|
|
344
|
+
clearTimeout(this.timeout);
|
|
345
|
+
this.timeout = undefined;
|
|
346
|
+
}
|
|
347
|
+
startTimeout() {
|
|
348
|
+
const { expireOn, expire } = this.closer || { expire: -1, expireOn: -1 };
|
|
349
|
+
if (expire === -1) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (this.timeout) {
|
|
353
|
+
this.stopTimeout();
|
|
354
|
+
}
|
|
355
|
+
this.timeout = setTimeout(() => {
|
|
356
|
+
this.clearExpired();
|
|
357
|
+
this.resetTimeout();
|
|
358
|
+
}, expireOn - Date.now());
|
|
359
|
+
}
|
|
360
|
+
clearExpired() {
|
|
361
|
+
for (const [key, value] of this.data) {
|
|
362
|
+
if (value.expireOn === -1) {
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (Date.now() >= value.expireOn) {
|
|
366
|
+
this.data.delete(key);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
exports.LimitedCollection = LimitedCollection;
|