Skip to content

Commit

Permalink
Adds support for passing integer numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
arashm committed Aug 24, 2019
1 parent da4fd16 commit 2279b74
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ For initializing `JDate` you may either pass an array of Jalali date to it or a
```javascript
const JDate = require('jalali-date');
const jdate = new JDate; // => default to today
const jdate2 = new JDate([1393, 10, 11]);
const jdate3 = new JDate(new Date(2014, 1, 3));
const jdate2 = new JDate(1393, 10, 11);
const jdate3 = new JDate([1393, 10, 11]);
const jdate4 = new JDate(new Date(2014, 1, 3));

```

### API
```javascript
jdate.date //=> [1393,5,13] An Array of Jalali Date
jdate.date //=> [1393, 5, 13] An Array of Jalali Date
jdate._d // => Gregorian Date Object

// Getters
Expand All @@ -59,7 +60,7 @@ jdate.format('dddd DD MMMM YYYY') // => پنج‌شنبه 12 شهریور 1394
// Static functions
JDate.isLeapYear(1393) // => false
JDate.daysInMonth(1393, 5) // => 31
JDate.toGregorian(1393,12,11) // => Gregorian Date object
JDate.toGregorian(1393, 12, 11) // => Gregorian Date object
JDate.toJalali(new Date) // => JDate object
```

Expand Down
27 changes: 18 additions & 9 deletions lib/jdate.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/jdate.min.js

Large diffs are not rendered by default.

21 changes: 15 additions & 6 deletions src/jdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ import Converter from './converter';
import * as helpers from './helpers';

export default class JDate {
constructor(input = new Date()) {
this.input = input;
if (Array.isArray(input)) {
this.date = input.map((num) => parseInt(num, 10));
constructor(...args) {
if (Array.isArray(args[0]) || args[0] instanceof Date) {
[this.input] = args;
} else if (args.length === 3) {
this.input = args;
} else if (!args.length) {
this.input = new Date();
} else {
throw new Error('Unexpected input');
}

if (Array.isArray(this.input)) {
this.date = this.input.map((num) => parseInt(num, 10));
this._d = this.toGregorian();
} else if (input instanceof Date) {
this._d = input;
} else if (this.input instanceof Date) {
this._d = this.input;
this.date = JDate.toJalali(this.input);
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/jdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ describe('JDate', () => {
expect(jdate._d.getDate()).toEqual(1);
});

it('should convert by passing just integers', () => {
const jdate = new JDate(1396, 10, 11);

expect(jdate.date).toEqual([1396, 10, 11]);
expect(jdate.input).toEqual([1396, 10, 11]);
expect(jdate._d).toBeInstanceOf(Date);
expect(jdate._d.getFullYear()).toEqual(2018);
expect(jdate._d.getMonth()).toEqual(0);
expect(jdate._d.getDate()).toEqual(1);
});

it('should convert a JS date object', () => {
const currentDate = new Date(2018, 0, 1);
const jdate = new JDate(currentDate);
Expand Down

0 comments on commit 2279b74

Please sign in to comment.