@homebound/activesupport
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -9,2 +9,3 @@ type KeysOfType<T, TProp> = { | ||
uniqueByKey(key: keyof T): Array<T>; | ||
uniqueBy(f: (el: T, index: number, array: T[]) => PropertyKey): Array<T>; | ||
compact(): Array<NonNullable<T>>; | ||
@@ -37,2 +38,3 @@ isEmpty: boolean; | ||
asyncMap<V>(f: (el: T, index: number, array: T[]) => Promise<V>): Promise<V[]>; | ||
asyncFlatMap<V>(f: (el: T, index: number, array: T[]) => Promise<V | V[]>): Promise<V[]>; | ||
asyncForEach(f: (el: T, index: number, array: T[]) => Promise<any>): Promise<void>; | ||
@@ -80,2 +82,5 @@ sum(this: Array<number | undefined>): number; | ||
interface ReadonlyArray<T> { | ||
unique(): Array<T>; | ||
uniqueByKey(key: keyof T): Array<T>; | ||
uniqueBy(f: (el: T, index: number, array: T[]) => PropertyKey): Array<T>; | ||
compact(): Array<NonNullable<T>>; | ||
@@ -107,2 +112,3 @@ isEmpty: boolean; | ||
asyncMap<V>(f: (el: T, index: number, array: T[]) => Promise<V>): Promise<V[]>; | ||
asyncFlatMap<V>(f: (el: T, index: number, array: T[]) => Promise<V | V[]>): Promise<V[]>; | ||
asyncForEach(f: (el: T, index: number, array: T[]) => Promise<any>): Promise<void>; | ||
@@ -109,0 +115,0 @@ sum(this: ReadonlyArray<number | undefined>): number; |
@@ -27,10 +27,17 @@ "use strict"; | ||
}; | ||
Array.prototype.uniqueByKey = function(key) { | ||
Array.prototype.uniqueBy = function(f) { | ||
const result = []; | ||
const group = this.groupBy((item) => item[key]); | ||
Object.keys(group).forEach((gKey) => { | ||
result.push(group[gKey].first); | ||
}); | ||
const set = /* @__PURE__ */ new Set(); | ||
for (let i = 0; i < this.length; i++) { | ||
const key = f(this[i], i, this); | ||
if (!set.has(key)) { | ||
result.push(this[i]); | ||
set.add(key); | ||
} | ||
} | ||
return result; | ||
}; | ||
Array.prototype.uniqueByKey = function(key) { | ||
return this.uniqueBy((el) => el[key]); | ||
}; | ||
Array.prototype.compact = function() { | ||
@@ -87,2 +94,5 @@ return this.filter(isDefined); | ||
}; | ||
Array.prototype.asyncFlatMap = async function(f) { | ||
return Promise.all(this.map(f)).then((result) => result.flat(1)); | ||
}; | ||
Array.prototype.asyncForEach = async function(f) { | ||
@@ -89,0 +99,0 @@ return Promise.all(this.map(f)).then(() => { |
{ | ||
"name": "@homebound/activesupport", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"main": "./dist/index.js", | ||
@@ -5,0 +5,0 @@ "module": "./dist/index.mjs", |
@@ -7,2 +7,3 @@ import type { KeysOfType } from "./utils"; | ||
uniqueByKey(key: keyof T): Array<T>; | ||
uniqueBy(f: (el: T, index: number, array: T[]) => PropertyKey): Array<T>; | ||
compact(): Array<NonNullable<T>>; | ||
@@ -35,2 +36,3 @@ isEmpty: boolean; | ||
asyncMap<V>(f: (el: T, index: number, array: T[]) => Promise<V>): Promise<V[]>; | ||
asyncFlatMap<V>(f: (el: T, index: number, array: T[]) => Promise<V | V[]>): Promise<V[]>; | ||
asyncForEach(f: (el: T, index: number, array: T[]) => Promise<any>): Promise<void>; | ||
@@ -79,2 +81,5 @@ sum(this: Array<number | undefined>): number; | ||
interface ReadonlyArray<T> { | ||
unique(): Array<T>; | ||
uniqueByKey(key: keyof T): Array<T>; | ||
uniqueBy(f: (el: T, index: number, array: T[]) => PropertyKey): Array<T>; | ||
compact(): Array<NonNullable<T>>; | ||
@@ -106,2 +111,3 @@ isEmpty: boolean; | ||
asyncMap<V>(f: (el: T, index: number, array: T[]) => Promise<V>): Promise<V[]>; | ||
asyncFlatMap<V>(f: (el: T, index: number, array: T[]) => Promise<V | V[]>): Promise<V[]>; | ||
asyncForEach(f: (el: T, index: number, array: T[]) => Promise<any>): Promise<void>; | ||
@@ -165,11 +171,19 @@ sum(this: ReadonlyArray<number | undefined>): number; | ||
/** Would be cool to allow an array of keys to make the criteria of "unique" more flexible */ | ||
Array.prototype.uniqueByKey = function <T>(key: keyof T): T[] { | ||
Array.prototype.uniqueBy = function <T>(f: (el: T, index: number, array: T[]) => PropertyKey): T[] { | ||
const result: T[] = []; | ||
const group = this.groupBy((item) => item[key]); | ||
Object.keys(group).forEach((gKey) => { | ||
result.push(group[gKey].first); | ||
}); | ||
const set = new Set<PropertyKey>(); | ||
for (let i = 0; i < this.length; i++) { | ||
const key = f(this[i], i, this); | ||
if (!set.has(key)) { | ||
result.push(this[i]); | ||
set.add(key); | ||
} | ||
} | ||
return result; | ||
}; | ||
Array.prototype.uniqueByKey = function <T>(key: keyof T): T[] { | ||
return this.uniqueBy((el) => el[key]); | ||
}; | ||
Array.prototype.compact = function () { | ||
@@ -244,2 +258,9 @@ return this.filter(isDefined); | ||
Array.prototype.asyncFlatMap = async function <T, V>( | ||
this: Array<T>, | ||
f: (el: T, index: number, array: T[]) => Promise<V | V[]>, | ||
): Promise<V[]> { | ||
return Promise.all(this.map(f)).then((result) => result.flat(1) as V[]); | ||
}; | ||
Array.prototype.asyncForEach = async function <T>( | ||
@@ -246,0 +267,0 @@ this: Array<T>, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
103995
1185