Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Streek committed Oct 11, 2018
0 parents commit 8082e44
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//calculate time remaining between now and completion in arbitrary amounts
class TimeToComplete {
constructor(destination) {
this.destination = destination || 100;
this.last = 0;
this.lastTime = Date.now();
}

//calculate time remaining;
remaining(current, consoleLogProgress = false) {
const now = Date.now();
const timeDifference = this.lastTime - now;
const difference = (this.destination) / (this.last - current);

const timeToComplete = timeDifference * difference;

const seconds = Math.abs(timeToComplete / 1000);
const minutes = Math.abs(timeToComplete / 1000 / 60);
const hours = Math.abs(timeToComplete / 1000 / 60 / 60);

if (consoleLogProgress) {
console.log(`This process should be done in ${seconds} seconds.`)
console.log(`This process should be done in ${minutes} minutes.`)
console.log(`This process should be done in ${hours} hours.`)
}

//set for next run;
this.lastTime = now;
this.last = current
return {
seconds,
minutes,
hours
}
}
}

const time = new TimeToComplete(100);

setTimeout(() => {
time.remaining(40, true);
}, 1000)

setTimeout(() => {
time.remaining(44, true);
}, 2000)
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "timetocomplete",
"version": "1.0.0",
"description": "A package that'll help you determin how long a process is going to take to complete",
"main": "index.js",
"scripts": {
"test": "npm test"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/Streek/TimeToComplete.git"
},
"author": "Keith Connolly",
"license": "MIT",
"bugs": {
"url": "https://github.com/Streek/TimeToComplete/issues"
},
"homepage": "https://github.com/Streek/TimeToComplete#readme"
}
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# TimeToComplete

This library exported as a class module.

## Download

* [Master build](https://github.com/Streek/TimeToComplete)
* [1.0 build](https://github.com/Streek/TimeToComplete/releases)

TimeToComplete is released under the [MIT license](https://raw.githubusercontent.com/streek/TimeToComplete/LICENSE) & supports modern environments.

## Installation

In a browser:
```html
<script src="TimeToComplete/index.js"></script>
```

Using npm:
```shell
$ npm i -g npm
$ npm i --save TimeToComplete
```

In Node.js:
```js
// Load the full build.
const timeClass = require('TimeToComplete');
const time = new timeClass;
```

0 comments on commit 8082e44

Please sign in to comment.