Skip to content

Commit

Permalink
Merge pull request #25 from akhilome/bg-fix-date-160341907
Browse files Browse the repository at this point in the history
#160341907 Fix Date Utility
  • Loading branch information
akhilome committed Sep 7, 2018
2 parents 469ab5c + 8ec6665 commit 04d276d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
11 changes: 4 additions & 7 deletions server/utils/date.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import padding from './padding';

function makeTodaysDate() {
const now = new Date(Date.now());
const [year, month, day] = [
now.getFullYear(),
now.getMonth(),
now.getDay(),
now.getMonth() + 1,
now.getDate(),
];

const padding = (num) => {
const padded = num > 10 ? `${num}` : `0${num}`;
return padded;
};

return `${year}-${padding(month)}-${padding(day)}`;
}

Expand Down
9 changes: 9 additions & 0 deletions server/utils/padding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const padding = (number) => {
if (Number.isNaN(Number(number)) || Number(number) < 1) return -1;
if (`${number}`.length > 1 || number > 10) {
return `${number}`;
}
return `0${number}`;
};

export default padding;
5 changes: 3 additions & 2 deletions tests/utils/date.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chai from 'chai';
import 'chai/register-should';
import dirtyChai from 'dirty-chai';
import makeTodaysDate from '../../server/utils/date';
import padding from '../../server/utils/padding';

chai.use(dirtyChai);

Expand All @@ -17,8 +18,8 @@ describe('Today\'s Date Utility Function', () => {
it('should return today\'s date', () => {
const today = new Date(Date.now());
makeTodaysDate().should.be.a('string').that.has.a.lengthOf('YYYY-MM-DD'.length);
makeTodaysDate().should.be.a('string').which.includes(today.getDay());
makeTodaysDate().should.be.a('string').which.includes(today.getMonth());
makeTodaysDate().should.be.a('string').which.includes(padding(today.getDate()));
makeTodaysDate().should.be.a('string').which.includes(padding(today.getMonth() + 1));
makeTodaysDate().should.be.a('string').which.includes(today.getFullYear());
});
});
19 changes: 19 additions & 0 deletions tests/utils/padding.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'chai/register-should';
import padding from '../../server/utils/padding';

describe('Padding Function', () => {
it('should return a padded number as string given a single digit', () => {
padding(2).should.be.a('string').that.has.lengthOf(2);
});

it('should not pad numbers which are not single digits', () => {
padding(200).should.be.eql('200');
padding('01').should.be.eql('01');
});

it('should only pad numbers', () => {
padding('Hey!').should.be.eql(-1);
padding('22').should.be.eql('22');
padding('1').should.be.eql('01');
});
});

0 comments on commit 04d276d

Please sign in to comment.