Skip to content

Commit 6cace6a

Browse files
committed
🔢 @lets/count: Consistent counter
1 parent d1a01e0 commit 6cace6a

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

packages/count/.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.*
2+
*.log
3+
spec.js

packages/count/.npmrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package-lock=false
2+
access=public

packages/count/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# count [![](https://img.shields.io/npm/v/@lets/count.svg)](https://www.npmjs.com/package/@lets/count) [![](https://img.shields.io/badge/source--000000.svg?logo=github&style=social)](https://github.com/omrilotan/mono/tree/master/packages/count)
2+
3+
## 🔢 Consistent counter
4+
5+
```js
6+
// Just count
7+
count() // 1st
8+
count() // 2nd
9+
10+
// Set counter
11+
count.set(14)
12+
count() // 15th
13+
count() // 16th
14+
15+
// Start counting from
16+
count.from(10)
17+
count() // 11th
18+
count() // 12th
19+
20+
// Count down
21+
count.down() // 11th
22+
count.down() // 10th
23+
24+
// Reset counter to 0
25+
count.reset()
26+
count.down() // -1st
27+
count.down() // -2nd
28+
```
29+
30+
## Transpiled version
31+
Environments which exclude node_modules from the transpiling pipeline should include the "browser" entry instead of "main". This exposes an ES5 commonjs module.
32+
33+
Also available for explicit import:
34+
```js
35+
const count = require('@lets/count/dist');
36+
```

packages/count/index.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const original = require('ordinal');
2+
const ordinal = num => num < 0
3+
? ['-', original(Math.abs(num))].join('')
4+
: original(num);
5+
6+
let current = 0;
7+
8+
/**
9+
* Count
10+
* @return {String}
11+
*/
12+
const count = () => ordinal(++current);
13+
14+
/**
15+
* Set
16+
* @param {Number} number
17+
* @return {count}
18+
*/
19+
count.set = number => {
20+
current = number;
21+
return count;
22+
}
23+
24+
/**
25+
* From
26+
* @param {Number} number
27+
* @return {String}
28+
*/
29+
count.from = number => count.set(number)();
30+
31+
/**
32+
* Reset to 0
33+
* @return {count}
34+
*/
35+
count.reset = () => count.set(0);
36+
37+
/**
38+
* Count down
39+
* @return {String}
40+
*/
41+
count.down = () => ordinal(--current);
42+
43+
module.exports = count;

packages/count/package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@lets/count",
3+
"version": "0.0.0",
4+
"description": "🔢 Consistent counter",
5+
"keywords": [
6+
"count",
7+
"consistent",
8+
"ordinal",
9+
"1st",
10+
"2nd",
11+
"🔢"
12+
],
13+
"author": "omrilotan",
14+
"license": "MIT",
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/omrilotan/mono.git"
18+
},
19+
"homepage": "https://omrilotan.com/mono/count/",
20+
"browser": "dist/index.js",
21+
"main": "index.js",
22+
"scripts": {
23+
"dist": "mkdir -p dist; ../../node_modules/.bin/babel index.js --out-file dist/index.js --config-file ../../.babelrc.js",
24+
"test": "cd ../../; npm t packages/count; cd -"
25+
},
26+
"dependencies": {
27+
"ordinal": "^1.0.2"
28+
}
29+
}

packages/count/spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const count = require('.');
2+
3+
describe('count', () => {
4+
it('Should count', () => {
5+
expect(count()).to.equal('1st');
6+
expect(count()).to.equal('2nd');
7+
});
8+
it('Should continue to count', () => {
9+
expect(count()).to.equal('3rd');
10+
expect(count()).to.equal('4th');
11+
});
12+
it('Should reset to 0', () => {
13+
expect(count()).to.equal('5th');
14+
count.reset();
15+
expect(count()).to.equal('1st');
16+
});
17+
it('Should set to given number', () => {
18+
expect(count()).to.equal('2nd');
19+
count.set(6);
20+
expect(count()).to.equal('7th');
21+
});
22+
it('Should start from given number', () => {
23+
expect(count()).to.equal('8th');
24+
expect(count.from(6)).to.equal('7th');
25+
});
26+
it('Should count down', () => {
27+
expect(count.down()).to.equal('6th');
28+
expect(count.down()).to.equal('5th');
29+
});
30+
it('Should count negative numbers', () => {
31+
count.reset();
32+
expect(count.down()).to.equal('-1st');
33+
expect(count.down()).to.equal('-2nd');
34+
});
35+
});

0 commit comments

Comments
 (0)