zeelib / Exports
Utility library in TypeScript, with a focus on Node and FP utils and emphasis on not rewriting builtin features.
npm i zeelib
See the docs below for details (generated from types and comments). Example:
import * as z from 'zeelib'
const answer = await z.prompt('Pick a letter')
if (z.isEmpty(answer)) process.exit(1)
Full Docs
[zeelib](README.md) / Exports- and
- average
- capitalizeFirstChar
- chunk
- clamp
- collapseNewlines
- collapseWhitespace
- collectBy
- combineRegex
- compose
- countIn
- curry
- debounce
- diff
- div
- drop
- dropWhile
- each
- escapeForRegex
- execute
- fileExists
- filter
- findIndices
- findPort
- flattenAndUniq
- flip
- fold
- getFreeDisk
- getFreeMemory
- getGlobal
- getHashFromDate
- getHashFromSystem
- getKeyByValue
- getLoadAverage
- getMemoryUsage
- getOrdinal
- getProjectRoot
- getRegexFlags
- getStdin
- getTerminalColumns
- getTerminalRows
- getTerminalSize
- getUserHome
- getUserShell
- greater
- groupBy
- has
- hasColor
- hasDuplicate
- head
- id
- init
- initials
- intersection
- is
- isArrayLike
- isBetween
- isBoolean
- isClass
- isDate
- isDefined
- isDirectory
- isEmpty
- isError
- isEven
- isFile
- isFloat
- isFunction
- isGenerator
- isGeneratorFunction
- isInstalled
- isInteger
- isJson
- isMap
- isNode
- isNull
- isNullOrUndefined
- isNumber
- isObject
- isOdd
- isPrimitive
- isPromise
- isRegExp
- isRoot
- isSemver
- isSet
- isString
- isSymLink
- isSymbol
- isUndefined
- isValidDate
- keep
- last
- len
- lesser
- lightenOrDarken
- lines
- mapObject
- memoize
- mzero
- objectToString
- once
- open
- pick
- pipe
- pluck
- product
- prompt
- range
- readJson
- reduce
- removeBOM
- replicate
- rgbToHex
- safeGet
- scaleToFit
- shuffle
- sleep
- sortBy
- sortObject
- span
- splitAt
- store
- stripAnsi
- sub
- sum
- tail
- take
- takeLast
- takeWhile
- tap
- touch
- truncate
- tryJson
- typeOf
- union
- uniq
- uniqBy
- unless
- unlines
- until
- unwords
- watch
- words
- writeJson
Ƭ AnyFn: (...args
: any
[]) => any
▸ (...args
): any
Name | Type |
---|---|
...args |
any [] |
any
Ƭ AnyMap: Record
<string
, any
>
Ƭ Cb: (error
: c.ExecFileException
| null
, stdout
: string
| Buffer
, stderr
: string
| Buffer
) => void
▸ (error
, stdout
, stderr
): void
Name | Type |
---|---|
error |
c.ExecFileException | null |
stdout |
string | Buffer |
stderr |
string | Buffer |
void
Ƭ ColMap: Record
<string
, (t
: string
) => string
>
Ƭ F: (...args
: any
[]) => any
▸ (...args
): any
Name | Type |
---|---|
...args |
any [] |
any
Ƭ Listener: (a
: AnyMap
, b
: AnyMap
) => any
▸ (a
, b
): any
Name | Type |
---|---|
a |
AnyMap |
b |
AnyMap |
any
Ƭ R: (...args
: any
[]) => Timer
▸ (...args
): Timer
Name | Type |
---|---|
...args |
any [] |
Ƭ R: string
| any
[] | AnyMap
Ƭ Timer: NodeJS.Timeout
| number
| null
Ƭ Updater: (a
: AnyMap
) => AnyMap
▸ (a
): AnyMap
Name | Type |
---|---|
a |
AnyMap |
• Const
colorize: ColMap
▸ and<T
>(a
): boolean
Returns false if any in input array is false
Name |
---|
T |
Name | Type |
---|---|
a |
T [] |
boolean
Example
and([ 1, 2, 3 ]) // => true
and([ 1, 2, 3, false ]) // => false
▸ average(xs
): number
Averages a list of numbers
Name | Type |
---|---|
xs |
number [] |
number
Example
average([ 2, 4, 6, 8 ]) // => 5
▸ capitalizeFirstChar(str
): string
Capitalizes the first character of string
Name | Type |
---|---|
str |
string |
string
Example
capitalizeFirstChar('things and stuff') // => 'Things and stuff'
▸ chunk<T
>(arr
, n
): T
[][]
Splits an array into chunks
Name |
---|
T |
Name | Type |
---|---|
arr |
T [] |
n |
number |
T
[][]
Example
chunk([1, 2, 3, 4 ], 2) // => [ [ 1, 2 ], [ 3, 4 ] ]
▸ clamp(val
, min
, max
): number
Takes a number, min, and max If number is between min and max, returns number Otherwise returns min or max
Name | Type |
---|---|
val |
number |
min |
number |
max |
number |
number
Example
clamp(100, 0, 1000) // => 100
clamp(100, 101, 1000) // => 101
clamp(100, 0, 99) // => 00
▸ collapseNewlines(str
): string
Collapses multiple newlines to two
Name | Type |
---|---|
str |
string |
string
Example
collapseNewlines('\n\n\n\n') // => '\n\n'
▸ collapseWhitespace(str
): string
Collapses consecutive whitespace to a single space
Name | Type |
---|---|
str |
string |
string
Example
collapseWhitespace('a\n\r\t\nb') // => 'a b'
▸ collectBy(p
): (a
: any
[]) => AnyMap
Collect an an array of objects by string key cred: gh:uniqname
Name | Type |
---|---|
p |
string |
fn
▸ (a
): AnyMap
Name | Type |
---|---|
a |
any [] |
Example
const data = [ { foo: 'a', bar: 'baz' }, { foo: 'b', bar: 'quux' }, { foo: 'a', bar: 'whatever' } ]
collectBy('foo')(data) // => { a: { foo: 'a', bar: 'whatever' }, b: { foo: 'b', bar: 'quux' } }
▸ combineRegex(rs
, opts?
): RegExp
Combines regular expressions
Name | Type | Default value |
---|---|---|
rs |
RegExp [] |
undefined |
opts |
string |
'' |
RegExp
Example
combineRegex([/[a-z]/, /[0-9]], 'g') ==> /[a-z][0-9]/g
▸ compose<T
>(...fns
): (t
: T
) => T
Right to left composition
Name |
---|
T |
Name | Type |
---|---|
...fns |
(t : T ) => T [] |
fn
▸ (t
): T
Name | Type |
---|---|
t |
T |
T
Example
const addOne = (a) => a + 1
const timesTwo = (a) => a * 2
compose(addOne, timesTwo)(2) // => 5
▸ countIn<T
>(el
, ls
): number
Find out how many of a given element is in an array or string.
Name |
---|
T |
Name | Type |
---|---|
el |
T |
ls |
string | T [] |
number
Example
countIn('a', 'abc') // => 1
countIn('a', [ 'a', 'b', 'c' ]) // => 1
▸ curry<T
>(fn
): F
Takes a function and returns a function that takes any number of arguments
Produces a curried function
Name |
---|
T |
Name | Type |
---|---|
fn |
F |
Example
const addThree = (a, b, c) => a + b + c
curry(addThree)(1)(1)(1) // => 3
▸ debounce(fn
, ms
): R
Simple debounce
Name | Type |
---|---|
fn |
(...args : any ) => any |
ms |
number |
Example
debounce(() => console.log('hi'), 5000)
▸ diff<T
>(a
, b
): T
[]
Diff two arrays
Name |
---|
T |
Name | Type |
---|---|
a |
T [] |
b |
T [] |
T
[]
Example
diff([ 1, 2, 3 ], [ 2, 3 ]) // => [ 1 ]
▸ div(ns
): number
Divide any amount of numbers
Name | Type |
---|---|
ns |
number [] |
number
Example
div([ 4, 2, 1 ]) // => 2
▸ drop<A
>(n
, arr
): A
[]
Like Haskell's drop
Name |
---|
A |
Name | Type |
---|---|
n |
number |
arr |
A [] |
A
[]
Example
drop(2, [ 1, 2, 3 ]) // => 3
▸ dropWhile<T
>(pred
, arr
): T
[]
The opposite of takeWhile
:
takes a predicate and array and returns an
array of the elements that didn't pass the predicate
Name |
---|
T |
Name | Type |
---|---|
pred |
(x : T ) => boolean |
arr |
T [] |
T
[]
Example
dropWhile(lessThanThree, [ 1, 2, 3, 4 ]) // => [ 3, 4 ]
▸ each<T
>(xs
, fn
): Record
<string
, any
> | T
[]
Takes an array or object and a function, and runs the function on each element
Name |
---|
T |
Name | Type |
---|---|
xs |
string | any [] | Record <string , any > |
fn |
(a : any , b : string | number ) => T |
Record
<string
, any
> | T
[]
Example
each([ 'a', 'b', 'c' ], id) // => 'a'
▸ escapeForRegex(s?
): string
Name | Type | Default value |
---|---|---|
s |
string |
'' |
string
Example
escapeForRegex('foo') // => foo
escapeForRegex('1 \\ {} []|') // => '1 \\\\ \{\} \\[\\]\\|'
▸ execute(cmd
): void
Like a standalone npm run
that obeys npm bin
Name | Type |
---|---|
cmd |
string |
void
Example
execute('standard-format -w')
▸ fileExists(filePath
): Promise
<boolean
>
Returns bool based on if passed path exists
Name | Type |
---|---|
filePath |
string |
Promise
<boolean
>
Example
await fileExists('./foo') // => Promise<true>
▸ filter<T
>(fn
, list
): T
[] | Record
<string
, T
>
filter
for array and object
Name |
---|
T |
Name | Type |
---|---|
fn |
(x : T , y : string | number ) => boolean |
list |
string | AnyMap | T [] |
T
[] | Record
<string
, T
>
Example
filter(id, [ 1, 2 ]) // => [ 1, 2 ]
filter(lessThanThree, [ 1, 2, 3, 4 ]) // => [ 1, 2 ]
filter(lessThanThree, { a: 1, b: 4 }) // => { a: 1 }
▸ findIndices(el
, arr
): number
[]
Find all indices of an item in an array
Name | Type |
---|---|
el |
any |
arr |
any [] |
number
[]
Example
findIndices(1, [ 1, 2, 1 ]) // => [ 0, 2 ]
▸ findPort(port
, cb
): void
Find next open port
Name | Type |
---|---|
port |
number |
cb |
(x : null | Error , y? : number ) => void |
void
Example
findPort(8000, (err, port) => console.log(`${port} is open`))
▸ flattenAndUniq<T
>(arr
): T
[]
Recursively flatten arrays then uniq what's left
Name |
---|
T |
Name | Type |
---|---|
arr |
T [] |
T
[]
Example
flattenAndUniq([ 1, 2, 3, [ 1, 2, 3 ]]) // => [ 1, 2, 3 ]
▸ flip(f
): (...args
: any
[]) => any
Flips order of received arguments and calls f.
Name | Type |
---|---|
f |
(...xs : any []) => any |
fn
▸ (...args
): any
Name | Type |
---|---|
...args |
any [] |
any
Example
const minus = (a, b) => a - b
minus(2, 1) // => 1
flip(minus)(2, 1) // => -1
▸ fold<T
>(f
, a
): T
Applies f to first two items of list, then to next, etc.
Name |
---|
T |
Name | Type |
---|---|
f |
(x : T , y : T ) => T |
a |
T [] |
T
Example
foldl1(increment, [ 1, 1, 1 ]) // => 3
▸ getFreeDisk(): number
Get free disk space
number
Example
getFreeDisk()
▸ getFreeMemory(): number
Get free memory
number
Example
getFreeMemory()
▸ getGlobal(): undefined
| typeof globalThis
Gets the global for your current context.
undefined
| typeof globalThis
Example
getGlobal() // => window, global, whatever
▸ getHashFromDate(): string
Returns a hash based on current timestamp
string
Example
getHashFromDate()
▸ getHashFromSystem(): string
Get a md5 hash based on hostname, process.ppid, and date
string
Example
getHashFromSystem()
▸ getKeyByValue(value
, object
): undefined
| string
Get the key for a value
Name | Type |
---|---|
value |
any |
object |
AnyMap |
undefined
| string
Example
getKeyByValue('bar', { foo: 'bar' }) // => 'foo'
▸ getLoadAverage(): number
Get load average
number
Example
getLoadAverage()
▸ getMemoryUsage(): number
Get memory usage
number
Example
getMemoryUsage()
▸ getOrdinal(n
): string
Adds ordinal onto integer
Works up to 999
Name | Type |
---|---|
n |
number |
string
Example
getOrdinal(1) // => '1st'
▸ getProjectRoot(): string
Get project root
string
Example
getProjectRoot() // /path/to/project
▸ getRegexFlags(r
): string
[]
Returns the flags for a given regular expression
Name | Type |
---|---|
r |
RegExp |
string
[]
Example
getRegexFlags(/foo/ig) // => [ 'g', 'i' ]
▸ getStdin(f
): void
Get stdin and do something with it.
Name | Type |
---|---|
f |
(a : string ) => void |
void
Example
getStdin((str) => {
console.log(str.split(''))
})
▸ getTerminalColumns(): number
Get columns of current terminal
number
Example
getTerminalColumns()
▸ getTerminalRows(): number
Get current terminal rows
number
Example
getTerminalRows()
▸ getTerminalSize(): Size
Returns size of the current terminal
Example
getTerminalSize() // => { columns: number, rows: number }
▸ getUserHome(): string
Get current user's home directory
string
Example
getUserHome() // => /home/z
▸ getUserShell(): string
Get the current user's shell, or an empty string on shell-less platforms
string
Example
getUserShell()
▸ greater(a
, b
): number
Get the greater of two numbers
Name | Type |
---|---|
a |
number |
b |
number |
number
Example
greater(1, 2) // => 2
▸ groupBy(p
): (a
: any
[]) => AnyMap
Collect an an array of objects by string key
Name | Type |
---|---|
p |
string |
fn
▸ (a
): AnyMap
Name | Type |
---|---|
a |
any [] |
Example
const data = [ { foo: 'a', bar: 'baz' }, { foo: 'b', bar: 'quux' }, { foo: 'a', bar: 'whatever' } ]
groupBy('foo')(data) // => { a: { foo: 'a', bar: 'whatever' }, b: { foo: 'b', bar: 'quux' } }
▸ has(p
, o
): boolean
See if an object has a property
Name | Type |
---|---|
p |
string |
o |
AnyMap |
boolean
Example
has('a' { b: 'c' }) // => false
▸ hasColor(): boolean
Returns true if the current terminal supports color
boolean
Example
hasColor()
▸ hasDuplicate<T
>(arr
): boolean
Returns true if an array has any duplicate elements
Name |
---|
T |
Name | Type |
---|---|
arr |
T [] |
boolean
Example
hasDuplicate([ 1, 1, 2 ]) // => true
▸ head<T
>(arr
): T
First element
Name |
---|
T |
Name | Type |
---|---|
arr |
T [] |
T
Example
head([ 1, 2, 3 ]) // => 1
▸ id<A
>(a
): A
id
Name |
---|
A |
Name | Type |
---|---|
a |
A |
A
Example
id(1) // => 1
id() // => undefined
▸ init(arr
): any
[]
Returns all but the last item of an array
Name | Type |
---|---|
arr |
any [] |
any
[]
Example
init([ 1, 2, 3 ]) // => [ 1, 2 ]
▸ initials(str
): string
Trims a string to just caps
Name | Type |
---|---|
str |
string |
string
Example
initials('Zac Anger') // => 'ZA'
▸ intersection<T
>(xs
, ys
): T
[]
Get the intersection of two arrays
Name |
---|
T |
Name | Type |
---|---|
xs |
T [] |
ys |
T [] |
T
[]
Example
intersection([ 1, 2 ], []) // => []
intersection([ 1, 2, 3 ], [1, 2]) // => [ 1, 2 ]
▸ is(t
, val
): boolean
Returns true if the value is of the type
Name | Type |
---|---|
t |
string |
val |
any |
boolean
Example
is('number', 2) // => true
▸ isArrayLike(v
): boolean
Returns true if the passed value is array-like
Name | Type |
---|---|
v |
any |
boolean
Example
isArrayLike({}) // => false
isArrayLike([ 1, 2 ]) // => true
▸ isBetween(a
, b
, mid
): boolean
Returns true if the last parameter is before the first and second parameters
Name | Type |
---|---|
a |
number |
b |
number |
mid |
number |
boolean
Example
isBetween(1, 3, 2) // => true
isBetween(2, 1, 2) // => false
▸ isBoolean(v
): boolean
Returns true if the value is a boolean
Name | Type |
---|---|
v |
any |
boolean
Example
isBoolean(true) // => true
▸ isClass(fn
): boolean
Returns true if passed fn is an ES2015 class
Name | Type |
---|---|
fn |
() => any |
boolean
Example
isClass(noop) // => false
▸ isDate(v
): boolean
Returns true if the value is a date
Name | Type |
---|---|
v |
any |
boolean
Example
isDate(new Date()) // => true
▸ isDefined(v
): boolean
Returns true if the value is defined
Name | Type |
---|---|
v |
any |
boolean
Example
isDefined(null) // => true
▸ isDirectory(filePath
): Promise
<boolean
>
Returns true if the path is a directory
Name | Type |
---|---|
filePath |
string |
Promise
<boolean
>
Example
await isDirectory('.') // => true
await isDirectory('./fake-path') // => false
▸ isEmpty(v
): boolean
Returns true if the value is empty
Name | Type |
---|---|
v |
any |
boolean
Example
isEmpty('') // => true
isEmpty({}) // => true
isEmpty([]) // => true
isEmpty(null) // => true
▸ isError(a
): boolean
Returns true if value is an error
Name | Type |
---|---|
a |
any |
boolean
Example
isError(new Error()) // => true
▸ isEven(n
): boolean
Returns true if the number is even
Name | Type |
---|---|
n |
number |
boolean
Example
isEven(2) // => true
▸ isFile(filePath
): Promise
<boolean
>
Returns true if the path is a file
Name | Type |
---|---|
filePath |
string |
Promise
<boolean
>
Example
isFile('./README.md') // => true
isFile('.') // => false
▸ isFloat(n
): boolean
Returns true if the number is a float
Name | Type |
---|---|
n |
number |
boolean
Example
isFloat(2) // => false
isFloat(2.2) // => true
▸ isFunction(v
): boolean
Returns true if the value is a function
Name | Type |
---|---|
v |
any |
boolean
Example
const noop = () => {}
isFunction(2) // => false
isFunction(noop) // => true
▸ isGenerator(v
): boolean
Returns true if passed val is a generator
Name | Type |
---|---|
v |
any |
boolean
Example
isGenerator(2) // => false
▸ isGeneratorFunction(v
): boolean
Returns true if val is a generator function
Name | Type |
---|---|
v |
any |
boolean
Example
isGeneratorFunction(2) // => false
▸ isInstalled(pkgName
): boolean
Returns true if the passed node_module name is installed
Name | Type |
---|---|
pkgName |
string |
boolean
Example
isInstalled('zeelib') // => true
▸ isInteger(v
): boolean
Returns true if the value is an integer
Name | Type |
---|---|
v |
any |
boolean
Example
isInteger(2) // => true
isInteger(1.1) // => false
▸ isJson(str
): boolean
Returns true if the string is valid JSON
Name | Type |
---|---|
str |
string |
boolean
Example
isJson(JSON.stringify({ a: 'b' })) // => true
▸ isMap(v
): boolean
Returns true if value is a map
Name | Type |
---|---|
v |
any |
boolean
Example
isMap(new Map()) // => true
▸ isNode(): boolean
Returns true if code is in Node
boolean
Example
isNode()
▸ isNull(v
): boolean
Returns true if the value is null
Name | Type |
---|---|
v |
any |
boolean
Example
isNull(null) // => true
▸ isNullOrUndefined(v
): boolean
Returns true if the value is null or undefined
Name | Type |
---|---|
v |
any |
boolean
Example
isNullOrUndefined(null) // => true
▸ isNumber(v
): boolean
Returns true if the value is a number and is not NaN
Name | Type |
---|---|
v |
any |
boolean
Example
isNumber(2) // => true
isNumber(NaN) // => false
▸ isObject(v
): boolean
Returns true if the value is an object
Name | Type |
---|---|
v |
any |
boolean
Example
isObject('asdf') // => false
▸ isOdd(n
): boolean
Returns true if the number is odd
Name | Type |
---|---|
n |
number |
boolean
Example
isOdd(1) // => true
▸ isPrimitive(v
): boolean
Returns true if value is a primitive
Name | Type |
---|---|
v |
any |
boolean
Example
isPrimitive(1) // => true
▸ isPromise(a
): boolean
Returns true if value is a promise
Name | Type |
---|---|
a |
any |
boolean
Example
isPromise(Promise.resolve())
▸ isRegExp(v
): boolean
Returns true if value is a RegExp
Name | Type |
---|---|
v |
any |
boolean
Example
isRegExp(/a/) // => true
▸ isRoot(): boolean
Check if current process is running as root.
boolean
Example
isRoot() // => true
▸ isSemver(v
): boolean
Returns true if the provided string is a valid semantic version
Name | Type |
---|---|
v |
string |
boolean
Example
isSemver("0.0.0") // => true
isSemver("0.") // => false
▸ isSet(v
): boolean
Returns true if value is a set
Name | Type |
---|---|
v |
any |
boolean
Example
isSet(new Set()) // => true
▸ isString(v
): boolean
Returns true if value is a string
Name | Type |
---|---|
v |
any |
boolean
Example
isString('a') // => true
▸ isSymLink(filePath
): Promise
<boolean
>
Returns true if path is a symlink
Name | Type |
---|---|
filePath |
string |
Promise
<boolean
>
Example
isSymLink('.') // => false
▸ isSymbol(a
): boolean
Returns true if value is a symbol
Name | Type |
---|---|
a |
any |
boolean
Example
isSymbol(Symbol.for('foo')) // => true
▸ isUndefined(v
): boolean
Returns true if value is undefined
Name | Type |
---|---|
v |
any |
boolean
Example
isUndefined(undefined) // => true
▸ isValidDate(d
): boolean
Returns true if the passed object is a valid Date
Name | Type |
---|---|
d |
Date |
boolean
Example
isValidDate('1234') // => false
▸ keep(x
): R
Returns an array or object with all falsey values removed
Name | Type |
---|---|
x |
R |
Example
keep([ 'a', null, '', 2]) // => [ 'a', 2 ]
keep({ a: '', b: null, c: 2 }) // => { c: 2 }
▸ last<A
>(arr
): A
Returns the last element of the array
Name |
---|
A |
Name | Type |
---|---|
arr |
A [] |
A
Example
last([ 1, 2, 3 ]) // => 3
▸ len(val
): number
Get length of element
Works for array, object, string, set, map, and function
Name | Type |
---|---|
val |
any |
number
Example
len('foo') // => 3
len([ 1, 2 ]) => 2
len((a, b) => a + b) // => 2
▸ lesser(a
, b
): number
Returns the lesser of two numbers
Name | Type |
---|---|
a |
number |
b |
number |
number
Example
lesser(1, 2) // => 1
▸ lightenOrDarken(col
, amt
): string
Lighten or darken a color
Name | Type |
---|---|
col |
string |
amt |
number |
string
Example
// lighten
const newCol = lightenOrDarken('#F06D06', 20)
// darken
const newCol = lightenOrDarken('#F06D06', -20)
▸ lines(str
): string
[]
Split a string on lines
Name | Type |
---|---|
str |
string |
string
[]
Example
lines('foo\nbar') // => [ 'foo', 'bar' ]
▸ mapObject(f
, o
, ctx?
): AnyMap
Map for objects
Name | Type |
---|---|
f |
(a : any , b : string , c : any ) => any |
o |
AnyMap |
ctx |
any |
Example
const f = (a) => a + ' world'
const d = { a: 'hello', b: 'sup' }
mapObject(f, d) // => { a: 'hello world', b: 'sup world' }
▸ memoize(fn
): any
A simple memoizing util
Name | Type |
---|---|
fn |
any |
any
Example
memoize((a) => a) // => [Function]
memoize((a) => a)(1) // => 1
▸ mzero(v?
): any
Get monadic empty/zero value for a type
Name | Type |
---|---|
v? |
any |
any
Example
mzero(1) // =>
mzero([1]) // => []
▸ objectToString(v
): string
toString
Name | Type |
---|---|
v |
any |
string
Example
objectToString({}) // => '[object Object]'
▸ once<U
>(fn
): (...args
: U
[]) => U
Only calls fn once; subsequent calls just return first val
Name |
---|
U |
Name | Type |
---|---|
fn |
<T>(...args : T []) => T |
fn
▸ (...args
): U
Name | Type |
---|---|
...args |
U [] |
U
Example
const o = once(id)
o(1) // => 1
o() // => 1
o(2) // => 1
▸ open(args
, opts
, cb
): void
Opens things. Works on Linux, Mac, and Windows
Name | Type |
---|---|
args |
string |
opts |
AnyMap |
cb |
Cb |
void
Example
open('http://zacanger.com')
▸ pick(ks
, o
): AnyMap
pick
as it is in rambda (not ramda)
Name | Type |
---|---|
ks |
string | string [] |
o |
AnyMap |
Example
pick('a', { a: 'a', b: 'b' }) // => { a: 'a' }
pick([ 'a', 'b' ], { a: 'a', b: 'b', c: 'c' }) // => { a: 'a', b: 'b' }
▸ pipe<T
>(...fns
): (data
: T
) => T
Left to right composition
Name |
---|
T |
Name | Type |
---|---|
...fns |
AnyFn [] |
fn
▸ (data
): T
Name | Type |
---|---|
data |
T |
T
Example
const addOne = (a) => a + 1
const timesTwo = (a) => a * 2
pipe(addOne, timesTwo)(2) // => 6
▸ pluck(key
, arr
): any
[]
Simple pluck
Name | Type |
---|---|
key |
string |
arr |
any [] |
any
[]
Example
pluck('a', [ { a: 'a' }, { a: 'b' } ]) // => [ 'a', 'b' ]
▸ product(nums
): number
Get the product of a list of numbers
Name | Type |
---|---|
nums |
number [] |
number
Example
product([ 1, 2, 3, 4 ]) // => 24
▸ prompt(question
): Promise
<string
>
Create a simple prompt for the terminal
Name | Type |
---|---|
question |
string |
Promise
<string
>
Example
const answer = await prompt('Do the thing')
if (answer.toLowercase() === 'y') { doTheThing() }
▸ range(a
, b
, step?
): number
[]
Range function
Takes a start and and end, and a step (defaults to 1).
This is inclusive. That is:
1..10,2 == 0,2,4,6,8,10
Name | Type | Default value |
---|---|---|
a |
number |
undefined |
b |
number |
undefined |
step |
number |
1 |
number
[]
Example
range(1, 4) // => [ 1, 2, 3, 4 ]
range(1, 10, 3) // => [ 1, 4, 7, 10 ]
▸ readJson(file
): Promise
<any
[] | AnyMap
>
Read json file and parse it
Name | Type |
---|---|
file |
string |
Promise
<any
[] | AnyMap
>
Example
const json = await readJson('./foo.json')
▸ reduce<A
, B
>(fn
, initialValue
, data
): B
Reduce
Name |
---|
A |
B |
Name | Type |
---|---|
fn |
(b : B , a : A ) => B |
initialValue |
B |
data |
A [] |
B
Example
reduce((a, b) => a + b, 0, [ 1, 2, 3, 4 ]) // => 10
▸ removeBOM(content
): string
The same as what's in Node's module
(see lib/internal/module
).
Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
because the buffer-to-string conversion in fs.readFileSync()
translates it to FEFF, the UTF-16 BOM.
Name | Type |
---|---|
content |
string |
string
Example
removeBOM(someContent)
▸ replicate<A
>(n
, p
): A
[]
Generates an array of the length of the first param, filled with the second param, calling the second param if it's a function
Name |
---|
A |
Name | Type |
---|---|
n |
number |
p |
A | (n : number , i : number ) => A |
A
[]
Example
replicate(3, 10) // => [ 10, 10, 10 ]
replicate(4, (a) => a + 1) // => [ 5, 5, 5, 5 ]
▸ rgbToHex(r
, g
, b
): string
Convert rgb to hex
Name | Type |
---|---|
r |
number |
g |
number |
b |
number |
string
Example
rgbToHex(255, 255, 255) // => '#ffffff'
▸ safeGet<A
>(path
, fallback?
): (obj
: AnyMap
) => undefined
| null
| A
Like _.get
: takes an access string and an optional fallback,
then an object
Name |
---|
A |
Name | Type |
---|---|
path |
string |
fallback? |
A |
fn
▸ (obj
): undefined
| null
| A
Name | Type |
---|---|
obj |
AnyMap |
undefined
| null
| A
Example
safeGet('a.b.c')({ a: { b: { c: 'd' } } }) // => 'd'
safeGet('a.b.e')({ a: { b: { c: 'd' } } }) // => undefined
safeGet('a.b.e', 'f')({ a: { b: { c: 'd' } } }) // => 'f'
▸ scaleToFit(width
, height
, maxWidth?
, maxHeight?
): Object
Takes a width, height, maxWidth, and maxHeight
Returns an object that tells you the largest you can scale to
Name | Type |
---|---|
width |
number |
height |
number |
maxWidth? |
number |
maxHeight? |
number |
Object
Name | Type |
---|---|
height |
number |
width |
number |
Example
scaleToFit(1400, 1200, 2000, 200) // => { width: 233.33333333333331, height: 200 }
▸ shuffle<A
>(arr
): A
[]
Randomly shuffle items in array
Name |
---|
A |
Name | Type |
---|---|
arr |
A [] |
A
[]
Example
shuffle([ 1, 2, 3, 4 ])
▸ sleep(ms
): Promise
<void
>
Simple sleep.
Name | Type |
---|---|
ms |
number |
Promise
<void
>
Example
const delay = await sleep(1000)
sleep(1000).then(doAThing)
▸ sortBy<A
>(f
, xs
): A
[]
Takes a sort fn and an array
Returns a sorted array
Name |
---|
A |
Name | Type |
---|---|
f |
(a : A ) => number |
xs |
A [] |
A
[]
Example
const list = [ { id: 1 }, { id: 3 }, { id: 2 } ]
sortBy(({ id }) => id, list) // => [ { id: 1 }, { id: 2 }, { id: 3 } ]
▸ sortObject(o
): AnyMap
Sort an object (recursively)
Name | Type |
---|---|
o |
AnyMap |
Example
sortObject({ b: 'c', a: 'd' }) // => { a: 'd', b: 'c' }
▸ span<A
>(pred
, arr
): [A
[], A
[]]
Similar to takeWhile
: returns a tuple
of all elements that pass predicate
and all elements that did not
Name |
---|
A |
Name | Type |
---|---|
pred |
(a : A ) => boolean |
arr |
A [] |
[A
[], A
[]]
Example
span(lessThanThree, [ 1, 2, 3, 4 ]) // => [ [ 1, 2 ], [ 3, 4 ] ]
▸ splitAt<A
>(num
, arr
): [A
[], A
[]]
Like Haskell's splitAt
splitAt n xs returns a tuple xs prefix of length n and second element is remainder of list
Name |
---|
A |
Name | Type |
---|---|
num |
number |
arr |
A [] |
[A
[], A
[]]
Example
splitAt(1, [ [ 'a', 'b' ], 'c' ]) // => [ [ [ 'a', 'b' ] ], [ 'c' ] ]
▸ store(state?
): Store
A very simple store implementation (think Redux-like)
Name | Type |
---|---|
state |
AnyMap |
Example
const state = store()
state.subscribe((next, prev) => next.foo)
state.update({ foo: 'bar' })
▸ stripAnsi(s?
): string
Remove ANSI escapes from string
Name | Type | Default value |
---|---|---|
s |
string |
'' |
string
Example
stripAnsi(colorize.blue('hello')) // => 'hello'
▸ sub(ns
): number
Subtract any amount of numbers
Name | Type |
---|---|
ns |
number [] |
number
Example
sub([ 4, 3, 2, 1 ]) // => -2
▸ sum(nums
): number
Sum an array of numbers
Name | Type |
---|---|
nums |
number [] |
number
Example
sum([ 1, 2, 3, 4 ]) // => 10
▸ tail<T
>(arr
): T
[]
All elements except first
Name |
---|
T |
Name | Type |
---|---|
arr |
T [] |
T
[]
Example
tail([ 1, 2, 3, 4 ]) // => [ 2, 3, 4 ]
▸ take<A
>(n
, a
): A
[]
Like Haskell's take
Name |
---|
A |
Name | Type |
---|---|
n |
number |
a |
A [] |
A
[]
Example
take(2, [ 1, 2, 3 ]) // => [ 1, 2 ]
▸ takeLast<A
>(num
, arr
): A
[]
Takes the last n items of array
Name |
---|
A |
Name | Type |
---|---|
num |
number |
arr |
A [] |
A
[]
Example
takeLast(2, [ 1, 2, 3, 4 ]) // => [ 3, 4 ]
▸ takeWhile<A
>(pred
, arr
): A
[]
Returns elements that pass predicate, until failure (ignores matches after failure)
Name |
---|
A |
Name | Type |
---|---|
pred |
(a : A , n : number ) => boolean |
arr |
A [] |
A
[]
Example
takeWhile((x) => x < 3, [ 1, 2, 3, 4 ]) // => [ 1, 2 ]
▸ tap(msg
): <A>(a
: A
) => A
Log a value to console, and return that value
Name | Type |
---|---|
msg |
string |
fn
▸ <A
>(a
): A
Name |
---|
A |
Name | Type |
---|---|
a |
A |
A
Example
const logger = tap('This is the thing!')
logger(2) // => this is the thing 2 ; 2
▸ touch(filePath
, contents?
): Promise
<void
>
Make a file if it doesn't exist already
Name | Type | Default value |
---|---|---|
filePath |
string |
undefined |
contents |
string |
'' |
Promise
<void
>
Example
touch('foo.txt')
touch('foo.txt', 'contentx')
▸ truncate(str
, limit
, tail?
): string
Truncate a string
Name | Type | Default value |
---|---|---|
str |
string |
undefined |
limit |
number |
undefined |
tail |
string |
'…' |
string
Example
truncate('asdf asdf asdf asdf', 4) // => 'asd…'
truncate('asdf asdf asdf asdf', 8, ' etc.') // => 'asd etc.'
▸ tryJson<T
>(o
): AnyMap
| T
Tries to parse JSON, if is JSON
Name |
---|
T |
Name | Type |
---|---|
o |
T |
AnyMap
| T
Example
tryJson('[1,2]') // => [ 1, 2 ]
tryJson([ 1, 2 ]) // => [ 1, 2 ]
▸ typeOf(a
): string
Enhanced typeof
Name | Type |
---|---|
a |
any |
string
Example
typeOf('a') // => 'string'
typeOf(new Date()) // => 'date'
▸ union<A
>(...xs
): A
[]
Get the union of any amount of arrays
Name |
---|
A |
Name | Type |
---|---|
...xs |
A [][] |
A
[]
Example
union([ 1, 2, 3 ], [ 2, 3, 4 ]) // => [ 1, 2, 3, 4 ]
▸ uniq<A
>(arr
): A
[]
Uniq an array
Name |
---|
A |
Name | Type |
---|---|
arr |
A [] |
A
[]
Example
uniq([ 1, 1, 2, 3 ]) // => [ 1, 2, 3 ]
▸ uniqBy(el
, xs
): AnyMap
[]
Unique an array by a string key of objects in array Returns only the values of the passed key
Name | Type |
---|---|
el |
string |
xs |
AnyMap [] |
AnyMap
[]
Example
const a = [ { foo: 'foo' }, { foo: 'bar' }, { foo: 'foo' } ]
uniqBy('foo', a) // => [ { foo: 'foo' }, { foo: 'bar' } ]
▸ unless<A
>(cond
, fn
): null
| A
Call a function if the condition is falsey
Name |
---|
A |
Name | Type |
---|---|
cond |
boolean |
fn |
() => A |
null
| A
Example
unless(() => true, false) // => true
▸ unlines(arr
): string
Join an array with lines
Name | Type |
---|---|
arr |
string [] |
string
Example
unlines([ 'foo', 'bar' ]) // => 'foo\nbar'
▸ until<T
>(p
, f
): (...args
: T
[]) => T
Until p f yields result of applying f until p holds
Name |
---|
T |
Name | Type |
---|---|
p |
(x : T ) => boolean |
f |
(...args : T []) => T |
fn
▸ (...args
): T
Name | Type |
---|---|
...args |
T [] |
T
Example
until(equals5, increment)(2) // => 5
▸ unwords(arr
): string
Join an array with spaces
Name | Type |
---|---|
arr |
string [] |
string
Example
unwords([ 'foo', bar ]) // => 'foo bar'
▸ watch(filePath
, cb
): void
Watch a file for changes, and call the function
Name | Type |
---|---|
filePath |
string |
cb |
(event : string , filename : null | string ) => void |
void
Example
watch('./foo', console.log)
▸ words(str
): string
[]
Split a string on spaces
Name | Type |
---|---|
str |
string |
string
[]
Example
words('foo bar') // => [ 'foo', 'bar' ]
▸ writeJson(path
, data
, indent
): Promise
<void
>
Write JSON from a stringifiable value
Name | Type |
---|---|
path |
string |
data |
any |
indent |
number |
Promise
<void
>
Example
await writeJson('foo.json', someObject, 4)