From f7366f6dfa62aa8cd91ddf8369d83177ec00ac3d Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Tue, 25 Jul 2017 22:29:35 +0100 Subject: [PATCH] use daysPerYear to calculate month / year seconds --- CHANGELOG.md | 5 +++++ README.md | 6 +++++- index.js | 7 ++++--- package.json | 2 +- test.js | 13 +++++++------ 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f295ab..f288ddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 5.0.0 + +- Add `daysPerYear` configuration option +- Use `daysPerYear` configuration option to convert months or years to seconds + ## 4.0.0 - Drop support for Node.js `< 4.0.0` diff --git a/README.md b/README.md index fc9796e..396afeb 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ A few assumptions are made by default: 2. There are 7 days per week 3. There are 4 weeks per month 4. There are 12 months per year +5. There are 365.25 days per year These options can be changed by passing an options object as an argument to `timestring`. @@ -120,6 +121,7 @@ The following options are configurable: 2. `daysPerWeek` 3. `weeksPerMonth` 4. `monthsPerYear` +5. `daysPerYear` ```js const timestring = require('timestring') @@ -138,7 +140,7 @@ In the example above `hoursPerDay` is being set to `1`. When the time string is This would be useful for specific application needs. -*Example - Employees of my company work 7.5 hours a day, and only work 5 days a week. In my time tracking app, when they type `1d` i want 7.5 hours to be tracked. When they type `1w` i want 5 days to be tracked etc.* +*Example - Employees of my company work 7.5 hours a day, and only work 5 days a week. In my time tracking app, when they type `1d` i want 7.5 hours to be tracked. When they type `1w` i want 5 days to be tracked etc.* ```js const timestring = require('timestring') @@ -154,3 +156,5 @@ let daysThisWeek = timestring('1w', 'd', opts) console.log(hoursToday) // will log 7.5 console.log(daysThisWeek) // will log 5 ``` + +It is important to note that the `daysPerYear` configuration option will be used to convert a month or year to seconds, so if you are using custom configuration options make sure that you adjust this value to suit if you expect to be parsing timestrings containing months or years. diff --git a/index.js b/index.js index f3ff176..fde6006 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,8 @@ const DEFAULT_OPTS = { hoursPerDay: 24, daysPerWeek: 7, weeksPerMonth: 4, - monthsPerYear: 12 + monthsPerYear: 12, + daysPerYear: 365.25 } /** @@ -88,8 +89,8 @@ function getUnitValues (opts) { unitValues.d = opts.hoursPerDay * unitValues.h unitValues.w = opts.daysPerWeek * unitValues.d - unitValues.mth = opts.weeksPerMonth * unitValues.w - unitValues.y = opts.monthsPerYear * unitValues.mth + unitValues.mth = (opts.daysPerYear / opts.monthsPerYear) * unitValues.d + unitValues.y = opts.daysPerYear * unitValues.d return unitValues } diff --git a/package.json b/package.json index 1e05939..9c1c0d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "timestring", - "version": "4.0.0", + "version": "5.0.0", "description": "Parse a human readable time string into a time based value", "main": "index.js", "scripts": { diff --git a/test.js b/test.js index 5d2d1e7..7536548 100644 --- a/test.js +++ b/test.js @@ -15,8 +15,8 @@ describe('timestring', () => { expect(timestring('1h')).to.equal(3600) expect(timestring('1d')).to.equal(86400) expect(timestring('1w')).to.equal(604800) - expect(timestring('1mth')).to.equal(2419200) - expect(timestring('1y')).to.equal(29030400) + expect(timestring('1mth')).to.equal(2629800) + expect(timestring('1y')).to.equal(31557600) }) it('can parse different unit identifiers', () => { @@ -56,11 +56,11 @@ describe('timestring', () => { }) unitMap.mth.forEach(unit => { - expect(timestring(`9 ${unit}`)).to.equal(21772800) + expect(timestring(`9 ${unit}`)).to.equal(23668200) }) unitMap.y.forEach(unit => { - expect(timestring(`1 ${unit}`)).to.equal(29030400) + expect(timestring(`1 ${unit}`)).to.equal(31557600) }) }) @@ -75,12 +75,13 @@ describe('timestring', () => { hoursPerDay: 1, daysPerWeek: 2, weeksPerMonth: 3, - monthsPerYear: 4 + monthsPerYear: 4, + daysPerYear: 30 } expect(timestring('1d', 'h', opts)).to.equal(1) expect(timestring('1w', 'd', opts)).to.equal(2) - expect(timestring('1mth', 'w', opts)).to.equal(3) + expect(timestring('1mth', 'w', opts)).to.equal(3.75) expect(timestring('1y', 'mth', opts)).to.equal(4) })