commafy-anything 2.1.0 → 3.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 CHANGED
@@ -1,5 +1,8 @@
1
1
  # Commafy anything 🍡
2
2
 
3
+ <a href="https://www.npmjs.com/package/commafy-anything"><img src="https://img.shields.io/npm/v/commafy-anything.svg" alt="Total Downloads"></a>
4
+ <a href="https://www.npmjs.com/package/commafy-anything"><img src="https://img.shields.io/npm/dw/commafy-anything.svg" alt="Latest Stable Version"></a>
5
+
3
6
  ```
4
7
  npm i commafy-anything
5
8
  ```
@@ -23,9 +26,9 @@ U built this package because I needed to add comma's to numbers! And I wanted to
23
26
  ## Usage
24
27
 
25
28
  ```js
26
- import commafy from 'commafy-anything'
29
+ import { commafy } from 'commafy-anything'
27
30
 
28
- commafy(1000) === '1000'
31
+ commafy(1000) === '1,000'
29
32
  commafy(10000) === '10,000'
30
33
  commafy(100000) === '100,000'
31
34
  commafy(1000000) === '1,000,000'
@@ -36,11 +39,12 @@ commafy(1000000) === '1,000,000'
36
39
 
37
40
  ```js
38
41
  // default:
39
- commafy(1000) === '1000'
42
+ commafy(1000) === '1,000'
40
43
 
41
44
  // thousands:
42
- const options = {thousands: true}
43
- commafy(1000, options) === '1,000'
45
+ const options = {thousandsComma: false}
46
+ commafy(1000, options) === '1000'
47
+ commafy(10000, options) === '10,000'
44
48
  ```
45
49
 
46
50
  ### Spaced decimals
@@ -79,7 +83,7 @@ I'm using simple regular expressions. The source code is in TypeScript, but the
79
83
  ```js
80
84
  function commafy (num, {stripDecimals, spacedDecimals} = {}) {
81
85
  const str = num.toString().split('.')
82
- if (str[0].length >= 5) {
86
+ if (str[0].length >= 4) {
83
87
  str[0] = str[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,')
84
88
  }
85
89
  if (stripDecimals) return str[0]
@@ -11,16 +11,16 @@ Object.defineProperty(exports, '__esModule', { value: true });
11
11
  * @returns {string} eg. '1,000,000'
12
12
  */
13
13
  function commafy(num, options) {
14
- var _a = options || {}, _b = _a.stripDecimals, stripDecimals = _b === void 0 ? false : _b, _c = _a.spacedDecimals, spacedDecimals = _c === void 0 ? false : _c, _d = _a.thousandsComma, thousandsComma = _d === void 0 ? true : _d, _e = _a.K, K = _e === void 0 ? false : _e;
15
- var _f = num.toString().split('.'), wholeNrStr = _f[0], _g = _f[1], decimalStr = _g === void 0 ? '' : _g;
16
- var str = [wholeNrStr, decimalStr];
17
- var minLength = thousandsComma ? 4 : 5;
14
+ const { stripDecimals = false, spacedDecimals = false, thousandsComma = true, K = false } = options || {};
15
+ const [wholeNrStr, decimalStr = ''] = num.toString().split('.');
16
+ const str = [wholeNrStr, decimalStr];
17
+ const minLength = thousandsComma ? 4 : 5;
18
18
  if (!K && wholeNrStr.length >= minLength) {
19
19
  str[0] = wholeNrStr.replace(/(\d)(?=(\d{3})+$)/g, '$1,');
20
20
  }
21
21
  if (K && wholeNrStr.length > 3) {
22
- var numInK = Math.round(num / 1000);
23
- var commafied = numInK.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
22
+ const numInK = Math.round(num / 1000);
23
+ const commafied = numInK.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
24
24
  str[0] = commafied + 'K';
25
25
  }
26
26
  if (stripDecimals || !decimalStr || K)
@@ -7,16 +7,16 @@
7
7
  * @returns {string} eg. '1,000,000'
8
8
  */
9
9
  function commafy(num, options) {
10
- var _a = options || {}, _b = _a.stripDecimals, stripDecimals = _b === void 0 ? false : _b, _c = _a.spacedDecimals, spacedDecimals = _c === void 0 ? false : _c, _d = _a.thousandsComma, thousandsComma = _d === void 0 ? true : _d, _e = _a.K, K = _e === void 0 ? false : _e;
11
- var _f = num.toString().split('.'), wholeNrStr = _f[0], _g = _f[1], decimalStr = _g === void 0 ? '' : _g;
12
- var str = [wholeNrStr, decimalStr];
13
- var minLength = thousandsComma ? 4 : 5;
10
+ const { stripDecimals = false, spacedDecimals = false, thousandsComma = true, K = false } = options || {};
11
+ const [wholeNrStr, decimalStr = ''] = num.toString().split('.');
12
+ const str = [wholeNrStr, decimalStr];
13
+ const minLength = thousandsComma ? 4 : 5;
14
14
  if (!K && wholeNrStr.length >= minLength) {
15
15
  str[0] = wholeNrStr.replace(/(\d)(?=(\d{3})+$)/g, '$1,');
16
16
  }
17
17
  if (K && wholeNrStr.length > 3) {
18
- var numInK = Math.round(num / 1000);
19
- var commafied = numInK.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
18
+ const numInK = Math.round(num / 1000);
19
+ const commafied = numInK.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
20
20
  str[0] = commafied + 'K';
21
21
  }
22
22
  if (stripDecimals || !decimalStr || K)
File without changes
package/package.json CHANGED
@@ -1,19 +1,30 @@
1
1
  {
2
2
  "name": "commafy-anything",
3
3
  "sideEffects": false,
4
- "version": "2.1.0",
4
+ "type": "module",
5
+ "version": "3.0.1",
5
6
  "description": "Adds comma's to a number. A simple and small integration",
6
- "main": "dist/index.cjs.js",
7
- "module": "dist/index.esm.js",
8
- "typings": "types/index.d.ts",
9
- "directories": {
10
- "test": "test"
7
+ "module": "./dist/index.es.js",
8
+ "main": "./dist/index.cjs",
9
+ "types": "./dist/types/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./dist/index.es.js",
13
+ "require": "./dist/index.cjs",
14
+ "types": "./dist/types/index.d.ts"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=12.13"
11
22
  },
12
23
  "scripts": {
13
- "test": "ava",
14
- "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
15
- "rollup": "rollup -c build/rollup.js",
16
- "build": "npm run lint && npm run test && npm run rollup"
24
+ "test": "vitest run",
25
+ "lint": "tsc --noEmit && eslint ./src --ext .ts",
26
+ "build": "rollup -c ./scripts/build.js",
27
+ "release": "npm run lint && del dist && npm run build && np"
17
28
  },
18
29
  "repository": {
19
30
  "type": "git",
@@ -26,31 +37,55 @@
26
37
  "number"
27
38
  ],
28
39
  "author": "Luca Ban - Mesqueeb",
40
+ "funding": "https://github.com/sponsors/mesqueeb",
29
41
  "license": "MIT",
30
42
  "bugs": {
31
43
  "url": "https://github.com/mesqueeb/commafy-anything/issues"
32
44
  },
33
45
  "homepage": "https://github.com/mesqueeb/commafy-anything#readme",
34
46
  "devDependencies": {
35
- "@typescript-eslint/eslint-plugin": "^4.8.1",
36
- "@typescript-eslint/parser": "^4.8.1",
37
- "ava": "^3.13.0",
38
- "eslint": "^7.13.0",
39
- "eslint-config-prettier": "^6.15.0",
40
- "eslint-plugin-tree-shaking": "^1.8.0",
41
- "rollup": "^2.33.2",
42
- "rollup-plugin-typescript2": "^0.29.0",
43
- "tsconfig-paths": "^3.9.0",
44
- "ts-node": "^9.0.0",
45
- "typescript": "^4.0.5"
47
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
48
+ "@typescript-eslint/parser": "^5.10.1",
49
+ "del-cli": "^4.0.1",
50
+ "eslint": "^8.7.0",
51
+ "eslint-config-prettier": "^8.3.0",
52
+ "eslint-plugin-tree-shaking": "^1.10.0",
53
+ "np": "^7.6.0",
54
+ "prettier": "^2.5.1",
55
+ "rollup": "^2.66.0",
56
+ "rollup-plugin-typescript2": "^0.31.1",
57
+ "typescript": "^4.5.5",
58
+ "vitest": "^0.2.1"
59
+ },
60
+ "np": {
61
+ "yarn": false,
62
+ "branch": "production"
46
63
  },
47
- "ava": {
48
- "extensions": [
49
- "ts"
64
+ "eslintConfig": {
65
+ "ignorePatterns": [
66
+ "node_modules",
67
+ "dist",
68
+ "scripts",
69
+ "test"
70
+ ],
71
+ "root": true,
72
+ "parser": "@typescript-eslint/parser",
73
+ "plugins": [
74
+ "@typescript-eslint",
75
+ "tree-shaking"
76
+ ],
77
+ "extends": [
78
+ "eslint:recommended",
79
+ "plugin:@typescript-eslint/eslint-recommended",
80
+ "plugin:@typescript-eslint/recommended",
81
+ "prettier"
50
82
  ],
51
- "require": [
52
- "tsconfig-paths/register",
53
- "ts-node/register"
54
- ]
83
+ "rules": {
84
+ "@typescript-eslint/no-empty-function": "off",
85
+ "@typescript-eslint/no-explicit-any": "off",
86
+ "@typescript-eslint/ban-ts-ignore": "off",
87
+ "tree-shaking/no-side-effects-in-initialization": "error",
88
+ "@typescript-eslint/ban-ts-comment": "off"
89
+ }
55
90
  }
56
91
  }
package/.eslintignore DELETED
@@ -1,9 +0,0 @@
1
- # don't ever lint node_modules
2
- node_modules
3
- # don't lint build output (make sure it's set to your correct build folder name)
4
- dist
5
- # don't lint nyc coverage output
6
- coverage
7
-
8
- test
9
- .eslintrc.js
package/.eslintrc.js DELETED
@@ -1,17 +0,0 @@
1
- // npm i -D @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-prettier eslint-plugin-tree-shaking
2
- module.exports = {
3
- root: true,
4
- parser: '@typescript-eslint/parser',
5
- plugins: ['@typescript-eslint', 'tree-shaking'],
6
- extends: [
7
- 'eslint:recommended',
8
- 'plugin:@typescript-eslint/eslint-recommended',
9
- 'plugin:@typescript-eslint/recommended',
10
- 'prettier/@typescript-eslint',
11
- ],
12
- rules: {
13
- '@typescript-eslint/no-explicit-any': 'off',
14
- '@typescript-eslint/ban-ts-ignore': 'off',
15
- 'tree-shaking/no-side-effects-in-initialization': 'error',
16
- },
17
- }
@@ -1,12 +0,0 @@
1
- # These are supported funding model platforms
2
-
3
- github: mesqueeb
4
- patreon: # Replace with a single Patreon username
5
- open_collective: # Replace with a single Open Collective username
6
- ko_fi: # Replace with a single Ko-fi username
7
- tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8
- community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
- liberapay: # Replace with a single Liberapay username
10
- issuehunt: # Replace with a single IssueHunt username
11
- otechie: # Replace with a single Otechie username
12
- custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
package/.prettierrc DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "printWidth": 100,
3
- "tabWidth": 2,
4
- "singleQuote": true,
5
- "trailingComma": "es5",
6
- "semi": false,
7
- "bracketSpacing": true,
8
- "quoteProps": "consistent"
9
- }
package/build/rollup.js DELETED
@@ -1,59 +0,0 @@
1
- /* eslint-disable */
2
-
3
- // npm install rollup-plugin-typescript2 typescript --save-dev
4
- import typescript from 'rollup-plugin-typescript2'
5
- // import { terser } from 'rollup-plugin-terser'
6
- // import resolve from 'rollup-plugin-node-resolve'
7
-
8
- // ------------------------------------------------------------------------------------------
9
- // formats
10
- // ------------------------------------------------------------------------------------------
11
- // amd – Asynchronous Module Definition, used with module loaders like RequireJS
12
- // cjs – CommonJS, suitable for Node and Browserify/Webpack
13
- // esm – Keep the bundle as an ES module file
14
- // iife – A self-executing function, suitable for inclusion as a <script> tag. (If you want to create a bundle for your application, you probably want to use this, because it leads to smaller file sizes.)
15
- // umd – Universal Module Definition, works as amd, cjs and iife all in one
16
- // system – Native format of the SystemJS loader
17
-
18
- // ------------------------------------------------------------------------------------------
19
- // setup
20
- // ------------------------------------------------------------------------------------------
21
- const pkg = require('../package.json')
22
- const name = pkg.name
23
- const className = name.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase())
24
- const external = Object.keys(pkg.dependencies || [])
25
- const plugins = [
26
- typescript({ useTsconfigDeclarationDir: true, tsconfigOverride: { exclude: ['test/**/*'] } }),
27
- ]
28
-
29
- // ------------------------------------------------------------------------------------------
30
- // Builds
31
- // ------------------------------------------------------------------------------------------
32
- function defaults (config) {
33
- // defaults
34
- const defaults = {
35
- plugins,
36
- external,
37
- }
38
- // defaults.output
39
- config.output = config.output.map(output => {
40
- return Object.assign(
41
- {
42
- sourcemap: false,
43
- name: className,
44
- },
45
- output
46
- )
47
- })
48
- return Object.assign(defaults, config)
49
- }
50
-
51
- export default [
52
- defaults({
53
- input: 'src/index.ts',
54
- output: [
55
- { file: 'dist/index.cjs.js', format: 'cjs' },
56
- { file: 'dist/index.esm.js', format: 'esm' },
57
- ],
58
- }),
59
- ]
package/src/index.ts DELETED
@@ -1,30 +0,0 @@
1
- /**
2
- * Adds comma's to a number
3
- *
4
- * @export
5
- * @param {(string | number)} num the number to commafy
6
- * @param {{ stripDecimals?: boolean, spacedDecimals?: boolean, thousandsComma?: boolean, K?: boolean }} [options] By default thousandsComma is enabled, if disabled it shows `1000` without comma (but `10,000` with)
7
- * @returns {string} eg. '1,000,000'
8
- */
9
- export function commafy (
10
- num: number,
11
- options?: { stripDecimals?: boolean, spacedDecimals?: boolean, thousandsComma?: boolean, K?: boolean }
12
- ): string {
13
- const { stripDecimals = false, spacedDecimals = false, thousandsComma = true, K = false } = options || {}
14
- const [wholeNrStr, decimalStr = ''] = num.toString().split('.')
15
- const str = [wholeNrStr, decimalStr]
16
- const minLength = thousandsComma ? 4 : 5
17
- if (!K && wholeNrStr.length >= minLength) {
18
- str[0] = wholeNrStr.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
19
- }
20
- if (K && wholeNrStr.length > 3) {
21
- const numInK = Math.round(num / 1000)
22
- const commafied = numInK.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,')
23
- str[0] = commafied + 'K'
24
- }
25
- if (stripDecimals || !decimalStr || K) return str[0]
26
- if (spacedDecimals && decimalStr.length >= minLength) {
27
- str[1] = decimalStr.replace(/(\d{3})/g, '$1 ').trim()
28
- }
29
- return str.join('.')
30
- }
package/test/index.ts DELETED
@@ -1,121 +0,0 @@
1
- import test from 'ava'
2
- import { commafy } from '../src/index'
3
-
4
- test('defaults', t => {
5
- t.is(commafy(1), '1')
6
- t.is(commafy(10), '10')
7
- t.is(commafy(100), '100')
8
- t.is(commafy(1000), '1,000')
9
- t.is(commafy(10000), '10,000')
10
- t.is(commafy(100000), '100,000')
11
- t.is(commafy(1000000), '1,000,000')
12
- t.is(commafy(10000000), '10,000,000')
13
- t.is(commafy(100000000), '100,000,000')
14
- t.is(commafy(1000000000), '1,000,000,000')
15
- t.is(commafy(10000000000), '10,000,000,000')
16
- t.is(commafy(100000000000), '100,000,000,000')
17
- t.is(commafy(1000000000000), '1,000,000,000,000')
18
- })
19
-
20
- test('K', t => {
21
- t.is(commafy(1, {K: true}), '1')
22
- t.is(commafy(14, {K: true}), '14')
23
- t.is(commafy(134, {K: true}), '134')
24
- t.is(commafy(1234, {K: true}), '1K')
25
- t.is(commafy(10234, {K: true}), '10K')
26
- t.is(commafy(100234, {K: true}), '100K')
27
- t.is(commafy(1000234, {K: true}), '1,000K')
28
- t.is(commafy(10000234, {K: true}), '10,000K')
29
- t.is(commafy(100000234, {K: true}), '100,000K')
30
- t.is(commafy(1000000234, {K: true}), '1,000,000K')
31
- t.is(commafy(10000000234, {K: true}), '10,000,000K')
32
- t.is(commafy(100000000234, {K: true}), '100,000,000K')
33
- t.is(commafy(1000000000234, {K: true}), '1,000,000,000K')
34
- t.is(commafy(1987, {K: true}), '2K')
35
- t.is(commafy(10987, {K: true}), '11K')
36
- t.is(commafy(100987, {K: true}), '101K')
37
- t.is(commafy(1000987, {K: true}), '1,001K')
38
- t.is(commafy(10000987, {K: true}), '10,001K')
39
- t.is(commafy(100000987, {K: true}), '100,001K')
40
- t.is(commafy(1000000987, {K: true}), '1,000,001K')
41
- t.is(commafy(10000000987, {K: true}), '10,000,001K')
42
- t.is(commafy(100000000987, {K: true}), '100,000,001K')
43
- t.is(commafy(1000000000987, {K: true}), '1,000,000,001K')
44
- t.is(commafy(1999, {K: true}), '2K')
45
- t.is(commafy(19999, {K: true}), '20K')
46
- t.is(commafy(199999, {K: true}), '200K')
47
- t.is(commafy(1999999, {K: true}), '2,000K')
48
- t.is(commafy(19999999, {K: true}), '20,000K')
49
- t.is(commafy(199999999, {K: true}), '200,000K')
50
- t.is(commafy(1999999999, {K: true}), '2,000,000K')
51
- t.is(commafy(19999999999, {K: true}), '20,000,000K')
52
- t.is(commafy(199999999999, {K: true}), '200,000,000K')
53
- t.is(commafy(1999999999999, {K: true}), '2,000,000,000K')
54
- })
55
-
56
- test('thousands', t => {
57
- // by default with
58
- t.is(commafy(1000), '1,000')
59
- t.is(commafy(9999), '9,999')
60
- t.is(commafy(1000.0001), '1,000.0001')
61
-
62
- // set to without
63
- t.is(commafy(1000, { thousandsComma: false }), '1000')
64
- t.is(commafy(9999, { thousandsComma: false }), '9999')
65
- t.is(commafy(1000.0001, { thousandsComma: false }), '1000.0001')
66
-
67
- // multiple options
68
- t.is(commafy(1000.0001, { thousandsComma: true, spacedDecimals: false }), '1,000.0001')
69
- t.is(commafy(1000.0001, { thousandsComma: true, spacedDecimals: true }), '1,000.000 1')
70
- t.is(commafy(1000.0001, { thousandsComma: false, spacedDecimals: true }), '1000.0001')
71
- })
72
-
73
- test('defaults - decimals', t => {
74
- t.is(commafy(1), '1')
75
- t.is(commafy(1.1), '1.1')
76
- t.is(commafy(1.01), '1.01')
77
- t.is(commafy(1.001), '1.001')
78
- t.is(commafy(1.0001), '1.0001')
79
- t.is(commafy(1.00001), '1.00001')
80
- t.is(commafy(1.000001), '1.000001')
81
- t.is(commafy(1.0000001), '1.0000001')
82
- t.is(commafy(1.00000001), '1.00000001')
83
- t.is(commafy(1.000000001), '1.000000001')
84
- t.is(commafy(1.0000000001), '1.0000000001')
85
- t.is(commafy(1.00000000001), '1.00000000001')
86
- t.is(commafy(1.000000000001), '1.000000000001')
87
- })
88
-
89
- test('spaced decimals', t => {
90
- const options = { spacedDecimals: true }
91
- t.is(commafy(1, options), '1')
92
- t.is(commafy(1.1, options), '1.1')
93
- t.is(commafy(1.01, options), '1.01')
94
- t.is(commafy(1.001, options), '1.001')
95
- t.is(commafy(1.0001, options), '1.000 1')
96
- t.is(commafy(1.00001, options), '1.000 01')
97
- t.is(commafy(1.000001, options), '1.000 001')
98
- t.is(commafy(1.0000001, options), '1.000 000 1')
99
- t.is(commafy(1.00000001, options), '1.000 000 01')
100
- t.is(commafy(1.000000001, options), '1.000 000 001')
101
- t.is(commafy(1.0000000001, options), '1.000 000 000 1')
102
- t.is(commafy(1.00000000001, options), '1.000 000 000 01')
103
- t.is(commafy(1.000000000001, options), '1.000 000 000 001')
104
- })
105
-
106
- test('strip decimals', t => {
107
- const options = { stripDecimals: true }
108
- t.is(commafy(1, options), '1')
109
- t.is(commafy(1.1, options), '1')
110
- t.is(commafy(1.01, options), '1')
111
- t.is(commafy(1.001, options), '1')
112
- t.is(commafy(1.0001, options), '1')
113
- t.is(commafy(1.00001, options), '1')
114
- t.is(commafy(1.000001, options), '1')
115
- t.is(commafy(1.0000001, options), '1')
116
- t.is(commafy(1.00000001, options), '1')
117
- t.is(commafy(1.000000001, options), '1')
118
- t.is(commafy(1.0000000001, options), '1')
119
- t.is(commafy(1.00000000001, options), '1')
120
- t.is(commafy(1.000000000001, options), '1')
121
- })
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "baseUrl": ".",
4
- "outDir": "./dist",
5
- "declaration": true,
6
- "declarationDir": "./types/",
7
- "target": "es5",
8
- "lib": ["es5", "es2015", "es2016", "es2017", "dom"]
9
- },
10
- "include": [
11
- "src/**/*",
12
- "test/**/*"
13
- ]
14
- }