Installation • Usage • API Reference • Contributors • License
yarn add @devoxa/flocky
import { sum } from '@devoxa/flocky'
sum([1, 2, 3])
// -> 6
Compute the average of the values in an array.
flocky.average([1, 4, 2, -4, 0])
// -> 0.6
Source • Minify: 77 B • Minify & GZIP: 79 B
Split an array of elements into groups of size
.
If the array can't be split evenly, the final chunk will contain the remaining elements.
flocky.chunk([1, 2, 3, 4, 5, 6, 7], 3)
// -> [[1, 2, 3], [4, 5, 6], [7]]
Source • Minify: 105 B • Minify & GZIP: 101 B
Create a deep clone of value
.
This method only supports types native to JSON, so all primitive types, arrays and objects.
const original = [{ a: 1 }, { b: 2 }]
const clone = flocky.clone(original)
original[0] === clone[0]
// -> false
Source • Minify: 69 B • Minify & GZIP: 69 B
Create an array with all falsy (undefined
, null
, false
, 0
, NaN
, ''
) values removed.
flocky.compact([1, 2, 3, null, 4, false, 0, NaN, 5, ''])
// -> [1, 2, 3, 4, 5]
Source • Minify: 61 B • Minify & GZIP: 64 B
Create a debounced function that delays invoking func
until wait
milliseconds
have elapsed since the last time the debounced function was invoked.
const func = () => console.log('Heavy processing happening')
const debouncedFunc = flocky.debounce(func, 250)
Source • Minify: 131 B • Minify & GZIP: 113 B
Create a version of an array, in which only the duplicated elements are kept.
The order of result values is determined by the order they occur in the array.
Can be passed an optional identity
function to select the identifying part of objects.
flocky.duplicates([1, 1, 2, 4, 2, 1, 6])
// -> [1, 2, 1]
flocky.duplicates(['foo', 'bar', 'foo', 'foobar'])
// -> ['foo']
const input = [{ id: 1, a: 1 }, { id: 1, a: 2 }, { id: 2, a: 3 }, { id: 1, a: 4 }]
flocky.duplicates(input, (element) => element.id)
// -> [{ id: 1, a: 2 }, { id: 1, a: 4 }]
Source • Minify: 277 B • Minify & GZIP: 147 B
Escape special characters in a string for use in a regular expression.
flocky.escapeRegExp('Hey. (1 + 1 = 2)')
// -> 'Hey\\. \\(1 \\+ 1 = 2\\)'
Source • Minify: 93 B • Minify & GZIP: 90 B
Flattens a nested object into an object with dot notation keys.
flocky.flatten({ a: { b: 1, c: 2 }, d: { e: { f: 3 } } })
// -> { 'a.b': 1, 'a.c': 2, 'd.e.f': 3 }
Source • Minify: 172 B • Minify & GZIP: 136 B
Get the value at a path
of an object
(with an optional defaultValue
)
const object = {a: {b: {c: 1}}}
flocky.get(object, 'a.b.c')
// -> 1
const object = {a: {b: {c: 1}}}
flocky.get(object, 'x.x.x')
// -> undefined
const object = {a: {b: {c: 1}}}
flocky.get(object, 'x.x.x', 'default')
// -> 'default'
Source • Benchmark • Minify: 424 B • Minify & GZIP: 266 B
Create a hashed string representation of the passed in data.
Argon2id
, scrypt
or bcrypt
for anything security related.
flocky.hash('some really long string')
// -> 'x1nr7uiv'
flocky.hash({id: 'AAA', name: 'BBB'})
// -> 'x16mynva'
Implementation Details
This method uses Murmur3 because it is small, fast and has fairly good collision characteristics (about 1 in 36000).
Source • Benchmark • Minify: 552 B • Minify & GZIP: 332 B
Generate a random identifier with UUID v4 format.
flocky.identifier()
// -> 'bfc8d57e-b9ab-4245-836e-d1fd99602e30'
Source • Minify: 275 B • Minify & GZIP: 196 B
Find all matches of a regular expression in a string.
flocky.matchAll(/f(o+)/g, 'foo bar baz foooo bar')
// -> [
// -> { match: 'foo', subMatches: ['oo'], index: 0 },
// -> { match: 'foooo', subMatches: ['oooo'], index: 12 },
// -> ]
Source • Minify: 178 B • Minify & GZIP: 133 B
Compute the maximum of the values in an array.
flocky.max([1, 4, 2, -3, 0])
// -> 4
Source • Minify: 58 B • Minify & GZIP: 63 B
Create a function that memoizes the return value of func
.
const func = (a, b) => a + b
const memoizedFunc = flocky.memoize(func)
const memoizedFuncWithTtl = flocky.memoize(func, { ttl: 30 * 1000 })
memoizedFunc(1, 2)
// -> 3
Implementation Details
This method's implementation is based on fast-memoize, with some improvements for variadic performance and additional support for a TTL based cache.
Source • Benchmark • Minify: 962 B • Minify & GZIP: 441 B
Compute the minimum of the values in an array.
flocky.min([1, 4, 2, -3, 0])
// -> -3
Source • Minify: 58 B • Minify & GZIP: 63 B
Create an object composed of all existing keys that are not specified in keys
.
const object = { a: 1, b: 2, c: 3 }
flocky.omit(object, ['a'])
// -> { b: 2, c: 3 }
Source • Benchmark • Minify: 143 B • Minify & GZIP: 129 B
Compute the kth percentile of the values in an array.
flocky.percentile([90, 85, 65, 72, 82, 96, 70, 79, 68, 84], 0.9)
// -> 90.6
Source • Minify: 217 B • Minify & GZIP: 156 B
Create an object composed of the specified keys
.
const object = { a: 1, b: 2, c: 3 }
flocky.pick(object, ['a', 'c'])
// -> { a: 1, c: 3 }
Source • Benchmark • Minify: 97 B • Minify & GZIP: 93 B
Run multiple promise-returning functions in parallel with limited concurrency.
await flocky.promisePool([
() => Promise.resolve(1),
() => Promise.resolve(2),
() => Promise.resolve(3),
() => Promise.resolve(4),
], 2)
// -> [1, 2, 3, 4]
Source • Minify: 182 B • Minify & GZIP: 142 B
Reject a promise if it does not resolve within timeoutMs
.
await flocky.promiseTimeout(Promise.resolve(1), 10)
// -> 1
Source • Minify: 305 B • Minify & GZIP: 181 B
Generate a random number between lower
and upper
(inclusive).
If float
is true or lower
or upper
is a float, a float is returned instead of an integer.
flocky.random(1, 10)
// -> 8
flocky.random(1, 20, true)
// -> 14.94849340769861
flocky.random(2.5, 3.5)
// -> 3.2341312319841373
Source • Minify: 219 B • Minify & GZIP: 128 B
Generate a random alphanumeric string with length
characters.
flocky.randomString(5)
// -> 'tfl0g'
Source • Minify: 230 B • Minify & GZIP: 201 B
Generate an array of numbers progressing from start
up to and including end
.
flocky.range(0, 5)
// -> [0, 1, 2, 3, 4, 5]
flocky.range(-5, -10)
// -> [-5, -6, -7, -8, -9, -10]
flocky.range(-6, -12, 2)
// -> [-6, -8, -10, -12]
Source • Minify: 131 B • Minify & GZIP: 111 B
Round a floating point number to precision
decimal places.
flocky.roundTo(3.141592653589, 4)
// -> 3.1416
flocky.roundTo(1.005, 2)
// -> 1.01
flocky.roundTo(1111.1, -2)
// -> 1100
Implementation Details
This method avoids floating-point errors by adjusting the exponent part of the string representation of a number instead of multiplying and dividing with powers of 10. The implementation is based on this example by Lam Wei Li.
Source • Minify: 209 B • Minify & GZIP: 150 B
Get a random element from the array
.
flocky.sample([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
// -> 8
Source • Minify: 79 B • Minify & GZIP: 79 B
Create an array of shuffled values.
flocky.shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
// -> [3, 7, 2, 1, 10, 4, 6, 9, 5, 8]
Implementation Details
This method uses a modern version of the Fisher-Yates shuffle.
Source • Minify: 152 B • Minify & GZIP: 131 B
Return a promise that waits for ms
milliseconds before resolving.
await flocky.sleep(25)
Source • Minify: 73 B • Minify & GZIP: 78 B
Generate a URL-safe slug of a string.
flocky.slugify(' Issue #123 is _important_! :)')
// -> 'issue-123-is-important'
Source • Minify: 114 B • Minify & GZIP: 104 B
Compute the sum of the values in an array.
flocky.sum([1, 4, 2, -4, 0])
// -> 3
Source • Minify: 60 B • Minify & GZIP: 67 B
Create a throttled function that invokes func
at most every wait
milliseconds.
If the invocation is throttled, func
will be invoked with the last arguments provided.
const func = () => console.log('Heavy processing happening')
const throttledFunc = flocky.throttle(func, 250)
Source • Minify: 209 B • Minify & GZIP: 156 B
Create a lookup map out of an array
of objects, with a lookup key
and an optional target
.
flocky.toMap(
[
{ id: 1, name: 'Stanley', age: 64 },
{ id: 2, name: 'Juliet', age: 57 },
{ id: 3, name: 'Alex', age: 19 }
],
'id'
)
// -> {
// -> 1: { id: 1, name: 'Stanley', age: 64 },
// -> 2: { id: 2, name: 'Juliet', age: 57 },
// -> 3: { id: 3, name: 'Alex', age: 19 }
// -> }
flocky.toMap(
[
{ id: 1, name: 'Stanley', age: 64 },
{ id: 2, name: 'Juliet', age: 57 },
{ id: 3, name: 'Alex', age: 19 }
],
'name',
'age'
)
// -> { Stanley: 64, Juliet: 57, Alex: 19 }
Source • Minify: 95 B • Minify & GZIP: 95 B
Unflattens an object with dot notation keys into a nested object.
flocky.unflatten({ 'a.b': 1, 'a.c': 2, 'd.e.f': 3 })
// -> { a: { b: 1, c: 2 }, d: { e: { f: 3 } } }
Source • Minify: 199 B • Minify & GZIP: 145 B
Create a duplicate-free version of an array, in which only the first occurrence of each element is kept.
The order of result values is determined by the order they occur in the array.
Can be passed an optional identity
function to select the identifying part of objects.
flocky.unique([1, 1, 2, 4, 2, 1, 6])
// -> [1, 2, 4, 6]
flocky.unique(['foo', 'bar', 'foo', 'foobar'])
// -> ['foo', 'bar', 'foobar']
const input = [{ id: 1, a: 1 }, { id: 1, a: 2 }, { id: 2, a: 3 }, { id: 1, a: 4 }]
flocky.unique(input, (element) => element.id)
// -> [{ id: 1, a: 1 }, { id: 2, a: 3 }]
Source • Benchmark • Minify: 238 B • Minify & GZIP: 153 B
Thanks goes to these wonderful people (emoji key):
David Reeß 💻 📖 |
Jeff Hage 💻 |
darthmaim 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
MIT