Skip to content

Instantly share code, notes, and snippets.

@kgarfinkel
kgarfinkel / gist:7062984
Created October 19, 2013 23:47
_.first return's an array of the first n elements of an array.
_.first = function(array, n) {
if (n === undefined) {
return array[0];
}
return slice.call(array, 0, n);
};
@kgarfinkel
kgarfinkel / gist:7063179
Created October 20, 2013 00:12
Javascript's 'call' re-implemented
var call = function(fn, context) {
var args = Array.prototype.slice.call(arguments, 2).join(‘,’),
result;
context.fn = fn;
result = eval(“context.fn(“+ args +”)”);
delete this.fn;
return result;
};
@kgarfinkel
kgarfinkel / gist:7063475
Created October 20, 2013 00:53
Underscore.js's 'once' re-implemented. Returns a function that can only be called one time. Repeated calls to the modified function will result in returning the value from the original call
_.once = function(func) {
var alreadyCalled = false,
result;
return function() {
if(!alreadyCalled) {
result = func.apply(this, arguments);
alreadyCalled = true;
}
@kgarfinkel
kgarfinkel / gist:7064078
Created October 20, 2013 02:16
Javascript's 'apply' re-implemented.
var apply = function(fn, context) {
var args = Array.prototype.slice.call(arguments, 2),
result;
context.fn = fn;
result = context.fn(args);
delete this.fn;
return result;
};
@kgarfinkel
kgarfinkel / gist:7064111
Last active December 26, 2015 00:29
Javascript's 'bind' re-implemented.
var bind = function(fn, context) {
var args = Array.prototype.slice.call(arguments, 2);
return function() {
return fn.apply(context, args.concat(Array.prototype.slice.call(arguments)));
};
};
// Sign a user in
signin: function (user, callback) {
var self = this,
attributes = { 'sessionId': 'foobar' };
if (typeof(user) == 'function') {
callback = user;
}
describe.only('when user is verified and all fields are valid', function () {
var apiUserSession = {
sessionId: 'full-sail',
user: apiUsers[users.user.api_id]
};
given.apiUserCanBeSignedInWith(apiUserSession);
given.anExistingApiUserWith(apiUsers[users.user.api_id]);
define [
'cdn.jquery'
'cdn.backbone'
'scripts/helpers/common'
'scripts/mediator'
'site/models/notification'
'site/collections/notifications'
'site/views/notification_collection'
], ($, Backbone, commonHelper, mediator, Notification, Notifications, NotificationCollectionView) ->
@kgarfinkel
kgarfinkel / gist:4fdafc33e45d3ec15cf1
Created November 11, 2014 19:16
Transferring Org Courses to a Personal Accout
// Fetch org course
$course_api me orgs/121444745/courses | jq '.[] | {"id": .id, "programId": .programId}'
{
"id": "qnmizo",
"programId": "121444745"
}
// Create new programId
$ course_api me programs post {}
{
# The PUT request takes one parameter, a `macros` object that lists the macros to update.
#
# Each macro must have an id and any of the following parameters:
#
# | Name | Description |
# | -------- | ----------------------------------- |
# | id | The ID of the macro to update. |
# | active | The new active status of the macro. |
# | position | The new position of the macro. |