Skip to content

Commit

Permalink
Add Decade tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inukshuk committed May 13, 2016
1 parent 9ed9743 commit 6a3e55b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const ExtDate = require('./src/date')
const Year = require('./src/year')
const Decade = require('./src/decade')
const Season = require('./src/season')
const Bitmask = require('./src/bitmask')
const types = require('./src/types')
Expand All @@ -21,6 +22,7 @@ function edtf(...args) {
module.exports = Object.assign(edtf, {
Date: ExtDate,
Year,
Decade,
Season,
Bitmask,
parse,
Expand Down
15 changes: 8 additions & 7 deletions src/decade.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Decade {
constructor(input) {
V[this] = []

this.uncertain = 0
this.approximate = 0

switch (typeof input) {
case 'number':
this.decade = input
Expand Down Expand Up @@ -104,17 +107,15 @@ class Decade {
}

toEDTF() {
let values = this.values.map(Decade.pad)
let decade = Decade.pad(this.decade)

if (this.uncertain.value)
values = this.uncertain.marks(values, '?')
decade = decade + '?'

if (this.approximate.value) {
values = this.approximate.marks(values, '~')
.map(value => value.replace(/~\?/, '%'))
}
if (this.approximate.value)
decade = (decade + '~').replace(/\?~/, '%')

return values[0]
return decade
}

static pad(number) {
Expand Down
26 changes: 26 additions & 0 deletions test/decade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const { Decade } = require('..')

describe('Decade', () => {

describe('.edtf', () => {
it('default', () =>
expect(new Decade().edtf).to.match(/^\d\d\d$/))

it('DDD', () => {
expect(new Decade([198]).edtf).to.eql('198')
expect(new Decade(99).edtf).to.eql('099')
})

it('-DDD', () => {
expect(new Decade(-9).edtf).to.eql('-009')
})

it('DDD~', () => {
expect(new Decade({ values: [198], approximate: true }).edtf)
.to.eql('198~')
})
})
})

0 comments on commit 6a3e55b

Please sign in to comment.