Skip to content

Commit

Permalink
Added support for the preferPast option
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyjb committed Oct 2, 2019
1 parent 734ed6b commit d25746a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
28 changes: 24 additions & 4 deletions module/date-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ export class DateParser {

constructor(
monthNames=DEFAULT_MONTH_NAMES,
shortMonthNames=DEFAULT_SHORT_MONTH_NAMES
shortMonthNames=DEFAULT_SHORT_MONTH_NAMES,
preferPast=false
) {
// Set the month names used when formatting/parsing dates
this.monthNames = monthNames
this.shortMonthNames = shortMonthNames

// If `true` then the parser will prefer past dates when determining
// what century to generate (if not given).
this.preferPast = preferPast
}

// -- Getters & Setters ---

get monthNames() {
return this._monthNames.slice()
}
Expand Down Expand Up @@ -79,6 +85,20 @@ export class DateParser {
return this.constructor.formatters[formatter](this, date)
}

/**
* Return a full year (4-digit) from a partial year (2-digit).
*/
getFullYear(year) {
let currentYear = (new Date()).getYear()
let century = parseInt((new Date()).getFullYear() / 100, 10)

if (this.preferPast && (year + 100) > currentYear) {
century -= 1
}

return year + (century * 100)
}

/**
* Attempt to parse (and return) a string as a date using the list of
* named parsers.
Expand Down Expand Up @@ -178,7 +198,7 @@ DateParser.parsers = {
// If the year doesn't contain the century then we add the current
// century to it.
if (year < 100) {
year += parseInt((new Date()).getFullYear() / 100, 10) * 100
year = inst.getFullYear(year)
}

// Convert the components to a native date
Expand Down Expand Up @@ -298,7 +318,7 @@ DateParser.parsers = {
// If the year doesn't contain the century then we add the current
// century to it.
if (year < 100) {
year += parseInt((new Date()).getFullYear() / 100, 10) * 100
year = inst.getFullYear(year)
}
}

Expand Down Expand Up @@ -379,7 +399,7 @@ DateParser.parsers = {
// If the year doesn't contain the century then we add the current
// century to it.
if (year < 100) {
year += parseInt((new Date()).getFullYear() / 100, 10) * 100
year = inst.getFullYear(year)
}

// Convert the components to a native date
Expand Down
10 changes: 9 additions & 1 deletion module/date-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ export class DatePicker {
*/
'parsers': ['human', 'dmy', 'iso'],

/**
* A flag indicating that when the date parser automatically
* generates a century for a date it should pick a century for
* for past dates over future dates.
*/
'preferPast': false,

/**
* A list of abbreviated month names used by the date picker.
*/
Expand Down Expand Up @@ -118,7 +125,8 @@ export class DatePicker {
// parse options that contain dates).
this._dateParser = new DateParser(
this._options.monthNames,
this._options.shortMonthNames
this._options.shortMonthNames,
this._options.preferPast
)

// Handle date based options
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "manhattan-date-picker",
"version": "1.0.0",
"version": "1.0.1",
"description": "Date parsing and picking for form fields.",
"engines": {
"node": ">=8.9.4"
Expand Down
63 changes: 63 additions & 0 deletions spec/date-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ chai.should()
chai.use(require('sinon-chai'))


// Utils

function freezeDate(d) {
Date._original = Date

class FrozenDate extends Date {
constructor() {
super()
return new Date._original(d)
}
}

global.Date = FrozenDate
}

function unfreezeDate() {
global.Date = Date._original
}


// Tests

describe('DateParser', () => {

describe('constructor', () => {
Expand Down Expand Up @@ -96,6 +118,10 @@ describe('DateParser', () => {
parser = new DateParser()
})

afterEach(() => {
parser = new DateParser()
})

describe('format', () => {
it('should format the given date using the named '
+ 'formatter', () => {
Expand All @@ -106,6 +132,43 @@ describe('DateParser', () => {
})
})

describe('getFullYear (preferPast: false)', () => {

beforeEach(() => {
freezeDate(new Date(2020, 5, 11))
})

afterEach(() => {
unfreezeDate()
})

it('should return a full year for the current century', () => {
const y = parser.getFullYear(50)
y.should.equal(2050)
})
})

describe('getFullYear (preferPast: true)', () => {

beforeEach(() => {
freezeDate(new Date(2020, 5, 11))
})

afterEach(() => {
unfreezeDate()
})

it('should return a full year for the previous century', () => {
parser.preferPast = true

let y = parser.getFullYear(50)
y.should.equal(1950)

y = parser.getFullYear(10)
y.should.equal(2010)
})
})

describe('parse', () => {
it('should attempt to parse the given string using the list of '
+ 'named parsers', () => {
Expand Down
1 change: 1 addition & 0 deletions spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
name="date"
value="11/06/2018"
data-mh-date-picker
data-mh-date-picker--prefer-past
>
</label>

Expand Down

0 comments on commit d25746a

Please sign in to comment.