Skip to content

Instantly share code, notes, and snippets.

@wiyoe
wiyoe / closure.js
Last active December 6, 2017 08:57
Javascript Closure
var counter = (function() {
var privateCounter = 0;
function changeBy(val) {
privateCounter += val;
}
return {
increment: function() {
changeBy(1);
},
decrement: function() {
@wiyoe
wiyoe / is-integer.js
Created December 6, 2017 11:36
Javascript isInteger Check
// For Internet Explorer support
Number.isInteger = Number.isInteger || function(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value;
};
Number.isInteger(45); // return true
@wiyoe
wiyoe / namespaces.js
Created December 8, 2017 07:16
Javascript Namespaces (Global)
/**
* Simple namespace util to extand Class.js functionality
* and wrap classes in namespace.
* @author tommy.rochette[followed by the usual sign]universalmind.com
* @type {*}
* @return Object
*/
window.namespace = function(namespaces){
'use strict';
var names = namespaces.split('.');
@wiyoe
wiyoe / difference.js
Last active December 29, 2017 12:09
Lodash objects difference and intersection.
var a = { 'a': 1, 'b': 2, 'c': 3 };
var b = { 'c': 3, 'd': 4, 'e': 5 };
_.intersection(_.keys(a), _.keys(b)); // ['c']
_.difference(_.keys(a), _.keys(b)); // ['a', 'b']
_.isEqual(a, b); // false
@wiyoe
wiyoe / moment-unix-timestamp.js
Last active November 7, 2021 15:31
Moment.js convert Unix epoch time to human readable time
moment("2017-12-27", "YYYY-MM-DD").unix(); // 1514322000
moment.unix(1514322000).format("DD-MM-YYYY"); // seconds
moment(1318781876406).format("DD-MM-YYYY H:m:s"); // milliseconds
@wiyoe
wiyoe / greater-than-zero.js
Last active January 3, 2018 07:18
Javascript Greater than Zero Regex
var reg = new RegExp("^[1-9][0-9]*$");
reg.test("5"); // true
reg.test("15"); // true
reg.test(15); // true
reg.test("0"); // false
reg.test("-1"); // false
reg.test("5abc"); // false
reg.test("abc"); // false
@wiyoe
wiyoe / callback.js
Last active January 4, 2018 11:04
Javascript callback example
function Thing(name) {
this.name = name;
}
Thing.prototype.doSomething = function(callback, salutation) {
// Call our callback, but using our own instance as the context
callback.call(this, salutation);
}
function foo(salutation) {
@wiyoe
wiyoe / loadash-map.js
Created January 4, 2018 11:03
Loadash array map function.
_.map(render.questionList.questions, function(x, index) {
x.itemIndex = index + 1;
return x;
});
@wiyoe
wiyoe / htmlspecialchars.js
Last active January 4, 2018 11:19
Javascript htmlspecialchars.
function escapeHtml(text) {
var map = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#039;'
};
@wiyoe
wiyoe / lodash-issame.js
Last active January 4, 2018 13:18
Lodash custom function (check value equality).
_.mixin({ // Lodash add custom function
'isSame' : function(x, y) {
return x == y;
}
});