Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/segment-boneyard/human-intervals
Create clean, human friendly labels for a chart.
https://github.com/segment-boneyard/human-intervals
Last synced: about 7 hours ago
JSON representation
Create clean, human friendly labels for a chart.
- Host: GitHub
- URL: https://github.com/segment-boneyard/human-intervals
- Owner: segment-boneyard
- Created: 2015-03-03T18:51:35.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-03-03T22:58:54.000Z (over 9 years ago)
- Last Synced: 2024-04-09T16:31:06.847Z (7 months ago)
- Language: JavaScript
- Size: 110 KB
- Stars: 3
- Watchers: 47
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
# human-intervals
## Installation
```
npm install human-intervals
``````
component install segmentio/human-intervals
```## Example
```js
var humanize = require('human-intervals');
var assert = require('assert');var values = [ 341, 12, 793, 42, 54, 591, 123, 271, 804 ];
var max = Math.max.apply(Math, values);assert.deepEqual(humanize({ segments: 2, value: max }).map(format), [ '0', '450', '900' ]);
assert.deepEqual(humanize({ segments: 3, value: max }).map(format), [ '0', '300', '600', '900' ]);
assert.deepEqual(humanize({ segments: 4, value: max }).map(format), [ '0', '210', '420', '630', '840' ]);
assert.deepEqual(humanize({ segments: 5, value: max }).map(format), [ '0', '180', '360', '540', '720', '900']);var values = [ 341, 12, 793, 42, 54, 591, 123, 271, 804, 1029 ].map(thousand);
var max = Math.max.apply(Math, values);assert.deepEqual(humanize({ segments: 2, value: max }).map(format), [ '0', '1M', '2M' ]);
assert.deepEqual(humanize({ segments: 3, value: max }).map(format), [ '0', '400K', '800K', '1.2M' ]);
assert.deepEqual(humanize({ segments: 4, value: max }).map(format), [ '0', '300K', '600K', '900K', '1.2M' ]);
assert.deepEqual(humanize({ segments: 5, value: max }).map(format), [ '0', '400K', '800K', '1.2M', '1.6M', '2M']);function format(value) {
if (value > 10e5) return (value / 10e5) + 'M'; // millions
if (value > 10e2) return (value / 10e2) + 'K'; // thousands
return String(value); // <= hundreds
}
```