reformat, linter
This commit is contained in:
parent
dd2c3c1f8c
commit
31df6a4542
1 changed files with 24 additions and 24 deletions
|
@ -12,36 +12,36 @@ export class Collection<K = string, V = any> extends Map<K, V> {
|
||||||
|
|
||||||
/** Get first value(s) in Collection */
|
/** Get first value(s) in Collection */
|
||||||
first(): V | undefined;
|
first(): V | undefined;
|
||||||
first(amount: number): V[];
|
first(amount: number): V[];
|
||||||
first(amount?: number): V | V[] | undefined {
|
first(amount?: number): V | V[] | undefined {
|
||||||
if (typeof amount === 'undefined') return this.values().next().value
|
if (typeof amount === 'undefined') return this.values().next().value
|
||||||
if (amount < 0) return this.last(amount * -1)
|
if (amount < 0) return this.last(amount * -1)
|
||||||
amount = Math.min(this.size, amount)
|
amount = Math.min(this.size, amount)
|
||||||
const iter = this.values()
|
const iter = this.values()
|
||||||
return Array.from({ length: amount }, (): V => iter.next().value)
|
return Array.from({ length: amount }, (): V => iter.next().value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get last value(s) in Collection */
|
/** Get last value(s) in Collection */
|
||||||
last(): V | undefined;
|
last(): V | undefined;
|
||||||
last(amount: number): V[];
|
last(amount: number): V[];
|
||||||
last(amount?: number): V | V[] | undefined {
|
last(amount?: number): V | V[] | undefined {
|
||||||
const arr = this.array()
|
const arr = this.array()
|
||||||
if (typeof amount === 'undefined') return arr[arr.length - 1]
|
if (typeof amount === 'undefined') return arr[arr.length - 1]
|
||||||
if (amount < 0) return this.first(amount * -1)
|
if (amount < 0) return this.first(amount * -1)
|
||||||
if (typeof amount !== 'number' && !amount) return []
|
if (!amount) return [] // eslint-disable-line
|
||||||
return arr.slice(-amount)
|
return arr.slice(-amount)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get random value(s) from Collection */
|
/** Get random value(s) from Collection */
|
||||||
random(): V;
|
random(): V;
|
||||||
random(amount: number): V[];
|
random(amount: number): V[];
|
||||||
random(amount?: number): V | V[] {
|
random(amount?: number): V | V[] {
|
||||||
let arr = this.array()
|
let arr = this.array()
|
||||||
if (typeof amount === 'undefined') return arr[Math.floor(Math.random() * arr.length)]
|
if (typeof amount === 'undefined') return arr[Math.floor(Math.random() * arr.length)]
|
||||||
if (arr.length === 0 || typeof amount !== 'number' && !amount) return []
|
if (arr.length === 0 || !amount) return [] // eslint-disable-line
|
||||||
arr = arr.slice()
|
arr = arr.slice()
|
||||||
return Array.from({ length: amount }, (): V => arr.splice(Math.floor(Math.random() * arr.length), 1)[0])
|
return Array.from({ length: amount }, (): V => arr.splice(Math.floor(Math.random() * arr.length), 1)[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Find a value from Collection using callback */
|
/** Find a value from Collection using callback */
|
||||||
find(callback: (value: V, key: K) => boolean): V | undefined {
|
find(callback: (value: V, key: K) => boolean): V | undefined {
|
||||||
|
|
Loading…
Reference in a new issue