- Matter.Common
- Methods
- Matter.Common._requireGlobal
- Matter.Common.chain
- Matter.Common.chainPathAfter
- Matter.Common.chainPathBefore
- Matter.Common.choose
- Matter.Common.clamp
- Matter.Common.clone
- Matter.Common.colorToNumber
- Matter.Common.extend
- Matter.Common.get
- Matter.Common.indexOf
- Matter.Common.info
- Matter.Common.isArray
- Matter.Common.isElement
- Matter.Common.isFunction
- Matter.Common.isPlainObject
- Matter.Common.isString
- Matter.Common.keys
- Matter.Common.log
- Matter.Common.map
- Matter.Common.nextId
- Matter.Common.now
- Matter.Common.random
- Matter.Common.set
- Matter.Common.shuffle
- Matter.Common.sign
- Matter.Common.topologicalSort
- Matter.Common.values
- Matter.Common.warn
- Item Index
- Properties
- Methods
Matter.Common
Defined in: src/core/Common.js:1
The Matter.Common
module contains utility functions that are common to all modules.
Methods
Matter.Common._requireGlobal
(globalName, moduleName)
→ private
Used to require external libraries outside of the bundle. It first looks for the globalName
on the environment's global namespace. If the global is not found, it will fall back to using the standard require
using the moduleName
.
Parameters
globalName
String
The global module name
moduleName
String
The fallback CommonJS module name
Returns
The loaded module
Matter.Common.chain
(funcs…)
→ Function
Takes n functions as arguments and returns a new function that calls them in order. The arguments applied when calling the new function will also be applied to every function passed. The value of this
refers to the last value returned in the chain that was not undefined
. Therefore if a passed function does not return a value, the previously returned value is maintained. After all passed functions have been called the new function returns the last returned value (if any). If any of the passed functions are a chain, then the chain will be flattened.
Parameters
funcs
Functionmultiple
The functions to chain.
Returns
FunctionA new function that calls the passed functions in order.
Matter.Common.chainPathAfter
(base, path, func)
→ Function
Chains a function to excute after the original function on the given path
relative to base
. See also docs for Common.chain
.
Parameters
base
Object
The base object
path
String
The path relative to base
func
Function
The function to chain after the original
Returns
FunctionThe chained function that replaced the original
Matter.Common.chainPathBefore
(base, path, func)
→ Function
Chains a function to excute before the original function on the given path
relative to base
. See also docs for Common.chain
.
Parameters
base
Object
The base object
path
String
The path relative to base
func
Function
The function to chain before the original
Returns
FunctionThe chained function that replaced the original
Matter.Common.choose
(choices)
→ Object
Randomly chooses a value from a list with equal probability. The function uses a seeded random generator.
Parameters
choices
Array
Returns
ObjectA random choice object from the array
Matter.Common.clamp
(value, min, max)
→ Number
Returns the given value clamped between a minimum and maximum value.
Parameters
value
Number
min
Number
max
Number
Returns
NumberThe value clamped between min and max inclusive
Matter.Common.clone
(obj, deep)
→
Creates a new clone of the object, if deep is true references will also be cloned.
Parameters
obj
Object
deep
Bool
Returns
obj cloned
Matter.Common.colorToNumber
(colorString)
→ Number
Converts a CSS hex colour string into an integer.
Parameters
colorString
String
Returns
NumberAn integer representing the CSS hex string
Matter.Common.extend
(obj, deep)
→
Extends the object in the first argument using the object in the second argument.
Parameters
obj
Object
deep
Boolean
Returns
obj extended
Matter.Common.get
(obj, path, [begin], [end])
→
Gets a value from base
relative to the path
string.
Parameters
obj
Object
The base object
path
String
The path relative to base
, e.g. 'Foo.Bar.baz'
[begin]
Numberoptional
Path slice begin
[end]
Numberoptional
Path slice end
Returns
The object at the given path
Matter.Common.indexOf
(haystack, needle)
→ Number
A cross browser compatible indexOf implementation.
Parameters
haystack
Array
needle
Object
Returns
NumberThe position of needle in haystack, otherwise -1.
Matter.Common.info
(objs…)
Shows a console.info
message only if the current Common.logLevel
allows it. The message will be prefixed with 'matter-js' to make it easily identifiable.
Parameters
objs
Objectmultiple
The objects to log.
Matter.Common.isArray
(obj)
→ Boolean
Returns true if the object is an array.
Parameters
obj
Object
Returns
BooleanTrue if the object is an array, otherwise false
Matter.Common.isElement
(obj)
→ Boolean
Returns true if the object is a HTMLElement, otherwise false.
Parameters
obj
Object
Returns
BooleanTrue if the object is a HTMLElement, otherwise false
Matter.Common.isFunction
(obj)
→ Boolean
Returns true if the object is a function.
Parameters
obj
Object
Returns
BooleanTrue if the object is a function, otherwise false
Matter.Common.isPlainObject
(obj)
→ Boolean
Returns true if the object is a plain object.
Parameters
obj
Object
Returns
BooleanTrue if the object is a plain object, otherwise false
Matter.Common.isString
(obj)
→ Boolean
Returns true if the object is a string.
Parameters
obj
Object
Returns
BooleanTrue if the object is a string, otherwise false
Matter.Common.keys
(obj)
→ String[]
Returns the list of keys for the given object.
Parameters
obj
Object
Returns
String[]keys
Matter.Common.log
(objs…)
Shows a console.log
message only if the current Common.logLevel
allows it. The message will be prefixed with 'matter-js' to make it easily identifiable.
Parameters
objs
Objectmultiple
The objects to log.
Matter.Common.map
(list, func)
→ Array
A cross browser compatible array map implementation.
Parameters
list
Array
func
Function
Returns
ArrayValues from list transformed by func.
Matter.Common.nextId
()→ Number
Returns the next unique sequential ID.
Returns
NumberUnique sequential ID
Matter.Common.now
()→ Number
Returns the current timestamp since the time origin (e.g. from page load). The result will be high-resolution including decimal places if available.
Returns
Numberthe current timestamp
Matter.Common.random
(min, max)
→ Number
Returns a random value between a minimum and a maximum value inclusive. The function uses a seeded random generator.
Parameters
min
Number
max
Number
Returns
NumberA random number between min and max inclusive
Matter.Common.set
(obj, path, val, [begin], [end])
→
Sets a value on base
relative to the given path
string.
Parameters
obj
Object
The base object
path
String
The path relative to base
, e.g. 'Foo.Bar.baz'
val
Object
The value to set
[begin]
Numberoptional
Path slice begin
[end]
Numberoptional
Path slice end
Returns
Pass through val
for chaining
Matter.Common.shuffle
(array)
→ Array
Shuffles the given array in-place. The function uses a seeded random generator.
Parameters
array
Array
Returns
Arrayarray shuffled randomly
Matter.Common.sign
(value)
→ Number
Returns the sign of the given value.
Parameters
value
Number
Returns
Number-1 if negative, +1 if 0 or positive
Matter.Common.topologicalSort
(graph)
→ Array
Takes a directed graph and returns the partially ordered set of vertices in topological order. Circular dependencies are allowed.
Parameters
graph
Object
Returns
ArrayPartially ordered set of vertices in topological order.
Matter.Common.values
(obj)
→ Array
Returns the list of values for the given object.
Parameters
obj
Object
Returns
ArrayArray of the objects property values
Matter.Common.warn
(objs…)
Shows a console.warn
message only if the current Common.logLevel
allows it. The message will be prefixed with 'matter-js' to make it easily identifiable.
Parameters
objs
Objectmultiple
The objects to log.
Item Index
Methods
- _requireGlobal
- chain
- chainPathAfter
- chainPathBefore
- choose
- clamp
- clone
- colorToNumber
- extend
- get
- indexOf
- info
- isArray
- isElement
- isFunction
- isPlainObject
- isString
- keys
- log
- map
- nextId
- now
- random
- set
- shuffle
- sign
- topologicalSort
- values
- warn
Properties
The following properties are specified for objects created by <span class="prefix">Matter.</span>.create
and for objects passed to it via the options
argument.
Properties
The following properties are specified for objects created by Matter.Common.create
and for objects passed to it via the options
argument.
Common.Common.logLevel
The console logging level to use, where each level includes all levels above and excludes the levels below. The default level is 'debug' which shows all console messages.
Possible level values are:
- 0 = None
- 1 = Debug
- 2 = Info
- 3 = Warn
- 4 = Error
Default: 1