Skip to content

Commit

Permalink
Merge pull request #37 from xsoh/add_sub_idate
Browse files Browse the repository at this point in the history
Fix 'iDate' add and sub
  • Loading branch information
xsoh committed May 28, 2018
2 parents 47e4c56 + 69d05db commit 3e33f26
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
undefined
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
language: node_js
node_js:
- '0.10'
- "8"
- "7"
- "6"
- "5"
- "4"
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "moment-hijri",
"main": "moment-hijri.js",
"version": "0.4.2",
"version": "2.1.0",
"homepage": "https://github.com/xsoh/moment-hijri",
"authors": [
"Suhail Alkowaileet <xsoh.k7@gmail.com>"
Expand All @@ -27,6 +27,6 @@
"tests"
],
"dependencies": {
"momentjs": "~2.10.3"
"momentjs": "^2.22.1"
}
}
8 changes: 7 additions & 1 deletion moment-hijri.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
, parseTokenOneOrTwoDigits = /\d\d?/, parseTokenOneToThreeDigits = /\d{1,3}/, parseTokenThreeDigits = /\d{3}/, parseTokenFourDigits = /\d{1,4}/, parseTokenSixDigits = /[+\-]?\d{1,6}/, parseTokenWord = /[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.?)|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i, parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/i, parseTokenT = /T/i, parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/

, unitAliases = {
hd: 'idate',
hm: 'imonth',
hy: 'iyear'
}
Expand Down Expand Up @@ -723,7 +724,10 @@
this.iYear(this.iYear() + val)
} else if (units === 'imonth') {
this.iMonth(this.iMonth() + val)
} else {
} else if (units === 'idate') {
this.iDate(this.iDate() + val)
}
else {
moment.fn.add.call(this, val, units)
}
return this
Expand All @@ -741,6 +745,8 @@
this.iYear(this.iYear() - val)
} else if (units === 'imonth') {
this.iMonth(this.iMonth() - val)
} else if (units === 'idate') {
this.iDate(this.iDate() - val)
} else {
moment.fn.subtract.call(this, val, units)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moment-hijri",
"version": "2.0.1",
"version": "2.1.0",
"description": "A Hijri calendar (Based on Umm al-Qura calculations) plugin for moment.js.",
"author": {
"name": "Suhail Alkowaileet",
Expand Down
61 changes: 60 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ describe('moment', function() {
it('should also has iYears alias', function() {
moment.fn.iYear.should.be.equal(moment.fn.iYears)
})

it('should add years', function() {
var m = moment('1409-07-18', 'iYYYY-iMM-iDD')
m.add(1, 'iYear')
m.format('iYYYY-iMM-iDD').should.be.equal('1410-07-18')
m.add(4, 'iYear')
m.format('iYYYY-iMM-iDD').should.be.equal('1414-07-18')
m.add(1, 'iYear')
m.format('iYYYY-iMM-iDD').should.be.equal('1415-07-18')
})

it('should subtract years', function() {
var m = moment('1409-07-18', 'iYYYY-iMM-iDD')
m.subtract(1, 'iYear')
m.format('iYYYY-iMM-iDD').should.be.equal('1408-07-18')
m.subtract(5, 'iYear')
m.format('iYYYY-iMM-iDD').should.be.equal('1403-07-18')
m.subtract(1, 'iYear')
m.format('iYYYY-iMM-iDD').should.be.equal('1402-07-18')
})
})

describe('#iMonth', function() {
Expand Down Expand Up @@ -372,6 +392,26 @@ describe('moment', function() {
m.iMonth('Jum-I')
m.format('iYYYY/iM/iD').should.be.equal('1436/5/10')
})

it('should add months', function() {
var m = moment('1409-07-18', 'iYYYY-iMM-iDD')
m.add(1, 'iMonth')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-08-18')
m.add(4, 'iMonth')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-12-18')
m.add(1, 'iMonth')
m.format('iYYYY-iMM-iDD').should.be.equal('1410-01-18')
})

it('should subtract months', function() {
var m = moment('1409-07-18', 'iYYYY-iMM-iDD')
m.subtract(1, 'iMonth')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-06-18')
m.subtract(5, 'iMonth')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-01-18')
m.subtract(1, 'iMonth')
m.format('iYYYY-iMM-iDD').should.be.equal('1408-12-18')
})
})

describe('#iDate', function() {
Expand Down Expand Up @@ -401,6 +441,26 @@ describe('moment', function() {
it('should also has iDates alias', function() {
moment.fn.iDate.should.be.equal(moment.fn.iDates)
})

it('should add days', function() {
var m = moment('1409-07-18', 'iYYYY-iMM-iDD')
m.add(1, 'iDate')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-07-19')
m.add(10, 'iDate')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-07-29')
m.add(1, 'iDate')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-08-01')
})

it('should subtract days', function() {
var m = moment('1409-07-18', 'iYYYY-iMM-iDD')
m.subtract(1, 'iDate')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-07-17')
m.subtract(10, 'iDate')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-07-07')
m.subtract(7, 'iDate')
m.format('iYYYY-iMM-iDD').should.be.equal('1409-06-30')
})
})

describe('#iDayOfYear', function() {
Expand Down Expand Up @@ -446,5 +506,4 @@ describe('moment', function() {

})


})

0 comments on commit 3e33f26

Please sign in to comment.