Matter.Common
The Matter.Common
module contains utility functions that are common to all modules.
Methods
Matter.Common.chain
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
Function multipleThe functions to chain.
Returns
A new function that calls the passed functions in order.
Matter.Common.chainPathAfter
Chains a function to excute after the original function on the given path
relative to base
.
See also docs for Common.chain
.
Parameters
Returns
The chained function that replaced the original
Matter.Common.chainPathBefore
Chains a function to excute before the original function on the given path
relative to base
.
See also docs for Common.chain
.
Parameters
Returns
The chained function that replaced the original
Matter.Common.choose
Randomly chooses a value from a list with equal probability. The function uses a seeded random generator.
Parameters
-
choices
Array
Returns
A random choice object from the array
Matter.Common.clamp
Returns the given value clamped between a minimum and maximum value.
Returns
The value clamped between min and max inclusive
Matter.Common.clone
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
Converts a CSS hex colour string into an integer.
Parameters
-
colorString
String
Returns
An integer representing the CSS hex string
Matter.Common.deprecated
Shows a deprecated console warning when the function on the given object is called. The target function will be replaced with a new function that first shows the warning and then calls the original function.
Parameters
Matter.Common.extend
Extends the object in the first argument using the object in the second argument.
Returns
obj extended
Matter.Common.get
Gets a value from base
relative to the path
string.
Parameters
Returns
The object at the given path
Matter.Common.getDecomp
()Returns the poly-decomp library module provided through Common.setDecomp
,
otherwise returns the global decomp
if set.
Returns
The poly-decomp library module if provided.
Matter.Common.indexOf
A cross browser compatible indexOf implementation.
Returns
The position of needle in haystack, otherwise -1.
Matter.Common.info
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
Object multipleThe objects to log.
Matter.Common.isArray
Returns true if the object is an array.
Parameters
-
obj
Object
Returns
True if the object is an array, otherwise false
Matter.Common.isElement
Returns true if the object is a HTMLElement, otherwise false.
Parameters
-
obj
Object
Returns
True if the object is a HTMLElement, otherwise false
Matter.Common.isFunction
Returns true if the object is a function.
Parameters
-
obj
Object
Returns
True if the object is a function, otherwise false
Matter.Common.isPlainObject
Returns true if the object is a plain object.
Parameters
-
obj
Object
Returns
True if the object is a plain object, otherwise false
Matter.Common.isString
Returns true if the object is a string.
Parameters
-
obj
Object
Returns
True if the object is a string, otherwise false
Matter.Common.keys
Returns the list of keys for the given object.
Parameters
-
obj
Object
Returns
keys
Matter.Common.log
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
Object multipleThe objects to log.
Matter.Common.map
A cross browser compatible array map implementation.
Returns
Values from list transformed by func.
Matter.Common.nextId
()Returns the next unique sequential ID.
Returns
Unique sequential ID
Matter.Common.now
()Returns the current timestamp since the time origin (e.g. from page load). The result is in milliseconds and will use high-resolution timing if available.
Returns
the current timestamp in milliseconds
Matter.Common.random
Returns a random value between a minimum and a maximum value inclusive. The function uses a seeded random generator.
Returns
A random number between min and max inclusive
Matter.Common.set
Sets a value on base
relative to the given path
string.
Parameters
Returns
Pass through val
for chaining
Matter.Common.setDecomp
Provide the poly-decomp library module to enable
concave vertex decomposition support when using Bodies.fromVertices
e.g. Common.setDecomp(require('poly-decomp'))
.
Parameters
-
decomp
ObjectThe poly-decomp library module.
Matter.Common.shuffle
Shuffles the given array in-place. The function uses a seeded random generator.
Parameters
-
array
Array
Returns
array shuffled randomly
Matter.Common.sign
Returns the sign of the given value.
Parameters
-
value
Number
Returns
-1 if negative, +1 if 0 or positive
Matter.Common.topologicalSort
Takes a directed graph and returns the partially ordered set of vertices in topological order. Circular dependencies are allowed.
Parameters
-
graph
Object
Returns
Partially ordered set of vertices in topological order.
Matter.Common.values
Returns the list of values for the given object.
Parameters
-
obj
Object
Returns
Array of the objects property values
Matter.Common.warn
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
Object multipleThe objects to log.
Properties / Options
The following properties if specified below are for objects created by Matter.Common.create
and may be passed to it as options
.
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