mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
chore: monorepo setup (#7175)
This commit is contained in:
16
packages/collection/.eslintrc.json
Normal file
16
packages/collection/.eslintrc.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"root": true,
|
||||
"extends": "marine/prettier/node",
|
||||
"parserOptions": {
|
||||
"project": "./tsconfig.eslint.json",
|
||||
"extraFileExtensions": [".mjs"]
|
||||
},
|
||||
"ignorePatterns": ["**/dist/*"],
|
||||
"env": {
|
||||
"jest": true
|
||||
},
|
||||
"rules": {
|
||||
"no-redeclare": 0,
|
||||
"@typescript-eslint/naming-convention": 0
|
||||
}
|
||||
}
|
||||
27
packages/collection/.gitignore
vendored
Normal file
27
packages/collection/.gitignore
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
# Packages
|
||||
node_modules/
|
||||
|
||||
# Log files
|
||||
logs/
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
|
||||
# Env
|
||||
.env
|
||||
|
||||
# Dist
|
||||
dist/
|
||||
typings/
|
||||
docs/**/*
|
||||
!docs/index.yml
|
||||
!docs/README.md
|
||||
|
||||
# Miscellaneous
|
||||
.tmp/
|
||||
coverage/
|
||||
tsconfig.tsbuildinfo
|
||||
8
packages/collection/.prettierrc.json
Normal file
8
packages/collection/.prettierrc.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"printWidth": 120,
|
||||
"useTabs": true,
|
||||
"singleQuote": true,
|
||||
"quoteProps": "as-needed",
|
||||
"trailingComma": "all",
|
||||
"endOfLine": "lf"
|
||||
}
|
||||
3
packages/collection/.versionrc
Normal file
3
packages/collection/.versionrc
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"releaseCommitMessageFormat": "chore(Release): publish"
|
||||
}
|
||||
63
packages/collection/CHANGELOG.md
Normal file
63
packages/collection/CHANGELOG.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
|
||||
# [0.4.0](https://github.com/discordjs/collection/compare/v0.3.2...v0.4.0) (2021-12-24)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add #reverse ([#48](https://github.com/discordjs/collection/issues/48)) ([8bcb5e2](https://github.com/discordjs/collection/commit/8bcb5e21bcc15f5b77612d8ff03dec6c37f4d449))
|
||||
* add Collection#ensure ([#52](https://github.com/discordjs/collection/issues/52)) ([3809eb4](https://github.com/discordjs/collection/commit/3809eb4d18e70459355d310919a3f57747eee3dd))
|
||||
|
||||
|
||||
|
||||
## [0.3.2](https://github.com/discordjs/collection/compare/v0.3.1...v0.3.2) (2021-10-29)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* update doc engine ([4c0e24f](https://github.com/discordjs/collection/commit/4c0e24fae0323db9de1991db9cfacc093d529abc))
|
||||
|
||||
|
||||
|
||||
## [0.3.1](https://github.com/discordjs/collection/compare/v0.3.0...v0.3.1) (2021-10-29)
|
||||
|
||||
|
||||
|
||||
# [0.3.0](https://github.com/discordjs/collection/compare/v0.2.4...v0.3.0) (2021-10-29)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add Collection#at() and Collection#keyAt() ([#46](https://github.com/discordjs/collection/issues/46)) ([66b30b9](https://github.com/discordjs/collection/commit/66b30b91069502493383c059cc38e27c152bf541))
|
||||
* improve documentation and resolve [#49](https://github.com/discordjs/collection/issues/49) ([aec01c6](https://github.com/discordjs/collection/commit/aec01c6ae3ff50b0b5f7c070bff10f01bf98d803))
|
||||
* ts-docgen ([463b131](https://github.com/discordjs/collection/commit/463b1314e60f2debc526454a6ccd7ce8a9a4ae8a))
|
||||
|
||||
|
||||
|
||||
## [0.2.4](https://github.com/discordjs/collection/compare/v0.2.3...v0.2.4) (2021-10-27)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* minification of names ([bd2fe2a](https://github.com/discordjs/collection/commit/bd2fe2a47c38f634b0334fe6e89f30f6f6a0b1f5))
|
||||
|
||||
|
||||
|
||||
## [0.2.3](https://github.com/discordjs/collection/compare/v0.2.2...v0.2.3) (2021-10-27)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* building with useDefineForClassFields false ([2a571d5](https://github.com/discordjs/collection/commit/2a571d5a2c90ed8b708c3c5c017e2f225cd494e9))
|
||||
|
||||
|
||||
|
||||
## [0.2.2](https://github.com/discordjs/collection/compare/v0.2.1...v0.2.2) (2021-10-27)
|
||||
|
||||
|
||||
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||
191
packages/collection/LICENSE
Normal file
191
packages/collection/LICENSE
Normal file
@@ -0,0 +1,191 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
Copyright 2015 - 2021 Noel Buechler
|
||||
Copyright 2015 - 2021 Amish Shah
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
46
packages/collection/README.md
Normal file
46
packages/collection/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
<div align="center">
|
||||
<br />
|
||||
<p>
|
||||
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
||||
</p>
|
||||
<br />
|
||||
<p>
|
||||
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
|
||||
<a href="https://www.npmjs.com/package/@discordjs/collection"><img src="https://img.shields.io/npm/v/@discordjs/collection.svg?maxAge=3600" alt="npm version" /></a>
|
||||
<a href="https://www.npmjs.com/package/@discordjs/collection"><img src="https://img.shields.io/npm/dt/@discordjs/collection.svg?maxAge=3600" alt="npm downloads" /></a>
|
||||
<a href="https://github.com/discordjs/collection/actions"><img src="https://github.com/discordjs/collection/workflows/Tests/badge.svg" alt="Build status" /></a>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
## About
|
||||
|
||||
`@discordjs/collection` is a powerful utility data structure used in discord.js.
|
||||
|
||||
## Installation
|
||||
|
||||
**Node.js 16.0.0 or newer is required.**
|
||||
|
||||
```sh-session
|
||||
npm install @discordjs/collection
|
||||
yarn add @discordjs/collection
|
||||
pnpm add @discordjs/collection
|
||||
```
|
||||
|
||||
## Links
|
||||
|
||||
- [Website](https://discord.js.org/) ([source](https://github.com/discordjs/website))
|
||||
- [Documentation](https://discord.js.org/#/docs/collection)
|
||||
- [discord.js Discord server](https://discord.gg/djs)
|
||||
- [GitHub](https://github.com/discordjs/collection)
|
||||
- [npm](https://www.npmjs.com/package/@discordjs/collection)
|
||||
|
||||
## Contributing
|
||||
|
||||
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
||||
[documentation](https://discord.js.org/#/docs/collection).
|
||||
See [the contribution guide](https://github.com/discordjs/collection/blob/main/.github/CONTRIBUTING.md) if you'd like to submit a PR.
|
||||
|
||||
## Help
|
||||
|
||||
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle
|
||||
nudge in the right direction, please don't hesitate to join our official [discord.js Server](https://discord.gg/djs).
|
||||
462
packages/collection/__tests__/collection.test.ts
Normal file
462
packages/collection/__tests__/collection.test.ts
Normal file
@@ -0,0 +1,462 @@
|
||||
import Collection from '../src';
|
||||
|
||||
type TestCollection = Collection<string, number>;
|
||||
|
||||
test('do basic map operations', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
expect(coll.size).toEqual(1);
|
||||
expect(coll.has('a')).toBeTruthy();
|
||||
expect(coll.get('a')).toStrictEqual(1);
|
||||
coll.delete('a');
|
||||
expect(coll.has('a')).toBeFalsy();
|
||||
expect(coll.get('a')).toStrictEqual(undefined);
|
||||
coll.clear();
|
||||
expect(coll.size).toStrictEqual(0);
|
||||
});
|
||||
|
||||
test('get the first item of the collection', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
expect(coll.first()).toStrictEqual(1);
|
||||
});
|
||||
|
||||
test('get the first 3 items of the collection where size equals', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
expect(coll.first(3)).toStrictEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
test('get the first 3 items of the collection where size is less', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
expect(coll.first(3)).toStrictEqual([1, 2]);
|
||||
});
|
||||
|
||||
test('get the last item of the collection', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
expect(coll.last()).toStrictEqual(2);
|
||||
});
|
||||
|
||||
test('get the last 3 items of the collection', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
expect(coll.last(3)).toStrictEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
test('get the last 3 items of the collection where size is less', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
expect(coll.last(3)).toStrictEqual([1, 2]);
|
||||
});
|
||||
|
||||
test('find an item in the collection', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
expect(coll.find((x) => x === 1)).toStrictEqual(1);
|
||||
expect(coll.find((x) => x === 10)).toStrictEqual(undefined);
|
||||
});
|
||||
|
||||
test('sweep items from the collection', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
const n1 = coll.sweep((x) => x === 2);
|
||||
expect(n1).toStrictEqual(1);
|
||||
expect([...coll.values()]).toStrictEqual([1, 3]);
|
||||
const n2 = coll.sweep((x) => x === 4);
|
||||
expect(n2).toStrictEqual(0);
|
||||
expect([...coll.values()]).toStrictEqual([1, 3]);
|
||||
});
|
||||
|
||||
test('filter items from the collection', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
const filtered = coll.filter((x) => x % 2 === 1);
|
||||
expect(coll.size).toStrictEqual(3);
|
||||
expect(filtered.size).toStrictEqual(2);
|
||||
expect([...filtered.values()]).toStrictEqual([1, 3]);
|
||||
});
|
||||
|
||||
test('partition a collection into two collections', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
coll.set('d', 4);
|
||||
coll.set('e', 5);
|
||||
coll.set('f', 6);
|
||||
const [even, odd] = coll.partition((x) => x % 2 === 0);
|
||||
expect([...even.values()]).toStrictEqual([2, 4, 6]);
|
||||
expect([...odd.values()]).toStrictEqual([1, 3, 5]);
|
||||
});
|
||||
|
||||
test('map items in a collection into an array', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
const mapped = coll.map((x) => x + 1);
|
||||
expect(mapped).toStrictEqual([2, 3, 4]);
|
||||
});
|
||||
|
||||
test('map items in a collection into a collection', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
const mapped = coll.mapValues((x) => x + 1);
|
||||
expect([...mapped.values()]).toStrictEqual([2, 3, 4]);
|
||||
});
|
||||
|
||||
test('flatMap items in a collection into a single collection', () => {
|
||||
const coll = new Collection<string, { a: Collection<string, number> }>();
|
||||
const coll1 = new Collection<string, number>();
|
||||
const coll2 = new Collection<string, number>();
|
||||
coll1.set('z', 1);
|
||||
coll1.set('x', 2);
|
||||
coll2.set('c', 3);
|
||||
coll2.set('v', 4);
|
||||
coll.set('a', { a: coll1 });
|
||||
coll.set('b', { a: coll2 });
|
||||
const mapped = coll.flatMap((x) => x.a);
|
||||
expect([...mapped.values()]).toStrictEqual([1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
test('check if some items pass a predicate', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
expect(coll.some((x) => x === 2)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('check if every items pass a predicate', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
expect(!coll.every((x) => x === 2)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('reduce collection into a single value with initial value', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
const sum = coll.reduce((a, x) => a + x, 0);
|
||||
expect(sum).toStrictEqual(6);
|
||||
});
|
||||
|
||||
test('reduce collection into a single value without initial value', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
const sum = coll.reduce((a, x) => a + x, 0);
|
||||
expect(sum).toStrictEqual(6);
|
||||
});
|
||||
|
||||
test('reduce empty collection without initial value', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
expect(() => coll.reduce((a: number, x) => a + x)).toThrowError(
|
||||
new TypeError('Reduce of empty collection with no initial value'),
|
||||
);
|
||||
});
|
||||
|
||||
test('iterate over each item', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
const a: [string, number][] = [];
|
||||
coll.each((v, k) => a.push([k, v]));
|
||||
expect(a).toStrictEqual([
|
||||
['a', 1],
|
||||
['b', 2],
|
||||
['c', 3],
|
||||
]);
|
||||
});
|
||||
|
||||
test('tap the collection', () => {
|
||||
const coll = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
coll.tap((c) => expect(c).toStrictEqual(coll));
|
||||
});
|
||||
|
||||
test('shallow clone the collection', () => {
|
||||
const coll = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
const clone = coll.clone();
|
||||
expect([...coll.values()]).toStrictEqual([...clone.values()]);
|
||||
});
|
||||
|
||||
test('merge multiple collections', () => {
|
||||
const coll1 = new Collection();
|
||||
coll1.set('a', 1);
|
||||
const coll2 = new Collection();
|
||||
coll2.set('b', 2);
|
||||
const coll3 = new Collection();
|
||||
coll3.set('c', 3);
|
||||
const merged = coll1.concat(coll2, coll3);
|
||||
expect([...merged.values()]).toStrictEqual([1, 2, 3]);
|
||||
expect(coll1 !== merged).toBeTruthy();
|
||||
});
|
||||
|
||||
test('check equality of two collections', () => {
|
||||
const coll1 = new Collection<string, number>();
|
||||
coll1.set('a', 1);
|
||||
const coll2 = new Collection<string, number>();
|
||||
coll2.set('a', 1);
|
||||
expect(coll1.equals(coll2)).toBeTruthy();
|
||||
coll2.set('b', 2);
|
||||
expect(!coll1.equals(coll2)).toBeTruthy();
|
||||
coll2.clear();
|
||||
expect(!coll1.equals(coll2)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('sort a collection in place', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 3);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 1);
|
||||
expect([...coll.values()]).toStrictEqual([3, 2, 1]);
|
||||
coll.sort((a, b) => a - b);
|
||||
expect([...coll.values()]).toStrictEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
test('random select from a collection', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
const chars = 'abcdefghijklmnopqrstuvwxyz';
|
||||
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26];
|
||||
|
||||
for (let i = 0; i < chars.length; i++) coll.set(chars[i], numbers[i]);
|
||||
|
||||
const random = coll.random(5);
|
||||
expect(random.length === 5).toBeTruthy();
|
||||
|
||||
const set = new Set(random);
|
||||
expect(set.size === random.length).toBeTruthy();
|
||||
});
|
||||
|
||||
test('when random param > collection size', () => {
|
||||
const coll = new Collection();
|
||||
coll.set('a', 3);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 1);
|
||||
|
||||
const random = coll.random(5);
|
||||
expect(random.length).toEqual(coll.size);
|
||||
});
|
||||
|
||||
test('sort a collection', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 3);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 1);
|
||||
expect([...coll.values()]).toStrictEqual([3, 2, 1]);
|
||||
const sorted = coll.sorted((a, b) => a - b);
|
||||
expect([...coll.values()]).toStrictEqual([3, 2, 1]);
|
||||
expect([...sorted.values()]).toStrictEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
describe('at() tests', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
|
||||
test('Positive index', () => {
|
||||
expect(coll.at(0)).toStrictEqual(1);
|
||||
});
|
||||
|
||||
test('Negative index', () => {
|
||||
expect(coll.at(-1)).toStrictEqual(2);
|
||||
});
|
||||
|
||||
test('Invalid positive index', () => {
|
||||
expect(coll.at(3)).toBeUndefined();
|
||||
});
|
||||
|
||||
test('Invalid negative index', () => {
|
||||
expect(coll.at(-3)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('keyAt() tests', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
|
||||
test('Positive index', () => {
|
||||
expect(coll.keyAt(0)).toStrictEqual('a');
|
||||
});
|
||||
|
||||
test('Negative index', () => {
|
||||
expect(coll.keyAt(-1)).toStrictEqual('b');
|
||||
});
|
||||
|
||||
test('Invalid positive index', () => {
|
||||
expect(coll.keyAt(3)).toBeUndefined();
|
||||
});
|
||||
|
||||
test('Invalid negative index', () => {
|
||||
expect(coll.keyAt(-3)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasAll() tests', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
|
||||
test('All keys exist in the collection', () => {
|
||||
expect(coll.hasAll('a', 'b')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('Some keys exist in the collection', () => {
|
||||
expect(coll.hasAll('b', 'c')).toBeFalsy();
|
||||
});
|
||||
|
||||
test('No keys exist in the collection', () => {
|
||||
expect(coll.hasAll('c', 'd')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('hasAny() tests', () => {
|
||||
const coll: TestCollection = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
|
||||
test('All keys exist in the collection', () => {
|
||||
expect(coll.hasAny('a', 'b')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('Some keys exist in the collection', () => {
|
||||
expect(coll.hasAny('b', 'c')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('No keys exist in the collection', () => {
|
||||
expect(coll.hasAny('c', 'd')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('reverse() tests', () => {
|
||||
const coll = new Collection();
|
||||
coll.set('a', 1);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 3);
|
||||
|
||||
coll.reverse();
|
||||
|
||||
expect([...coll.values()]).toStrictEqual([3, 2, 1]);
|
||||
expect([...coll.keys()]).toStrictEqual(['c', 'b', 'a']);
|
||||
});
|
||||
|
||||
describe('random thisArg tests', () => {
|
||||
const coll = new Collection();
|
||||
coll.set('a', 3);
|
||||
coll.set('b', 2);
|
||||
coll.set('c', 1);
|
||||
|
||||
const object = {};
|
||||
const string = 'Hi';
|
||||
const boolean = false;
|
||||
const symbol = Symbol('testArg');
|
||||
const array = [1, 2, 3];
|
||||
|
||||
coll.set('d', object);
|
||||
coll.set('e', string);
|
||||
coll.set('f', boolean);
|
||||
coll.set('g', symbol);
|
||||
coll.set('h', array);
|
||||
|
||||
test('thisArg test: number', () => {
|
||||
coll.some(function thisArgTest1(value) {
|
||||
expect(this.valueOf()).toStrictEqual(1);
|
||||
expect(typeof this).toEqual('number');
|
||||
return value === this;
|
||||
}, 1);
|
||||
});
|
||||
|
||||
test('thisArg test: object', () => {
|
||||
coll.some(function thisArgTest2(value) {
|
||||
expect(this).toStrictEqual(object);
|
||||
expect(this.constructor === Object).toBeTruthy();
|
||||
return value === this;
|
||||
}, object);
|
||||
});
|
||||
|
||||
test('thisArg test: string', () => {
|
||||
coll.some(function thisArgTest3(value) {
|
||||
expect(this.valueOf()).toStrictEqual(string);
|
||||
expect(typeof this).toEqual('string');
|
||||
return value === this;
|
||||
}, string);
|
||||
});
|
||||
|
||||
test('thisArg test: boolean', () => {
|
||||
coll.some(function thisArgTest4(value) {
|
||||
expect(this.valueOf()).toStrictEqual(boolean);
|
||||
expect(typeof this).toEqual('boolean');
|
||||
return value === this;
|
||||
}, boolean);
|
||||
});
|
||||
|
||||
test('thisArg test: symbol', () => {
|
||||
coll.some(function thisArgTest5(value) {
|
||||
expect(this.valueOf()).toStrictEqual(symbol);
|
||||
expect(typeof this).toEqual('symbol');
|
||||
return value === this;
|
||||
}, symbol);
|
||||
});
|
||||
|
||||
test('thisArg test: array', () => {
|
||||
coll.some(function thisArgTest6(value) {
|
||||
expect(this).toStrictEqual(array);
|
||||
expect(Array.isArray(this)).toBeTruthy();
|
||||
return value === this;
|
||||
}, 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);
|
||||
});
|
||||
});
|
||||
17
packages/collection/babel.config.js
Normal file
17
packages/collection/babel.config.js
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @type {import('@babel/core').TransformOptions}
|
||||
*/
|
||||
module.exports = {
|
||||
parserOpts: { strictMode: true },
|
||||
sourceMaps: 'inline',
|
||||
presets: [
|
||||
[
|
||||
'@babel/preset-env',
|
||||
{
|
||||
targets: { node: 'current' },
|
||||
modules: 'commonjs',
|
||||
},
|
||||
],
|
||||
'@babel/preset-typescript',
|
||||
],
|
||||
};
|
||||
1
packages/collection/docs/README.md
Normal file
1
packages/collection/docs/README.md
Normal file
@@ -0,0 +1 @@
|
||||
## [View the documentation here.](https://discord.js.org/#/docs/collection)
|
||||
5
packages/collection/docs/index.yml
Normal file
5
packages/collection/docs/index.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
- name: General
|
||||
files:
|
||||
- name: Welcome
|
||||
id: welcome
|
||||
path: ../../README.md
|
||||
11
packages/collection/jest.config.js
Normal file
11
packages/collection/jest.config.js
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @type {import('@jest/types').Config.InitialOptions}
|
||||
*/
|
||||
module.exports = {
|
||||
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
|
||||
testEnvironment: 'node',
|
||||
collectCoverage: true,
|
||||
collectCoverageFrom: ['src/**/*.ts'],
|
||||
coverageDirectory: 'coverage',
|
||||
coverageReporters: ['text', 'lcov', 'clover'],
|
||||
};
|
||||
75
packages/collection/package.json
Normal file
75
packages/collection/package.json
Normal file
@@ -0,0 +1,75 @@
|
||||
{
|
||||
"name": "@discordjs/collection",
|
||||
"version": "0.4.0",
|
||||
"description": "Utility data structure used in discord.js",
|
||||
"scripts": {
|
||||
"test": "jest --pass-with-no-tests",
|
||||
"build": "tsup",
|
||||
"lint": "eslint src --ext mjs,js,ts",
|
||||
"lint:fix": "eslint src --ext mjs,js,ts --fix",
|
||||
"format": "prettier --write **/*.{ts,js,json,yml,yaml}",
|
||||
"docs": "typedoc --json docs/typedoc-out.json src/index.ts && node scripts/docs.mjs",
|
||||
"prepublishOnly": "yarn build && yarn lint && yarn test",
|
||||
"changelog": "git cliff --prepend ./CHANGELOG.md -l -c ../../cliff.toml -r ../../ --include-path './*'"
|
||||
},
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.js"
|
||||
},
|
||||
"directories": {
|
||||
"lib": "src",
|
||||
"test": "__tests__"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"contributors": [
|
||||
"Crawl <icrawltogo@gmail.com>",
|
||||
"Amish Shah <amishshah.2k@gmail.com>",
|
||||
"SpaceEEC <spaceeec@yahoo.com>",
|
||||
"Vlad Frangu <kingdgrizzle@gmail.com>"
|
||||
],
|
||||
"license": "Apache-2.0",
|
||||
"keywords": [
|
||||
"map",
|
||||
"collection",
|
||||
"utility"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/discordjs/discord.js.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/discordjs/discord.js/issues"
|
||||
},
|
||||
"homepage": "https://discord.js.org",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.16.5",
|
||||
"@babel/preset-env": "^7.16.5",
|
||||
"@babel/preset-typescript": "^7.16.5",
|
||||
"@discordjs/ts-docgen": "^0.3.4",
|
||||
"@types/jest": "^27.0.3",
|
||||
"@types/node": "^16.11.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.8.0",
|
||||
"@typescript-eslint/parser": "^5.8.0",
|
||||
"eslint": "^8.5.0",
|
||||
"eslint-config-marine": "^9.1.0",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "^27.4.5",
|
||||
"prettier": "^2.5.1",
|
||||
"standard-version": "^9.3.2",
|
||||
"tsup": "^5.11.8",
|
||||
"typedoc": "^0.22.10",
|
||||
"typescript": "^4.5.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=16.0.0"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
7
packages/collection/scripts/docs.mjs
Normal file
7
packages/collection/scripts/docs.mjs
Normal file
@@ -0,0 +1,7 @@
|
||||
import { runGenerator } from '@discordjs/ts-docgen';
|
||||
|
||||
runGenerator({
|
||||
existingOutput: 'docs/typedoc-out.json',
|
||||
custom: 'docs/index.yml',
|
||||
output: 'docs/docs.json',
|
||||
});
|
||||
687
packages/collection/src/index.ts
Normal file
687
packages/collection/src/index.ts
Normal file
@@ -0,0 +1,687 @@
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export interface CollectionConstructor {
|
||||
new (): Collection<unknown, unknown>;
|
||||
new <K, V>(entries?: ReadonlyArray<readonly [K, V]> | null): Collection<K, V>;
|
||||
new <K, V>(iterable: Iterable<readonly [K, V]>): Collection<K, V>;
|
||||
readonly prototype: Collection<unknown, unknown>;
|
||||
readonly [Symbol.species]: CollectionConstructor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
export interface Collection<K, V> extends Map<K, V> {
|
||||
constructor: CollectionConstructor;
|
||||
}
|
||||
|
||||
/**
|
||||
* A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has
|
||||
* an ID, for significantly improved performance and ease-of-use.
|
||||
*/
|
||||
export class Collection<K, V> extends Map<K, V> {
|
||||
public static readonly default: typeof Collection = Collection;
|
||||
|
||||
/**
|
||||
* 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 default value
|
||||
*
|
||||
* @example
|
||||
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||
*/
|
||||
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);
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if all of the elements exist in the collection.
|
||||
*
|
||||
* @param keys - The keys of the elements to check for
|
||||
*
|
||||
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||
*/
|
||||
public hasAll(...keys: K[]) {
|
||||
return keys.every((k) => super.has(k));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if any of the elements exist in the collection.
|
||||
*
|
||||
* @param keys - The keys of the elements to check for
|
||||
*
|
||||
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||
*/
|
||||
public hasAny(...keys: K[]) {
|
||||
return keys.some((k) => super.has(k));
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the first value(s) in this collection.
|
||||
*
|
||||
* @param amount Amount of values to obtain from the beginning
|
||||
*
|
||||
* @returns A single value if no amount is provided or an array of values, starting from the end if amount is negative
|
||||
*/
|
||||
public first(): V | undefined;
|
||||
public first(amount: number): V[];
|
||||
public first(amount?: number): V | V[] | undefined {
|
||||
if (typeof amount === 'undefined') return this.values().next().value;
|
||||
if (amount < 0) return this.last(amount * -1);
|
||||
amount = Math.min(this.size, amount);
|
||||
const iter = this.values();
|
||||
return Array.from({ length: amount }, (): V => iter.next().value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the first key(s) in this collection.
|
||||
*
|
||||
* @param amount Amount of keys to obtain from the beginning
|
||||
*
|
||||
* @returns A single key if no amount is provided or an array of keys, starting from the end if
|
||||
* amount is negative
|
||||
*/
|
||||
public firstKey(): K | undefined;
|
||||
public firstKey(amount: number): K[];
|
||||
public firstKey(amount?: number): K | K[] | undefined {
|
||||
if (typeof amount === 'undefined') return this.keys().next().value;
|
||||
if (amount < 0) return this.lastKey(amount * -1);
|
||||
amount = Math.min(this.size, amount);
|
||||
const iter = this.keys();
|
||||
return Array.from({ length: amount }, (): K => iter.next().value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the last value(s) in this collection.
|
||||
*
|
||||
* @param amount Amount of values to obtain from the end
|
||||
*
|
||||
* @returns A single value if no amount is provided or an array of values, starting from the start if
|
||||
* amount is negative
|
||||
*/
|
||||
public last(): V | undefined;
|
||||
public last(amount: number): V[];
|
||||
public last(amount?: number): V | V[] | undefined {
|
||||
const arr = [...this.values()];
|
||||
if (typeof amount === 'undefined') return arr[arr.length - 1];
|
||||
if (amount < 0) return this.first(amount * -1);
|
||||
if (!amount) return [];
|
||||
return arr.slice(-amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the last key(s) in this collection.
|
||||
*
|
||||
* @param amount Amount of keys to obtain from the end
|
||||
*
|
||||
* @returns A single key if no amount is provided or an array of keys, starting from the start if
|
||||
* amount is negative
|
||||
*/
|
||||
public lastKey(): K | undefined;
|
||||
public lastKey(amount: number): K[];
|
||||
public lastKey(amount?: number): K | K[] | undefined {
|
||||
const arr = [...this.keys()];
|
||||
if (typeof amount === 'undefined') return arr[arr.length - 1];
|
||||
if (amount < 0) return this.firstKey(amount * -1);
|
||||
if (!amount) return [];
|
||||
return arr.slice(-amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Identical to [Array.at()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at).
|
||||
* Returns the item at a given index, allowing for positive and negative integers.
|
||||
* Negative integers count back from the last item in the collection.
|
||||
*
|
||||
* @param index The index of the element to obtain
|
||||
*/
|
||||
public at(index: number) {
|
||||
index = Math.floor(index);
|
||||
const arr = [...this.values()];
|
||||
return arr.at(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Identical to [Array.at()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at).
|
||||
* Returns the key at a given index, allowing for positive and negative integers.
|
||||
* Negative integers count back from the last item in the collection.
|
||||
*
|
||||
* @param index The index of the key to obtain
|
||||
*/
|
||||
public keyAt(index: number) {
|
||||
index = Math.floor(index);
|
||||
const arr = [...this.keys()];
|
||||
return arr.at(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains unique random value(s) from this collection.
|
||||
*
|
||||
* @param amount Amount of values to obtain randomly
|
||||
*
|
||||
* @returns A single value if no amount is provided or an array of values
|
||||
*/
|
||||
public random(): V | undefined;
|
||||
public random(amount: number): V[];
|
||||
public random(amount?: number): V | V[] | undefined {
|
||||
const arr = [...this.values()];
|
||||
if (typeof amount === 'undefined') return arr[Math.floor(Math.random() * arr.length)];
|
||||
if (!arr.length || !amount) return [];
|
||||
return Array.from(
|
||||
{ length: Math.min(amount, arr.length) },
|
||||
(): V => arr.splice(Math.floor(Math.random() * arr.length), 1)[0],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains unique random key(s) from this collection.
|
||||
*
|
||||
* @param amount Amount of keys to obtain randomly
|
||||
*
|
||||
* @returns A single key if no amount is provided or an array
|
||||
*/
|
||||
public randomKey(): K | undefined;
|
||||
public randomKey(amount: number): K[];
|
||||
public randomKey(amount?: number): K | K[] | undefined {
|
||||
const arr = [...this.keys()];
|
||||
if (typeof amount === 'undefined') return arr[Math.floor(Math.random() * arr.length)];
|
||||
if (!arr.length || !amount) return [];
|
||||
return Array.from(
|
||||
{ length: Math.min(amount, arr.length) },
|
||||
(): K => arr.splice(Math.floor(Math.random() * arr.length), 1)[0],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Identical to [Array.reverse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)
|
||||
* but returns a Collection instead of an Array.
|
||||
*/
|
||||
public reverse() {
|
||||
const entries = [...this.entries()].reverse();
|
||||
this.clear();
|
||||
for (const [key, value] of entries) this.set(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for a single item where the given function returns a truthy value. This behaves like
|
||||
* [Array.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find).
|
||||
* <warn>All collections used in Discord.js are mapped using their `id` property, and if you want to find by id you
|
||||
* should use the `get` method. See
|
||||
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get) for details.</warn>
|
||||
*
|
||||
* @param fn The function to test with (should return boolean)
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection.find(user => user.username === 'Bob');
|
||||
*/
|
||||
public find<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): V2 | undefined;
|
||||
public find(fn: (value: V, key: K, collection: this) => boolean): V | undefined;
|
||||
public find<This, V2 extends V>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => value is V2,
|
||||
thisArg: This,
|
||||
): V2 | undefined;
|
||||
public find<This>(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): V | undefined;
|
||||
public find(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): V | undefined {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
for (const [key, val] of this) {
|
||||
if (fn(val, key, this)) return val;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for the key of a single item where the given function returns a truthy value. This behaves like
|
||||
* [Array.findIndex()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex),
|
||||
* but returns the key rather than the positional index.
|
||||
*
|
||||
* @param fn The function to test with (should return boolean)
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection.findKey(user => user.username === 'Bob');
|
||||
*/
|
||||
public findKey<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): K2 | undefined;
|
||||
public findKey(fn: (value: V, key: K, collection: this) => boolean): K | undefined;
|
||||
public findKey<This, K2 extends K>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => key is K2,
|
||||
thisArg: This,
|
||||
): K2 | undefined;
|
||||
public findKey<This>(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): K | undefined;
|
||||
public findKey(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): K | undefined {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
for (const [key, val] of this) {
|
||||
if (fn(val, key, this)) return key;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes items that satisfy the provided filter function.
|
||||
*
|
||||
* @param fn Function used to test (should return a boolean)
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @returns The number of removed entries
|
||||
*/
|
||||
public sweep(fn: (value: V, key: K, collection: this) => boolean): number;
|
||||
public sweep<T>(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): number;
|
||||
public sweep(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): number {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
const previousSize = this.size;
|
||||
for (const [key, val] of this) {
|
||||
if (fn(val, key, this)) this.delete(key);
|
||||
}
|
||||
return previousSize - this.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Identical to
|
||||
* [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter),
|
||||
* but returns a Collection instead of an Array.
|
||||
*
|
||||
* @param fn The function to test with (should return boolean)
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection.filter(user => user.username === 'Bob');
|
||||
*/
|
||||
public filter<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): Collection<K2, V>;
|
||||
public filter<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): Collection<K, V2>;
|
||||
public filter(fn: (value: V, key: K, collection: this) => boolean): Collection<K, V>;
|
||||
public filter<This, K2 extends K>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => key is K2,
|
||||
thisArg: This,
|
||||
): Collection<K2, V>;
|
||||
public filter<This, V2 extends V>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => value is V2,
|
||||
thisArg: This,
|
||||
): Collection<K, V2>;
|
||||
public filter<This>(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): Collection<K, V>;
|
||||
public filter(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): Collection<K, V> {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
const results = new this.constructor[Symbol.species]<K, V>();
|
||||
for (const [key, val] of this) {
|
||||
if (fn(val, key, this)) results.set(key, val);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions the collection into two collections where the first collection
|
||||
* contains the items that passed and the second contains the items that failed.
|
||||
*
|
||||
* @param fn Function used to test (should return a boolean)
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* const [big, small] = collection.partition(guild => guild.memberCount > 250);
|
||||
*/
|
||||
public partition<K2 extends K>(
|
||||
fn: (value: V, key: K, collection: this) => key is K2,
|
||||
): [Collection<K2, V>, Collection<Exclude<K, K2>, V>];
|
||||
public partition<V2 extends V>(
|
||||
fn: (value: V, key: K, collection: this) => value is V2,
|
||||
): [Collection<K, V2>, Collection<K, Exclude<V, V2>>];
|
||||
public partition(fn: (value: V, key: K, collection: this) => boolean): [Collection<K, V>, Collection<K, V>];
|
||||
public partition<This, K2 extends K>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => key is K2,
|
||||
thisArg: This,
|
||||
): [Collection<K2, V>, Collection<Exclude<K, K2>, V>];
|
||||
public partition<This, V2 extends V>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => value is V2,
|
||||
thisArg: This,
|
||||
): [Collection<K, V2>, Collection<K, Exclude<V, V2>>];
|
||||
public partition<This>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => boolean,
|
||||
thisArg: This,
|
||||
): [Collection<K, V>, Collection<K, V>];
|
||||
public partition(
|
||||
fn: (value: V, key: K, collection: this) => boolean,
|
||||
thisArg?: unknown,
|
||||
): [Collection<K, V>, Collection<K, V>] {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
const results: [Collection<K, V>, Collection<K, V>] = [
|
||||
new this.constructor[Symbol.species]<K, V>(),
|
||||
new this.constructor[Symbol.species]<K, V>(),
|
||||
];
|
||||
for (const [key, val] of this) {
|
||||
if (fn(val, key, this)) {
|
||||
results[0].set(key, val);
|
||||
} else {
|
||||
results[1].set(key, val);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to
|
||||
* [Array.flatMap()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap).
|
||||
*
|
||||
* @param fn Function that produces a new Collection
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection.flatMap(guild => guild.members.cache);
|
||||
*/
|
||||
public flatMap<T>(fn: (value: V, key: K, collection: this) => Collection<K, T>): Collection<K, T>;
|
||||
public flatMap<T, This>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => Collection<K, T>,
|
||||
thisArg: This,
|
||||
): Collection<K, T>;
|
||||
public flatMap<T>(fn: (value: V, key: K, collection: this) => Collection<K, T>, thisArg?: unknown): Collection<K, T> {
|
||||
const collections = this.map(fn, thisArg);
|
||||
return new this.constructor[Symbol.species]<K, T>().concat(...collections);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps each item to another value into an array. Identical in behavior to
|
||||
* [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
|
||||
*
|
||||
* @param fn Function that produces an element of the new array, taking three arguments
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection.map(user => user.tag);
|
||||
*/
|
||||
public map<T>(fn: (value: V, key: K, collection: this) => T): T[];
|
||||
public map<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): T[];
|
||||
public map<T>(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): T[] {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
const iter = this.entries();
|
||||
return Array.from({ length: this.size }, (): T => {
|
||||
const [key, value] = iter.next().value;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
||||
return fn(value, key, this);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps each item to another value into a collection. Identical in behavior to
|
||||
* [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
|
||||
*
|
||||
* @param fn Function that produces an element of the new collection, taking three arguments
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection.mapValues(user => user.tag);
|
||||
*/
|
||||
public mapValues<T>(fn: (value: V, key: K, collection: this) => T): Collection<K, T>;
|
||||
public mapValues<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): Collection<K, T>;
|
||||
public mapValues<T>(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): Collection<K, T> {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
const coll = new this.constructor[Symbol.species]<K, T>();
|
||||
for (const [key, val] of this) coll.set(key, fn(val, key, this));
|
||||
return coll;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there exists an item that passes a test. Identical in behavior to
|
||||
* [Array.some()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some).
|
||||
*
|
||||
* @param fn Function used to test (should return a boolean)
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection.some(user => user.discriminator === '0000');
|
||||
*/
|
||||
public some(fn: (value: V, key: K, collection: this) => boolean): boolean;
|
||||
public some<T>(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): boolean;
|
||||
public some(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): boolean {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
for (const [key, val] of this) {
|
||||
if (fn(val, key, this)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if all items passes a test. Identical in behavior to
|
||||
* [Array.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every).
|
||||
*
|
||||
* @param fn Function used to test (should return a boolean)
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection.every(user => !user.bot);
|
||||
*/
|
||||
public every<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): this is Collection<K2, V>;
|
||||
public every<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): this is Collection<K, V2>;
|
||||
public every(fn: (value: V, key: K, collection: this) => boolean): boolean;
|
||||
public every<This, K2 extends K>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => key is K2,
|
||||
thisArg: This,
|
||||
): this is Collection<K2, V>;
|
||||
public every<This, V2 extends V>(
|
||||
fn: (this: This, value: V, key: K, collection: this) => value is V2,
|
||||
thisArg: This,
|
||||
): this is Collection<K, V2>;
|
||||
public every<This>(fn: (this: This, value: V, key: K, collection: this) => boolean, thisArg: This): boolean;
|
||||
public every(fn: (value: V, key: K, collection: this) => boolean, thisArg?: unknown): boolean {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
for (const [key, val] of this) {
|
||||
if (!fn(val, key, this)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a function to produce a single value. Identical in behavior to
|
||||
* [Array.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).
|
||||
*
|
||||
* @param fn Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||
* and `collection`
|
||||
* @param initialValue Starting value for the accumulator
|
||||
*
|
||||
* @example
|
||||
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||
*/
|
||||
public reduce<T>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T {
|
||||
let accumulator!: T;
|
||||
|
||||
if (typeof initialValue !== 'undefined') {
|
||||
accumulator = initialValue;
|
||||
for (const [key, val] of this) accumulator = fn(accumulator, val, key, this);
|
||||
return accumulator;
|
||||
}
|
||||
let first = true;
|
||||
for (const [key, val] of this) {
|
||||
if (first) {
|
||||
accumulator = val as unknown as T;
|
||||
first = false;
|
||||
continue;
|
||||
}
|
||||
accumulator = fn(accumulator, val, key, this);
|
||||
}
|
||||
|
||||
// No items iterated.
|
||||
if (first) {
|
||||
throw new TypeError('Reduce of empty collection with no initial value');
|
||||
}
|
||||
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Identical to
|
||||
* [Map.forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach),
|
||||
* but returns the collection instead of undefined.
|
||||
*
|
||||
* @param fn Function to execute for each element
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection
|
||||
* .each(user => console.log(user.username))
|
||||
* .filter(user => user.bot)
|
||||
* .each(user => console.log(user.username));
|
||||
*/
|
||||
public each(fn: (value: V, key: K, collection: this) => void): this;
|
||||
public each<T>(fn: (this: T, value: V, key: K, collection: this) => void, thisArg: T): this;
|
||||
public each(fn: (value: V, key: K, collection: this) => void, thisArg?: unknown): this {
|
||||
this.forEach(fn as (value: V, key: K, map: Map<K, V>) => void, thisArg);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a function on the collection and returns the collection.
|
||||
*
|
||||
* @param fn Function to execute
|
||||
* @param thisArg Value to use as `this` when executing function
|
||||
*
|
||||
* @example
|
||||
* collection
|
||||
* .tap(coll => console.log(coll.size))
|
||||
* .filter(user => user.bot)
|
||||
* .tap(coll => console.log(coll.size))
|
||||
*/
|
||||
public tap(fn: (collection: this) => void): this;
|
||||
public tap<T>(fn: (this: T, collection: this) => void, thisArg: T): this;
|
||||
public tap(fn: (collection: this) => void, thisArg?: unknown): this {
|
||||
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||
fn(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an identical shallow copy of this collection.
|
||||
*
|
||||
* @example
|
||||
* const newColl = someColl.clone();
|
||||
*/
|
||||
public clone() {
|
||||
return new this.constructor[Symbol.species](this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||
*
|
||||
* @param collections Collections to merge
|
||||
*
|
||||
* @example
|
||||
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||
*/
|
||||
public concat(...collections: Collection<K, V>[]) {
|
||||
const newColl = this.clone();
|
||||
for (const coll of collections) {
|
||||
for (const [key, val] of coll) newColl.set(key, val);
|
||||
}
|
||||
return newColl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this collection shares identical items with another.
|
||||
* This is different to checking for equality using equal-signs, because
|
||||
* the collections may be different objects, but contain the same data.
|
||||
*
|
||||
* @param collection Collection to compare with
|
||||
*
|
||||
* @returns Whether the collections have identical contents
|
||||
*/
|
||||
public equals(collection: Collection<K, V>) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!collection) return false; // runtime check
|
||||
if (this === collection) return true;
|
||||
if (this.size !== collection.size) return false;
|
||||
for (const [key, value] of this) {
|
||||
if (!collection.has(key) || value !== collection.get(key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The sort method sorts the items of a collection in place and returns it.
|
||||
* The sort is not necessarily stable in Node 10 or older.
|
||||
* The default sort order is according to string Unicode code points.
|
||||
*
|
||||
* @param compareFunction Specifies a function that defines the sort order.
|
||||
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||
*
|
||||
* @example
|
||||
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||
*/
|
||||
public sort(compareFunction: Comparator<K, V> = Collection.defaultSort) {
|
||||
const entries = [...this.entries()];
|
||||
entries.sort((a, b): number => compareFunction(a[1], b[1], a[0], b[0]));
|
||||
|
||||
// Perform clean-up
|
||||
super.clear();
|
||||
|
||||
// Set the new entries
|
||||
for (const [k, v] of entries) {
|
||||
super.set(k, v);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The intersect method returns a new structure containing items where the keys are present in both original structures.
|
||||
*
|
||||
* @param other The other Collection to filter against
|
||||
*/
|
||||
public intersect(other: Collection<K, V>) {
|
||||
const coll = new this.constructor[Symbol.species]<K, V>();
|
||||
for (const [k, v] of other) {
|
||||
if (this.has(k)) coll.set(k, v);
|
||||
}
|
||||
return coll;
|
||||
}
|
||||
|
||||
/**
|
||||
* The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
|
||||
*
|
||||
* @param other The other Collection to filter against
|
||||
*/
|
||||
public difference(other: Collection<K, V>) {
|
||||
const coll = new this.constructor[Symbol.species]<K, V>();
|
||||
for (const [k, v] of other) {
|
||||
if (!this.has(k)) coll.set(k, v);
|
||||
}
|
||||
for (const [k, v] of this) {
|
||||
if (!other.has(k)) coll.set(k, v);
|
||||
}
|
||||
return coll;
|
||||
}
|
||||
|
||||
/**
|
||||
* The sorted method sorts the items of a collection and returns it.
|
||||
* The sort is not necessarily stable in Node 10 or older.
|
||||
* The default sort order is according to string Unicode code points.
|
||||
*
|
||||
* @param compareFunction Specifies a function that defines the sort order.
|
||||
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||
* according to the string conversion of each element.
|
||||
*
|
||||
* @example
|
||||
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||
*/
|
||||
public sorted(compareFunction: Comparator<K, V> = Collection.defaultSort) {
|
||||
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
|
||||
}
|
||||
|
||||
public toJSON() {
|
||||
// toJSON is called recursively by JSON.stringify.
|
||||
return [...this.values()];
|
||||
}
|
||||
|
||||
private static defaultSort<V>(firstValue: V, secondValue: V): number {
|
||||
return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export type Comparator<K, V> = (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number;
|
||||
|
||||
export default Collection;
|
||||
20
packages/collection/tsconfig.eslint.json
Normal file
20
packages/collection/tsconfig.eslint.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"allowJs": true
|
||||
},
|
||||
"include": [
|
||||
"**/*.ts",
|
||||
"**/*.tsx",
|
||||
"**/*.js",
|
||||
"**/*.mjs",
|
||||
"**/*.jsx",
|
||||
"**/*.test.ts",
|
||||
"**/*.test.js",
|
||||
"**/*.test.mjs",
|
||||
"**/*.spec.ts",
|
||||
"**/*.spec.js",
|
||||
"**/*.spec.mjs"
|
||||
],
|
||||
"exclude": []
|
||||
}
|
||||
4
packages/collection/tsconfig.json
Normal file
4
packages/collection/tsconfig.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
14
packages/collection/tsup.config.ts
Normal file
14
packages/collection/tsup.config.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { Options } from 'tsup';
|
||||
|
||||
export const tsup: Options = {
|
||||
clean: true,
|
||||
dts: true,
|
||||
entryPoints: ['src/index.ts'],
|
||||
format: ['esm', 'cjs'],
|
||||
minify: true,
|
||||
// if false: causes Collection.constructor to be a minified value like: 'o'
|
||||
keepNames: true,
|
||||
skipNodeModulesBundle: true,
|
||||
sourcemap: true,
|
||||
target: 'es2021',
|
||||
};
|
||||
Reference in New Issue
Block a user