@cobalt-engine/utils

2.0.3 • Public • Published

Utils

Set of useful methods.

Module is a part of core functionality so you don't need to install it.

Link

var utils = require('utils');

API

Object

mixin(target, source, keys)

Copies properties from source object to target object. Returns target object. keys - array of property names to copy (if not specified - all properties will be copied).

toObject(keys, values)

Creates an object using keys as property names and values as property values.

mapToArray(map)

Represents map as array of its elements. Each element in the array is maps element extended with its id in the map. Example:

var object = {
		value1: { number: 3 },
		value2: { number: 4 }
	};

utils.mapToArray(object); // [{ id: 'value1', number: 3 }, { id: 'value2', number: 4 }]

The primitives

template(string, data)
  • string - template
  • data - data object

Interpolation of string. Example:

utils.template('Hello {name}', { name: 'Ann' }); // 'Hello Ann'

Insert the data from the data in pattern string.

rangeValue(value, min, max)

If value is beyond the range [min..max] the nearest limit will be returned. Example:

utils.rangeValue(10, 2, 5); // 5
utils.rangeValue(0, 2, 5); // 2
utils.rangeValue(3, 2, 5); // 3

Others

dispatchEvent(target, event, options)
  • target - element which receives event
  • event - event name
  • options - object with settings, transferred to constructor CustomEvent.

Options can include a field detail for transmission of additional information.

load(url, callback, async)

Simple XMLHttpRequest.

Observer

To create instance of Observer user Observer constructor:

var observer = new utils.Observer();

Instance of constructor has the following methods:

  • subscribe(event, callback) - adds callback for event. When event publishes Observer executes callback

  • unsubscribe(event, callback) - removes callback listener for event event

  • publish(event, data) - create an event event and send data to all listeners of this event.

  • destroy() - removes all added subscriptions

/@cobalt-engine/utils/

    Package Sidebar

    Install

    npm i @cobalt-engine/utils

    Weekly Downloads

    124

    Version

    2.0.3

    License

    MIT

    Unpacked Size

    16.8 kB

    Total Files

    10

    Last publish

    Collaborators

    • oleg322
    • v.kobyletskiy
    • anna-shavurska
    • vasylshylov