Skip to content

Commit

Permalink
Issue #456: Adding min max year to AutoCorrectedDatePipe (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
dclark27 authored and lozjackson committed Jul 30, 2018
1 parent 9303da8 commit 46e275f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
5 changes: 5 additions & 0 deletions addons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ to Text Mask.
### `createAutoCorrectedDatePipe`

The `createAutoCorrectedDatePipe` returns a `autoCorrectedDatePipe`, which can help the user in entering a date.
The `createAutoCorrectedDatePipe` accepts a string specifying date format and an object with the following keys:

1. `minYear` (number): the minimum year allowed in the date field `mask`.
1. `maxYear` (number): the maximum year allowed in the date field `mask`.


For example, if the user enters a value
larger than `1` in the 1st slot of month, it appends `0` to it. That is `4` => `04`. It does a similar thing for the
Expand Down
15 changes: 11 additions & 4 deletions addons/src/createAutoCorrectedDatePipe.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const maxValueMonth = [31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
const formatOrder = ['yyyy', 'yy', 'mm', 'dd', 'HH', 'MM', 'SS']
export default function createAutoCorrectedDatePipe(dateFormat = 'mm dd yyyy') {
export default function createAutoCorrectedDatePipe(dateFormat = 'mm dd yyyy', {
minYear = 1,
maxYear = 9999
} = {}) {
const dateFormatArray = dateFormat
.split(/[^dmyHMS]+/)
.sort((a, b) => formatOrder.indexOf(a) - formatOrder.indexOf(b))
return function(conformedValue) {
const indexesOfPipedChars = []
const maxValue = {'dd': 31, 'mm': 12, 'yy': 99, 'yyyy': 9999, 'HH': 23, 'MM': 59, 'SS': 59}
const minValue = {'dd': 1, 'mm': 1, 'yy': 0, 'yyyy': 1, 'HH': 0, 'MM': 0, 'SS': 0}
const maxValue = {'dd': 31, 'mm': 12, 'yy': 99, 'yyyy': maxYear, 'HH': 23, 'MM': 59, 'SS': 59}
const minValue = {'dd': 1, 'mm': 1, 'yy': 0, 'yyyy': minYear, 'HH': 0, 'MM': 0, 'SS': 0}
const conformedValueArr = conformedValue.split('')

// Check first digit
Expand All @@ -33,7 +36,11 @@ export default function createAutoCorrectedDatePipe(dateFormat = 'mm dd yyyy') {
month = value || 0
}
const maxValueForFormat = format === 'dd' ? maxValueMonth[month] : maxValue[format]

if (format === 'yyyy' && (minYear !== 1 || maxYear !== 9999)) {
const scopedMaxValue = parseInt(maxValue[format].toString().substring(0, textValue.length), 10)
const scopedMinValue = parseInt(minValue[format].toString().substring(0, textValue.length), 10)
return value < scopedMinValue || value > scopedMaxValue
}
return value > maxValueForFormat || (textValue.length === length && value < minValue[format])
})

Expand Down
67 changes: 67 additions & 0 deletions addons/test/createAutoCorrectedDatePipe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,71 @@ describe('createAutoCorrectedDatePipe', () => {
)
})
})
describe('createAutoCorrectDatePipe with min year', () => {
let autoCorrectedDateTimePipe

it('accepts minimum year as the second parameter and returns a date time pipe function', () => {
autoCorrectedDateTimePipe = createAutoCorrectedDatePipe('mm dd yyyy', {minYear: 1999})
})

it('returns false if year 1st digit is less than 1', () => {
expect(autoCorrectedDateTimePipe('12/31/0')).to.equal(false)
})

it('returns false if year 2st digit is less than 9', () => {
expect(autoCorrectedDateTimePipe('12/31/18')).to.equal(false)
})

it('returns false if year 3rd digit is less than 9', () => {
expect(autoCorrectedDateTimePipe('12/31/198')).to.equal(false)
})

it('returns false if year 4th digit is less than 9', () => {
expect(autoCorrectedDateTimePipe('12/31/1998')).to.equal(false)
})

it('allows for min year', () => {
let pipe = createAutoCorrectedDatePipe('mm dd yyyy', {minYear: 1999})
expect(pipe('12 31 1999')).to.deep.equal({value: '12 31 1999', indexesOfPipedChars: []})
})
})

describe('createAutoCorrectDatePipe with min and max year', () => {
let autoCorrectedDateTimePipe

it('accepts min and max year as the second/third parameter and returns a date time pipe function', () => {
autoCorrectedDateTimePipe = createAutoCorrectedDatePipe('mm dd yyyy', {minYear: 1999, maxYear: 2020})
})

it('returns false if year 1st digit is more than 2', () => {
expect(autoCorrectedDateTimePipe('12/31/3')).to.equal(false)
})

it('returns false if year 2st digit is more than 0', () => {
expect(autoCorrectedDateTimePipe('12/31/21')).to.equal(false)
})

it('returns false if year 3rd digit is more than 2', () => {
expect(autoCorrectedDateTimePipe('12/31/203')).to.equal(false)
})

it('returns false if year 4th digit is more than 0', () => {
expect(autoCorrectedDateTimePipe('12/31/2021')).to.equal(false)
})

it('allows for a year at the top side of the range', () => {
let pipe = createAutoCorrectedDatePipe('mm dd yyyy', {minYear: 1990, maxYear: 2020})
expect(pipe('12 31 2020')).to.deep.equal({value: '12 31 2020', indexesOfPipedChars: []})
})

it('allows for a year within the range', () => {
let pipe = createAutoCorrectedDatePipe('mm dd yyyy', {minYear: 1990, maxYear: 2020})
expect(pipe('12 31 2000')).to.deep.equal({value: '12 31 2000', indexesOfPipedChars: []})
})

it('allows for a year at the bottom side of the range', () => {
let pipe = createAutoCorrectedDatePipe('mm dd yyyy', {minYear: 1990, maxYear: 2020})
expect(pipe('12 31 1990')).to.deep.equal({value: '12 31 1990', indexesOfPipedChars: []})
})
})
})

0 comments on commit 46e275f

Please sign in to comment.