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 anAppContainerinstance.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-componentrequires 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 anobjectarray: test whether a value is anarraystring: test whether a value is astringnumber: test whether a value is anumberbool: test whether a value is aboolfunc: test whether a value is afunctionundef: test whether a value isnullorundefinednotUndef: test whether a value is NOTnullandundefined
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.