Skip to content

Instantly share code, notes, and snippets.

View michael-mafi's full-sized avatar

Michael Mafi michael-mafi

View GitHub Profile
@michael-mafi
michael-mafi / isPrototypeOf2.js
Last active February 5, 2017 01:39
isPrototypeOf2 function 🐾
function isPrototypeOf(prototype, object){
if (prototype === null || undefined){
return 'null or undefined';
}
var objectPrototype = Object.getPrototypeOf(object);
while(objectPrototype){
if(prototype === objectPrototype){
return true;
}
objectPrototype = Object.getPrototypeOf(objectPrototype);
@michael-mafi
michael-mafi / 💾.js
Last active February 4, 2017 00:07
todoMVC taking 'store' out of render and putting it into it's own function - gomix preview https://gomix.com/#!/project/dirty-beam
// Step 1 // Remove the call to util.store from the App.render function below
render: function(){
var todos = this.getFilteredTodos();
$('#todo-list').html(this.todoTemplate(todos));
$('#main').toggle(todos.length > 0);
$('#toggle-all').prop('checked', this.getActiveTodos().length === 0);
this.renderFooter();
$('#new-todo').focus();
},
@michael-mafi
michael-mafi / bugfix.js
Created February 3, 2017 21:08
bug fix challenge for todo MVC by Luca https://github.com/tastejs/todomvc/pull/1737
After trying a lot of ways removing code seemed to produce the desired effect.
// The code below was commented out in the main app.js file
// if (!val) {
// this.destroy(e);
// return;
// }
@michael-mafi
michael-mafi / libSys.js
Last active February 7, 2017 02:57
library loading system
var libs = {};
var libSys = function(libName, depArr, cb){
if (libName.length > 1 && depArr.length >= 1){
var storArr = [];
for(var i = 0; i < depArr.length; i++){
storArr.push(libs[depArr[i]]);
}
libs[libName] = cb.apply(this, storArr);
} else {
libs[libName] = cb();
@michael-mafi
michael-mafi / compareObjects.js
Created January 10, 2017 17:28
Object comparison in JS
Object.equals = function( x, y ) {
if ( x === y ) return true;
// if both x and y are null or undefined and exactly the same
if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) return false;
// if they are not strictly equal, they both need to be Objects
if ( x.constructor !== y.constructor ) return false;
// they must have the exact same prototype chain, the closest we can do is
// test there constructor.
@michael-mafi
michael-mafi / isPrototypeOf.js
Last active January 11, 2017 00:14
isPrototypeOf.js
function isPrototypeOf(parent, child){
if (parent === null || undefined){
return false;
}
if (Object.getPrototypeOf(child) === null || undefined){
return false;
}
return Object.getPrototypeOf(child) === parent || Object.prototype ? true: false;
}
isPrototypeOf(dog, myDog); // returns true
@michael-mafi
michael-mafi / runWithDebugger.js
Last active January 31, 2017 06:36
runWithDebugger.js
function runWithDebugger(func, args){
if(args.length>=1){
debugger;
return func.call(this, args);
}
}
//use a var arr = [[[]]]; //////// also side note: don't forget Object.keys() Object.values() and Object.entries()
function transformData(arr){
var output = {};
array.forEach(function(item, index){
if(!item)return;
if(Array.isArray(item)){
output[index] = transformData(item);
}else{
output[index] = item;
@michael-mafi
michael-mafi / ceasarsCipher.js
Last active December 20, 2016 11:46
ceasars cipher
function rot13(str) {
return str.split('').map.call(str, function(char) {
x = char.charCodeAt(0);
if (x < 65 || x > 90) {
return String.fromCharCode(x);
}
else if (x < 78) {
return String.fromCharCode(x + 13);
}
return String.fromCharCode(x - 13);
@michael-mafi
michael-mafi / palindrome.js
Created December 17, 2016 03:42
palindrome in JS
function palindrome(str) {
var len = str.length;
for ( var i = 0; i < Math.floor(len/2); i++ ) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}