Skip to content

Commit

Permalink
Implement L1-L2 UA string maker
Browse files Browse the repository at this point in the history
  • Loading branch information
inukshuk committed May 11, 2016
1 parent ae0d1a6 commit a97f0e2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/bitmask.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ class Bitmask {
})
}

marks(values, symbol = '?') {
return values
.map((value, idx) => [
this.qualified(idx * 2) ? symbol : '',
value,
this.qualified(idx * 2 + 1) ? symbol : ''
].join(''))
}

qualified(idx) {
return !!(idx && this.value === Bitmask.UA[idx])
}

qualify(idx) {
return (this.value = this.value | Bitmask.UA[idx]), this
}
Expand Down
10 changes: 9 additions & 1 deletion src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,15 @@ class ExtDate extends Date {
let values = this.values.map(pad)

if (this.unspecified.value)
values = this.unspecified.masks(values)
return this.unspecified.masks(values).join('-')

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

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

return values.join('-')
}
Expand Down
32 changes: 31 additions & 1 deletion test/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Date } = require('..')

describe('Date', () => {

describe.only('.edtf', () => {
describe('.edtf', () => {
it('default', () =>
expect(new Date().edtf)
.to.match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ$/))
Expand Down Expand Up @@ -52,5 +52,35 @@ describe('Date', () => {
it('YXYX-MX-DD', () =>
expect(new Date({ values: [2014, 3, 15], unspecified: 'yxyxmxdd' }).edtf)
.to.eql('2X1X-0X-15'))

it('YYYY-MM-DD?', () =>
expect(new Date({ values: [2014, 3, 15], uncertain: true }).edtf)
.to.eql('2014-04-15?'))

it('YYYY-MM-?DD', () =>
expect(new Date({ values: [2014, 3, 15], uncertain: 'day' }).edtf)
.to.eql('2014-04-?15'))

it('YYYY-MM?-DD', () =>
expect(new Date({ values: [2014, 3, 15], uncertain: 'xxxxxxdd' }).edtf)
.to.eql('2014-04?-15'))

it('YYYY-?MM-DD', () =>
expect(new Date({ values: [2014, 3, 15], uncertain: 'month' }).edtf)
.to.eql('2014-?04-15'))

it('YYYY?-MM-DD', () =>
expect(new Date({ values: [2014, 3, 15], uncertain: 'year' }).edtf)
.to.eql('2014?-04-15'))

it('YYYY-MM?-~DD', () =>
expect(new Date({
values: [2004, 5, 11], uncertain: 'xxxxxxdd', approximate: 'day'
}).edtf).to.eql('2004-06?-~11'))

it('YYYY-%MM-DD', () =>
expect(new Date({
values: [2004, 5, 11], uncertain: 'month', approximate: 'month'
}).edtf).to.eql('2004-%06-11'))
})
})

0 comments on commit a97f0e2

Please sign in to comment.