Skip to content

Commit

Permalink
Added UMD & bower support, bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
atruskie committed Jan 30, 2015
1 parent 6312758 commit 8b72173
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ node_modules

# Users Environment Variables
.lock-wscript

.idea
24 changes: 24 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "round-date",
"main": "roundDate.js",
"version": "1.0.1",
"homepage": "https://github.com/atruskie/round-date",
"description": "A small module for rounding dates to arbitrary intervals",
"moduleType": [
"amd",
"globals",
"node"
],
"keywords": [
"round",
"date"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
36 changes: 0 additions & 36 deletions index.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "round-date",
"version": "1.0.0",
"version": "1.0.1",
"description": "A small module for rounding dates to arbitrary intervals",
"main": "index.js",
"main": "roundDate.js",
"scripts": {
"test": "tape test/*.js"
},
Expand Down
53 changes: 53 additions & 0 deletions roundDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function () {

function roundDate(roundStyle, roundToSeconds, date) {
if (arguments.length !== 3) {
throw new Error("Expected 2 arguments");
}

var startOfDay = new Date(date).setHours(0, 0, 0),
roundToMilliseconds = roundToSeconds * 1000,
msFromMidnight = (+date) - startOfDay,
remainder = msFromMidnight % roundToMilliseconds,
result = msFromMidnight;

if (remainder !== 0) {
if (roundStyle === "round") {
var fraction = (msFromMidnight / roundToMilliseconds) % 1;
roundStyle = fraction >= 0.5 ? "ceil" : "floor";
}

if (roundStyle === "floor") {
result -= remainder
}
else if (roundStyle === "ceil") {
result += (roundToMilliseconds - remainder);
}
else {
return;
}
}

return new Date(startOfDay + result);
}

return {
round: roundDate.bind(null, "round"),
floor: roundDate.bind(null, "floor"),
ceil: roundDate.bind(null, "ceil")
};
}));

0 comments on commit 8b72173

Please sign in to comment.