Skip to content

Commit

Permalink
Initial working commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gf3 committed May 23, 2012
0 parents commit 53ca919
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
13 changes: 13 additions & 0 deletions Cakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{exec} = require 'child_process'

responder = (err, stdout, stderr) ->
throw err if err
console.log stdout if stdout
console.error stderr if stderr

task 'build', 'Build project from src/*.coffee to lib/*.js', ->
exec 'coffee --compile --output lib/ src/', responder

task 'test', 'Run the test suite', ->
exec './node_modules/.bin/mocha --compilers coffee:coffee-script --reporter spec --ui bdd --colors', responder

25 changes: 25 additions & 0 deletions UNLICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>

86 changes: 86 additions & 0 deletions lib/moment-range.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{ "name": "moment-range"
, "description": "Fancy date ranges for Moment.js"
, "author": "Gianni Chiappetta <[email protected]> (http://gf3.ca)"
, "homepage": "https://github.com/gf3/moment-range"
, "bugs": "https://github.com/gf3/moment-range/issues"
, "main": "./lib/moment-range"
, "directories": { "lib" : "./lib" }
, "version": "0.1.0"
, "engines":
{ "node": "*"
}
, "dependencies":
{ "moment": "1.3.0"
}
, "devDependencies":
{ "coffee-script": "~1.3.3"
, "mocha": "1.0.3"
, "should": "0.6.3"
}
, "repository":
{ "type": "git"
, "url": "https://[email protected]/gf3/moment-range.git"
}
, "license":
{ "type": "Public Domain"
, "url": "https://github.com/gf3/moment-range/raw/master/UNLICENSE"
}
}
57 changes: 57 additions & 0 deletions src/moment-range.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
moment =
if require?
require 'moment'
else
@moment

###*
* DateRange class to store ranges and query dates.
* @typedef {!Object}
*###
class DateRange
###*
* DateRange instance.
* @param {(Moment|Date)} start Start of interval.
* @param {(Moment|Date)} end End of interval.
* @constructor
*###
constructor: (@start, @end) ->

###*
* Determine if the current interval contains a given moment/date.
* @param {(Moment|Date)} moment Date to check.
* @return {!boolean}
*###
contains: (moment) ->
@start < moment < @end

###*
* Date range in milliseconds. Allows basic coercion math of date ranges.
* @return {!number}
*###
valueOf: ->
@end - @start

###*
* Build a date range.
* @param {(Moment|Date)} start Start of range.
* @param {(Moment|Date)} end End of range.
* @this {Moment}
* @return {!DateRange}
*###
moment.fn.range = (start, end) ->
new DateRange start, end

###*
* Check if the current moment is within a given date range.
* @param {!DateRange} range Date range to check.
* @this {Moment}
* @return {!boolean}
*###
moment.fn.within = (range) ->
range.contains @_d

# LOL CommonJS
if module?.exports?
module.exports = moment

47 changes: 47 additions & 0 deletions test/moment-range.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'should'
moment = require '../lib/moment-range'

describe 'Moment', ->
dr = moment().range new Date(2011, 2, 5), new Date(2011, 5, 5)
m_1 = moment("2011-04-15", "YYYY-MM-DD")
m_2 = moment("2012-12-25", "YYYY-MM-DD")

describe '#range()', ->
it 'should return a DateRange'

describe '#within()', ->
it 'should determine if the current moment is within a given range', ->
m_1.within(dr).should.be.true
m_2.within(dr).should.be.false

describe 'DateRange', ->
d_1 = new Date 2011, 2, 5
d_2 = new Date 2011, 5, 5
d_3 = new Date 2011, 4, 9
d_4 = new Date 1988, 0, 1
m_1 = moment("06-05-1996", "MM-DD-YYYY")
m_2 = moment("11-05-1996", "MM-DD-YYYY")
m_3 = moment("08-12-1996", "MM-DD-YYYY")
m_4 = moment("01-01-2012", "MM-DD-YYYY")

describe '#contains()', ->
it 'should work with Date objects', ->
dr = moment().range(d_1, d_2)
dr.contains(d_3).should.be.true
dr.contains(d_4).should.be.false

it 'should work with Moment objects', ->
dr = moment().range(m_1, m_2)
dr.contains(m_3).should.be.true
dr.contains(m_4).should.be.false

describe '#valueOf()', ->
it 'should be the value of the range in milliseconds', ->
dr = moment().range(d_1, d_2)
dr.valueOf().should.eql d_2.getTime() - d_1.getTime()

it 'should correctly coerce to a number', ->
dr_1 = moment().range(d_4, d_2)
dr_2 = moment().range(d_3, d_2)
(dr_1 > dr_2).should.be.true

0 comments on commit 53ca919

Please sign in to comment.