use daysPerYear to calculate month / year seconds
This commit is contained in:
parent
6a85f0f261
commit
f7366f6dfa
@ -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`
|
||||
|
@ -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.
|
||||
|
7
index.js
7
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
|
||||
}
|
||||
|
@ -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": {
|
||||
|
13
test.js
13
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)
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user