From 692fa01d6fb6c98724562441ed7ff10a945bb723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20P=C3=B6hls?= Date: Sun, 4 Feb 2024 05:38:58 +0100 Subject: [PATCH] support count() without predicate function --- src/set.ts | 95 ++++------------------------------------------------- test/set.js | 7 ++++ 2 files changed, 13 insertions(+), 89 deletions(-) diff --git a/src/set.ts b/src/set.ts index cf3d0a8..acd934e 100644 --- a/src/set.ts +++ b/src/set.ts @@ -12,8 +12,6 @@ export class SuperchargedSet implements Iterable { /** * Create a new set instance. - * - * @param items */ constructor (...items: Values) { this.set = new Set() @@ -23,9 +21,6 @@ export class SuperchargedSet implements Iterable { /** * Create a new set instance of the given `values`. - * - * @param {Iterable} values - * */ static from (...values: Values): SuperchargedSet { return new this(...values) @@ -33,7 +28,6 @@ export class SuperchargedSet implements Iterable { /** * Returns an iterable of the values in the set. - * */ [Symbol.iterator] (): IterableIterator { return this.values() @@ -41,9 +35,6 @@ export class SuperchargedSet implements Iterable { /** * Adds the given `values` to the set. - * - * @param {*} values - * */ add (...values: Values): this { for (const value of this.resolveValues(...values)) { @@ -57,9 +48,6 @@ export class SuperchargedSet implements Iterable { /** * Returns a flat array of items removing `undefined` and `null` values. - * - * @param values - * */ private resolveValues (...values: Values): T[] { return values @@ -85,9 +73,6 @@ export class SuperchargedSet implements Iterable { /** * Determine whether the given `value` is iterable. - * - * @param value - * */ private isIterable (value: any): value is Iterable { return Array.from(value).length > 0 @@ -95,9 +80,6 @@ export class SuperchargedSet implements Iterable { /** * Determine whether all items in the set match the given `predicate` function. - * - * @param {Function} predicate - * */ all (predicate: (item: T, index: number, set: SuperchargedSet) => unknown): boolean { return this.toArray().every((value, index) => { @@ -107,9 +89,6 @@ export class SuperchargedSet implements Iterable { /** * Determine whether at least one item in the set matches the given `predicate` function. - * - * @param {Function} predicate - * */ any (predicate: (item: T, index: number, set: SuperchargedSet) => unknown): boolean { return this.findIndex(predicate) !== -1 @@ -117,9 +96,6 @@ export class SuperchargedSet implements Iterable { /** * Returns the value at the given `index` or undefined if the index exceeds the set’s size. - * - * @param {Number} index - * */ at (index: number): T | undefined { index = Math.trunc(index) || 0 @@ -137,7 +113,6 @@ export class SuperchargedSet implements Iterable { /** * Clears the set by removing all items. - * */ clear (): this { this.set.clear() @@ -147,9 +122,6 @@ export class SuperchargedSet implements Iterable { /** * Appends values to the end of the array. - * - * @param {*} values - * */ concat (...values: Array): SuperchargedSet { return SuperchargedSet.from( @@ -158,20 +130,17 @@ export class SuperchargedSet implements Iterable { } /** - * Returns the number of items matching the given `predicate`. - * - * @param {Function} predicate - * + * Returns the number of items matching the given `predicate`. Returns the + * set’s `size` if you don’t provide a predicate function. */ - count (predicate: (item: T, index: number, set: SuperchargedSet) => item is S): number { - return this.filter(predicate).size() + count (predicate?: (item: T, index: number, set: SuperchargedSet) => item is S): number { + return typeof predicate === 'function' + ? this.filter(predicate).size() + : this.size() } /** * Delete the given `value` from the set. - * - * @param {*} value - * */ delete (value: T): SuperchargedSet { this.set = new Set( @@ -185,9 +154,6 @@ export class SuperchargedSet implements Iterable { /** * Returns a set containing only items matching the given `predicate`. - * - * @param {Function} predicate - * */ filter (predicate: (item: T, index: number, set: SuperchargedSet) => unknown): SuperchargedSet filter (predicate: (item: T, index: number, set: SuperchargedSet) => item is S): SuperchargedSet @@ -202,9 +168,6 @@ export class SuperchargedSet implements Iterable { /** * Returns the first item in the set matching the given `predicate` * function, or `undefined` if no such item was found. - * - * @param {Function} predicate - * */ find (predicate: (item: T, index: number, set: SuperchargedSet) => unknown): T | undefined find (predicate: (item: T, index: number, set: SuperchargedSet) => item is S): S | undefined @@ -217,9 +180,6 @@ export class SuperchargedSet implements Iterable { /** * Returns the index of the first element in the set where the * given `predicate` function is `true`. Returns -1 otherwise. - * - * @param {Function} predicate - * */ findIndex (predicate: (item: T, index: number, set: SuperchargedSet) => unknown): number findIndex (predicate: (item: T, index: number, set: SuperchargedSet) => item is S): number @@ -232,9 +192,6 @@ export class SuperchargedSet implements Iterable { /** * Returns the last item in the set matching the given `predicate` * function. Returns `undefined` if no item was found in the set. - * - * @param {Function} predicate - * */ findLast (predicate: (item: T, index: number, set: SuperchargedSet) => item is S): S | undefined findLast (predicate: (item: T, index: number, set: SuperchargedSet) => unknown): T | undefined @@ -253,9 +210,6 @@ export class SuperchargedSet implements Iterable { /** * Returns the index of the last item in the set that matches the * given `predicate` function. Returns -1 otherwise. - * - * @param {Function} predicate - * */ findLastIndex (predicate: (item: T, index: number, set: SuperchargedSet) => item is S): number findLastIndex (predicate: (item: T, index: number, set: SuperchargedSet) => unknown): number @@ -275,9 +229,6 @@ export class SuperchargedSet implements Iterable { * Returns the first element of the set or returns the first item in * the set matching the given `predicate` function. Returns * `undefined` if no matching item is found or available. - * - * @param {Function} predicate - * */ first (predicate?: (item: T, index: number, set: SuperchargedSet) => unknown): T | undefined { return predicate @@ -290,9 +241,6 @@ export class SuperchargedSet implements Iterable { * the set matching the given `predicate` function. Returns * `undefined` if no matching item is found or available. If no predicate * is given then the last item in the set is returned. - * - * @param {Function} predicate - * */ last (predicate?: (item: T, index: number, set: SuperchargedSet) => unknown): T | undefined { return predicate @@ -304,9 +252,6 @@ export class SuperchargedSet implements Iterable { * Returns a new set instance containing the results of applying the * given `transform` function to each item in the set. Ultimately, * it flattens the mapped results one level deep. - * - * @param {Function} transform - * */ flatMap (transform: (item: T, index: number, set: SuperchargedSet) => R): SuperchargedSet { return this.map((item, index) => { @@ -316,7 +261,6 @@ export class SuperchargedSet implements Iterable { /** * Flattens the items in the set one level deep. - * */ flatten (): SuperchargedSet { return SuperchargedSet.from( @@ -326,8 +270,6 @@ export class SuperchargedSet implements Iterable { /** * Runs the given `action` on each item in the set. - * - * @param {Function} action */ forEach (action: (item: T, index: number, set: SuperchargedSet) => void): void { this.toArray().forEach((item, index) => { @@ -337,9 +279,6 @@ export class SuperchargedSet implements Iterable { /** * Determine whether the set contains the given `value`. - * - * @param {*} value - * */ has (value: T): boolean { return !!this.find((item: T) => { @@ -350,9 +289,6 @@ export class SuperchargedSet implements Iterable { /** * Returns true if an item in the set matches * the given `value` or `predicate` - * - * @param {Function|T} valueOrPredicate - * */ includes (value: T): boolean includes (predicate: (item: T, index: number) => boolean): boolean @@ -368,9 +304,6 @@ export class SuperchargedSet implements Iterable { /** * Returns a set containing all items that are contained in all collections, this set and and the given `sets`. - * - * @param {Array>} sets - * */ intersect (...sets: Array>): SuperchargedSet { return this.filter((value) => { @@ -388,7 +321,6 @@ export class SuperchargedSet implements Iterable { /** * Determine whether the set is empty (contains no entries). - * */ isEmpty (): boolean { return this.size() === 0 @@ -396,9 +328,6 @@ export class SuperchargedSet implements Iterable { /** * Determine whether the set does not contain the given `value`. - * - * @param {*} value - * */ isMissing (value: T): boolean { return !this.has(value) @@ -406,7 +335,6 @@ export class SuperchargedSet implements Iterable { /** * Determine whether the set is not empty (contains entries). - * */ isNotEmpty (): boolean { return !this.isEmpty() @@ -414,9 +342,6 @@ export class SuperchargedSet implements Iterable { /** * Returns a string by concatenating all of the items using the given `separator`. - * - * @param {String} separator - * */ join (separator?: string): string join (separator?: (item: T, index: number, set: SuperchargedSet) => string): string @@ -429,9 +354,6 @@ export class SuperchargedSet implements Iterable { /** * Returns a new set instance containing the results of applying the * given `transform` function to each item in the set. - * - * @param {Function} transform - * */ map (transform: (item: T, index: number, set: SuperchargedSet) => R): SuperchargedSet { return SuperchargedSet.from( @@ -445,10 +367,6 @@ export class SuperchargedSet implements Iterable { * Invokes the `operation` function on each item in the set. The return value * of the operation function is the accumulated result, and is provided as * an argument in the next call to the operation function. - * - * @param operation - * @param initial - * */ reduce(operation: (previous: U, current: T, index: number, set: SuperchargedSet) => U, initial: U): U { return this.toArray().reduce((carry, value, index) => { @@ -458,7 +376,6 @@ export class SuperchargedSet implements Iterable { /** * Returns a set containing the items in reversed order. - * */ reverse (): SuperchargedSet { return SuperchargedSet.from( diff --git a/test/set.js b/test/set.js index a8e12b9..927afc5 100644 --- a/test/set.js +++ b/test/set.js @@ -362,6 +362,13 @@ test('concat', () => { }) test('count', () => { + expect( + Set + .from([1, 2, 3, 4, 5]) + .map(num => num * 2) + .count() + ).toEqual(5) + expect( Set .from([1, 2, 3, 4, 5])