@raindrops-on-roses/number-nice-number 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/package.json +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# @raindrops-on-roses/nice-number
|
|
2
|
+
|
|
3
|
+
`niceNumber` utility from [raindrops-on-roses](https://www.npmjs.com/package/raindrops-on-roses).
|
|
4
|
+
|
|
5
|
+
Returns a human-friendly number based on the `1`, `2`, `5`, `10` progression, scaled to the appropriate power of ten. It is useful for chart scales, tick intervals, and similar numeric steps.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @raindrops-on-roses/nice-number
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { niceNumber } from "@raindrops-on-roses/nice-number";
|
|
17
|
+
|
|
18
|
+
niceNumber(37); // 50
|
|
19
|
+
niceNumber(23, true); // 20
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
By default, the result is rounded upward so it is greater than or equal to the provided range.
|
|
23
|
+
|
|
24
|
+
When `round` is `true`, the nearest human-friendly value is returned instead.
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
niceNumber(101); // 200
|
|
28
|
+
niceNumber(149, true); // 100
|
|
29
|
+
niceNumber(150, true); // 200
|
|
30
|
+
niceNumber(0.15, true); // 0.2
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## API
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
niceNumber(range: number, round?: boolean): number
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Parameters
|
|
40
|
+
|
|
41
|
+
- `range` - The positive numeric range to convert to a human-friendly value.
|
|
42
|
+
- `round` - Whether to return the nearest human-friendly value instead of rounding upward. Defaults to `false`.
|
|
43
|
+
|
|
44
|
+
### Returns
|
|
45
|
+
|
|
46
|
+
A human-friendly number expressed as `1`, `2`, `5`, or `10` times a power of ten.
|
|
47
|
+
|
|
48
|
+
## With the complete library
|
|
49
|
+
|
|
50
|
+
You can also install the complete `raindrops-on-roses` package:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
npm install raindrops-on-roses
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
and import `niceNumber` from the umbrella package:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { niceNumber } from "raindrops-on-roses";
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Lore
|
|
63
|
+
|
|
64
|
+
> We talk about being in our 30s, and our 40s, and our 50s. We don’t talk about being in our 37s.
|
|
65
|
+
|
|
66
|
+
## Repository
|
|
67
|
+
|
|
68
|
+
[graphieros/raindrops-on-roses](https://github.com/graphieros/raindrops-on-roses)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a human-friendly number based on the `1`, `2`, `5`, `10` progression
|
|
3
|
+
* scaled to the appropriate power of ten. Useful to produce chart scales, tick itervals, etc.
|
|
4
|
+
*
|
|
5
|
+
* By default, the result is rounded upward so it is greater than or equal to
|
|
6
|
+
* the provided range. When `round` is `true`, the nearest human-friendly value
|
|
7
|
+
* is returned instead.
|
|
8
|
+
*
|
|
9
|
+
* ---
|
|
10
|
+
*
|
|
11
|
+
* @param range - The positive numeric range to convert to a human-friendly value
|
|
12
|
+
* @param round - Whether to return the nearest human-friendly value instead of rounding upward
|
|
13
|
+
* @returns A human-friendly number expressed as `1`, `2`, `5`, or `10` times a power of ten
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* niceNumber(37); // 50
|
|
18
|
+
* niceNumber(23, true); // 20
|
|
19
|
+
*```
|
|
20
|
+
* ---
|
|
21
|
+
*
|
|
22
|
+
* Lore:
|
|
23
|
+
We talk about being in our 30s, and our 40s, and our 50s. We don’t talk about being in our 37s.
|
|
24
|
+
*/
|
|
25
|
+
export declare function niceNumber(range: number, round?: boolean): number;
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,UAAQ,GAAG,MAAM,CA+B/D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
function e(e, t = !1) {
|
|
3
|
+
let n = 10 ** Math.floor(Math.log10(e)), r = e / n, i = 2 ** -52 * Math.max(1, Math.abs(r)), a;
|
|
4
|
+
return a = t ? r < 1.5 - i ? 1 : r < 3 - i ? 2 : r < 7 - i ? 5 : 10 : r <= 1 + i ? 1 : r <= 2 + i ? 2 : r <= 5 + i ? 5 : 10, a * n;
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
7
|
+
export { e as niceNumber };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Returns a human-friendly number based on the `1`, `2`, `5`, `10` progression\n * scaled to the appropriate power of ten. Useful to produce chart scales, tick itervals, etc.\n *\n * By default, the result is rounded upward so it is greater than or equal to\n * the provided range. When `round` is `true`, the nearest human-friendly value\n * is returned instead.\n *\n * ---\n *\n * @param range - The positive numeric range to convert to a human-friendly value\n * @param round - Whether to return the nearest human-friendly value instead of rounding upward\n * @returns A human-friendly number expressed as `1`, `2`, `5`, or `10` times a power of ten\n *\n * @example\n * ```ts\n * niceNumber(37); // 50\n * niceNumber(23, true); // 20\n *```\n * ---\n *\n * Lore:\nWe talk about being in our 30s, and our 40s, and our 50s. We don’t talk about being in our 37s.\n */\nexport function niceNumber(range: number, round = false): number {\n const exponent = Math.floor(Math.log10(range));\n const magnitude = Math.pow(10, exponent);\n const fraction = range / magnitude;\n const epsilon = Number.EPSILON * Math.max(1, Math.abs(fraction));\n\n let niceFraction: number;\n\n if (round) {\n if (fraction < 1.5 - epsilon) {\n niceFraction = 1;\n } else if (fraction < 3 - epsilon) {\n niceFraction = 2;\n } else if (fraction < 7 - epsilon) {\n niceFraction = 5;\n } else {\n niceFraction = 10;\n }\n } else {\n if (fraction <= 1 + epsilon) {\n niceFraction = 1;\n } else if (fraction <= 2 + epsilon) {\n niceFraction = 2;\n } else if (fraction <= 5 + epsilon) {\n niceFraction = 5;\n } else {\n niceFraction = 10;\n }\n }\n\n return niceFraction * magnitude;\n}\n"],"mappings":";AAwBA,SAAgB,EAAW,GAAe,IAAQ,IAAe;CAE/D,IAAM,IAAqB,MADV,KAAK,MAAM,KAAK,MAAM,CAAK,CACb,GACzB,IAAW,IAAQ,GACnB,eAA2B,KAAK,IAAI,GAAG,KAAK,IAAI,CAAQ,CAAC,GAE3D;CAwBJ,OAtBA,AAkBI,IAlBA,IACE,IAAW,MAAM,IACJ,IACN,IAAW,IAAI,IACT,IACN,IAAW,IAAI,IACT,IAEA,KAGb,KAAY,IAAI,IACH,IACN,KAAY,IAAI,IACV,IACN,KAAY,IAAI,IACV,IAEA,IAIZ,IAAe;AACxB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@raindrops-on-roses/number-nice-number",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "vite build && tsc -p tsconfig.json"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/graphieros/raindrops-on-roses.git",
|
|
25
|
+
"directory": "packages/number-nice-number"
|
|
26
|
+
}
|
|
27
|
+
}
|