Skip to content

Instantly share code, notes, and snippets.

@gogromat
Last active July 11, 2023 16:50
Show Gist options
  • Save gogromat/5193597 to your computer and use it in GitHub Desktop.
Save gogromat/5193597 to your computer and use it in GitHub Desktop.
Different JS tricks, features, etc.
// Check if variable is array or object
// (From underscore.js each() implementation)
var array = [];
var object = {a:1,b:2};
if (obj == null) return;
//actually it is logically equivalent to this ugliness: if ((obj === null) || (obj === undefined))
if (obj.forEach && obj.forEach === Array.prototype.forEach) {
// array, native forEach looping
} else if (obj.length === +obj.length) {
// very nerdy.
// IF this is array, integer === +integer, it passes here.
// The lowest length array can have is 0.
// IF this is object, however...
// obj.length = undefined, +obj.length = +undefined = NaN
// undefined !== NaN
} else {
// ... is object
}
//==========================================================================================================================
// Accessor Properties example - temperature converter
// Crockford's Accessor properties example
// from "Crockford on JavaScript - Chapter 2: And Then There Was JavaScript"
// http://www.youtube.com/watch?v=RO1Wnu-xKoY
var temperature_converter = {};
(function(converter) {
Object.defineProperty(converter, 'F', {
get: function () {
return parseFloat((((this.C * 9) / 5) + 32).toFixed(2));
},
set: function (value) {
this.C = parseFloat(((value - 32) * (5 / 9)).toFixed(2));
},
enumerable: true
});
})(temperature_converter);
// This converter works both ways.
// If you set C first, just by getting F,
// you just end up calling get() accessor property
// If you set F first, set() gets called, setting C.
temperature_converter.F = 100;
//temperature_converter.C returns 37.75
temperature_converter.C = 200
//temperature_converter.F returns 392
// Crockford's Accessor properties example
// from "Crockford on JavaScript - Chapter 2: And Then There Was JavaScript"
// http://www.youtube.com/watch?v=RO1Wnu-xKoY
// (added float and toFixed)
var distance_converter = {};
(function(converter) {
Object.defineProperty(converter, 'inch', {
get: function () {
return parseFloat((this.mm / 25.4).toFixed(2));
},
set: function (value) {
this.mm = parseFloat((value * 25.4).toFixed(2));
},
enumerable: true
});
})(distance_converter);
distance_converter.mm = 100;
//distance_converter.inch = 3.94
clear();
function toType (obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
var test = {
"array": function() {
console.log("ARRAY");
},
"object": function() {
console.log("OBJECT");
},
"default": function() {
console.log("DEFAULT");
}
}
var data = ""; // => DEFAULT, [] => ARRAY, {} => OBJECT
var gear = test[toType(data)] || test["default"];
gear();
var nativeForEach = Array.prototype.forEach,
breaker = {};
var each = function(obj, iterator, context) {
if (obj == null) return;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (var i = 0, l = obj.length; i < l; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (iterator.call(context, obj[key], key, obj) === breaker) return;
}
}
}
};
function group (list, value, context) {
var result = {};
//var iterator = value; // or wrap to function
each(list, function(object, index) {
var key = value.call(context, object);
if (!result.hasOwnProperty(key)) {
result[key] = [];
}
result[key].push(object);
});
return result;
}
var collection = [
{a:3,b:2,color:"green"},
{a:3,b:4,color:"green"},
{a:3,b:5,color:"blue"},
{a:3,b:8,color:"blue"}
];
group(collection,
function(object) { return object.color; }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment