Skip to content

Instantly share code, notes, and snippets.

@ArnonEilat
Created March 27, 2017 11:42
Show Gist options
  • Save ArnonEilat/d3b5661f8fcc061c57922e909deb617b to your computer and use it in GitHub Desktop.
Save ArnonEilat/d3b5661f8fcc061c57922e909deb617b to your computer and use it in GitHub Desktop.
var docCookies = {
getItem: function (sKey) {
if (!sKey) { return null; }
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
case Number:
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
break;
case String:
sExpires = "; expires=" + vEnd;
break;
case Date:
sExpires = "; expires=" + vEnd.toUTCString();
break;
}
}
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
return true;
},
removeItem: function (sKey, sPath, sDomain) {
if (!this.hasItem(sKey)) { return false; }
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
return true;
},
hasItem: function (sKey) {
if (!sKey) { return false; }
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
keys: function () {
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
return aKeys;
}
};
const timeDiff = function (endTime, startTime = new Date()) {
let total = Math.abs(Date.parse(endTime) - Date.parse(startTime));
let seconds = Math.floor((total / 1000) % 60);
let minutes = Math.floor((total / 1000 / 60) % 60);
let hours = Math.floor((total / (1000 * 60 * 60)) % 24);
let days = Math.floor(total / (1000 * 60 * 60 * 24));
return { days, hours, minutes, seconds };
}
var templater = function (html, data) {
var replaceVars = function (obj) {
for (var x in obj) {
var re = '{{\\s?' + x + '\\s?}}';
html = html.replace(new RegExp(re, 'ig'), obj[x]);
}
return html;
};
var htmlString = replaceVars(data);
var parser = new DOMParser();
var doc = parser.parseFromString(htmlString, 'application/xml');
return doc.children[0];
};
var template = templater('<p class="gggg">hey there {{ name }}</p>', { name: 'Neo' })
console.log('template', template);
debugger;
document.body.appendChild(template);
/**
* @description return true if and only if date is valid date.
* @see http://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript
*/
export const isValidDate = function (date) {
if (typeof date === 'undefined') {
throw new TypeError('Bad arguments supply: \'date\' is undefined');
}
if (Object.prototype.toString.call(date) === '[object Date]') {
// it is a date
if (isNaN(date.getTime()) === true) { // d.valueOf() could also work
return false; // date is not valid
} else {
return true; // date is valid
}
} else {
return false; // not a date
}
};
/**
* @description Convert everything to boolean.
* @param value {Any}
* @returns {boolean}
*/
export const toBoolean = function (value) {
if (typeof value === 'string') {
value = value.toLowerCase().trim();
}
switch (value) {
case 'true':
case true:
case 'yes':
case '1':
case 1:
return true;
case 'false':
case false:
case 'no':
case '0':
case 0:
case undefined:
case null:
return false;
default:
return Boolean(value);
}
};
/**
* @description Determines if a reference is a `String`.
* Copy of angular.isString
* @param {*} value Reference to check.
* @returns {boolean} True if `value` is a `String`.
* @see https://docs.angularjs.org/api/ng/function/angular.isString
*/
export const isString = value => typeof value === 'string';
/**
* @description Determines if a reference is an `Object`. Unlike `typeof` in JavaScript, `null`s are not
* considered to be objects. Note that JavaScript arrays are objects.
* Copy of angular.isObject
* @param {*} value Reference to check.
* @returns {boolean} True if `value` is an `Object` but not `null`.
* @see https://docs.angularjs.org/api/ng/function/angular.isObject
* @see http://jsperf.com/isobject4
*/
export const isObject = value => value !== null && typeof value === 'object';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment