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

complete up to save/load #5

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions .diary
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"body": "I'm standing outside Brad's house #yolo",
"date": "04/03/2017"
}
]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# assignment_test_driven_diary
Build a command line diary interface, driven and protected by a suite of comprehensive tests.
Mark and Nicholas
91 changes: 91 additions & 0 deletions spec/diarySpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const Diary = require('../src/diary.js');

describe('When we make a diary entry', function() {
let diary, d;

beforeEach(function() {
diary = new Diary();
d = diary.getFormattedDate();
});

it('adds an entry to current diary with date appended', function() {
expect(diary.entry(`Brad is everything to me!`)).toEqual({
body: 'Brad is everything to me!',
date: `${d}`
});
});

it('adds an entry to current diary with a date argument', function() {
var d = 'January at 4PM';
expect(diary.entry(`Brad is everything to me!`, d)).toEqual({
body: 'Brad is everything to me!',
date: 'January at 4PM'
});
});
});

describe('When we want to view diary entries', function() {
beforeEach(function() {
diary = new Diary();
d = diary.getFormattedDate();
diary.entry("I'm standing outside Brad's house #yolo");
diary.entry("I'm at Brad's window #yolo");
diary.entry('OMG. What have I done? #sorrynotsorry', '04/03/2017');
});

it('should return all entries', function() {
expect(diary.entries()).toEqual([
{ body: `I'm standing outside Brad's house #yolo`, date: `${d}` },
{ body: `I'm at Brad's window #yolo`, date: `${d}` },
{ body: `OMG. What have I done? #sorrynotsorry`, date: `04/03/2017` }
]);
});

it('we can see all tags', function() {
expect(diary.tags()).toEqual(['yolo', 'sorrynotsorry']);
});

it('should return every entry with given tag', function() {
expect(diary.entriesWithTag('yolo')).toEqual([
{ body: `I'm standing outside Brad's house #yolo`, date: `${d}` },
{ body: `I'm at Brad's window #yolo`, date: `${d}` }
]);
});

it('should return all entries written today', function() {
expect(diary.today()).toEqual([
{ body: `I'm standing outside Brad's house #yolo`, date: `${d}` },
{ body: `I'm at Brad's window #yolo`, date: `${d}` }
]);
});

it('should return all entries written on given date', function() {
expect(diary.date('04/03/2017')).toEqual([
{ body: `OMG. What have I done? #sorrynotsorry`, date: `04/03/2017` }
]);
});

it('should return all entries containing given string'), function() {
expect(diary.search('Brad')).toEqual([
{ body: `I'm standing outside Brad's house #yolo`, date: `${d}` },
{ body: `I'm at Brad's window #yolo`, date: `${d}` }
]);
};
});

describe('When we want to manage data', function() {
const fs = require("fs");

beforeEach(function() {
diary = new Diary();
d = diary.getFormattedDate();
diary.entry("I'm standing outside Brad's house #yolo", "04/03/2017");
});

it('should save all current entries to a provided location', function() {
diary.save('./.diary')
const diaryFile = require('../.diary')
expect(diaryFile).toBe("I'm standing outside Brad's house #yolo");
})

})
103 changes: 103 additions & 0 deletions src/diary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const fs = require("fs");

class Diary {
constructor() {
this.entriesLog = [];
}

getFormattedDate() {
var todayTime = new Date();
var month = todayTime.getMonth() + 1;
var day = todayTime.getDate();
var year = todayTime.getFullYear();
return month + '/' + day + '/' + year;
}

entry(entry, customDate) {
var newEntry = {};

if (customDate) {
newEntry['body'] = entry;
newEntry['date'] = customDate;
} else {
newEntry['body'] = entry;
newEntry['date'] = this.getFormattedDate();
}

this.entriesLog.push(newEntry);
return newEntry;
}

entries() {
return this.entriesLog;
}

//Begin tag functions
getTags() {
let tagsList = [];
this.entriesLog.forEach(entry => {
let tagMatch = entry.body.match(/#\w+/);
let tag = tagMatch[0].substring(1);
tagsList.push(tag);
});
return tagsList;
}

tags() {
let tags = [];
let tagsList = this.getTags();
//Filters duplicates
tagsList.forEach(tag => {
if (!tags.includes(tag)) {
tags.push(tag);
}
});

return tags;
}

entriesWithTag(tag) {
let entriesWithTags = [];

this.entriesLog.forEach(entry => {
let tagMatch = entry.body.match(/#\w+/);
if (tagMatch[0].substring(1) === tag) {
entriesWithTags.push(entry);
}
});
return entriesWithTags;
}

today() {
let todaysDate = this.getFormattedDate();
return this.date(todaysDate);
}

date(requestedDate) {
let requestedDateEntries = [];
this.entriesLog.forEach(entry => {
if (entry.date === requestedDate) {
requestedDateEntries.push(entry);
}
});
return requestedDateEntries;
}

search(query) {
let entriesWithQuery = [];

this.entriesLog.forEach(entry => {
if (entry.body.includes(query)) {
entriesWithQuery.push(entry);
}
});
return entriesWithQuery;
}

save(path) {

fs.writeFileSync(path, JSON.stringify(this.entriesLog, null, 2))
}
}

module.exports = Diary;