Skip to content

Commit 8d8023a

Browse files
committed
feat(collection): added is method
1 parent f9b3eb5 commit 8d8023a

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

docs/helpers/collection.md

+14
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ numCollection.shuffle(); // Collection[3, 1, 4, 2, 5]
7171
numCollection.shuffle(); // Collection[2, 5, 1, 4, 3]
7272
```
7373

74+
#### is
75+
76+
The `is` method determines if the collection is equal to the given collection. It uses deep equality to ensure every element in the collection is equal to the given collection in the same order.
77+
78+
```js
79+
import { Collection } from '@upfrontjs/framework';
80+
81+
const collection = new Collection([1, 2, 3, 4, 5]);
82+
collection.is(collection); // true
83+
collection.is(new Collection([1, 2, 3, 4, 5])); // true
84+
collection.is(collection.slice(0, 1)); // false
85+
(new Collection([{ id: 1 }])).is(new Collection([{ id: 2 }])); // false
86+
```
87+
7488
#### isEmpty
7589

7690
The `isEmpty` method determines whether the collection is empty or not.

src/Support/Collection.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,33 @@ export default class Collection<T> implements Jsonable, Arrayable<T>, Iterable<T
174174
* {@link https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm|Durstenfeld algorithm}.
175175
*/
176176
public shuffle(): this {
177+
if (this.length <= 1) {
178+
return this._newInstance(this.toArray());
179+
}
180+
177181
const items = this.toArray();
178182

179183
for (let i = items.length - 1; i > 0; i--) {
180184
const j = Math.floor(Math.random() * (i + 1));
181185
[items[i], items[j]] = [items[j]!, items[i]!];
182186
}
183187

184-
return this._newInstance(items);
188+
const result = this._newInstance(items);
189+
190+
return this.is(result) ? this.shuffle() : result;
191+
}
192+
193+
/**
194+
* Check that the collection the same as the given one.
195+
*
196+
* @param value
197+
*
198+
* @return {this}
199+
*/
200+
public is(value: unknown): value is this {
201+
return value instanceof this.constructor &&
202+
(value as Collection<any>).length === this.length &&
203+
this.every((item, index) => isEqual(item, (value as Collection<any>)[index]));
185204
}
186205

187206
/**
@@ -725,10 +744,10 @@ export default class Collection<T> implements Jsonable, Arrayable<T>, Iterable<T
725744
*
726745
* @throws {Error}
727746
*/
728-
public pluck<Keys extends (Readonly<keyof T> | keyof T)[]>(
747+
public pluck<Keys extends (Readonly<keyof T> )[]>(
729748
properties: Keys
730749
): Collection<Pick<T, Keys[number]>>;
731-
public pluck<Keys extends Readonly<keyof T> | keyof T>(
750+
public pluck<Keys extends Readonly<keyof T> >(
732751
properties: Keys
733752
): Collection<Pick<T, Keys>>;
734753
public pluck<V>(properties: MaybeArray<string>): Collection<V>;

tests/Support/Collection.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ describe('Collection', () => {
138138
});
139139
});
140140

141+
describe('is()', () => {
142+
const elements = [1, 2, 3, 4, 5];
143+
144+
beforeEach(() => {
145+
collection = new Collection(elements);
146+
});
147+
148+
it('should assert if the collection is equal to another collection', () => {
149+
expect(collection.is(new Collection(elements))).toBe(true);
150+
expect(collection.is(collection)).toBe(true);
151+
expect(collection.is(collection.slice(0, 1))).toBe(false);
152+
});
153+
});
154+
141155
describe('isEmpty()', () => {
142156
const elements = [1, 2, 3, 4, 5];
143157

0 commit comments

Comments
 (0)