Spork.js is a collection of small functions.
https://www.npmjs.com/package/@mitsunee/spork
Via yarn/npm: Install the package from the registry:
npm install --save-dev @mitsunee/spork
# or
yarn add -D @mitsunee/spork
Spork contains separate packages for the module and browser version. If you are using Spork in a browser please specify this during the import. Both require
and ES6 import
are supported.
Import the package:
// All modules
import Spork from "@mitsunee/spork"; // for node script modules
import Spork from "@mitsunee/spork/browser"; // for in-browser use
// or specific module(s)
import { range, clamp } from "@mitsunee/spork"; // for node script modules
import { range, clamp } from "@mitsunee/spork/browser"; // for in-browser use
For Browsers: If you are not using npm/yarn you can also download the latest spork-{version}.js
or spork-{version}.min.js
from ./browser and include it manually.
<script src="spork-{version}.min.js"></script>
Spork offers the following Modules:
clamp
v1.0.0+
Equivalent to what Math.clamp will hopefully one day be
Returns the best possible value between a value, minimum and maximum
clamp(value: Number, min: Number, max: Number): (Number|Boolean)
- Number
value
- The value to be clamped - Number
min
- The minimum value to be returned - Number
max
- The maximum value to be returned
Returns: Number (or false for invalid arguments)
range
v1.0.0+
Generates an Array with all Numbers in a specified range
range(start: Number [, stop: Number [, step: Number [, maxLength: Number]]]): Array
- Number start - Start value. If this is the only argument this is length instead and the start value will be 0.
- Number stop (optional, see above) - stop value
- Number step (optional, default: 1) - steps between values
- Number maxLength (optional) - maximum number of items in the resulting Array
To skip optional arguments please use undefined
.
range(3, undefined, 0.5);
// [ 0, 0.5, 1, 1.5, 2, 2.5 ]
Returns: Array (or false if no valid argument was provided)
fetchJson
v1.0.0+, async (Node script version requires package node-fetch
)
Asynchronous wrapper function for fetch that automatically parses received json data into an object
fetchJson(url: String [, option: Mixed]): (Object|Boolean)
- String
url
- the url to fetch - Mixed
option
(optional) - either:- Object: your custom init (note that only typeof is checked, validity is not questioned)
- String
"forceReload"
for the provided forceReload header - Boolean false (default)
Returns: Object (potentially Array) or false if something went wrong (see error in console)
interval
v1.2.0+
Wrapper class for setInterval
constructor: Creates new instance.
new interval(callback: Function, intervalLength: Number[, intervalLengthUnit: String])
- Function
callback
- callback function to run on each interval - Number
intervalLength
- the length of each interval - String
intervalLengthUnit
(optional, default: "ms") - the unit for the length given
start: Starts the interval
myInterval.start();
Returns: Boolean - true
if interval was started. Rejects with false
if interval was already running.
stop: Stops the interval
myInterval.stop();
Returns: Boolean - true
if interval was stopped. Rejects with false
if interval wasn't running.
setTime: While not running set/change the interval length
myInterval.setTime(intervalLength: Number[, intervalLengthUnit: String])
- Number
intervalLength
- the length of each interval - String
intervalLengthUnit
(optional, default: "ms") - the unit for the length given
Returns: Boolean - true
if new time was set. Rejects with false
if interval was running.
setCallback: While not running set/change the callback function
myInterval.setCallback(callback: Function)
- Function
callback
- callback function to run on each interval
Returns: Boolean - true
if new callback was set. Rejects with false
if interval was running.
startTime
(Number|null): timestamp of when the interval was started.null
if it's not currently runningrunning
(Boolean):true
if the interval is running,false
if not.
timeout
v1.2.0+
Wrapper class for setTimeout
constructor: Creates new instance.
new timeout(callback: Function, timeoutLength: Number[, timeoutLengthUnit: String])
- Function
callback
- callback function to run after the timeout finishes - Number
timerLength
- the length of the timeout - String
timerLengthUnit
(optional, default: "ms") - the unit for the length given
start: Starts the timeout
myTimeout.start();
Returns: Boolean - true
if timeout was started. Rejects with false
if timeout was already running.
stop: Stops the timeout
myTimeout.cancel();
Returns: Boolean - true
if timeout was canceled. Rejects with false
if timeout wasn't running.
setTime: While not running set/change the timeout length
myTimeout.setTime(timeoutLength: Number[, timeoutLengthUnit: String])
- Number
timerLength
- the length of the timeout - String
timerLengthUnit
(optional, default: "ms") - the unit for the length given
Returns: Boolean - true
if new time was set. Rejects with false
if timeout was running.
setCallback: While not running set/change the callback function
myTimeout.setCallback(callback: Function)
- Function
callback
- callback function to run after the timeout finishes
Returns: Boolean - true
if new callback was set. Rejects with false
if timeout was running.
startTime
(Number|null): timestamp of when the timeout was started.null
if it's not currently runningrunning
(Boolean):true
if the timeout is running,false
if not.
Time units used by interval and timeout
- Hours as:
"h"
,"hr"
,"hrs"
,"hour"
,"hours"
- Minutes as:
"m"
,"min"
,"mins"
,"minute"
,"minutes"
- Seconds as:
"s"
,"sec"
,"secs"
,"second"
,"seconds"
- Milliseconds as:
"ms"
- Unrecognized strings are treated as Milliseconds
log (node-only)
v1.2.0+, not available for browsers (requires package chalk
)
Logs any value, objects are printed in prettified json syntax.
log(value: Any[, maxdepth: Number|false])
- Any
value
- Value to log - Number
maxdepth
(optional, default: false) - maxdepth for objects/arrays.false
acts like Infinity
- 1.2.0
- Added
interval
,timeout
andlog
modules - (Hopefully) fixed errors related to symlinks
- Added
- 1.1.x
- Fixed external dependencies, moved browser builds