Skip to content
Browse files

reworked and published

  • Loading branch information...
1 parent 69b3aef commit c8532c6955dd7f5ea4ce804aa2ee6744edbfde77 Rocky Meza committed Feb 11, 2012
Showing with 115 additions and 87 deletions.
  1. +2 −0 .jshintrc
  2. +12 −2 Makefile
  3. +26 −13 moment.isocalendar.js
  4. +4 −3 package.json
  5. +1 −2 test/helpers/date2iso.py
  6. +70 −67 test/isocalendar.js
View
2 .jshintrc
@@ -0,0 +1,2 @@
+{ "laxcomma": true
+}
View
14 Makefile
@@ -1,3 +1,13 @@
-.PHONY: test
-test:
+SRC = moment.isocalendar.js
+
+.PHONY: test mocha test-watch lint
+test: mocha lint
+
+mocha:
./node_modules/.bin/mocha
+
+test-watch:
+ ./node_modules/.bin/mocha --watch $(SRC) test/isocalendar.js
+
+lint:
+ ./node_modules/.bin/jshint $(SRC)
View
39 moment.isocalendar.js
@@ -1,27 +1,35 @@
-if ( typeof require != 'undefined' ) {
- var moment = require('moment');
+if ( typeof require != 'undefined' )
+{
+ moment = require('moment');
}
+
;(function(moment) {
- var firstIsoWeekOfYear = function(year) {
- var m = moment([year]);
+ var firstIsoWeekOfYear = exports.firstIsoWeekOfYear = function(year) {
+ var m = moment([year])
+ , day = m.day();
+
+ if ( day === 0 )
+ {
+ day = 7;
+ }
// ISO week is the first week with a Thursday, so if the first
// day of the year is Friday, move to next Sunday.
- if ( m.day() > 4 ) {
+ if ( day > 4 ) {
m.add('weeks', 1);
}
- m.subtract('days', m.day());
+ m.subtract('days', day - 1);
return m;
}
;
moment.fn.isocalendar = function() {
var year = this.year()
- , firstSunday = firstIsoWeekOfYear(year)
- , week = Math.floor(this.diff(firstSunday, 'weeks', true)) + 1
+ , firstMonday = firstIsoWeekOfYear(year)
+ , week = Math.floor(this.diff(firstMonday, 'weeks', true)) + 1
, day = this.day()
, minute = this.hours() * 60 + this.minutes()
;
@@ -37,18 +45,23 @@ if ( typeof require != 'undefined' ) {
week = moment([year, 11, 31, 0, 0]).isocalendar()[1];
}
+ if ( day === 0 )
+ {
+ day = 7;
+ }
+
return [ year
, week
, day
, minute
];
};
- moment.fromIsocalendar = function(year, week, day, minute) {
- var date = firstIsoWeekOfYear(year).add({
- weeks: week - 1
- , days: day
- , minutes: minute
+ moment.fromIsocalendar = function(array) {
+ var date = firstIsoWeekOfYear(array[0]).add({
+ weeks: array[1] - 1
+ , days: array[2] - 1
+ , minutes: array[3]
});
return date;
View
7 package.json
@@ -11,10 +11,11 @@
, "main": "./moment.isocalendar"
, "engines": { "node": "*"
}
-, "dependencies": { "moment": ">=1.0.0"
+, "dependencies": { "moment": ">=1.4.0"
}
-, "devDependencies": { "mocha": "*"
- , "should": "*"
+, "devDependencies": { "mocha": "latest"
+ , "should": "latest"
+ , "jshint": "latest"
}
, "repository": { "type": "git"
, "url": "https://github.com/rockymeza/moment-isocalendar.git"
View
3 test/helpers/date2iso.py
@@ -6,11 +6,10 @@
import json
date = datetime.datetime(*map(int, sys.argv[1:]))
-date += datetime.timedelta(days=1)
year, week, day = date.isocalendar()
minutes = date.hour * 60 + date.minute
-ret = (year, week, day - 1, minutes)
+ret = (year, week, day, minutes)
print(json.dumps(ret))
View
137 test/isocalendar.js
@@ -1,83 +1,86 @@
var moment = require('moment')
, should = require('should')
- , exec = require('child_process').exec;
+ , exec = require('child_process').exec
+ , sys = require('util')
+ ;
require('../moment.isocalendar');
-;(function(m) {
- function date2iso(date, done) {
- var date_string = date.format('YYYY M D H m');
-
- exec('python test/helpers/date2iso.py ' + date_string, function(error, stdin, stdout) {
- var expected = JSON.parse(stdin);
-
- date.isocalendar().should.eql(expected);
-
- if ( done )
- done();
- });
- }
-
- function iso2date(date, done) {
- var iso = date.isocalendar()
- , iso_string = iso.join(' ');
-
- exec('python test/helpers/iso2date.py ' + iso_string, function(error, stdin, stdout) {
- var resp = JSON.parse(stdin)
- , expected = moment(new Date(resp[0], resp[1] - 1, resp[2], resp[3], resp[4]))
- , actual = moment.fromIsocalendar.apply(moment, iso);
-
- actual.format('LLLL').should.equal(expected.format('LLLL'));
- });
-
- if ( done )
- done();
+function date2iso(date, iso) {
+ var m = moment(date);
+ try {
+ m.isocalendar().should.eql(iso);
+ } catch(e) {
+ console.log(date, m, m.isocalendar(), iso);
+ throw e;
}
-
- function testBackAndForth(date, done) {
- var relative = date.isocalendar()
- , date2 = moment.fromIsocalendar.apply(moment, relative)
- , relative2 = date2.isocalendar()
- ;
-
- date.format('LLLL').should.equal(date2.format('LLLL'));
- relative.should.eql(relative2);
-
- if ( done )
- done();
+}
+
+function iso2date(iso, date) {
+ moment.fromIsocalendar(iso).should.eql(moment(date));
+}
+
+function backAndForth(date) {
+ var iso = date.isocalendar()
+ , date2 = moment.fromIsocalendar(iso)
+ ;
+
+ try {
+ date.should.eql(date2);
+ } catch (e) {
+ console.log(date.format('LLLL'), iso, date2.format('LLLL'), e);
+ throw e;
}
+}
- function rand(max) {
- return Math.floor(Math.random() * max);
- }
+describe('isocalendar', function() {
+ it('should work for a normal day', function() {
+ date2iso([2012, 1, 11, 5, 14], [2012, 6, 6, 314]);
+ });
- function doDone(bool, done) {
- return function() { if ( bool ) done(); };
- }
+ it('should transform sunday to 7', function() {
+ date2iso([2012, 1, 12, 5, 14], [2012, 6, 7, 314]);
+ });
- function randomly(times, func, done) {
- for (var i = times; i > 0; i--) {
- var date = moment(new Date(2000, 0, 1, 0, 0)).add('minutes', rand(365 * 100 * 60 * 24));
+ it('years that start on Monday', function() {
+ date2iso([2017, 8, 4, 0, 0], [2017, 36, 1, 0]);
+ });
+});
- func(date, doDone(i == 1, done));
- }
- }
+describe('fromIsocalendar', function() {
+ it('should work for normal days', function() {
+ iso2date([2012, 6, 6, 314], [2012, 1, 11, 5, 14]);
+ });
- describe('isocalendar', function() {
- // if you start spawning too much, test start failing
- var times = 50;
+ it('should work for sundays', function() {
+ iso2date([2012, 6, 7, 314], [2012, 1, 12, 5, 14]);
+ });
+});
+
+describe('random', function() {
+ it('extracted from random', function() {
+ backAndForth(moment([2012, 7, 6, 6, 31]));
+ backAndForth(moment([1977, 7, 1, 6, 2]));
+ backAndForth(moment([1982, 7, 2, 6, 51]));
+ backAndForth(moment([1992, 6, 25, 6, 24]));
+ backAndForth(moment([1991, 4, 6, 6, 22]));
+ });
- it('random testBackAndForth', function(done) {
- randomly(times, testBackAndForth, done);
- });
+ it('back and forth', function() {
+ var times = 10000
+ , i
+ , _d
+ , date
+ ;
- it('random iso2date', function(done) {
- randomly(times, iso2date, done);
- });
+ for (i = 0; i < times; i++)
+ {
+ _d = new Date(Math.floor(Math.random() * 2000000000000));
+ _d.setMilliseconds(0);
+ _d.setSeconds(0);
+ date = moment(_d);
- it('random date2iso', function(done) {
- randomly(times, date2iso, done);
- });
+ backAndForth(date);
+ }
});
-
-})(moment);
+});

0 comments on commit c8532c6

Please sign in to comment.
Something went wrong with that request. Please try again.