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

The global module name

The fallback CommonJS module name

Returns

The loaded module

@ src/core/Common.js:539

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

The functions to chain.

Returns

FunctionA new function that calls the passed functions in order.

@ src/core/Common.js:457

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

The base object

The path relative to base

The function to chain after the original

Returns

FunctionThe chained function that replaced the original

@ src/core/Common.js:523

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

The base object

The path relative to base

The function to chain before the original

Returns

FunctionThe chained function that replaced the original

@ src/core/Common.js:507

Matter.Common.choose

(choices)

Object

Randomly chooses a value from a list with equal probability. The function uses a seeded random generator.

Parameters

Returns

ObjectA random choice object from the array

@ src/core/Common.js:162

Matter.Common.clamp

(value, min, max)

Number

Returns the given value clamped between a minimum and maximum value.

Parameters

Returns

NumberThe value clamped between min and max inclusive

@ src/core/Common.js:227

Matter.Common.clone

(obj, deep)

Creates a new clone of the object, if deep is true references will also be cloned.

Parameters

  • deep Bool

Returns

obj cloned

@ src/core/Common.js:59

Matter.Common.colorToNumber

(colorString)

Number

Converts a CSS hex colour string into an integer.

Parameters

Returns

NumberAn integer representing the CSS hex string

@ src/core/Common.js:291

Matter.Common.extend

(obj, deep)

Extends the object in the first argument using the object in the second argument.

Parameters

Returns

obj extended

@ src/core/Common.js:17

Matter.Common.get

(obj, path, [begin], [end])

Gets a value from base relative to the path string.

Parameters

The base object

The path relative to base, e.g. 'Foo.Bar.baz'

Path slice begin

Path slice end

Returns

The object at the given path

@ src/core/Common.js:110

Matter.Common.indexOf

(haystack, needle)

Number

A cross browser compatible indexOf implementation.

Parameters

Returns

NumberThe position of needle in haystack, otherwise -1.

@ src/core/Common.js:370

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

The objects to log.

@ src/core/Common.js:337

Matter.Common.isArray

(obj)

Boolean

Returns true if the object is an array.

Parameters

Returns

BooleanTrue if the object is an array, otherwise false

@ src/core/Common.js:187

Matter.Common.isElement

(obj)

Boolean

Returns true if the object is a HTMLElement, otherwise false.

Parameters

Returns

BooleanTrue if the object is a HTMLElement, otherwise false

@ src/core/Common.js:173

Matter.Common.isFunction

(obj)

Boolean

Returns true if the object is a function.

Parameters

Returns

BooleanTrue if the object is a function, otherwise false

@ src/core/Common.js:197

Matter.Common.isPlainObject

(obj)

Boolean

Returns true if the object is a plain object.

Parameters

Returns

BooleanTrue if the object is a plain object, otherwise false

@ src/core/Common.js:207

Matter.Common.isString

(obj)

Boolean

Returns true if the object is a string.

Parameters

Returns

BooleanTrue if the object is a string, otherwise false

@ src/core/Common.js:217

Matter.Common.keys

(obj)

String[]

Returns the list of keys for the given object.

Parameters

Returns

String[]keys

@ src/core/Common.js:70

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

The objects to log.

@ src/core/Common.js:325

Matter.Common.map

(list, func)

Array

A cross browser compatible array map implementation.

Parameters

Returns

ArrayValues from list transformed by func.

@ src/core/Common.js:389

Matter.Common.nextId

()→ Number

Returns the next unique sequential ID.

Returns

NumberUnique sequential ID

@ src/core/Common.js:361

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

@ src/core/Common.js:253

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

Returns

NumberA random number between min and max inclusive

@ src/core/Common.js:271

Matter.Common.set

(obj, path, val, [begin], [end])

Sets a value on base relative to the given path string.

Parameters

The base object

The path relative to base, e.g. 'Foo.Bar.baz'

The value to set

Path slice begin

Path slice end

Returns

Pass through val for chaining

@ src/core/Common.js:129

Matter.Common.shuffle

(array)

Array

Shuffles the given array in-place. The function uses a seeded random generator.

Parameters

Returns

Arrayarray shuffled randomly

@ src/core/Common.js:145

Matter.Common.sign

(value)

Number

Returns the sign of the given value.

Parameters

Returns

Number-1 if negative, +1 if 0 or positive

@ src/core/Common.js:243

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

Returns

ArrayPartially ordered set of vertices in topological order.

@ src/core/Common.js:410

Matter.Common.values

(obj)

Array

Returns the list of values for the given object.

Parameters

Returns

ArrayArray of the objects property values

@ src/core/Common.js:87

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

The objects to log.

@ src/core/Common.js:349

Item Index

Methods

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

Number

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

@ src/core/Common.js:309