No dependency, single file helpers.
npm install @appliedblockchain/helpersNo dependency, single file helpers. Skewed towards ethereum.
npm i @appliedblockchain/helpers
#### Table of Contents
- addressOfPrivateKey
- Parameters
- addressOfPublicKey
- Parameters
- blockHashOf
- Parameters
- booleanOfString
- Parameters
- bufferOfHexOrHex0x
- Parameters
- bufferOfHex
- Parameters
- bufferOfHex0x
- Parameters
- bufferOfUnsigned
- Parameters
- catchOf
- Parameters
- defaultCmp
- Parameters
- eachPromise
- Parameters
- evenZeroPaddedOf
- Parameters
- eventuallyTrue
- Parameters
- fibonacciOf
- Parameters
- hasKeys
- Parameters
- heightOf
- Parameters
- hexOfUnsigned
- Parameters
- hex0xOfUnsigned
- Parameters
- indexedByKey
- Parameters
- isLikeDateString
- Parameters
- isPending
- Parameters
- isSafePositive
- Parameters
- isSafeUnsigned
- Parameters
- jsonOfUrl
- Parameters
- makeRetrace
- noop
- objectOfStrings
- Parameters
- racePromises
- Parameters
- randomUint32
- shSync
- Parameters
- sortedDifference
- Parameters
- sortedIntersection
- Parameters
- defaultTimeout
- spread
- Parameters
- stopOfMonitor
- Parameters
- timeoutOf
- Parameters
#### Parameters
- privateKey Buffer
Returns Buffer ethereum address of provided privateKey.
Returns ethereum address of provided public key.
#### Parameters
- publicKey Buffer
Returns Buffer
#### Parameters
- url string
- blockNumberOrTag ("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.
Parses string and returns true for "true", "on", "yes" and "1" strings, false otherwise.
#### Parameters
- value string
Returns boolean
Returns Buffer representation of provided hex (ie. "ff") or hex0x (ie. "0xff") string.
#### Parameters
- value string
- Throws any TypeError On invalid input.
#### Parameters
- value string
Returns any buffer representation of provided hex string.
#### Parameters
- value string
Returns Buffer buffer representation of provided hex0x string.
#### Parameters
- value number
Returns Buffer the smallest buffer representation of provided safe, unsigned integer number. When input is 0 - empty buffer is returned.
#### Parameters
- message string
- result T
Returns function (error: Error): T
- **See:
**
Default comparision function.
#### Parameters
- x any
- y any
Returns (-1 \| 0 \| 1)
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>
#### Parameters
- value string
Returns any input string padded with 0 if length is odd, otherwise returns string as is.
Retries up to n times (roughtly seconds) call to f waiting for true result, which breaks the loop and returnstrue. If f didn't return true throws last result value.
#### Parameters
- f function (): Promise<R>
- n number (optional, default 30)
Returns Promise<(true | R)>
Yields fibonacci sequence capped to max (default max safe integer).
#### Parameters
- max number (optional, default defaultMax)
Returns void
Type: function (any): boolean
#### Parameters
- value
Returns any true if value has one or more keys, false otherwise.
#### Parameters
- url string
- timeout number (optional, default defaultTiemout)
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.
#### Parameters
- value number
Returns string even/byte-padded, hex string representation of a safe, unsigned number.
#### Parameters
- value number
Returns string 0x-prefixed hex string representation of a safe, unsigned number.
#### Parameters
- values Array<T>
- key string
Returns {} transposed entries from xs, indexed by key. Non-unique keys will overwrite previous values.
Non-existing keys will be indexed under undefined key.
Checks if value looks like YYYY-MM-DD date string.
#### Parameters
- value any
Returns boolean
Returns true if promise has not settled yet, false otherwise.
#### Parameters
- value Promise<any>
Returns boolean
#### Parameters
- value number
Returns boolean true if value is safe integer greater than zero, false otherwise.
#### Parameters
- value any
Returns boolean true if value is safe unsigned number, false otherwise.
Type: function (string): Promise<any>
#### Parameters
- url
Returns any json from basic http(s) get request on url.
Captures extra stack trace, usage await foo().catch(makeRetrace()).err argument is being modified with new stack value.
Identity function.
#### Parameters
Returns {} membership object (set) for provided list of elements (strings).
Like Promise.race but doen't complain about unhandled rejections that happen after race has settled.
#### Parameters
- promises Array<Promise<any>>
Returns Promise<any>
Returns number random unsigned integer that fits 32 bits.
Executes simple shell command.
#### Parameters
- cmd string
- $1 {trim: boolean?} (optional, default {})
- $1.trim (optional, default true)
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>
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>
Expire cached nonces after 12 seconds.
Spread n calls to f function ms milliseconds apart.
#### Parameters
- n number
- ms number
- f function (): Promise<any>
Creates monitor similar to setInterval but allows single execution at a time only. Concurrent invocation attempts
are ignored.
#### Parameters
- f function (): Promise<any>
- milliseconds number (optional, default 1*1000)
Returns any stop function.
#### Parameters
- milliseconds number
- message string (optional, default ` Timeout of ${String(milliseconds)}ms exceeded. )code
- (number \| string) (optional, default '@appliedblockchain/helpers/timeout'`)
Returns any cancellable, rejecting timeout promise.
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.