@meltwater/tau
v3.2.0
Published
Functional time and date tools for JavaScript inspired by Luxon.
Downloads
80
Readme
τ
Functional time and date tools for JavaScript inspired by Luxon. Plays well with φ.
Description
Exports the Luxon API in a functional programming style along with the custom functions defined in this package so they all may be imported from the same place.
Installation
Add this as a dependency to your project using npm with
$ npm install @meltwater/tauor using Yarn with
$ yarn add @meltwater/tauUsage
See the complete API documentation and working examples.
import { compose } from '@meltwater/phi' // standard compose function
import { toIso, fromNow } from '@meltwater/tau'
const sinceStartOfToday = compose(
toRelative,
startOf('day'),
fromNow
)
sinceStartOfToday() //=> '12 hours ago'import { compose, gt } from '@meltwater/phi'
import { fromIso, diffNow, days } from '@meltwater/tau'
const isIsoMoreThanNDaysOld = n => compose(
gt(-n),
days,
diffNow('days'),
fromIso
)
const isIsoMoreThan3DaysOld = isIsoMoreThanNDaysOld(3)
isIsoMoreThan3DaysOld('2018-01-24T20:27:49.288Z') //=> trueLuxon Functional API
In general, each public static method, member, and method in the Luxon API is exported at the top level with the same name. All functions are curried and the argument order is reversed. Exceptions are documented below.
Naming conventions
- Acronyms follow normal camelcase rules are and not uppercase,
e.g.,
DateTime.isInDSTis exported asisInDst. - For instance methods, the last argument corresponds to the instance (
this), e.g.,DateTime.fromMillis(0).toIso()becomestoIso(fromMillis(0)). - Some names are changed to avoid conflicts.
- Some signatures are changed for compatibility with the functional style e.g., all variadic methods.
- Any function which takes options has both a
*withOptionsvariant which takes options as its first argument, and a normal variant with one less argument bound to the default options. - Some Luxon methods have arguments with default values. In these cases, extra functions are provided corresponding to those defaults.
- Functions with a
Utcsuffix have been wrapped withsetZone('utc').
DateTime
diff,diffWithOptions,diffMillis: the last argument is an array[x, y]and computes the differencex - y.diffNow,diffNowWithOptionsanddiffNowMilliscompute the differencex - now(wherexis the last argument).
Renamed or added
DateTime.fromFormat -> fromFormatWithOptionsfromFormatfromFormatUtc
DateTime.fromFormatExplain -> fromFormatExplainWithOptionsfromFormatExplainfromFormatExplainUtc
DateTime.fromHTTP -> fromHttpWithOptionsfromHttpfromHttpUtc
DateTime.fromISO -> fromIsoWithOptionsfromIsofromIsoUtc
DateTime.fromJSDate -> fromJsDateWithOptionsfromJsDatefromJsDateUtc
DateTime.fromMillis -> fromMillisWithOptionsfromMillisfromMillisUtc
DateTime.fromSeconds -> fromSecondsWithOptionsfromSecondsfromSecondsUtc
DateTime.fromObject -> fromObjectWithOptionsfromObject
DateTime.fromRFC2822 -> fromRfc2822WithOptionsfromRfc2822fromRfc2822Utc
DateTime.fromSQL -> fromSqlWithOptionsfromSqlfromSqlUtc
DateTime.diff -> diffWithOptionsdiffdiffMillis
DateTime.diffNow -> diffNowWithOptionsdiffNowdiffNowMillis
DateTime.resolvedLocaleOptions -> resolvedLocaleOptionsWithOptionsresolvedLocaleOptions
DateTime.setZone -> setZoneWithOptionssetZonesetZoneLocalsetZoneSystemsetZoneUtc
DateTime.toISOTime -> toIsoTimeWithOptionstoIsoTime
DateTime.toLocaleParts -> toLocalePartsWithOptionstoLocaleParts
DateTime.toLocaleString -> toLocaleStringWithOptionstoLocaleStringtoLocaleStringShort
DateTime.toRelative -> toRelativeWithOptionstoRelative
DateTime.toRelativeCalendar -> toRelativeCalendarWithOptionstoRelativeCalendar
DateTime.toSQL -> toSqlWithOptionstoSql
DateTime.toSQLTime -> toSqlTimeWithOptionstoSqlTime
DateTime.toUTC -> toUtcWithOptionstoUtctoUtcZero
DateTime.toJSON -> toJsonDateTime.toBSON -> toBsonDateTime.invalid -> invalidWithExplanationinvalid
Signature changed
DateTime.local(...) -> local([...])(order preserved)DateTime.utc(...) -> utc([...])(order preserved)DateTime.min(x, y, ...) -> min(x, y)(exactly two arguments)DateTime.min(x, y, ...) -> max(x, y)(exactly two arguments)
Duration
Duration.fromISO -> durationFromIsoWithOptionsdurationFromIso
Duration.fromMillis -> durationFromMillisWithOptionsdurationFromMillis
Duration.fromObject -> durationFromObjectWithOptionsdurationFromObject
Duration.fromDurationLike -> fromDurationLikeWithOptionsfromDurationLike
Duration.invalid -> durationInvalidWithExplanationdurationInvalid
Duration.toHuman -> toHumanWithOptionstoHuman
Signature changed
duration.shiftTo(...) -> shiftTo([...], duration)(order preserved)
Interval
Renamed or Added
Interval.fromISO -> intervalFromIsoWithOptionsintervalFromIso
Interval.invalid -> intervalInvalidWithExplanationintervalInvalid
Interval.toDuration -> toDurationWithOptionstoDurationtoDurationMillis
Interval.hasSame -> hasSameEndpoints
Signature changes
Interval.fromDateTimes(start, end) -> fromDateTimes([start, end])Interval.splitAt(...) -> splitAt([...], interval)(order preserved)Interval.difference(...) -> difference([...], interval)(order preserved)
Info
Info.eras -> erasWithOptionseraserasShorterasLong
Info.hasDST -> hasDsthasDstLocalhasDstSystem
Info.isValidIANAZone -> isValidIanaZoneisValidIanaZoneLocalisValidIanaZoneSystem
Info.meridiems -> meridiemsWithOptionsmeridiems
Info.months -> calendarMonthsWithOptionscalendarMonthscalendarMonthsLong
Info.monthsFormat -> monthsFormatWithOptionsmonthsFormatmonthsFormatLong
Info.weekdays -> weekdaysWithOptionsweekdaysweekdaysLong
Info.weekdaysFormat -> weekdaysFormatWithOptionsweekdaysFormatweekdaysFormatLong
Zone
Renamed or added
Zone.offset -> offsetAtZone.offsetName -> offsetNameAtWithOptionsoffsetNameAt
Settings
- Getters are exported as functions with the corresponding Luxon name,
e.g.,
defaultLocale(). - Setters are prefixed with
set, e.g.,setDefaultLocale('utc').
Development Quickstart
$ git clone https://github.com/meltwater/tau.git
$ cd tau
$ nvm install
$ yarnRun each command below in a separate terminal window:
$ yarn run watch
$ yarn run test:watchDevelopment and Testing
Source code
The tau source is hosted on GitHub. Clone the project with
$ git clone [email protected]:meltwater/tau.gitRequirements
You will need Node.js with npm, Yarn, and a Node.js debugging client.
Be sure that all commands run under the correct Node version, e.g., if using nvm, install the correct version with
$ nvm installSet the active version for each shell session with
$ nvm useInstall the development dependencies with
$ yarnCircleCI
CircleCI should already be configured: this section is for reference only.
The following environment variables must be set on CircleCI:
NPM_TOKEN: npm token for installing and publishing packages.NPM_TEAM: npm team to grant read-only package access (formatorg:team, optional).CODECOV_TOKEN: Codecov token for uploading coverage reports (optional).
These may be set manually or by running the script ./.circleci/envvars.sh.
Documentation
For documentation publishing, CircleCI must have an SSH key for github.com
added as a deploy key
with write access on the GitHub repository.
Development tasks
Primary development tasks are defined under scripts in package.json
and available via yarn run.
View them with
$ yarn runProduction build
Lint, test, and transpile the production build to dist with
$ yarn run distPublishing a new release
Release a new version using npm version.
This will run all tests, update the version number,
create and push a tagged commit,
and trigger CircleCI to publish the new version to npm.
- Update the CHANGELOG before each new release after version 1.
- New versions are released when the commit message is a valid version number.
- Versions are only published on release branches:
masterbranch or any branch matchingver/*. - If branch protection options are enabled,
you must first run
npm versionon a separate branch, wait for the commit to pass any required checks, then merge and push the changes to a release branch. - Do not use the GitHub pull request button to merge version commits as the commit tagged with the new version number will not match after merging.
Examples
See the full documentation on using examples.
View all examples with
$ yarn run exampleLinting
Linting against the JavaScript Standard Style and JSON Lint is handled by gulp.
View available commands with
$ yarn run gulp --tasksRun all linters with
$ yarn run lintIn a separate window, use gulp to watch for changes and lint JavaScript and JSON files with
$ yarn run watchAutomatically fix most JavaScript formatting errors with
$ yarn run formatTests
Unit and integration testing is handled by AVA and coverage is reported by Istanbul and uploaded to Codecov.
- Test files end in
.spec.js. - Unit tests are placed under
libalongside the tested module. - Integration tests are placed in
test. - Static files used in tests are placed in
fixtures.
Watch and run tests on changes with
$ yarn run test:watchIf using AVA snapshot testing, update snapshots with
$ yarn run test:updateGenerate a coverage report with
$ yarn run reportAn HTML version will be saved in coverage.
Debugging tests
Create a breakpoint by adding the statement debugger to the test
and start a debug session with, e.g.,
$ yarn run test:inspect lib/unix.spec.jsWatch and restart the debugging session on changes with
$ yarn run test:inspect:watch lib/unix.spec.jsContributing
The author and active contributors may be found in package.json,
$ jq .author < package.json
$ jq .contributors < package.jsonTo submit a patch:
- Request repository access by submitting a new issue.
- Create your feature branch (
git checkout -b my-new-feature). - Make changes and write tests.
- Commit your changes (
git commit -am 'Add some feature'). - Push to the branch (
git push origin my-new-feature). - Create a new Pull Request.
License
This npm package is licensed under the MIT license.
Warranty
This software is provided by the copyright holders and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright holder or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
