Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Common): update the Common.now #55

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ module.exports = function(grunt) {
linkNatives: true
}
}
},
jasmine : {
src : ['src/**/*.js', '!src/module/*'],
options : {
specs : 'spec/**/*.js'
}
}
});

Expand All @@ -105,9 +111,10 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-yuidoc');
grunt.loadNpmTasks('grunt-contrib-jasmine');

grunt.registerTask('default', ['test', 'build']);
grunt.registerTask('test', ['jshint']);
grunt.registerTask('test', ['jshint', 'jasmine']);
grunt.registerTask('dev', ['build:dev', 'connect:watch', 'watch']);

grunt.registerTask('build', function(mode) {
Expand Down Expand Up @@ -145,11 +152,11 @@ module.exports = function(grunt) {

if (isEdge)
grunt.config.set('docVersion', 'edge version (master)');

grunt.task.run('yuidoc');
});

grunt.registerTask('set_config', 'Set a config property.', function(name, val) {
grunt.config.set(name, val);
});
};
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
],
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-connect": "~0.6.0",
"grunt-contrib-copy": "~0.5.0",
"grunt-contrib-jasmine": "^0.8.2",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-uglify": "~0.2.7",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-yuidoc": "~0.5.1"
},
Expand Down
130 changes: 130 additions & 0 deletions spec/core/CommonSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
'use strict';

describe('Common', function() {
describe('now', function() {
it('should return performance.now if available', function() {
// We mock window.performance
var performanceMock = {
now: function() {
return 42;
}
};
window.performance = performanceMock;
spyOn(performanceMock, 'now').and.callThrough();;

var result = Common.now();

expect(performanceMock.now).toHaveBeenCalled();
expect(result).toBe(42);
});

it('should return performance.webkitNow if available and performance.now is not available', function() {
// We mock window.performance
var performanceMock = {
webkitNow: function() {
return 42;
}
};
window.performance = performanceMock;
spyOn(performanceMock, 'webkitNow').and.callThrough();

var result = Common.now();

expect(performanceMock.webkitNow).toHaveBeenCalled();
expect(result).toBe(42);
});

it('should return performance.msNow if available and performance.now and performance.webkitNow are not available', function() {
// We mock window.performance
var performanceMock = {
msNow: function() {
return 42;
}
};
window.performance = performanceMock;
spyOn(performanceMock, 'msNow').and.callThrough();

var result = Common.now();

expect(performanceMock.msNow).toHaveBeenCalled();
expect(result).toBe(42);
});

it('should return performance.oNow if available and performance.now, performance.webkitNow and performance.msNow are not available', function() {
// We mock window.performance
var performanceMock = {
oNow: function() {
return 42;
}
};
window.performance = performanceMock;
spyOn(performanceMock, 'oNow').and.callThrough();

var result = Common.now();

expect(performanceMock.oNow).toHaveBeenCalled();
expect(result).toBe(42);
});

it('should return performance.mozNow if available and performance.now, performance.webkitNow, performance.msNow and performance.oNow are not available', function() {
// We mock window.performance
var performanceMock = {
mozNow: function() {
return 42;
}
};
window.performance = performanceMock;
spyOn(performanceMock, 'mozNow').and.callThrough();

var result = Common.now();

expect(performanceMock.mozNow).toHaveBeenCalled();
expect(result).toBe(42);
});

it('should return Date.now if available and performance is not available', function() {
// We mock window.performance
window.performance = undefined;

// We mock window.Date
function DateMock() {
this.getTime = function() {
return 42;
};
}
DateMock.now = function() {
return 42;
}
window.Date = DateMock;
var DateMockInstance = new DateMock();
spyOn(DateMockInstance, 'getTime').and.callThrough();
spyOn(DateMock, 'now').and.callThrough();

var result = Common.now();

expect(DateMock.now).toHaveBeenCalled();
expect(DateMockInstance.getTime).not.toHaveBeenCalled();
expect(result).toBe(42);
});

it('should return Date.getTime if nothing else is available', function() {
// We mock window.performance
window.performance = undefined;

// We mock window.Date
function DateMock() {
this.getTime = function() {
return 42;
};
}
window.Date = DateMock;
var DateMockInstance = new DateMock();
var spy = spyOn(window, 'Date').and.callThrough();

var result = Common.now();

expect(window.Date).toHaveBeenCalled();
expect(result).toBe(42);
});
});
});
61 changes: 37 additions & 24 deletions src/core/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var Common = {};
}
}
}

return obj;
};

Expand Down Expand Up @@ -91,15 +91,15 @@ var Common = {};
*/
Common.values = function(obj) {
var values = [];

if (Object.keys) {
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
values.push(obj[keys[i]]);
}
return values;
}

// avoid hasOwnProperty for performance
for (var key in obj)
values.push(obj[key]);
Expand All @@ -113,15 +113,15 @@ var Common = {};
* @param {number} percent
* @return {string} A hex colour string made by lightening or darkening color by percent
*/
Common.shadeColor = function(color, percent) {
Common.shadeColor = function(color, percent) {
// http://stackoverflow.com/questions/5560248/programmatically-lighten-or-darken-a-hex-color
var colorInteger = parseInt(color.slice(1),16),
amount = Math.round(2.55 * percent),
R = (colorInteger >> 16) + amount,
B = (colorInteger >> 8 & 0x00FF) + amount,
var colorInteger = parseInt(color.slice(1),16),
amount = Math.round(2.55 * percent),
R = (colorInteger >> 16) + amount,
B = (colorInteger >> 8 & 0x00FF) + amount,
G = (colorInteger & 0x0000FF) + amount;
return "#" + (0x1000000 + (R < 255 ? R < 1 ? 0 : R :255) * 0x10000
+ (B < 255 ? B < 1 ? 0 : B : 255) * 0x100
return "#" + (0x1000000 + (R < 255 ? R < 1 ? 0 : R :255) * 0x10000
+ (B < 255 ? B < 1 ? 0 : B : 255) * 0x100
+ (G < 255 ? G < 1 ? 0 : G : 255)).toString(16).slice(1);
};

Expand Down Expand Up @@ -168,7 +168,7 @@ var Common = {};
(typeof obj.ownerDocument ==="object");
}
};

/**
* Description
* @method clamp
Expand All @@ -184,7 +184,7 @@ var Common = {};
return max;
return value;
};

/**
* Description
* @method sign
Expand All @@ -194,27 +194,40 @@ var Common = {};
Common.sign = function(value) {
return value < 0 ? -1 : 1;
};

/**
* Description
* @method now
* @return {number} the current timestamp (high-res if avaliable)
*/
Common.now = function() {
// http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript
// https://gist.github.com/davidwaterston/2982531

var perf = window.performance;

if (perf) {
perf.now = perf.now || perf.webkitNow || perf.msNow || perf.oNow || perf.mozNow;
return +(perf.now());
}

return +(new Date());
// Returns the number of milliseconds elapsed since either the browser navigationStart event or
// the UNIX epoch, depending on availability.
// Where the browser supports 'performance' we use that as it is more accurate (microsoeconds
// will be returned in the fractional part) and more reliable as it does not rely on the system time.
// Where 'performance' is not available, we will fall back to Date().getTime().
// jsFiddle: http://jsfiddle.net/davidwaterston/xCXvJ

var performance = window.performance || {};

performance.now = (function() {
return performance.now ||
performance.webkitNow ||
performance.msNow ||
performance.oNow ||
performance.mozNow ||
window.Date.now ||
function() {
return new Date().getTime();
};
})();

return performance.now();
};


/**
* Description
* @method random
Expand Down Expand Up @@ -305,4 +318,4 @@ var Common = {};
return Common._seed / 233280;
};

})();
})();