@fluxerjs/collection 1.0.2

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.
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Extended Map with utility methods for key-value collections.
3
+ * Similar to discord.js Collection.
4
+ */
5
+ declare class Collection<K, V> extends Map<K, V> {
6
+ /**
7
+ * Obtains the first value(s) in this collection.
8
+ * @param amount - Amount of values to obtain (optional)
9
+ */
10
+ first(): V | undefined;
11
+ first(amount: number): V[];
12
+ /**
13
+ * Obtains the last value(s) in this collection.
14
+ * @param amount - Amount of values to obtain (optional)
15
+ */
16
+ last(): V | undefined;
17
+ last(amount: number): V[];
18
+ /**
19
+ * Returns a random value from this collection.
20
+ * @param amount - Amount of values to randomly obtain (optional)
21
+ */
22
+ random(): V | undefined;
23
+ random(amount: number): V[];
24
+ /**
25
+ * Searches for a single item where the given function returns a truthy value.
26
+ */
27
+ find(fn: (value: V, key: K) => boolean): V | undefined;
28
+ /**
29
+ * Returns the key of the first item where the given function returns a truthy value.
30
+ */
31
+ findKey(fn: (value: V, key: K) => boolean): K | undefined;
32
+ /**
33
+ * Creates a new collection with all elements that pass the test implemented by the provided function.
34
+ */
35
+ filter(fn: (value: V, key: K) => boolean): Collection<K, V>;
36
+ /**
37
+ * Creates a new array with the results of calling the provided function on every element.
38
+ */
39
+ map<T>(fn: (value: V, key: K) => T): T[];
40
+ /**
41
+ * Tests whether at least one element passes the test implemented by the provided function.
42
+ */
43
+ some(fn: (value: V, key: K) => boolean): boolean;
44
+ /**
45
+ * Tests whether all elements pass the test implemented by the provided function.
46
+ */
47
+ every(fn: (value: V, key: K) => boolean): boolean;
48
+ /**
49
+ * Applies a function against an accumulator and each element to reduce the collection to a single value.
50
+ */
51
+ reduce<T>(fn: (accumulator: T, value: V, key: K) => T, initialValue: T): T;
52
+ /**
53
+ * Partitions the collection into two collections: one that passes the predicate and one that fails.
54
+ */
55
+ partition(fn: (value: V, key: K) => boolean): [Collection<K, V>, Collection<K, V>];
56
+ /**
57
+ * Invokes the given function and returns this collection (for chaining).
58
+ */
59
+ tap(fn: (collection: this) => void): this;
60
+ /**
61
+ * Creates an identical shallow copy of this collection.
62
+ */
63
+ clone(): Collection<K, V>;
64
+ /**
65
+ * Combines this collection with others into a new collection.
66
+ */
67
+ concat(...collections: ReadonlyCollection<K, V>[]): Collection<K, V>;
68
+ /**
69
+ * Sorts the collection in place and returns it.
70
+ */
71
+ sort(compareFn?: (a: V, b: V, aKey: K, bKey: K) => number): this;
72
+ /**
73
+ * Returns an array of the values in this collection.
74
+ */
75
+ toJSON(): V[];
76
+ /**
77
+ * Returns a string representation of the collection (array of values).
78
+ */
79
+ toString(): string;
80
+ }
81
+ /**
82
+ * Read-only view of a Collection (e.g. for method return types).
83
+ */
84
+ type ReadonlyCollection<K, V> = Omit<Collection<K, V>, 'set' | 'delete' | 'clear'>;
85
+
86
+ export { Collection, type ReadonlyCollection };
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Extended Map with utility methods for key-value collections.
3
+ * Similar to discord.js Collection.
4
+ */
5
+ declare class Collection<K, V> extends Map<K, V> {
6
+ /**
7
+ * Obtains the first value(s) in this collection.
8
+ * @param amount - Amount of values to obtain (optional)
9
+ */
10
+ first(): V | undefined;
11
+ first(amount: number): V[];
12
+ /**
13
+ * Obtains the last value(s) in this collection.
14
+ * @param amount - Amount of values to obtain (optional)
15
+ */
16
+ last(): V | undefined;
17
+ last(amount: number): V[];
18
+ /**
19
+ * Returns a random value from this collection.
20
+ * @param amount - Amount of values to randomly obtain (optional)
21
+ */
22
+ random(): V | undefined;
23
+ random(amount: number): V[];
24
+ /**
25
+ * Searches for a single item where the given function returns a truthy value.
26
+ */
27
+ find(fn: (value: V, key: K) => boolean): V | undefined;
28
+ /**
29
+ * Returns the key of the first item where the given function returns a truthy value.
30
+ */
31
+ findKey(fn: (value: V, key: K) => boolean): K | undefined;
32
+ /**
33
+ * Creates a new collection with all elements that pass the test implemented by the provided function.
34
+ */
35
+ filter(fn: (value: V, key: K) => boolean): Collection<K, V>;
36
+ /**
37
+ * Creates a new array with the results of calling the provided function on every element.
38
+ */
39
+ map<T>(fn: (value: V, key: K) => T): T[];
40
+ /**
41
+ * Tests whether at least one element passes the test implemented by the provided function.
42
+ */
43
+ some(fn: (value: V, key: K) => boolean): boolean;
44
+ /**
45
+ * Tests whether all elements pass the test implemented by the provided function.
46
+ */
47
+ every(fn: (value: V, key: K) => boolean): boolean;
48
+ /**
49
+ * Applies a function against an accumulator and each element to reduce the collection to a single value.
50
+ */
51
+ reduce<T>(fn: (accumulator: T, value: V, key: K) => T, initialValue: T): T;
52
+ /**
53
+ * Partitions the collection into two collections: one that passes the predicate and one that fails.
54
+ */
55
+ partition(fn: (value: V, key: K) => boolean): [Collection<K, V>, Collection<K, V>];
56
+ /**
57
+ * Invokes the given function and returns this collection (for chaining).
58
+ */
59
+ tap(fn: (collection: this) => void): this;
60
+ /**
61
+ * Creates an identical shallow copy of this collection.
62
+ */
63
+ clone(): Collection<K, V>;
64
+ /**
65
+ * Combines this collection with others into a new collection.
66
+ */
67
+ concat(...collections: ReadonlyCollection<K, V>[]): Collection<K, V>;
68
+ /**
69
+ * Sorts the collection in place and returns it.
70
+ */
71
+ sort(compareFn?: (a: V, b: V, aKey: K, bKey: K) => number): this;
72
+ /**
73
+ * Returns an array of the values in this collection.
74
+ */
75
+ toJSON(): V[];
76
+ /**
77
+ * Returns a string representation of the collection (array of values).
78
+ */
79
+ toString(): string;
80
+ }
81
+ /**
82
+ * Read-only view of a Collection (e.g. for method return types).
83
+ */
84
+ type ReadonlyCollection<K, V> = Omit<Collection<K, V>, 'set' | 'delete' | 'clear'>;
85
+
86
+ export { Collection, type ReadonlyCollection };
package/dist/index.js ADDED
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Collection: () => Collection
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/Collection.ts
28
+ var Collection = class _Collection extends Map {
29
+ first(amount) {
30
+ if (amount === void 0) return this.values().next().value;
31
+ const arr = this.toJSON();
32
+ if (amount <= 0) return [];
33
+ return arr.slice(0, amount);
34
+ }
35
+ last(amount) {
36
+ const arr = this.toJSON();
37
+ if (amount === void 0) return arr[arr.length - 1];
38
+ if (amount <= 0) return [];
39
+ return arr.slice(-amount);
40
+ }
41
+ random(amount) {
42
+ const arr = this.toJSON();
43
+ if (arr.length === 0) return amount === void 0 ? void 0 : [];
44
+ if (amount === void 0) return arr[Math.floor(Math.random() * arr.length)];
45
+ const copy = [...arr];
46
+ const result = [];
47
+ for (let i = 0; i < Math.min(amount, arr.length); i++) {
48
+ const index = Math.floor(Math.random() * copy.length);
49
+ result.push(copy.splice(index, 1)[0]);
50
+ }
51
+ return result;
52
+ }
53
+ /**
54
+ * Searches for a single item where the given function returns a truthy value.
55
+ */
56
+ find(fn) {
57
+ for (const [key, value] of this) {
58
+ if (fn(value, key)) return value;
59
+ }
60
+ return void 0;
61
+ }
62
+ /**
63
+ * Returns the key of the first item where the given function returns a truthy value.
64
+ */
65
+ findKey(fn) {
66
+ for (const [key, value] of this) {
67
+ if (fn(value, key)) return key;
68
+ }
69
+ return void 0;
70
+ }
71
+ /**
72
+ * Creates a new collection with all elements that pass the test implemented by the provided function.
73
+ */
74
+ filter(fn) {
75
+ const result = new _Collection();
76
+ for (const [key, value] of this) {
77
+ if (fn(value, key)) result.set(key, value);
78
+ }
79
+ return result;
80
+ }
81
+ /**
82
+ * Creates a new array with the results of calling the provided function on every element.
83
+ */
84
+ map(fn) {
85
+ const result = [];
86
+ for (const [key, value] of this) {
87
+ result.push(fn(value, key));
88
+ }
89
+ return result;
90
+ }
91
+ /**
92
+ * Tests whether at least one element passes the test implemented by the provided function.
93
+ */
94
+ some(fn) {
95
+ for (const [key, value] of this) {
96
+ if (fn(value, key)) return true;
97
+ }
98
+ return false;
99
+ }
100
+ /**
101
+ * Tests whether all elements pass the test implemented by the provided function.
102
+ */
103
+ every(fn) {
104
+ for (const [key, value] of this) {
105
+ if (!fn(value, key)) return false;
106
+ }
107
+ return true;
108
+ }
109
+ /**
110
+ * Applies a function against an accumulator and each element to reduce the collection to a single value.
111
+ */
112
+ reduce(fn, initialValue) {
113
+ let acc = initialValue;
114
+ for (const [key, value] of this) {
115
+ acc = fn(acc, value, key);
116
+ }
117
+ return acc;
118
+ }
119
+ /**
120
+ * Partitions the collection into two collections: one that passes the predicate and one that fails.
121
+ */
122
+ partition(fn) {
123
+ const pass = new _Collection();
124
+ const fail = new _Collection();
125
+ for (const [key, value] of this) {
126
+ if (fn(value, key)) pass.set(key, value);
127
+ else fail.set(key, value);
128
+ }
129
+ return [pass, fail];
130
+ }
131
+ /**
132
+ * Invokes the given function and returns this collection (for chaining).
133
+ */
134
+ tap(fn) {
135
+ fn(this);
136
+ return this;
137
+ }
138
+ /**
139
+ * Creates an identical shallow copy of this collection.
140
+ */
141
+ clone() {
142
+ return new _Collection(this);
143
+ }
144
+ /**
145
+ * Combines this collection with others into a new collection.
146
+ */
147
+ concat(...collections) {
148
+ const result = this.clone();
149
+ for (const coll of collections) {
150
+ for (const [key, value] of coll) result.set(key, value);
151
+ }
152
+ return result;
153
+ }
154
+ /**
155
+ * Sorts the collection in place and returns it.
156
+ */
157
+ sort(compareFn) {
158
+ const entries = [...this.entries()].sort((a, b) => {
159
+ if (compareFn) return compareFn(a[1], b[1], a[0], b[0]);
160
+ return 0;
161
+ });
162
+ this.clear();
163
+ for (const [key, value] of entries) {
164
+ this.set(key, value);
165
+ }
166
+ return this;
167
+ }
168
+ /**
169
+ * Returns an array of the values in this collection.
170
+ */
171
+ toJSON() {
172
+ return [...this.values()];
173
+ }
174
+ /**
175
+ * Returns a string representation of the collection (array of values).
176
+ */
177
+ toString() {
178
+ return `Collection(${this.size})`;
179
+ }
180
+ };
181
+ // Annotate the CommonJS export names for ESM import in node:
182
+ 0 && (module.exports = {
183
+ Collection
184
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,157 @@
1
+ // src/Collection.ts
2
+ var Collection = class _Collection extends Map {
3
+ first(amount) {
4
+ if (amount === void 0) return this.values().next().value;
5
+ const arr = this.toJSON();
6
+ if (amount <= 0) return [];
7
+ return arr.slice(0, amount);
8
+ }
9
+ last(amount) {
10
+ const arr = this.toJSON();
11
+ if (amount === void 0) return arr[arr.length - 1];
12
+ if (amount <= 0) return [];
13
+ return arr.slice(-amount);
14
+ }
15
+ random(amount) {
16
+ const arr = this.toJSON();
17
+ if (arr.length === 0) return amount === void 0 ? void 0 : [];
18
+ if (amount === void 0) return arr[Math.floor(Math.random() * arr.length)];
19
+ const copy = [...arr];
20
+ const result = [];
21
+ for (let i = 0; i < Math.min(amount, arr.length); i++) {
22
+ const index = Math.floor(Math.random() * copy.length);
23
+ result.push(copy.splice(index, 1)[0]);
24
+ }
25
+ return result;
26
+ }
27
+ /**
28
+ * Searches for a single item where the given function returns a truthy value.
29
+ */
30
+ find(fn) {
31
+ for (const [key, value] of this) {
32
+ if (fn(value, key)) return value;
33
+ }
34
+ return void 0;
35
+ }
36
+ /**
37
+ * Returns the key of the first item where the given function returns a truthy value.
38
+ */
39
+ findKey(fn) {
40
+ for (const [key, value] of this) {
41
+ if (fn(value, key)) return key;
42
+ }
43
+ return void 0;
44
+ }
45
+ /**
46
+ * Creates a new collection with all elements that pass the test implemented by the provided function.
47
+ */
48
+ filter(fn) {
49
+ const result = new _Collection();
50
+ for (const [key, value] of this) {
51
+ if (fn(value, key)) result.set(key, value);
52
+ }
53
+ return result;
54
+ }
55
+ /**
56
+ * Creates a new array with the results of calling the provided function on every element.
57
+ */
58
+ map(fn) {
59
+ const result = [];
60
+ for (const [key, value] of this) {
61
+ result.push(fn(value, key));
62
+ }
63
+ return result;
64
+ }
65
+ /**
66
+ * Tests whether at least one element passes the test implemented by the provided function.
67
+ */
68
+ some(fn) {
69
+ for (const [key, value] of this) {
70
+ if (fn(value, key)) return true;
71
+ }
72
+ return false;
73
+ }
74
+ /**
75
+ * Tests whether all elements pass the test implemented by the provided function.
76
+ */
77
+ every(fn) {
78
+ for (const [key, value] of this) {
79
+ if (!fn(value, key)) return false;
80
+ }
81
+ return true;
82
+ }
83
+ /**
84
+ * Applies a function against an accumulator and each element to reduce the collection to a single value.
85
+ */
86
+ reduce(fn, initialValue) {
87
+ let acc = initialValue;
88
+ for (const [key, value] of this) {
89
+ acc = fn(acc, value, key);
90
+ }
91
+ return acc;
92
+ }
93
+ /**
94
+ * Partitions the collection into two collections: one that passes the predicate and one that fails.
95
+ */
96
+ partition(fn) {
97
+ const pass = new _Collection();
98
+ const fail = new _Collection();
99
+ for (const [key, value] of this) {
100
+ if (fn(value, key)) pass.set(key, value);
101
+ else fail.set(key, value);
102
+ }
103
+ return [pass, fail];
104
+ }
105
+ /**
106
+ * Invokes the given function and returns this collection (for chaining).
107
+ */
108
+ tap(fn) {
109
+ fn(this);
110
+ return this;
111
+ }
112
+ /**
113
+ * Creates an identical shallow copy of this collection.
114
+ */
115
+ clone() {
116
+ return new _Collection(this);
117
+ }
118
+ /**
119
+ * Combines this collection with others into a new collection.
120
+ */
121
+ concat(...collections) {
122
+ const result = this.clone();
123
+ for (const coll of collections) {
124
+ for (const [key, value] of coll) result.set(key, value);
125
+ }
126
+ return result;
127
+ }
128
+ /**
129
+ * Sorts the collection in place and returns it.
130
+ */
131
+ sort(compareFn) {
132
+ const entries = [...this.entries()].sort((a, b) => {
133
+ if (compareFn) return compareFn(a[1], b[1], a[0], b[0]);
134
+ return 0;
135
+ });
136
+ this.clear();
137
+ for (const [key, value] of entries) {
138
+ this.set(key, value);
139
+ }
140
+ return this;
141
+ }
142
+ /**
143
+ * Returns an array of the values in this collection.
144
+ */
145
+ toJSON() {
146
+ return [...this.values()];
147
+ }
148
+ /**
149
+ * Returns a string representation of the collection (array of values).
150
+ */
151
+ toString() {
152
+ return `Collection(${this.size})`;
153
+ }
154
+ };
155
+ export {
156
+ Collection
157
+ };
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@fluxerjs/collection",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "1.0.2",
7
+ "description": "Extended Map with utility methods for @fluxerjs/core",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.mjs",
15
+ "require": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "devDependencies": {
22
+ "tsup": "^8.3.0",
23
+ "typescript": "^5.6.0"
24
+ },
25
+ "scripts": {
26
+ "build": "tsup src/index.ts --format cjs,esm --dts",
27
+ "clean": "rm -rf dist"
28
+ }
29
+ }