Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
abranhe committed Apr 24, 2019
0 parents commit 967150f
Show file tree
Hide file tree
Showing 9 changed files with 281 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 = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js
node_js:
- '10'

notifications:
email: false
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const optionalText = text => {
if (typeof text === 'number' || text === undefined) {
text = '';
}

return text || 'min to read';
};

module.exports = (text, altTextOrWpm, wpm) => {
if (typeof text !== 'string') {
throw new TypeError(
`Expected an String in the first argument, got ${typeof text}`,
);
}

if (wpm !== undefined && typeof wpm !== 'number') {
throw new TypeError(
`Expected a Number in the third argument, got ${typeof wpm}`,
);
}

let wordsPerMin = 265;

if (typeof altTextOrWpm === 'number') {
wordsPerMin = altTextOrWpm;
}

if (wpm !== undefined && typeof wpm === 'number') {
wordsPerMin = wpm;
}

const timeTaken = Math.round(text.split(' ').length / wordsPerMin);
return `${timeTaken || 'less than a'} ${optionalText(altTextOrWpm)}`;
};
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Abraham Hernandez <abraham@abranhe.com> (abranhe.com)

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.
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "minutes-to-read",
"version": "1.0.0",
"description": "It happened a while ago",
"main": "index.js",
"scripts": {
"test": "ava & xo"
},
"repository": "abranhe/minutes-to-read",
"author": {
"name": "Abraham Hernandez",
"email": "abraham@abranhe.com",
"url": "https://abranhe.com"
},
"files": [
"index.js"
],
"keywords": [
"medium",
"read",
"words",
"wpm",
"minutes",
"time",
"time-to-read"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/abranhe/minutes-to-read/issues"
},
"homepage": "https://p.abranhe.com/minutes-to-read",
"devDependencies": {
"ava": "^1.4.1",
"xo": "^0.24.0"
},
"xo": {
"ignores": [
"test.js"
]
}
}
107 changes: 107 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
[<img src="https://cdn.abranhe.com/projects/minutes-to-read/logo.png" height="80%" width="80%">](https://npmjs.org/minutes-to-read)

<br/>

[![](https://api.abranhe.com/badges/travis/minutes-to-read)](https://travis-ci.org/abranhe/minutes-to-read)
[![](https://img.shields.io/github/license/abranhe/minutes-to-read.svg)](https://github.com/abranhe/minutes-to-read/blob/master/license)
[![](https://abranhe.com/badge.svg)](https://github.com/abranhe)
[![](https://cdn.abranhe.com/badges/cash-me.svg)](https://cash.me/$abranhe)
[![](https://cdn.abranhe.com/badges/patreon.svg)](https://patreon.com/abranhe)
[![](https://cdn.abranhe.com/badges/paypal.svg)](https://paypal.me/abranhe/10)
[![](https://img.shields.io/npm/v/minutes-to-read.svg)](https://npmjs.org/minutes-to-read)


## Install

```
$ npm install minutes-to-read
```

## Usage

```js
const m2r = require('minutes-to-read');

m2r('Lorem ipsum');
// => less than a min to read

m2r('Lorem ipsum', 'minute');
// => less than a minute

const longText = 'Lorem ipsum dolor sit am...' // 500 words

m2r(longText, 'minutes to finish');
// => 2 minutes to finish

m2r(longText, 'minutes to read if you reading rate is 2 wpm', 2);
// => 250 minutes to read if you reading rate is 2 wpm
```

## API

### `m2r(text)`

- **text** (`string`): A paragraph, easy or sentence

###### Example:

```js
m2r('Lorem ipsum');
// => less than a minute


m2r('Lorem ipsum dolor sit am...'); // 500 words
// => 2 minutes to finish
```

### `m2r(text, customText)`

- **text** (`string`): A paragraph, easy or sentence
- **customText** (`string`): A custom text you want to add

###### Example:

```js
m2r('Lorem ipsum', 'minute to finish');
// => less than a minute to finish
```

### `m2r(text, customText, wpm)`

- **text** (`string`): A paragraph, easy or sentence
- **customText** (`string`): A custom text you want to add
- **wpm** (`number`): Custom words per minutes

###### Example:

For a 250 text at 2 words per minutes it will take 250 minutes.

```js
m2r('Lorem ipsum dolor sit am...', 'minutes to finish', 2); // 500 words
// => 250 minutes to finish
```

### `m2r(text,wpm)`

- **text** (`string`): A paragraph, easy or sentence
- **wpm** (`number`): Custom words per minutes

###### Example:

For a 2 words text reading a word per minute it will take 2 minutes.

```js
m2r('Lorem ipsum', 1);
// => 2 minutes to finish
```

## Team

|[![](https://api.abranhe.com/avatar?s=50)](https://abranhe.com)|
| :-: |
| [Carlos Araham](https://github.com/abranhe) |


## License

[MIT](https://github.com/abranhe/minutes-to-read/blob/master/license) License © [Carlos Abraham](https://github.com/abranhe)
54 changes: 54 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import test from 'ava';
import m2r from '.';

test('Throw error when incorrect datatype in the first argument', (t) => {
const error = t.throws(() => {
m2r(3);
}, TypeError);

t.is(error.message, 'Expected an String in the first argument, got number');
});

test('Throw error when incorrect datatype in the third argument', (t) => {
const error = t.throws(() => {
m2r('str', 'str', 'str');
}, TypeError);

t.is(error.message, 'Expected a Number in the third argument, got string');
});

const shortText = 'Lorem ipsum';
const longText =
'Lorem ipsum dolor sit amet consectetur adipiscing elit condimentum iaculis, feugiat aenean sollicitudin platea posuere fusce eu urna, dapibus integer sapien tellus molestie semper egestas ornare. Sociis habitant suspendisse lacinia bibendum nibh duis at rhoncus, litora mi nisi curae quam nisl pellentesque nulla aliquam, euismod massa senectus commodo per eleifend magna. Malesuada ad cursus maecenas et sociosqu ac nunc fermentum dui habitasse, volutpat turpis proin hendrerit mus leo phasellus primis libero, arcu purus nam viverra est augue conubia sodales metus. Donec praesent elementum vitae aptent mollis cras magnis tincidunt, montes faucibus suscipit ultrices vulputate convallis eros, cubilia pharetra interdum a facilisi potenti risus. Diam class ut morbi parturient velit luctus pulvinar porta hac, nascetur placerat congue ante tortor varius odio. Himenaeos lobortis erat vivamus vestibulum neque mauris pretium mattis, facilisis lacus justo vel scelerisque dignissim auctor blandit, etiam dis in non laoreet torquent id. Tempus ligula tempor orci dictumst nostra venenatis enim gravida ullamcorper, taciti porttitor fringilla eget dictum fames ultricies vehicula rutrum, netus quis quisque accumsan ridiculus consequat aliquet felis. Imperdiet tristique curabitur nullam penatibus lectus sagittis sem cum sed, nec natoque inceptos placerat fames non odio enim ligula ac, potenti congue cras at pharetra lectus consequat donec. Nascetur litora mattis arcu vitae sollicitudin facilisi leo quis condimentum himenaeos, mi torquent commodo sed nunc natoque metus massa. Sapien porttitor est nisl curabitur dictum magna ornare praesent, ridiculus posuere eget tortor diam sagittis urna maecenas ante, vivamus vestibulum gravida eleifend netus accumsan nec. Dapibus viverra primis proin nullam luctus eu varius lacus id, ad augue convallis tincidunt bibendum justo curae tellus per senectus, platea dis quam pellentesque eros a rutrum fringilla. Erat elementum mus nulla morbi sodales ut porta pulvinar tristique orci turpis iaculis venenatis, molestie suspendisse penatibus vehicula habitant imperdiet etiam feugiat magnis phasellus libero cum. Nibh risus et ullamcorper hendrerit nam vel tempus volutpat scelerisque fermentum aliquet, neque purus nostra lacinia mauris class vulputate in ultricies quisque, dignissim suscipit cursus felis fusce hac habitasse montes inceptos rhoncus. Tempor velit mollis blandit laoreet dui cubilia dictumst semper, egestas pretium duis sociis auctor taciti aliquam, faucibus facilisis conubia lobortis aptent parturient interdum. Euismod integer ultrices aenean sociosqu malesuada sem nisi, lectus per magna cubilia ultrices parturient habitasse tristique, nullam tempor habitant a class auctor. Porta orci enim vel lobortis pellentesque ornare faucibus cras commodo morbi, eget donec aliquet sapien id ad leo dui tortor. Etiam luctus aliquam justo laoreet eros torquent pulvinar ligula semper sem, suspendisse libero risus mollis vivamus nisl urna tincidunt himenaeos, massa nunc euismod venenatis rutrum condimentum tellus nostra elementum. Magnis facilisi litora sollicitudin neque iaculis at, senectus hendrerit maecenas lacinia suscipit nibh aenean, dis primis vehicula malesuada natoque. Quis ridiculus velit varius pretium ultricies tempus, integer inceptos volutpat nec ac, praesent nam sodales scelerisque molestie. Curabitur et phasellus gravida non sociis montes mattis taciti diam cursus mi, curae convallis feugiat nisi bibendum interdum eu conubia vulputate. Sociosqu posuere vitae rhoncus fames dignissim egestas ut, eleifend potenti imperdiet dictumst vestibulum augue, duis fermentum cum mus arcu pharetra.';

test('Check a text less than a minute to read', (t) => {
t.is(m2r(shortText), 'less than a min to read');
});

test('Check a text less than a minute to read with other action', (t) => {
t.is(m2r(shortText, 'minute to finish'), 'less than a minute to finish');
});

test('Check a text less than a minute to read with custom wpm (1 wpm)', (t) => {
t.is(m2r(shortText, 1), '2 min to read');
});

test('Check a text less than a minute to read with custom text and custom wpm (1 wpm)', (
t,
) => {
t.is(m2r(shortText, 'minutes to finish', 1), '2 minutes to finish');
});

test('Check a long text', (t) => {
t.is(m2r(longText), '2 min to read');
});

test('Check text with custom wpm (1 word per minute)', (t) => {
t.is(m2r(longText, 1), '500 min to read');
});

test('Check text with custom wpm (1 word per minute) and custom alt text', (
t,
) => {
t.is(m2r(longText, 'minutes to finish', 1), '500 minutes to finish');
});

0 comments on commit 967150f

Please sign in to comment.