Chaplin.utils → Source

Chaplin’s utils provide common functions for use throughout the project.

These functions are generic and not related to any specific Chaplin components. Useful functions for messing with Chaplin are available in Chaplin.helpers.

beget(parent)

A standard Javascript helper function that creates an object which delegates to another object. (see Douglas Crockford's Javascript: The Good Parts for more details). Uses Object.create when available, and falls back to a polyfill if not present.

readonly(object, [*properties])

Makes properties of object read-only so they cannot be overwritten. The success of this operation depends on the current environment’s support.

getPrototypeChain(object)

Gets the whole chain of prototypes for object.

getAllPropertyVersions(object, property)

Get all different value versions for property from object’s prototype chain. Usage:

class A
  prop: 1
class B extends A
  prop: 2

b = new B
getAllPropertyVersions b, 'prop'  # => [1, 2]
function A() {}
A.prototype.prop = 1;

function B() {}
B.prototype = Object.create(A);

var b = new B;
getAllPropertyVersions(b, 'prop'); // => [1, 2]

upcase(str)

Ensure the first character of str is capitalized

utils.upcase 'larry bird' # 'Larry bird'
utils.upcase 'AIR'        # 'AIR'
utils.upcase('larry bird'); // 'Larry bird'
utils.upcase('AIR');        // 'AIR'

modifierKeyPressed(event)

Looks at an event object event to determine if the shift, alt, ctrl, or meta keys were pressed. Useful in link click handling (i.e. if you need ctrl-click or shift-click to open the link in a new window).