Skip to content

Commit

Permalink
support count() without predicate function
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuspoehls committed Feb 4, 2024
1 parent 88e8b63 commit 692fa01
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 89 deletions.
95 changes: 6 additions & 89 deletions src/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Create a new set instance.
*
* @param items
*/
constructor (...items: Values<T>) {
this.set = new Set()
Expand All @@ -23,27 +21,20 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Create a new set instance of the given `values`.
*
* @param {Iterable} values
*
*/
static from<T> (...values: Values<T>): SuperchargedSet<T> {
return new this<T>(...values)
}

/**
* Returns an iterable of the values in the set.
*
*/
[Symbol.iterator] (): IterableIterator<T> {
return this.values()
}

/**
* Adds the given `values` to the set.
*
* @param {*} values
*
*/
add (...values: Values<T>): this {
for (const value of this.resolveValues(...values)) {
Expand All @@ -57,9 +48,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Returns a flat array of items removing `undefined` and `null` values.
*
* @param values
*
*/
private resolveValues (...values: Values<T>): T[] {
return values
Expand All @@ -85,19 +73,13 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Determine whether the given `value` is iterable.
*
* @param value
*
*/
private isIterable (value: any): value is Iterable<T> {
return Array.from(value).length > 0
}

/**
* Determine whether all items in the set match the given `predicate` function.
*
* @param {Function} predicate
*
*/
all (predicate: (item: T, index: number, set: SuperchargedSet<T>) => unknown): boolean {
return this.toArray().every((value, index) => {
Expand All @@ -107,19 +89,13 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* 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<T>) => unknown): boolean {
return this.findIndex(predicate) !== -1
}

/**
* 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
Expand All @@ -137,7 +113,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Clears the set by removing all items.
*
*/
clear (): this {
this.set.clear()
Expand All @@ -147,9 +122,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Appends values to the end of the array.
*
* @param {*} values
*
*/
concat (...values: Array<T | T[]>): SuperchargedSet<T> {
return SuperchargedSet.from(
Expand All @@ -158,20 +130,17 @@ export class SuperchargedSet<T> implements Iterable<T> {
}

/**
* 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<S extends T> (predicate: (item: T, index: number, set: SuperchargedSet<T>) => item is S): number {
return this.filter(predicate).size()
count<S extends T> (predicate?: (item: T, index: number, set: SuperchargedSet<T>) => 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<T> {
this.set = new Set(
Expand All @@ -185,9 +154,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Returns a set containing only items matching the given `predicate`.
*
* @param {Function} predicate
*
*/
filter (predicate: (item: T, index: number, set: SuperchargedSet<T>) => unknown): SuperchargedSet<T>
filter<S extends T> (predicate: (item: T, index: number, set: SuperchargedSet<T>) => item is S): SuperchargedSet<T>
Expand All @@ -202,9 +168,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
/**
* 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<T>) => unknown): T | undefined
find<S extends T> (predicate: (item: T, index: number, set: SuperchargedSet<T>) => item is S): S | undefined
Expand All @@ -217,9 +180,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
/**
* 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<T>) => unknown): number
findIndex<S extends T> (predicate: (item: T, index: number, set: SuperchargedSet<T>) => item is S): number
Expand All @@ -232,9 +192,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
/**
* 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<S extends T> (predicate: (item: T, index: number, set: SuperchargedSet<T>) => item is S): S | undefined
findLast (predicate: (item: T, index: number, set: SuperchargedSet<T>) => unknown): T | undefined
Expand All @@ -253,9 +210,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
/**
* Returns the index of the last item in the set that matches the
* given `predicate` function. Returns -1 otherwise.
*
* @param {Function} predicate
*
*/
findLastIndex<S extends T> (predicate: (item: T, index: number, set: SuperchargedSet<T>) => item is S): number
findLastIndex (predicate: (item: T, index: number, set: SuperchargedSet<T>) => unknown): number
Expand All @@ -275,9 +229,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
* 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<T>) => unknown): T | undefined {
return predicate
Expand All @@ -290,9 +241,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
* 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<T>) => unknown): T | undefined {
return predicate
Expand All @@ -304,9 +252,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
* 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<R> (transform: (item: T, index: number, set: SuperchargedSet<T>) => R): SuperchargedSet<R> {
return this.map<R>((item, index) => {
Expand All @@ -316,7 +261,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Flattens the items in the set one level deep.
*
*/
flatten (): SuperchargedSet<T> {
return SuperchargedSet.from(
Expand All @@ -326,8 +270,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Runs the given `action` on each item in the set.
*
* @param {Function} action
*/
forEach (action: (item: T, index: number, set: SuperchargedSet<T>) => void): void {
this.toArray().forEach((item, index) => {
Expand All @@ -337,9 +279,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Determine whether the set contains the given `value`.
*
* @param {*} value
*
*/
has (value: T): boolean {
return !!this.find((item: T) => {
Expand All @@ -350,9 +289,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
/**
* 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
Expand All @@ -368,9 +304,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Returns a set containing all items that are contained in all collections, this set and and the given `sets`.
*
* @param {Array<Iterable<T>>} sets
*
*/
intersect (...sets: Array<Iterable<T>>): SuperchargedSet<T> {
return this.filter((value) => {
Expand All @@ -388,35 +321,27 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Determine whether the set is empty (contains no entries).
*
*/
isEmpty (): boolean {
return this.size() === 0
}

/**
* Determine whether the set does not contain the given `value`.
*
* @param {*} value
*
*/
isMissing (value: T): boolean {
return !this.has(value)
}

/**
* Determine whether the set is not empty (contains entries).
*
*/
isNotEmpty (): boolean {
return !this.isEmpty()
}

/**
* 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<T>) => string): string
Expand All @@ -429,9 +354,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
/**
* Returns a new set instance containing the results of applying the
* given `transform` function to each item in the set.
*
* @param {Function} transform
*
*/
map<R> (transform: (item: T, index: number, set: SuperchargedSet<T>) => R): SuperchargedSet<R> {
return SuperchargedSet.from(
Expand All @@ -445,10 +367,6 @@ export class SuperchargedSet<T> implements Iterable<T> {
* 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<U>(operation: (previous: U, current: T, index: number, set: SuperchargedSet<T>) => U, initial: U): U {
return this.toArray().reduce((carry, value, index) => {
Expand All @@ -458,7 +376,6 @@ export class SuperchargedSet<T> implements Iterable<T> {

/**
* Returns a set containing the items in reversed order.
*
*/
reverse (): SuperchargedSet<T> {
return SuperchargedSet.from(
Expand Down
7 changes: 7 additions & 0 deletions test/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 692fa01

Please sign in to comment.