Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #19 from chrisfosterelli/json-to-object
Browse files Browse the repository at this point in the history
JSON -> Object
  • Loading branch information
chrisfosterelli authored Jul 12, 2016
2 parents ae06855 + be50816 commit d8bff9e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class Entry {
}

if (typeof message === 'object')
return this._fromJSON(message)
return this._fromObject(message)

this.version = version
this.user = user
Expand All @@ -28,7 +28,7 @@ export default class Entry {
this.parseTags(message)
}

_fromJSON(doc) {
_fromObject(doc) {
Object.assign(this, doc)
const start = new Date(this.start)
const end = new Date(this.end)
Expand Down Expand Up @@ -64,7 +64,7 @@ export default class Entry {
return { start, end }
}

toJSON() {
toObject() {
return {
_id: this._id,
version: this.version,
Expand All @@ -84,7 +84,7 @@ export default class Entry {
}
}

static fromJSON(doc) {
static fromObject(doc) {
let e = new Entry(doc.user, doc)

return e
Expand Down
48 changes: 24 additions & 24 deletions src/test/entry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,24 @@ test('set timezone offset', t => {
test('created date added to entry', t => {
const today = new Date()
const e = new Entry(userId, '8am-5pm working on things')
const json = e.toJSON()
const obj = e.toObject()

t.ok(e.ref, 'ref is set')
t.ok(moment(today).isSame(e.ref, 'second'), 'ref date is today')
t.ok(json.ref, 'ref is set on json')
t.ok(moment(today).isSame(json.ref, 'second'), 'json.ref date is today')
t.ok(obj.ref, 'ref is set on object')
t.ok(moment(today).isSame(obj.ref, 'second'), 'obj.ref date is today')
t.end()
})

test('passed reference date added to entry', t => {
const date = new Date(2016, 0, 1, 12, 15, 30) // Jan 1, 2016 12:15:30
const e = new Entry(userId, '8am-5pm working on things', {date})
const json = e.toJSON()
const obj = e.toObject()

t.ok(e.ref, 'ref is set')
t.ok(moment(date).isSame(e.ref, 'second'), 'ref date is Jan 1')
t.ok(json.ref, 'ref is set on json')
t.ok(moment(date).isSame(json.ref, 'second'), 'json.ref date is Jan 1')
t.ok(obj.ref, 'ref is set on object')
t.ok(moment(date).isSame(obj.ref, 'second'), 'obj.ref date is Jan 1')
t.end()
})

Expand Down Expand Up @@ -210,32 +210,32 @@ test('parsed time string is present', t => {
t.end()
})

test('toJSON() returns a json obj', t => {
test('toObject() returns an object', t => {
const e = new Entry(userId, '8am-10am worked on some things #tag1 #tag2')

const json = e.toJSON()
t.equals(json.message, '8am-10am worked on some things #tag1 #tag2', 'message')
t.ok(json.hasDates, 'hasDates')
t.equals(e.start, json.start, 'start')
t.equals(e.end, json.end, 'end')
t.equals(json.duration.seconds, 2*60*60, 'duration seconds')
t.equals(json.time, e.time, 'time')
t.equals(json._id, e._id, '_id')
t.equals(json.version, version, 'version')
t.deepEquals(json.tags, [...e.tags], 'tags array')
t.ok(moment.utc(json.startArr).isSame(json.start), 'start and startArr are same date')
t.ok(moment.utc(json.endArr).isSame(json.end), 'end and endArr are same date')
t.ok(json.startArr instanceof Array, 'startArr is an array')
t.ok(json.endArr instanceof Array, 'endArr is an array')
const obj = e.toObject()
t.equals(obj.message, '8am-10am worked on some things #tag1 #tag2', 'message')
t.ok(obj.hasDates, 'hasDates')
t.equals(e.start, obj.start, 'start')
t.equals(e.end, obj.end, 'end')
t.equals(obj.duration.seconds, 2*60*60, 'duration seconds')
t.equals(obj.time, e.time, 'time')
t.equals(obj._id, e._id, '_id')
t.equals(obj.version, version, 'version')
t.deepEquals(obj.tags, [...e.tags], 'tags array')
t.ok(moment.utc(obj.startArr).isSame(obj.start), 'start and startArr are same date')
t.ok(moment.utc(obj.endArr).isSame(obj.end), 'end and endArr are same date')
t.ok(obj.startArr instanceof Array, 'startArr is an array')
t.ok(obj.endArr instanceof Array, 'endArr is an array')

t.end()
})

test('fromJSON() will create an Entry from existing document', t => {
test('fromObject() will create an Entry from existing document', t => {
const date = new Date('Jan 25, 2015 0:00:00')
const existing = new Entry(userId, '8am-10am worked on things #tag1 #tag2', {date})
const json = existing.toJSON()
const e = Entry.fromJSON(json)
const obj = existing.toObject()
const e = Entry.fromObject(obj)

t.equals(existing._id, e._id, '_id matches')
t.equals(existing.version, e.version, 'version matches')
Expand Down

0 comments on commit d8bff9e

Please sign in to comment.