Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for weekStartsOn and prettier config #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"extends": "eslint:recommended",
"extends": [
"eslint:recommended",
"plugin:prettier/recommended",
"prettier"
],
"env": {
"commonjs": true,
"es6": true,
Expand All @@ -8,9 +12,7 @@
"parserOptions": {
"ecmaVersion": 2018
},
"ignorePatterns": [
"node_modules"
],
"ignorePatterns": ["node_modules"],
"rules": {
"no-unused-vars": "off"
}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ pnpm-debug.log
/shrinkwrap.yaml

/parser.js

.vscode
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
parser.js
grammar.js
.github/workflows
9 changes: 9 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
useTabs: true,
tabWidth: 4,
singleQuote: true,
semi: false,
bracketSpacing: false,
arrowParens: 'avoid',
trailingComma: 'none'
}
10 changes: 7 additions & 3 deletions date-fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

const lex = require('./lex')

const createParse = (dateFns) => {
const parseHumanRelativeTime = (str, now = new Date()) => {
const createParse = dateFns => {
const parseHumanRelativeTime = (
str,
now = new Date(),
opts = {weekStartsOn: 0}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
opts = {weekStartsOn: 0}
opts = {}

) => {
const instructions = lex(str)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const instructions = lex(str)
const {
weekStartsOn,
} = {
weekStartsOn: 0,
...opt,
}
const instructions = lex(str)

let res = now
for (const [cmd, ...args] of instructions) {
Expand All @@ -15,7 +19,7 @@ const createParse = (dateFns) => {
throw err
}

res = dateFns[cmd](res, ...args)
res = dateFns[cmd](res, ...args, opts)
}
return res
}
Expand Down
2 changes: 1 addition & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
`$$ = [['startOfWeek'], ['addWeeks', 1], ['setDay', $2]]`
], [
'LAST month',
`$$ = [['startOfMonth'], ['subMonths', 1], ['setMonth', $2]]`
`$$ = [['startOfMonth'], ['subDays', 1], ['startOfMonth']]`
], [
'LAST day_of_week',
`$$ = [['startOfWeek'], ['subWeeks', 1], ['setDay', $2]]`
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ const cmds = new Map([
['setYear', (dt, y) => dt.set({year: y})],

['startOfWeek', dt => dt.startOf('week')],
['startOfMonth', dt => dt.startOf('month')],
['endOfWeek', dt => dt.endOf('week')]
])

const createParse = (DateTime) => {
const createParse = DateTime => {
const parseHumanRelativeTime = (str, now = DateTime.local()) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give the Luxor parseHumanRelativeTime variant the same signature as the date-fns variant, by adding opt = {}.

const instructions = lex(str)
let res = now
Expand Down
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@
"node": ">=16"
},
"peerDependencies": {
"date-fns": "^2.7.0",
"luxon": "^2.3.0"
"date-fns": "2.7.0",
"luxon": "2.3.0"
},
"devDependencies": {
"date-fns": "^2.7.0",
"date-fns-tz": "^1.0.8",
"eslint": "^8.6.0",
"luxon": "^2.3.0",
"syntax-cli": "^0.1.12"
"date-fns": "2.28.0",
"date-fns-tz": "1.3.3",
"eslint": "8.13.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.0.0",
"luxon": "2.3.1",
"prettier": "2.6.2",
"syntax-cli": "0.1.25"
},
"scripts": {
"lint": "eslint --ignore-path .gitignore .",
"build": "syntax-cli -g grammar.js -m CLR1 -o parser.js",
"test": "node test.js",
"prettier": "prettier --check .",
"prepublishOnly": "npm run lint && npm run build && npm test"
}
}
38 changes: 25 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ const parseHumanRelativeTime = require('parse-human-relative-time')(DateTime)
const tz = 'Europe/Berlin'
const dt = DateTime.fromISO('2019-03-31T01:59+01:00').setZone(tz)

parseHumanRelativeTime('in 2 minutes', dt)
.toISO({suppressSeconds: true, suppressMilliseconds: true})
parseHumanRelativeTime('in 2 minutes', dt).toISO({
suppressSeconds: true,
suppressMilliseconds: true
})
// 2019-03-31T03:01+02:00
```


## Installation

```shell
npm install parse-human-relative-time
```


## Usage

When using `luxon`, note that [it currently always follows ISO weekdays (`0` = Monday) instead of the locale](https://github.com/moment/luxon/issues/373).
Expand All @@ -37,7 +37,9 @@ When using `luxon`, note that [it currently always follows ISO weekdays (`0` = M

```js
const dateFns = require('date-fns')
const parseHumanRelative = require('parse-human-relative-time/date-fns')(dateFns)
const parseHumanRelative = require('parse-human-relative-time/date-fns')(
dateFns
)
const {format} = require('date-fns-tz')

// Europe/Berlin switched to DST at 31st of March at 2am.
Expand All @@ -47,7 +49,18 @@ const timeZone = 'Europe/Berlin'
const withDST = parseHumanRelative('in 2 minutes', withoutDST)
format(withDST, 'HH:mm zz', {timeZone})
// 03:01 GMT+2
````
```

#### weekStartsOn

For date-fns we have an extra parameter for weekStartsOn, e.g.:

````js
parseHumanRelative('last week', new Date(), { weekStartsOn = 1})
// would return last monday
```

Default is 0 = Sunday.

### Lexing into instructions

Expand All @@ -58,7 +71,7 @@ lexHumanRelativeTime('next tuesday 5pm')
```

```js
[
;[
// next tuesday
['startOfWeek'],
['addWeeks', 1],
Expand All @@ -72,20 +85,19 @@ lexHumanRelativeTime('next tuesday 5pm')
]
```


## Why yet another package?

**Other packages don't handle time zones correctly**, because they

- mess up timezones (e.g. [`parse-messy-time`](https://github.com/substack/parse-messy-time)),
- parse to relative milliseconds, in some [DST](https://en.wikipedia.org/wiki/Daylight_saving_time) cases (e.g. [`parse-relative-time`](https://github.com/fczbkk/parse-relative-time) & [`timestring`](https://github.com/mike182uk/timestring)),
- require or even monkey-patch a specific date/time library (e.g. [`relative-time-parser`](https://github.com/cmaurer/relative.time.parser)).
- mess up timezones (e.g. [`parse-messy-time`](https://github.com/substack/parse-messy-time)),
- parse to relative milliseconds, in some [DST](https://en.wikipedia.org/wiki/Daylight_saving_time) cases (e.g. [`parse-relative-time`](https://github.com/fczbkk/parse-relative-time) & [`timestring`](https://github.com/mike182uk/timestring)),
- require or even monkey-patch a specific date/time library (e.g. [`relative-time-parser`](https://github.com/cmaurer/relative.time.parser)).

Some actually do it right, but don't support a lot of expressions, e.g. [`relative-time-expression`](https://github.com/Frezc/relative-time-expression).

This package **parses** a human relative time string (e.g. `next Tuesday 2pm`) **into a set of manipulation *instructions* and applies them to a `Date`** using [Luxon](https://moment.github.io/luxon/) or [`date-fns`](https://date-fns.org). It therefore separates parsing and manipulation, letting the date/time lib handle the complex topic of time zones.

This package **parses** a human relative time string (e.g. `next Tuesday 2pm`) **into a set of manipulation _instructions_ and applies them to a `Date`** using [Luxon](https://moment.github.io/luxon/) or [`date-fns`](https://date-fns.org). It therefore separates parsing and manipulation, letting the date/time lib handle the complex topic of time zones.

## Contributing

If you have a question or need support using `parse-human-relative-time`, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, refer to [the issues page](https://github.com/derhuerst/parse-human-relative-time/issues).
````
Loading