Skip to content

Commit 525e63d

Browse files
committed
fix: exports
1 parent 5717cd8 commit 525e63d

File tree

10 files changed

+571
-336
lines changed

10 files changed

+571
-336
lines changed

dist/cjs/index.cjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
function commafy(num, options) {
4+
const { stripDecimals = false, spacedDecimals = false, thousandsComma = true, K = false } = options || {};
5+
const [wholeNrStr, decimalStr = ""] = num.toString().split(".");
6+
const str = [wholeNrStr, decimalStr];
7+
const minLength = thousandsComma ? 4 : 5;
8+
if (!K && wholeNrStr.length >= minLength) {
9+
str[0] = wholeNrStr.replace(/(\d)(?=(\d{3})+$)/g, "$1,");
10+
}
11+
if (K && wholeNrStr.length > 3) {
12+
const numInK = Math.round(num / 1e3);
13+
const commafied = numInK.toString().replace(/(\d)(?=(\d{3})+$)/g, "$1,");
14+
str[0] = commafied + "K";
15+
}
16+
if (stripDecimals || !decimalStr || K)
17+
return str[0];
18+
if (spacedDecimals && decimalStr.length >= minLength) {
19+
str[1] = decimalStr.replace(/(\d{3})/g, "$1 ").trim();
20+
}
21+
return str.join(".");
22+
}
23+
24+
exports.commafy = commafy;

dist/cjs/index.d.cts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
declare function commafy(num: number, options?: {
10+
stripDecimals?: boolean;
11+
spacedDecimals?: boolean;
12+
thousandsComma?: boolean;
13+
K?: boolean;
14+
}): string;
15+
16+
export { commafy };

dist/index.cjs

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
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 declare function commafy(num: number, options?: {
10-
stripDecimals?: boolean;
11-
spacedDecimals?: boolean;
12-
thousandsComma?: boolean;
13-
K?: boolean;
14-
}): string;
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+
declare function commafy(num: number, options?: {
10+
stripDecimals?: boolean;
11+
spacedDecimals?: boolean;
12+
thousandsComma?: boolean;
13+
K?: boolean;
14+
}): string;
15+
16+
export { commafy };

dist/index.es.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

dist/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function commafy(num, options) {
2+
const { stripDecimals = false, spacedDecimals = false, thousandsComma = true, K = false } = options || {};
3+
const [wholeNrStr, decimalStr = ""] = num.toString().split(".");
4+
const str = [wholeNrStr, decimalStr];
5+
const minLength = thousandsComma ? 4 : 5;
6+
if (!K && wholeNrStr.length >= minLength) {
7+
str[0] = wholeNrStr.replace(/(\d)(?=(\d{3})+$)/g, "$1,");
8+
}
9+
if (K && wholeNrStr.length > 3) {
10+
const numInK = Math.round(num / 1e3);
11+
const commafied = numInK.toString().replace(/(\d)(?=(\d{3})+$)/g, "$1,");
12+
str[0] = commafied + "K";
13+
}
14+
if (stripDecimals || !decimalStr || K)
15+
return str[0];
16+
if (spacedDecimals && decimalStr.length >= minLength) {
17+
str[1] = decimalStr.replace(/(\d{3})/g, "$1 ").trim();
18+
}
19+
return str.join(".");
20+
}
21+
22+
export { commafy };

0 commit comments

Comments
 (0)