2020-12-03 04:06:41 +00:00
|
|
|
/** Enhanced Map with various utility functions */
|
2020-11-01 11:22:09 +00:00
|
|
|
export class Collection<K = string, V = any> extends Map<K, V> {
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Set a key to value in Collection */
|
2020-11-03 09:21:29 +00:00
|
|
|
set(key: K, value: V): this {
|
2020-10-31 11:45:33 +00:00
|
|
|
return super.set(key, value)
|
|
|
|
}
|
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Get Array of values in Collection */
|
2020-11-03 09:21:29 +00:00
|
|
|
array(): V[] {
|
2020-10-31 11:45:33 +00:00
|
|
|
return [...this.values()]
|
|
|
|
}
|
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Get first value in Collection */
|
2020-12-14 07:29:14 +00:00
|
|
|
first(): V | undefined;
|
|
|
|
first(amount: number): V[];
|
|
|
|
first(amount?: number): V | V[] | undefined {
|
|
|
|
if (typeof amount === 'undefined') return this.values().next().value
|
|
|
|
if (amount < 0) return this.last(amount * -1)
|
|
|
|
amount = Math.min(this.size, amount)
|
|
|
|
const iter = this.values()
|
|
|
|
return Array.from({ length: amount }, (): V => iter.next().value)
|
|
|
|
}
|
2020-10-31 11:45:33 +00:00
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Get last value in Collection */
|
2020-12-14 07:29:14 +00:00
|
|
|
last(): V | undefined;
|
|
|
|
last(amount: number): V[];
|
|
|
|
last(amount?: number): V | V[] | undefined {
|
|
|
|
const arr = this.array()
|
|
|
|
if (typeof amount === 'undefined') return arr[arr.length - 1]
|
|
|
|
if (amount < 0) return this.first(amount * -1)
|
|
|
|
if (!amount) return []
|
|
|
|
return arr.slice(-amount)
|
|
|
|
}
|
2020-10-31 11:45:33 +00:00
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Get a random value from Collection */
|
2020-12-14 07:29:14 +00:00
|
|
|
random(): V;
|
|
|
|
random(amount: number): V[];
|
|
|
|
random(amount?: number): V | V[] {
|
|
|
|
let arr = this.array()
|
|
|
|
if (typeof amount === 'undefined') return arr[Math.floor(Math.random() * arr.length)]
|
|
|
|
if (arr.length === 0 || !amount) return []
|
|
|
|
arr = arr.slice()
|
|
|
|
return Array.from({ length: amount }, (): V => arr.splice(Math.floor(Math.random() * arr.length), 1)[0])
|
|
|
|
}
|
2020-10-31 11:45:33 +00:00
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Find a value from Collection using callback */
|
2020-11-03 09:21:29 +00:00
|
|
|
find(callback: (value: V, key: K) => boolean): V | undefined {
|
2020-10-31 11:45:33 +00:00
|
|
|
for (const key of this.keys()) {
|
2020-11-03 09:21:29 +00:00
|
|
|
const value = this.get(key) as V
|
|
|
|
// eslint-disable-next-line standard/no-callback-literal
|
2020-10-31 11:45:33 +00:00
|
|
|
if (callback(value, key)) return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Filter out the Collection using callback */
|
2020-11-03 09:21:29 +00:00
|
|
|
filter(callback: (value: V, key: K) => boolean): Collection<K, V> {
|
2020-10-31 11:45:33 +00:00
|
|
|
const relevant = new Collection<K, V>()
|
|
|
|
this.forEach((value, key) => {
|
|
|
|
if (callback(value, key)) relevant.set(key, value)
|
2020-11-03 09:21:29 +00:00
|
|
|
})
|
|
|
|
return relevant
|
2020-10-31 11:45:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Map the collection */
|
2020-11-03 09:21:29 +00:00
|
|
|
map<T>(callback: (value: V, key: K) => T): T[] {
|
2020-10-31 11:45:33 +00:00
|
|
|
const results = []
|
|
|
|
for (const key of this.keys()) {
|
2020-11-03 09:21:29 +00:00
|
|
|
const value = this.get(key) as V
|
2020-10-31 11:45:33 +00:00
|
|
|
results.push(callback(value, key))
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** Check if any of the values/keys in Collection satisfies callback */
|
2020-11-03 09:21:29 +00:00
|
|
|
some(callback: (value: V, key: K) => boolean): boolean {
|
2020-10-31 11:45:33 +00:00
|
|
|
for (const key of this.keys()) {
|
2020-11-03 09:21:29 +00:00
|
|
|
const value = this.get(key) as V
|
2020-10-31 11:45:33 +00:00
|
|
|
if (callback(value, key)) return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-05 02:24:08 +00:00
|
|
|
/** Check if every value/key in Collection satisfies callback */
|
2020-11-03 09:21:29 +00:00
|
|
|
every(callback: (value: V, key: K) => boolean): boolean {
|
2020-10-31 11:45:33 +00:00
|
|
|
for (const key of this.keys()) {
|
2020-11-03 09:21:29 +00:00
|
|
|
const value = this.get(key) as V
|
2020-10-31 11:45:33 +00:00
|
|
|
if (!callback(value, key)) return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Reduce the Collection to a single value */
|
2020-10-31 11:45:33 +00:00
|
|
|
reduce<T>(
|
|
|
|
callback: (accumulator: T, value: V, key: K) => T,
|
2020-12-02 12:29:52 +00:00
|
|
|
initialValue?: T
|
2020-10-31 11:45:33 +00:00
|
|
|
): T {
|
2020-11-03 09:21:29 +00:00
|
|
|
let accumulator: T = initialValue as T
|
2020-10-31 11:45:33 +00:00
|
|
|
|
|
|
|
for (const key of this.keys()) {
|
2020-11-03 09:21:29 +00:00
|
|
|
const value = this.get(key) as V
|
2020-10-31 11:45:33 +00:00
|
|
|
accumulator = callback(accumulator, value, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return accumulator
|
|
|
|
}
|
2020-11-01 11:22:09 +00:00
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Create a Collection from an Object */
|
2020-11-03 09:21:29 +00:00
|
|
|
static fromObject<V>(object: { [key: string]: V }): Collection<string, V> {
|
2020-11-01 11:22:09 +00:00
|
|
|
return new Collection<string, V>(Object.entries(object))
|
|
|
|
}
|
|
|
|
|
2020-12-03 04:06:41 +00:00
|
|
|
/** Convert Collection to an object */
|
2020-11-03 09:21:29 +00:00
|
|
|
toObject(): { [name: string]: V } {
|
|
|
|
return Object.fromEntries(this)
|
2020-11-01 11:22:09 +00:00
|
|
|
}
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|