(function (root, factory) { if (typeof define === 'function' && define.amd) { define('strophe-utils', function () { return factory(); }); } else { // Browser globals root.stropheUtils = factory(); } }(this, function () { var utils = { partial: function(func) { /* Partially apply a function by creating a version that has had * some of its arguments pre-filled, without changing its dynamic * `this` context. _ acts as a placeholder, allowing any combination * of arguments to be pre-filled. * * Copied from MIT licensed Underscore.js 1.8.3 * (c) 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative * Reporters & Editors. * * Parameters: * (Function) func - either a map of cookie names * to string values or to maps of cookie values. */ var boundArgs = slice.call(arguments, 1); var bound = function() { var position = 0, length = boundArgs.length; var args = Array(length); for (var i = 0; i < length; i++) { args[i] = boundArgs[i] === _ ? arguments[position++] : boundArgs[i]; } while (position < arguments.length) args.push(arguments[position++]); return executeBound(func, bound, this, this, args); }; return bound; }, utf16to8: function (str) { var i, c; var out = ""; var len = str.length; for (i = 0; i < len; i++) { c = str.charCodeAt(i); if ((c >= 0x0000) && (c <= 0x007F)) { out += str.charAt(i); } else if (c > 0x07FF) { out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F)); out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); } else { out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F)); out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); } } return out; }, addCookies: function (cookies) { /* Parameters: * (Object) cookies - either a map of cookie names * to string values or to maps of cookie values. * * For example: * { "myCookie": "1234" } * * or: * { "myCookie": { * "value": "1234", * "domain": ".example.org", * "path": "/", * "expires": expirationDate * } * } * * These values get passed to Strophe.Connection via * options.cookies */ var cookieName, cookieObj, isObj, cookieValue, expires, domain, path; for (cookieName in (cookies || {})) { expires = ''; domain = ''; path = ''; cookieObj = cookies[cookieName]; isObj = typeof cookieObj == "object"; cookieValue = escape(unescape(isObj ? cookieObj.value : cookieObj)); if (isObj) { expires = cookieObj.expires ? ";expires="+cookieObj.expires : ''; domain = cookieObj.domain ? ";domain="+cookieObj.domain : ''; path = cookieObj.path ? ";path="+cookieObj.path : ''; } document.cookie = cookieName+'='+cookieValue + expires + domain + path; } } }; return utils; }));