UNPKG

dpaw-brocket-relay

Version:

Interapp communication via websockets

86 lines (73 loc) 1.78 kB
'use strict'; /** * @fileOverview * @name utility.js<relay> * @author Gavin Coombes * @license BSD-3-Clause * * Local util script just inlines functions from dpaw-brocket-utility and lodash */ /** * Relay utility * @module relay/utility */ /** * Represents the utility object * @class */ var utility = function utility() {}; var log = Function.prototype.bind.call(console.log, console); utility.prop = prop; /** Check for an object (from lodash) */ utility.isObject = require('lodash/lang/isObject'); /** Check for a string */ utility.isString = require('lodash/lang/isString'); /** * Check an object has a property. [lodash] * @param {object} - the object * @param {string} - the property * @returns {boolean} */ utility.has = require('lodash/object/has'); utility.defaults = require('lodash/object/defaults'); /** * Return a getter-setter function * @param {*} prev - The previous value. * @returns {*} - The current value. */ function prop(prev) { // Return a function a getter-setter [see mithril.js] var _prop = function _prop(val) { if (arguments.length) { prev = val; } return prev; }; _prop.toJSON = function () { return prev; }; return _prop; }; /** * Generate a random string. * @returns {string} Six random characters */ function s6() { return Math.floor((1 + Math.random()) * 0x1000000).toString(16).substring(1); }; function now() { return new Date().toISOString(); } module.exports = { /** Make a getter-setter */ prop: prop, /** Log to console */ log: log, /** Make a cool string */ _s6: s6, has: require('lodash/object/has'), defaults: require('lodash/object/defaults'), isObject: require('lodash/lang/isObject'), isString: require('lodash/lang/isString'), now: now };