utils
Overview
utils
module provides a list of useful utility functions.
utils.is
utils.is
provides a list of useful data type checking functions:
appContainer
: test whether a value is anAppContainer
instance.managedComponentInstance
: test whether a value is a managed component instance.namespacedAction
: test whether a value is a namespacedAction.action
: test whether a value is an valid action object.fractal-component
requires action type must be a symbolsymbol
: test whether a value is a symbol.
You should use this function to test whether a value is a symbol
rather than using typeof
operator for better performance & compatibility. Since babel 7, loose
mode of @babel/preset-env
will now automatically exclude the typeof-symbol transform.
pattern
: test whether a value is an action patternchannel
: test whether a value is a channelbuffer
: test whether a value is a Bufferiterator
: test whether a value is a iteratoriterable
: test whether a value is a iterablepromise
: test whether a value is promiseobject
: test whether a value is anobject
array
: test whether a value is anarray
string
: test whether a value is astring
number
: test whether a value is anumber
bool
: test whether a value is abool
func
: test whether a value is afunction
undef
: test whether a value isnull
orundefined
notUndef
: test whether a value is NOTnull
andundefined
Example:
import { utils } from "fractal-component";
const actionValid = {
type : Symbol("ACTION_TYPE_ONE")
};
const actionInvalid = {
type : "ACTION_TYPE_TWO"
};
console.log(utils.is.action(actionValid)); // --- true
console.log(utils.is.action(actionInvalid)); // --- false
utils.isInNode
Whether code is running in nodejs environment.
import { utils } from "fractal-component";
utils.isInNode(); //--- return true if running in nodejs otherwise return false
utils.isDevMode
Whether code is running under development
mode. If you use webpack
, You can use the mode option to set development
mode when create code bundle. e.g.
webpack --mode=development
By default, Redux DevTools
is only allowed to connect to your App if you bundle your code under development
mode. You can change this behivour by reduxDevToolsDevOnly
option when creates the AppContainer.
utils.getPackageVersion
Return fractal-component
version number.
utils.symbolToString
Get string representation of a symbol.
While you can call toString() on symbols, you can't use string concatenation with them (see here):
Symbol('foo') + 'bar'; // TypeError: Can't convert symbol to string
You want to use this function to get the string representation of a symbol
rather than using bar.toString()
as most minifier (e.g. UglifyJS) will replace bar.toString()
with ''+bar
in order to reduce code size when bundle your code. This will, however, produce an error when bar
is a symbol
.