commafy-anything 3.0.2 → 3.0.3
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 +46 -12
- package/dist/index.cjs +0 -2
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -7,21 +7,26 @@
|
|
|
7
7
|
npm i commafy-anything
|
|
8
8
|
```
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Return a number as string with `,` or `K`. A simple and small integration.
|
|
11
11
|
|
|
12
12
|
## Motivation
|
|
13
13
|
|
|
14
|
-
U built this package because I needed to add comma's to numbers! And I wanted to build it myself. 😄
|
|
14
|
+
U built this package because I needed to add comma's and K to numbers! And I wanted to build it myself. 😄
|
|
15
15
|
|
|
16
|
-
## Meet the family
|
|
16
|
+
## Meet the family (more tiny utils with TS support)
|
|
17
17
|
|
|
18
|
-
- [
|
|
18
|
+
- [is-what 🙉](https://github.com/mesqueeb/is-what)
|
|
19
|
+
- [is-where 🙈](https://github.com/mesqueeb/is-where)
|
|
19
20
|
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
|
|
21
|
+
- [check-anything 👁](https://github.com/mesqueeb/check-anything)
|
|
22
|
+
- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)
|
|
23
|
+
- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)
|
|
24
|
+
- [map-anything 🗺](https://github.com/mesqueeb/map-anything)
|
|
20
25
|
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
|
|
21
|
-
- [
|
|
22
|
-
- [
|
|
26
|
+
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
|
|
27
|
+
- [case-anything 🐫](https://github.com/mesqueeb/case-anything)
|
|
23
28
|
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
|
|
24
|
-
- [
|
|
29
|
+
- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)
|
|
25
30
|
|
|
26
31
|
## Usage
|
|
27
32
|
|
|
@@ -35,20 +40,47 @@ commafy(1000000) === '1,000,000'
|
|
|
35
40
|
// etc.
|
|
36
41
|
```
|
|
37
42
|
|
|
43
|
+
### K
|
|
44
|
+
|
|
45
|
+
You can show numbers as 1K.
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
const options = { K: true }
|
|
49
|
+
// when smaller than 1000 will be shown as is, without K
|
|
50
|
+
commafy(123, options) === '123'
|
|
51
|
+
|
|
52
|
+
// when larger than 1000 it will round up/down behind the K
|
|
53
|
+
commafy(1234, options) === '1K'
|
|
54
|
+
commafy(10234, options) === '10K'
|
|
55
|
+
commafy(100234, options) === '100K'
|
|
56
|
+
commafy(1000234, options) === '1,000K'
|
|
57
|
+
|
|
58
|
+
commafy(1955, options) === '2K'
|
|
59
|
+
commafy(10955, options) === '11K'
|
|
60
|
+
commafy(100955, options) === '101K'
|
|
61
|
+
commafy(1000955, options) === '1,001K'
|
|
62
|
+
```
|
|
63
|
+
|
|
38
64
|
### Thousands
|
|
39
65
|
|
|
66
|
+
You can disable a comma to be added when the number is between `1000` ~ `9999`.
|
|
67
|
+
|
|
40
68
|
```js
|
|
41
69
|
// default:
|
|
42
70
|
commafy(1000) === '1,000'
|
|
43
71
|
|
|
44
|
-
|
|
45
|
-
const options = {thousandsComma: false}
|
|
72
|
+
const options = { thousandsComma: false }
|
|
46
73
|
commafy(1000, options) === '1000'
|
|
74
|
+
commafy(9999, options) === '9999'
|
|
75
|
+
|
|
76
|
+
// beyond 9999 it will always have a comma
|
|
47
77
|
commafy(10000, options) === '10,000'
|
|
48
78
|
```
|
|
49
79
|
|
|
50
80
|
### Spaced decimals
|
|
51
81
|
|
|
82
|
+
You can add spaces to decimals.
|
|
83
|
+
|
|
52
84
|
```js
|
|
53
85
|
// default:
|
|
54
86
|
commafy(1.0001) === '1.0001'
|
|
@@ -57,7 +89,7 @@ commafy(1.000001) === '1.000001'
|
|
|
57
89
|
commafy(1.0000001) === '1.0000001'
|
|
58
90
|
|
|
59
91
|
// spaced decimals:
|
|
60
|
-
const options = {spacedDecimals: true}
|
|
92
|
+
const options = { spacedDecimals: true }
|
|
61
93
|
commafy(1.0001, options) === '1.0001'
|
|
62
94
|
commafy(1.00001, options) === '1.000 01'
|
|
63
95
|
commafy(1.000001, options) === '1.000 001'
|
|
@@ -66,12 +98,14 @@ commafy(1.0000001, options) === '1.000 0001'
|
|
|
66
98
|
|
|
67
99
|
### Strip decimals
|
|
68
100
|
|
|
101
|
+
You can add strip away decimals.
|
|
102
|
+
|
|
69
103
|
```js
|
|
70
104
|
// default:
|
|
71
105
|
commafy(1.0001) === '1.0001'
|
|
72
106
|
|
|
73
107
|
// strip decimals:
|
|
74
|
-
const options = {stripDecimals: true}
|
|
108
|
+
const options = { stripDecimals: true }
|
|
75
109
|
commafy(1.001, options) === '1'
|
|
76
110
|
commafy(1.999, options) === '1'
|
|
77
111
|
```
|
|
@@ -81,7 +115,7 @@ commafy(1.999, options) === '1'
|
|
|
81
115
|
I'm using simple regular expressions. The source code is in TypeScript, but the essense of my source code looks something like this:
|
|
82
116
|
|
|
83
117
|
```js
|
|
84
|
-
function commafy
|
|
118
|
+
function commafy(num, { stripDecimals, spacedDecimals } = {}) {
|
|
85
119
|
const str = num.toString().split('.')
|
|
86
120
|
if (str[0].length >= 4) {
|
|
87
121
|
str[0] = str[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,')
|
package/dist/index.cjs
CHANGED
package/package.json
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
"name": "commafy-anything",
|
|
3
3
|
"sideEffects": false,
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "3.0.
|
|
5
|
+
"version": "3.0.3",
|
|
6
6
|
"description": "Adds comma's to a number. A simple and small integration",
|
|
7
7
|
"module": "./dist/index.es.js",
|
|
8
8
|
"main": "./dist/index.cjs",
|
|
9
9
|
"types": "./dist/types/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
+
"types": "./dist/types/index.d.ts",
|
|
12
13
|
"import": "./dist/index.es.js",
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
14
|
-
"types": "./dist/types/index.d.ts"
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"scripts": {
|
|
24
24
|
"test": "vitest run",
|
|
25
25
|
"lint": "tsc --noEmit && eslint ./src --ext .ts",
|
|
26
|
-
"build": "rollup -c ./scripts/build.js",
|
|
26
|
+
"build": "rollup --bundleConfigAsCjs -c ./scripts/build.js",
|
|
27
27
|
"release": "npm run lint && del dist && npm run build && np"
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
@@ -44,18 +44,18 @@
|
|
|
44
44
|
},
|
|
45
45
|
"homepage": "https://github.com/mesqueeb/commafy-anything#readme",
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
48
|
-
"@typescript-eslint/parser": "^5.
|
|
47
|
+
"@typescript-eslint/eslint-plugin": "^5.59.2",
|
|
48
|
+
"@typescript-eslint/parser": "^5.59.2",
|
|
49
49
|
"del-cli": "^4.0.1",
|
|
50
|
-
"eslint": "^8.
|
|
51
|
-
"eslint-config-prettier": "^8.
|
|
50
|
+
"eslint": "^8.40.0",
|
|
51
|
+
"eslint-config-prettier": "^8.8.0",
|
|
52
52
|
"eslint-plugin-tree-shaking": "^1.10.0",
|
|
53
|
-
"np": "^7.
|
|
54
|
-
"prettier": "^2.
|
|
55
|
-
"rollup": "^
|
|
56
|
-
"rollup-plugin-typescript2": "^0.
|
|
57
|
-
"typescript": "^4.
|
|
58
|
-
"vitest": "^0.
|
|
53
|
+
"np": "^7.7.0",
|
|
54
|
+
"prettier": "^2.8.8",
|
|
55
|
+
"rollup": "^3.21.5",
|
|
56
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
57
|
+
"typescript": "^4.9.5",
|
|
58
|
+
"vitest": "^0.31.0"
|
|
59
59
|
},
|
|
60
60
|
"np": {
|
|
61
61
|
"yarn": false,
|