Skip to content

Instantly share code, notes, and snippets.

View JoeChapman's full-sized avatar
🎯
Focusing

JoeChapman JoeChapman

🎯
Focusing
View GitHub Profile
@JoeChapman
JoeChapman / basicSpyCallcount.js
Created September 26, 2012 20:06
Jasmine spy basics call count
expect(spyTrigger.call).toEqual(1);
@JoeChapman
JoeChapman / basicSpy.js
Created September 26, 2012 20:00
Jasmines spy basics
var spyTrigger = spyOn(Events, "trigger");
@JoeChapman
JoeChapman / mock-constructor.js
Created September 25, 2012 19:26
Mocking a constructor function
var mockObject = spyOn(object, Constructor).andCallFake(function () {
return {
method1: function () {},
method2: function () {}
};
});
@JoeChapman
JoeChapman / hoisting-function-definitions.js
Created April 5, 2012 15:51
Hoisting function definitions
var a = function () {
return a();
var a = function () {
return 1;
};
function a() {
return 2;
}
@JoeChapman
JoeChapman / hoisting-variables.js
Created March 28, 2012 09:02
The affects of hoisting
var a = 1,
fn = function () {
alert(a);
var a = 10;
};
// a is undefined when alerted!
@JoeChapman
JoeChapman / sort-nested-objects.js
Created March 27, 2012 13:19
Sorting an array of nested objects
var arr = [
{details: {name: {first: 'Joseph', last: 'Chapman'}}},
{details: {name: {first: 'Andrew', last: 'Chapman'}}},
{details: {name: {first: '234', last: '111'}}},
{details: {name: {first: 'Joseph', last: 'Hinge'}}},
{details: {name: {first: 'Simon', last: 'Andrews'}}},
{details: {name: {first: 'Robert', last: 'Hinge'}}}
];
var sortBy = function (key, minor) {
@JoeChapman
JoeChapman / sort-by-more-than-one.js
Created March 27, 2012 09:26
Sorting an array of objects by more than one key
var arr = [
{first: 'Joseph', last: 'Chapman'},
{first: 'Andrew', last: 'Chapman'},
{first: '234', last: '111'},
{first: 'Joseph', last: 'Hinge'},
{first: 'Simon', last: 'Andrews'},
{first: 'Robert', last: 'Hinge'}
];
var sortBy = function (key, minor) {
@JoeChapman
JoeChapman / sort-array-of-objects.js
Created March 27, 2012 08:27
Sorting an array of objects by key
var arr = [
{first: 'Joseph', last: 'Smith'},
{first: 'Joseph', last: 'Chapman'},
{first: 'Joseph', last: 'Hinge'}
];
var sortBy = function (key) {
return function (o, p) {
var a, b;
if (o && p && typeof o === 'object' && typeof p === 'object') {
@JoeChapman
JoeChapman / sort-alphanumeric-values.js
Created March 22, 2012 13:45
Sorting alphanumeric values in ascending order with a compare function
var arr = ['z', 564, 12, 785, 'a', 'dc', 'o', 43, 's', 'r'];
arr.sort(function (a, b) {
if (a === b) {
return 0;
}
if (typeof a === typeof b) {
return a < b ? -1 : 1;
}
return typeof a < typeof b ? -1 : 1;
@JoeChapman
JoeChapman / sort-numeric-values-correctly.js
Created March 21, 2012 13:59
Sorting numeric values in ascending order with a compare function
var arr = [300, 40, 5];
arr.sort(function (a, b) {
return a - b;
});
// Result
5, 40, 300