Skip to content

Commit

Permalink
feat: isOverStatePensionAge() no longer requires gender
Browse files Browse the repository at this point in the history
  • Loading branch information
gunjam committed Apr 8, 2021
1 parent c7e99dc commit d947f70
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ const spaString = getStatePensionDateAsString('1990-03-25', 'female');
```

### `isOverStatePensionAge()`
Takes the same parameters as `getStatePensionDate()` but returns a `boolean` if the State Pension age date is today or in the past.
Takes a date of birth and returns a `boolean`, returning `true` if the State Pension age date is today or in the past, `false` if it’s in the future.

**For example:**

```javascript
const {isOverStatePensionAge} = require('get-state-pension-date');

// boolean: true
const overSpa = isOverStatePensionAge('1953-03-25', 'male');
const overSpa = isOverStatePensionAge('1953-03-25');

// boolean: false
const workingAge = isOverStatePensionAge('1990-03-25', 'female');
const workingAge = isOverStatePensionAge('1990-03-25');
```

All functions will throw if the date of birth is not a `YYYY-MM-DD` formatted string or if the gender is not a string of `male` or `female`.
Expand Down
12 changes: 7 additions & 5 deletions src/get-state-pension-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ const getStatePensionDateAsString = (dateOfBirth, gender) => {
};

/**
* Function determine whether a person of a given 'gender' and 'Date of birth'
* would be over State Pension age.
* Function to determine whether a person is currently over State Pension age
* based on their date of birth.
*
* @param {string} dateOfBirth Input date of birth string (YYYY-MM-DD)
* @param {string} gender gender as string ('male' or 'female')
* @returns {boolean} true if over SPA, false if not
*/
const isOverStatePensionAge = (dateOfBirth, gender) => {
const spaDate = getStatePensionDate(dateOfBirth, gender);
const isOverStatePensionAge = dateOfBirth => {
// Since December 2018 the State Pension age has been equalised for men and
// women so we no longer need a gender flag to passed by the user, hardcoding
// for this call.
const spaDate = getStatePensionDate(dateOfBirth, 'male');
const now = Date.now();
return spaDate.getTime() <= now;
};
Expand Down
13 changes: 8 additions & 5 deletions test/is-over-state-pension-age.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

const t = require('tap');
const timekeeper = require('timekeeper');
const {isOverStatePensionAge} = require('../src/get-state-pension-date');
const {isOverStatePensionAge: overSPA} = require('../src/get-state-pension-date');

// Freeze date so SPA date for 1954-03-25 is always 'today'
timekeeper.freeze(new Date(2019, 8, 6));

// Return type
t.type(isOverStatePensionAge('1953-12-05', 'male'), 'boolean', 'returns a boolean');
t.type(overSPA('1953-12-05'), 'boolean', 'returns a boolean');

// Reuses getStatePensionDate() validation
t.throws(() => overSPA('fail'), 'throws if date of birth is not valid');

// Tests
t.ok(isOverStatePensionAge('1953-12-05', 'male'), 'true when SPA date is in the past');
t.ok(isOverStatePensionAge('1954-03-25', 'female'), 'true when SPA date is today');
t.notOk(isOverStatePensionAge('1990-12-05', 'female'), 'false when SPA date is in the future');
t.ok(overSPA('1953-12-05'), 'true when SPA date is in the past');
t.ok(overSPA('1954-03-25'), 'true when SPA date is today');
t.notOk(overSPA('1990-12-05'), 'false when SPA date is in the future');

timekeeper.reset();

0 comments on commit d947f70

Please sign in to comment.