Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffijoe committed May 28, 2019
0 parents commit 00a7682
Show file tree
Hide file tree
Showing 22 changed files with 8,062 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
npm-debug.log
yarn-error.log
coverage
.nyc_output
lib
.test-out
.DS_Store
.idea/workspace.xml
.sublime-workspace
.npmrc
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
examples
coverage
.test-out
src
**/__tests__/**
**/__helpers__/**
31 changes: 31 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Oh yeah!
language: node_js
cache:
directories:
- node_modules
notifications:
email: false

# Add additional versions here as appropriate.
node_js:
- "stable"
- "12"
- "10"
- "8"

# Lint errors should trigger a failure.
before_script:
- npm run lint
- npm run build

# Runs the coverage script (which runs the tests)
script: npm run cover

# Code coverage
after_success:
- npm run coveralls
- npm run semantic-release

branches:
except:
- /^v\d+\.\d+\.\d+$/
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"coverage": true,
".idea": true,
"lib": true,
"**/*.sublime-*": true,
"node_modules": true
}
}
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) Jeff Hansen 2019 to present.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# dration

###### pronounced "duration"

A dependency-free JavaScript library for working with durations/time spans using pure functions and primitive types, written in TypeScript.

[![npm](https://img.shields.io/npm/v/dration.svg?maxAge=1000)](https://www.npmjs.com/package/dration)
[![dependency Status](https://img.shields.io/david/jeffijoe/dration.svg?maxAge=1000)](https://david-dm.org/jeffijoe/dration)
[![devDependency Status](https://img.shields.io/david/dev/jeffijoe/dration.svg?maxAge=1000)](https://david-dm.org/jeffijoe/dration)
[![Build Status](https://img.shields.io/travis/jeffijoe/dration.svg?maxAge=1000)](https://travis-ci.org/jeffijoe/dration)
[![Coveralls](https://img.shields.io/coveralls/jeffijoe/dration.svg?maxAge=1000)](https://coveralls.io/github/jeffijoe/dration)
[![npm](https://img.shields.io/npm/dt/dration.svg?maxAge=1000)](https://www.npmjs.com/package/dration)
[![npm](https://img.shields.io/npm/l/dration.svg?maxAge=1000)](https://github.com/jeffijoe/dration/blob/master/LICENSE.md)

# Install

With `npm`:

```
npm install dration
```

Or with `yarn`

```
yarn add dration
```

There are also UMD and ES Modules builds in `lib/`. The entire library is tree-shakeable, and has the proper configurations in `package.json` for bundlers like Webpack to pick it up.

# Usage

The functions are pure and work with milliseconds. The benefit of this is you don't need "add", "subtract", "greater than" and "equals"-style functions; _just use regular JavaScript!_

```ts
import * as Duration from 'dration'

const value = Duration.of(0, 0, 2) // creates a duration of 2 minutes
console.log(value) // 120000
console.log(typeof value) // "number"

// `fromMinutes` is a shortcut to creating a duration of 2 minutes
const newValue = value + Duration.fromMinutes(2)
console.log(newValue) // 240000
console.log(Duration.format(newValue)) // 00:04:00

// A nice bonus of just using numbers, you can add a duration to a date quite easily:
const twoDaysFromNow = new Date(Date.now() + Duration.fromDays(2))
```

# API

The functions exposed by dration are quite simple and all follow the same form.
When referring to a "duration", it means a numeric value in milliseconds.

## `of(ms: number, s?: number, m?: number, h?: number, d?: number): number`

Probably the most important function, converts individual time components to a duration. Leaving any arguments out is the same as passing `0`.

The arguments are:

1. milliseconds
2. seconds
3. minutes
4. hours
5. days

## The `from` functions

There are `from` functions for each unit, and they all take a single parameter, the amount:

- `fromSeconds(seconds: number): number`
- `fromMinutes(minutes: number): number`
- `fromHours(hours: number): number`
- `fromDays(days: number): number`

## `between(left: Date, right: Date): number`

Returns the duration (milliseconds) between 2 dates.

```ts
const start = new Date('2019-05-28T12:00:00Z')
const end = new Date('2019-05-29T14:00:00Z')

const value = Duration.between(start, end)

console.log(value) // 1 02:00:00
```

## `format(duration: number): string`

Formats a duration to a string in `d hh:mm:ss.sss` format, with days and milliseconds left out if zero.

```ts
console.log(
format(
of(50, 4, 33, 2, 1)
)
)
// "1 02:33:04.050"
```

# Author

Jeff Hansen — [@Jeffijoe](https://twitter.com/Jeffijoe)
105 changes: 105 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"name": "dration",
"version": "0.0.0-semantic",
"description": "",
"main": "lib/index.js",
"module": "lib/dration.module.js",
"jsnext:main": "lib/dration.module.js",
"browser": "lib/dration.browser.js",
"umd:main": "lib/dration.umd.js",
"react-native": "lib/dration.browser.js",
"typings": "lib/index.d.ts",
"engines": {
"node": ">=6.0.0"
},
"scripts": {
"build": "rimraf lib && tsc -p tsconfig.build.json && rollup -c",
"test": "jest",
"test:watch": "jest --watchAll",
"lint": "tslint --project tsconfig.json --fix \"src/**/*.ts\" && prettier --write \"src/**/*.ts\"",
"cover": "jest --coverage",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls",
"semantic-release": "semantic-release"
},
"repository": {
"type": "git",
"url": "https://github.com/jeffijoe/dration.git"
},
"files": [
"lib",
"LICENSE.md",
"README.md"
],
"directories": {
"lib": "lib"
},
"keywords": [],
"author": "Jeff Hansen <jeff@jeffijoe.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/jeffijoe/dration/issues"
},
"homepage": "https://github.com/jeffijoe/dration#readme",
"dependencies": {},
"devDependencies": {
"@semantic-release/condition-codeship": "^1.1.0",
"@semantic-release/release-notes-generator": "^7.1.4",
"@types/jest": "^24.0.13",
"@types/node": "^12.0.2",
"coveralls": "^3.0.3",
"husky": "^2.3.0",
"jest": "^24.8.0",
"lint-staged": "^8.1.7",
"prettier": "^1.17.1",
"rimraf": "^2.6.3",
"rollup": "^1.12.4",
"rollup-plugin-commonjs": "^10.0.0",
"rollup-plugin-node-resolve": "^5.0.0",
"rollup-plugin-typescript2": "^0.21.1",
"semantic-release": "^15.13.12",
"ts-jest": "^24.0.2",
"tslint": "^5.16.0",
"tslint-config-prettier": "^1.18.0",
"tslint-config-standard": "^8.0.1",
"typescript": "^3.4.5"
},
"lint-staged": {
"*.ts": [
"tslint --fix --project tsconfig.json",
"prettier --write",
"git add"
]
},
"jest": {
"testEnvironment": "node",
"testRegex": "(/__tests__/.*\\.(test|spec))\\.(ts|tsx|js)$",
"collectCoverageFrom": [
"src/**/*.ts"
],
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"modulePaths": [
"src"
],
"coverageDirectory": "<rootDir>/coverage",
"transform": {
"\\.(ts|tsx)": "ts-jest"
},
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"globals": {}
},
"prettier": {
"semi": false,
"singleQuote": true
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
64 changes: 64 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import typescript from 'rollup-plugin-typescript2'
import commonjs from 'rollup-plugin-commonjs'
import resolve from 'rollup-plugin-node-resolve'

const tsOpts = {
cacheRoot: './node_modules/.rpt2',
typescript: require('typescript'),
tsconfig: 'tsconfig.build.json',
tsconfigOverride: {
compilerOptions: {
// Don't emit declarations, that's done by the regular build.
declaration: false,
module: 'ESNext'
}
}
}

export default [
// Build 1: ES6 modules for Node.
{
input: 'src/index.ts',
treeshake: { pureExternalModules: true },
output: [
{
file: 'lib/dration.module.js',
format: 'es'
}
],
plugins: [typescript(tsOpts)]
},
// Build 2: ES modules + UMD for browser builds.
{
input: 'src/index.ts',
treeshake: { pureExternalModules: true },
output: [
{
name: 'Duration',
file: 'lib/dration.browser.js',
format: 'es'
},
{
name: 'Duration',
file: 'lib/dration.umd.js',
format: 'umd'
}
],
plugins: [
typescript(
Object.assign({}, tsOpts, {
tsconfigOverride: {
compilerOptions: {
target: 'es5',
declaration: false,
noUnusedLocals: false,
module: 'ESNext'
}
}
})
),
resolve(),
commonjs()
]
}
]
Loading

0 comments on commit 00a7682

Please sign in to comment.