function bool(x) { return (x === 1 || x === '1' || x === true || x === 'true'); } /** html entities */ function e(x) { return String(x) .replace(/&/g, '&') .replace(/"/g, '"') .replace(/'/g, ''') .replace(//g, '>'); } /** Returns true if argument is array [] */ function isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; } /** Returns true if argument is object {} */ function isObject(obj) { return Object.prototype.toString.call(obj) === '[object Object]'; } /** escape a string to have no special meaning in regex */ function regexEscape(s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); } /** Convert RSSI 0-255 to % */ function rssiPerc(rssi) { var r = parseInt(rssi); if (r > -50) return 100; // 100% if (r < -100) return 0; // 0% return Math.round(2 * (r + 100)); // approximation } /** Perform a substitution in the given string. * * Arguments - array or list of replacements. * Arguments numeric keys will replace {0}, {1} etc. * Named keys also work, ie. {foo: "bar"} -> replaces {foo} with bar. * * Braces are added to keys if missing. * * @returns {String} result */ String.prototype.format = function () { var out = this; var repl = arguments; if (arguments.length == 1 && (isArray(arguments[0]) || isObject(arguments[0]))) { repl = arguments[0]; } for (var ph in repl) { if (repl.hasOwnProperty(ph)) { var ph_orig = ph; if (!ph.match(/^\{.*\}$/)) { ph = '{' + ph + '}'; } // replace all occurrences var pattern = new RegExp(regexEscape(ph), "g"); out = out.replace(pattern, repl[ph_orig]); } } return out; };