Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#160341907 Fix Date Utility #25

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});