diff --git a/.README.md b/.README.md new file mode 100644 index 0000000..ee1aabb --- /dev/null +++ b/.README.md @@ -0,0 +1,70 @@ +# A natural language parser for dates, times, and durations + +[![NPM Version][npm-image]][npm-url] +[![Downloads Stats][npm-downloads]][npm-url] + +A tiny utility library **with no dependencies** that parses natural language +**dates**, **times**, and **intervals** to either a `Date` instance or a +numerical value in milliseconds. Here are some examples of possible inputs: + +- **two weeks ago** +- **1 day ago** +- **in 2 hours and 5 minutes** +- **in a month** +- **2018-01-01T00:00:00.000Z** + +## Installation + +```bash +npm i --save time-speak +``` + +## Usage + +Import the `parse` function call it with a string containing a natural language +representation of either a time in the future, past, or a duration. The return +value is a timestamp in milliseconds if the input is a duration, otherwise it +is a `Date` instance. + +```js +import { parse } from 'time-speak' + +const pastDate = parse('2 days and 4 hours ago') +const pastDateWithNumberWords = parse('two days and four hours ago') +const futureDate = parse('in 4 hours') +const durationMS = parse('6 months') + +console.log({ + pastDateWithNumberWords, // 2023-12-19T13:02:39.768Z + pastDate, // 2023-12-19T13:02:39.768Z + futureDate, // 2023-12-21T21:02:39.768Z + durationMS // 15552000000 +}) +``` + +## API Reference + +> The standalone JSDoc reference can be found in [DOCUMENTATION.md](DOCUMENTATION.md) + +<%= api %> + +## Release History + +See _[CHANGELOG.md](./CHANGELOG.md)_ for more information. + +## License + +Distributed under the **MIT** license. See [LICENSE.md](./LICENSE.md) for more +information. + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request + +[npm-image]: https://img.shields.io/npm/v/time-speak.svg?style=flat-square +[npm-url]: https://npmjs.org/package/time-speak +[npm-downloads]: https://img.shields.io/npm/dm/time-speak.svg?style=flat-square diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md new file mode 100644 index 0000000..b0f498b --- /dev/null +++ b/DOCUMENTATION.md @@ -0,0 +1,136 @@ +## Classes + +
Error thrown when the input cannot be parsed to a date or duration. It
+optionally accepts a detail
parameter that can be used to provide more
+information about why parsing failed.
A mapping of time units to their millisecond values.
+A mapping of plural time units to their singular counterparts.
+Parses a string into a Date
or number of milliseconds. The string can
+describe the date in natural language (e.g. "tomorrow", "in 2 hours and 3
+minutes", "a month ago", etc.) or be a valid date string (e.g. "2018-01-01").
InvalidInputError
](#InvalidInputError)
+
+
+### invalidInputError.\_detail
+An optional detailed description of the reason why parsing failed.
+
+**Kind**: instance property of [InvalidInputError
](#InvalidInputError)
+
+
+### invalidInputError.detail ⇒
+Get the detail explaining why parsing failed. If no detail was provided,
+it will return an empty string.
+
+**Kind**: instance property of [InvalidInputError
](#InvalidInputError)
+**Returns**: The detail string.
+
+
+### invalidInputError.input ⇒
+Get the original input that could not be parsed.
+
+**Kind**: instance property of [InvalidInputError
](#InvalidInputError)
+**Returns**: The input string.
+
+
+## TimeUnit
+A mapping of time units to their millisecond values.
+
+**Kind**: global variable
+
+
+## TimeUnitPlural
+A mapping of plural time units to their singular counterparts.
+
+**Kind**: global variable
+
+
+## parse(input) ⇒
+Parses a string into a `Date` or number of milliseconds. The string can
+describe the date in natural language (e.g. "tomorrow", "in 2 hours and 3
+minutes", "a month ago", etc.) or be a valid date string (e.g. "2018-01-01").
+
+**Kind**: global function
+**Returns**: The parsed date or number of milliseconds.
+**Throws**:
+
+- [InvalidInputError](#InvalidInputError)
+This exception is thrown if the input is invalid.
+
+**Access**: public
+
+| Param | Description |
+| --- | --- |
+| input | The string to parse. |
+
+**Example**
+Here are some example invocations:
+```
+const ... = parse('1 day ago')
+const ... = parse('three days ago')
+const ... = parse('in 2 hours and 3 minutes')
+const ... = parse('a month')
+const ... = parse('2018-01-01')
+const ... = parse('2018-01-01T00:00:00.000Z')
+```
+**Example**
+Here are some example invocations that throw an exception:
+```
+parse('in 2 hours and 3 minutes ago')
+parse('a month in the past')
+```
diff --git a/README.md b/README.md
index fdad6c8..d508b4f 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,7 @@ A tiny utility library **with no dependencies** that parses natural language
**dates**, **times**, and **intervals** to either a `Date` instance or a
numerical value in milliseconds. Here are some examples of possible inputs:
+- **two weeks ago**
- **1 day ago**
- **in 2 hours and 5 minutes**
- **in a month**
@@ -29,16 +30,160 @@ is a `Date` instance.
import { parse } from 'time-speak'
const pastDate = parse('2 days and 4 hours ago')
+const pastDateWithNumberWords = parse('two days and four hours ago')
const futureDate = parse('in 4 hours')
const durationMS = parse('6 months')
console.log({
- pastDate, // 2023-12-19T13:02:39.768Z
- futureDate, // 2023-12-21T21:02:39.768Z
- durationMS // 15552000000
+ pastDateWithNumberWords, // 2023-12-19T13:02:39.768Z
+ pastDate, // 2023-12-19T13:02:39.768Z
+ futureDate, // 2023-12-21T21:02:39.768Z
+ durationMS // 15552000000
})
```
+## API Reference
+
+> The standalone JSDoc reference can be found in [DOCUMENTATION.md](DOCUMENTATION.md)
+
+## Classes
+
+Error thrown when the input cannot be parsed to a date or duration. It
+optionally accepts a detail
parameter that can be used to provide more
+information about why parsing failed.
A mapping of time units to their millisecond values.
+A mapping of plural time units to their singular counterparts.
+Parses a string into a Date
or number of milliseconds. The string can
+describe the date in natural language (e.g. "tomorrow", "in 2 hours and 3
+minutes", "a month ago", etc.) or be a valid date string (e.g. "2018-01-01").
InvalidInputError
](#InvalidInputError)
+
+
+### invalidInputError.\_detail
+An optional detailed description of the reason why parsing failed.
+
+**Kind**: instance property of [InvalidInputError
](#InvalidInputError)
+
+
+### invalidInputError.detail ⇒
+Get the detail explaining why parsing failed. If no detail was provided,
+it will return an empty string.
+
+**Kind**: instance property of [InvalidInputError
](#InvalidInputError)
+**Returns**: The detail string.
+
+
+### invalidInputError.input ⇒
+Get the original input that could not be parsed.
+
+**Kind**: instance property of [InvalidInputError
](#InvalidInputError)
+**Returns**: The input string.
+
+
+## TimeUnit
+A mapping of time units to their millisecond values.
+
+**Kind**: global variable
+
+
+## TimeUnitPlural
+A mapping of plural time units to their singular counterparts.
+
+**Kind**: global variable
+
+
+## parse(input) ⇒
+Parses a string into a `Date` or number of milliseconds. The string can
+describe the date in natural language (e.g. "tomorrow", "in 2 hours and 3
+minutes", "a month ago", etc.) or be a valid date string (e.g. "2018-01-01").
+
+**Kind**: global function
+**Returns**: The parsed date or number of milliseconds.
+**Throws**:
+
+- [InvalidInputError](#InvalidInputError)
+This exception is thrown if the input is invalid.
+
+**Access**: public
+
+| Param | Description |
+| --- | --- |
+| input | The string to parse. |
+
+**Example**
+Here are some example invocations:
+```
+const ... = parse('1 day ago')
+const ... = parse('three days ago')
+const ... = parse('in 2 hours and 3 minutes')
+const ... = parse('a month')
+const ... = parse('2018-01-01')
+const ... = parse('2018-01-01T00:00:00.000Z')
+```
+**Example**
+Here are some example invocations that throw an exception:
+```
+parse('in 2 hours and 3 minutes ago')
+parse('a month in the past')
+```
+
+
## Release History
See _[CHANGELOG.md](./CHANGELOG.md)_ for more information.
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index d2cf3a0..15e58f4 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -2,6 +2,8 @@
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
+## [1.7.0](https://github.com/f3rno64/time-speak/compare/v1.6.0...v1.7.0) (2024-01-15)
+
## [1.6.0](https://github.com/f3rno64/time-speak/compare/v1.5.1...v1.6.0) (2023-12-22)
### [1.5.1](https://github.com/f3rno64/time-speak/compare/v1.5.0...v1.5.1) (2023-12-22)
diff --git a/docs/classes/InvalidInputError.html b/docs/classes/InvalidInputError.html
index d5cf11e..d967924 100644
--- a/docs/classes/InvalidInputError.html
+++ b/docs/classes/InvalidInputError.html
@@ -1,7 +1,7 @@
-
Error thrown when the input cannot be parsed to a date or duration. It +
- Preparing search index...
- The search index is not available
time-speakClass InvalidInputError
Error thrown when the input cannot be parsed to a date or duration. It optionally accepts a
-detail
parameter that can be used to provide more information about why parsing failed.Hierarchy
Index
Constructors
Hierarchy
Index
Constructors
Properties
detail
parameter will be included in the error message if provided.Parameters
input: string
The input that could not be parsed
Optional
detail: stringA more detailed description of the error
-Returns InvalidInputError
Properties
Private
_detailAn optional detailed description of the reason why parsing failed.
-Private
_inputThe input that could not be parsed.
-Optional
causemessage
name
Optional
stackStatic
Optional
prepareType declaration
Optional override for formatting stack traces
+Returns InvalidInputError
Properties
Private
_detailAn optional detailed description of the reason why parsing failed.
+Private
_inputThe input that could not be parsed.
+Optional
causemessage
name
Optional
stackStatic
Optional
prepareType declaration
Optional override for formatting stack traces
Parameters
err: Error
stackTraces: CallSite[]
Returns any
See
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
Static
stackAccessors
detail
Get the detail explaining why parsing failed. If no detail was provided, it will return an empty string.
Returns string
The detail string.
-input
Get the original input that could not be parsed.
+input
Get the original input that could not be parsed.
Returns string
The input string.
-Methods
Static
captureCreate .stack property on a target object
+Methods
Static
captureCreate .stack property on a target object
Parameters
targetObject: object
Optional
constructorOpt: FunctionReturns void
Settings
Member Visibility
Theme
On This Page
- Preparing search index...
- The search index is not available
time-speakFunction parse
Parses a string into a
Date
or number of milliseconds. The string can +- Preparing search index...
- The search index is not available
time-speakFunction parse
Parses a string into a
Date
or number of milliseconds. The string can describe the date in natural language (e.g. "tomorrow", "in 2 hours and 3 minutes", "a month ago", etc.) or be a valid date string (e.g. "2018-01-01").Parameters
input: string
The string to parse.
@@ -11,4 +11,4 @@Example
Here are some example invocations:
Example
Here are some example invocations that throw an exception:
-Settings
Member Visibility
Theme
Settings
Member Visibility
Theme
- Preparing search index...
- The search index is not available
time-speaktime-speak
A natural language parser for dates, times, and durations
+
- Preparing search index...
- The search index is not available
time-speaktime-speak
A natural language parser for dates, times, and durations
A tiny utility library with no dependencies that parses natural language dates, times, and intervals to either a
Date
instance or a numerical value in milliseconds. Here are some examples of possible inputs:+- two weeks ago
- 1 day ago
- in 2 hours and 5 minutes
- in a month
@@ -15,7 +16,111 @@
representation of either a time in the future, past, or a duration. The return
value is a timestamp in milliseconds if the input is a duration, otherwise it
is a
Date
instance. -Settings
Member Visibility
Theme
On This Page
Settings
Member Visibility
Theme
On This Page
- Preparing search index...
- The search index is not available
time-speaktime-speak
Index
Classes
- Preparing search index...
- The search index is not available
time-speaktime-speak
Index
Classes
Functions
Settings
Member Visibility
Theme