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 an AppContainer 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 symbol
  • symbol: 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 pattern
  • channel: test whether a value is a channel
  • buffer: test whether a value is a Buffer
  • iterator: test whether a value is a iterator
  • iterable: test whether a value is a iterable
  • promise: test whether a value is promise
  • object: test whether a value is an object
  • array: test whether a value is an array
  • string: test whether a value is a string
  • number: test whether a value is a number
  • bool: test whether a value is a bool
  • func: test whether a value is a function
  • undef: test whether a value is null or undefined
  • notUndef: test whether a value is NOT null and undefined

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.

results matching ""

    No results matching ""