Skip to content

Instantly share code, notes, and snippets.

@particlebanana
Created June 21, 2012 22:29
Show Gist options
  • Save particlebanana/2969002 to your computer and use it in GitHub Desktop.
Save particlebanana/2969002 to your computer and use it in GitHub Desktop.
if (typeof define !== 'function') { var define = require('amdefine')(module); }
define(function() {
return {
indexOf : function(arr, item) {
return arr.indexOf( item );
},
sum : function(arr) {
var sum = 0;
arr.forEach( function( i ) {
sum += i;
});
return sum;
},
remove : function(arr, item) {
arr.splice( arr.indexOf( item ), 1 );
return arr;
},
append : function( arr, item ) {
arr.push( item );
return arr;
},
truncate : function(arr) {
arr.pop( arr );
return arr;
},
concat : function(arr1, arr2) {
return arr1.concat( arr2 );
},
insert : function(arr, item, index) {
arr.splice( index, 0, item );
return arr;
},
count : function(arr, item) {
return arr.filter( function( i ) {
return i === item;
}).length;
},
duplicates : function(arr) {
var dups = [], buffer;
arr.sort( function( a, b ) {
return a - b;
});
buffer = null;
arr.forEach( function( i ) {
if ( buffer === i ) {
dups.push( i );
}
buffer = i;
});
return dups;
},
square : function(arr) {
var buffer = [];
arr.forEach( function( i ) {
buffer.push( i * i );
});
return buffer;
},
findAllOccurrences : function(arr, target) {
var buffer = [];
arr.forEach( function( i ) {
if( i === target ) {
buffer.push( arr.indexOf(i) );
arr.splice( arr.indexOf( i ), 1, null );
}
});
return buffer;
}
};
});
define(function() {
return {
async : function() {
var deferred = new $.Deferred();
deferred.resolve( true );
return deferred.promise();
},
manipulateRemoteData : function(url) {
// Create a deferred to return
var deferred = new $.Deferred();
$.get(url, function( data, status, xhr ) {
// sort the people array by name
var people = data.people.map(function( person ) {
return person.name;
}).sort();
// resolve the sorted array
deferred.resolve( people );
});
// return the promise
return deferred.promise();
}
};
});
if (typeof define !== 'function') { var define = require('amdefine')(module); }
define([ 'use!underscore' ], function(_) {
return {
fizzBuzz : function(num) {
// write a function that receives a number as its argument;
// if the number is divisible by 3, the function should return 'fizz';
// if the number is divisible by 5, the function should return 'buzz';
// if the number is divisible the 3 and 5, the function should return
// 'fizzbuzz';
// otherwise the function should return the number
var str = '';
if( num % 3 === 0 ) {
str += 'fizz';
}
if( num % 5 === 0 ) {
str += 'buzz';
}
return str.length === 0 ? num : str;
},
or : function(a, b) {
return a || b;
},
and : function(a, b) {
return a && b;
}
};
});
if (typeof define !== 'function') { var define = require('amdefine')(module); }
define(function() {
return {
argsAsArray : function(fn, arr) {
return fn.apply( null, arr );
},
speak : function(fn, obj) {
return fn.call( obj );
},
functionFunction : function(str) {
return function( s ) {
return str + ', ' + s;
};
},
partial : function(fn, str1, str2) {
var args = [ str1, str2 ];
return function( punct ) {
return fn( args[0], args[1], punct );
};
},
useArguments : function() {
var args = Array.prototype.slice.call( arguments ),
sum = 0;
args.forEach( function( i ) {
sum += i;
});
return sum;
},
callIt : function(fn) {
var args = Array.prototype.slice.call( arguments, 1 );
return fn.apply( null, args );
},
curryIt : function(fn) {
var args = Array.prototype.slice.call( arguments, 1 );
return function() {
var arr = args.concat( Array.prototype.slice.call( arguments ) );
return fn.apply( null, arr );
};
},
makeClosures : function(arr, fn) {
var funcs = [],
closure;
arr.forEach( function( i ) {
closure = function() {
return fn.call( this, i );
};
funcs.push( closure );
});
return funcs;
}
};
});
if (typeof define !== 'function') { var define = require('amdefine')(module); }
define(function() {
return {
createModule : function(str1, str2) {
return {
name: str2,
greeting: str1,
sayIt: function() {
return this.greeting + ', ' + this.name;
}
};
}
};
});
if (typeof define !== 'function') { var define = require('amdefine')(module); }
define(function() {
return {
alterContext : function(fn, obj) {
return fn.call( obj );
},
alterObjects : function(constructor, greeting) {
constructor.prototype.greeting = greeting;
},
iterate : function(obj) {
var arr = [];
for( var key in obj ) {
if( obj.hasOwnProperty( key ) ) {
arr.push( key + ': ' + obj[key] );
}
}
return arr;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment