Summary
No dependency, single file helpers. Skewed towards ethereum.
Usage
npm i @appliedblockchain/helpers
Api
Table of Contents
addressOfPrivateKey
Parameters
Returns Buffer ethereum address of provided privateKey
.
addressOfPublicKey
Returns ethereum address of provided public key.
Parameters
Returns Buffer
blockHashOf
Parameters
url
stringblockNumberOrTag
("earliest"
| "latest"
| "pending"
| number) (optional, default 'latest'
)timeout
number (optional, default defaultTiemout
)
Returns Promise<string?> block hash as hex0x string or null from provided ethereum jsonrpc endpoint url
in specified timeout
; null
if timeout has been reached.
booleanOfString
Parses string and returns true
for "true"
, "on"
, "yes"
and "1"
strings, false
otherwise.
Parameters
Returns boolean
bufferOfHexOrHex0x
Returns Buffer
representation of provided hex (ie. "ff"
) or hex0x (ie. "0xff"
) string.
Parameters
defaultCmp
Default comparision function.
Parameters
Returns (-1
| 0
| 1
)
eachPromise
Similar to Promise.all
but yields after each promise settlement.
Exceptions are captured and results are yielded as [ err, result, i ]
tuple.
Parameters
Returns AsyncGenerator<[Error?, T?, number], any, any>
flagOfBoolean
Returns boolean value of command line argument.
Parameters
Examples
const flagOfBoolean = require('@appliedblockchain/helpers/flag-of-boolean')
console.log(flagOfBoolean('--bar'))
heightOf
Parameters
Returns Promise<number> Height of the chain from provided ethereum jsonrpc endpoint url
or NaN
if timeout has been reached or any other problem occured.
hex0xOfNumber
Parameters
Returns string 0x-prefixed hex string representation of a safe, non-negative integer number.
isLikeDateString
Checks if value
looks like YYYY-MM-DD
date string.
Parameters
Returns boolean
isPending
Returns true
if promise has not settled yet, false
otherwise.
Parameters
Returns boolean
isSafeNonNegative
Returns true
if value is safe integer greater than or equal to zero, false
otherwise.
Parameters
Returns boolean
isSafePositive
Parameters
Returns boolean true
if value is safe integer greater than zero, false
otherwise.
makeRetrace
Captures extra stack trace, usage await foo().catch(makeRetrace())
.
noop
Identity function.
randomUint32
Returns number random unsigned integer that fits 32 bits.
shSync
Executes simple shell command.
Parameters
cmd
string$1
any (optional, default {}
)
$1.trim
(optional, default true
)
sortedDifference
Computes xs - ys difference in linear time between two, unique, ascending arrays of values.
Comparision function can be flipped for descending arrays.
Leaks values of arrays in thrown error.
Parameters
xs
Array<T>ys
Array<T>cmp
function (T, T): (-1
| 0
| 1
) (optional, default defaultCmp
)
Returns Array<T>
sortedIntersection
Computes intersection in linear time between two, unique, ascending arrays of values.
Comparision function can be flipped for descending arrays.
Leaks values of arrays in thrown error.
Parameters
xs
Array<T>ys
Array<T>cmp
function (T, T): (-1
| 0
| 1
) (optional, default defaultCmp
)
Returns Array<T>
defaultTimeout
Expire cached nonces after 1 minute.
spread
Spread n
calls to f
function ms
milliseconds apart.
Parameters
Returns Promise<Array<any>>
License
MIT License
Copyright 2019 Applied Blockchain
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.