@qrvey/utils 1.10.0-12 → 1.10.0-14
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/dist/cjs/dates/adapters/ISOToNumericOffset.js +4 -1
- package/dist/cjs/dates/adapters/numericOffsetToISO.js +2 -2
- package/dist/cjs/dates/helpers/getUTCFormatByOffset.js +1 -1
- package/dist/cjs/general/mix/isEmpty.js +2 -0
- package/dist/cjs/general/string/index.d.ts +0 -1
- package/dist/cjs/general/string/index.js +0 -1
- package/dist/cjs/general/string/padLeadingZeros.d.ts +16 -3
- package/dist/cjs/general/string/padLeadingZeros.js +33 -6
- package/dist/dates/adapters/ISOToNumericOffset.js +4 -1
- package/dist/dates/adapters/numericOffsetToISO.js +2 -2
- package/dist/dates/helpers/getUTCFormatByOffset.js +1 -1
- package/dist/general/mix/isEmpty.js +2 -0
- package/dist/general/string/index.d.ts +0 -1
- package/dist/general/string/index.js +0 -1
- package/dist/general/string/padLeadingZeros.d.ts +16 -3
- package/dist/general/string/padLeadingZeros.js +33 -6
- package/package.json +4 -3
- package/dist/cjs/general/string/fillLeadingZeros.d.ts +0 -15
- package/dist/cjs/general/string/fillLeadingZeros.js +0 -56
- package/dist/general/string/fillLeadingZeros.d.ts +0 -15
- package/dist/general/string/fillLeadingZeros.js +0 -52
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ISOToNumericOffset = void 0;
|
|
4
4
|
const isNaNV2_1 = require("../../general/mix/isNaNV2");
|
|
5
|
+
const getSign_1 = require("../../general/numeric/getSign");
|
|
5
6
|
const isValidISOOffset_1 = require("../helpers/isValidISOOffset");
|
|
6
7
|
/**
|
|
7
8
|
* Gets the numeric offset (minutes) from the ISO offset (string)
|
|
@@ -16,6 +17,8 @@ function ISOToNumericOffset(offset) {
|
|
|
16
17
|
if (!(0, isValidISOOffset_1.isValidISOOffset)(offset))
|
|
17
18
|
return 0;
|
|
18
19
|
const [hours, minutes] = offset.split(":").map(Number);
|
|
19
|
-
|
|
20
|
+
const sign = (0, getSign_1.getSign)(hours);
|
|
21
|
+
const absoluteOffset = Math.abs(hours) * 60 + minutes;
|
|
22
|
+
return sign === "-" ? -1 * absoluteOffset : absoluteOffset;
|
|
20
23
|
}
|
|
21
24
|
exports.ISOToNumericOffset = ISOToNumericOffset;
|
|
@@ -22,13 +22,13 @@ function numericOffsetToISO(offset, leadZeros = true) {
|
|
|
22
22
|
newOffset = (0, ISOToNumericOffset_1.ISOToNumericOffset)(newOffset);
|
|
23
23
|
}
|
|
24
24
|
if (typeof newOffset !== "number" && (0, isNaNV2_1.isNaNV2)(newOffset))
|
|
25
|
-
return leadZeros ? "+00:00" : "+0
|
|
25
|
+
return leadZeros ? "+00:00" : "+0";
|
|
26
26
|
if (typeof newOffset === "string" && !(0, isNaNV2_1.isNaNV2)(newOffset))
|
|
27
27
|
newOffset = +newOffset;
|
|
28
28
|
const sign = (0, getSign_1.getSign)(newOffset);
|
|
29
29
|
newOffset = Math.abs(newOffset);
|
|
30
30
|
const hours = (0, padLeadingZeros_1.padLeadingZeros)(Math.floor(newOffset / 60), leadZeroNumber);
|
|
31
|
-
const minutes = (0, padLeadingZeros_1.padLeadingZeros)(newOffset % 60,
|
|
31
|
+
const minutes = (0, padLeadingZeros_1.padLeadingZeros)(newOffset % 60, 2);
|
|
32
32
|
return `${sign}${hours}:${minutes}`;
|
|
33
33
|
}
|
|
34
34
|
exports.numericOffsetToISO = numericOffsetToISO;
|
|
@@ -9,7 +9,7 @@ const numericOffsetToISO_1 = require("../adapters/numericOffsetToISO");
|
|
|
9
9
|
*/
|
|
10
10
|
function getUTCFormatByOffset(timezone) {
|
|
11
11
|
let newOffset = (0, numericOffsetToISO_1.numericOffsetToISO)(timezone === null || timezone === void 0 ? void 0 : timezone.offset, false);
|
|
12
|
-
if (newOffset.search(":
|
|
12
|
+
if (newOffset.search(":00") > -1) {
|
|
13
13
|
newOffset = newOffset.slice(0, newOffset.indexOf(":"));
|
|
14
14
|
}
|
|
15
15
|
return `UTC${newOffset}`;
|
|
@@ -26,6 +26,8 @@ function isEmpty(variable, includeFalsy = false) {
|
|
|
26
26
|
(!(variable instanceof Date) &&
|
|
27
27
|
typeof variable !== "function" &&
|
|
28
28
|
(0, isObject_1.isObject)(variable) &&
|
|
29
|
+
Object.entries(variable).length === 0) ||
|
|
30
|
+
((0, getTag_1.getTag)(variable) === "[object NodeList]" &&
|
|
29
31
|
Object.entries(variable).length === 0));
|
|
30
32
|
}
|
|
31
33
|
exports.isEmpty = isEmpty;
|
|
@@ -15,6 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./capitalize"), exports);
|
|
18
|
-
__exportStar(require("./fillLeadingZeros"), exports);
|
|
19
18
|
__exportStar(require("./padLeadingZeros"), exports);
|
|
20
19
|
__exportStar(require("./parseUrl"), exports);
|
|
@@ -3,13 +3,26 @@
|
|
|
3
3
|
* @example
|
|
4
4
|
* num = 100
|
|
5
5
|
* size = 2
|
|
6
|
+
* padding = true
|
|
6
7
|
* => Returns "100"
|
|
7
8
|
* @example
|
|
8
9
|
* num = 100
|
|
9
10
|
* size = 5
|
|
11
|
+
* padding = true
|
|
10
12
|
* => Returns "00100"
|
|
11
|
-
* @
|
|
12
|
-
*
|
|
13
|
+
* @example
|
|
14
|
+
* num = 100
|
|
15
|
+
* size = 2
|
|
16
|
+
* padding = false
|
|
17
|
+
* => Returns "00100"
|
|
18
|
+
* @example
|
|
19
|
+
* num = 100
|
|
20
|
+
* size = 5
|
|
21
|
+
* padding = false
|
|
22
|
+
* => Returns "00000100"
|
|
23
|
+
* @param {number | string} num the number
|
|
24
|
+
* @param {number} size the quantity of zeros to add
|
|
25
|
+
* @param {number} padding True: to pad with missing zeros; depending on the num length. False: to fill zeros by the amount.
|
|
13
26
|
* @returns the string of the number with the prefixed zeros
|
|
14
27
|
*/
|
|
15
|
-
export declare function padLeadingZeros(num: number | string,
|
|
28
|
+
export declare function padLeadingZeros(num: number | string, amount?: number, padding?: boolean): string;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.padLeadingZeros = void 0;
|
|
4
|
+
const mix_1 = require("../mix");
|
|
4
5
|
const isNaNV2_1 = require("../mix/isNaNV2");
|
|
5
6
|
const getSign_1 = require("../numeric/getSign");
|
|
6
7
|
/**
|
|
@@ -8,25 +9,51 @@ const getSign_1 = require("../numeric/getSign");
|
|
|
8
9
|
* @example
|
|
9
10
|
* num = 100
|
|
10
11
|
* size = 2
|
|
12
|
+
* padding = true
|
|
11
13
|
* => Returns "100"
|
|
12
14
|
* @example
|
|
13
15
|
* num = 100
|
|
14
16
|
* size = 5
|
|
17
|
+
* padding = true
|
|
15
18
|
* => Returns "00100"
|
|
16
|
-
* @
|
|
17
|
-
*
|
|
19
|
+
* @example
|
|
20
|
+
* num = 100
|
|
21
|
+
* size = 2
|
|
22
|
+
* padding = false
|
|
23
|
+
* => Returns "00100"
|
|
24
|
+
* @example
|
|
25
|
+
* num = 100
|
|
26
|
+
* size = 5
|
|
27
|
+
* padding = false
|
|
28
|
+
* => Returns "00000100"
|
|
29
|
+
* @param {number | string} num the number
|
|
30
|
+
* @param {number} size the quantity of zeros to add
|
|
31
|
+
* @param {number} padding True: to pad with missing zeros; depending on the num length. False: to fill zeros by the amount.
|
|
18
32
|
* @returns the string of the number with the prefixed zeros
|
|
19
33
|
*/
|
|
20
|
-
function padLeadingZeros(num,
|
|
21
|
-
if ((
|
|
34
|
+
function padLeadingZeros(num, amount = 0, padding = true) {
|
|
35
|
+
if (isValidLeadingZerosParams(num, amount))
|
|
22
36
|
return num;
|
|
37
|
+
const newPadding = !(0, mix_1.isEmpty)(padding, true);
|
|
23
38
|
const sign = getNumSign(num);
|
|
24
39
|
let absoluteStringNumber = getAbsoluteNum(num, sign);
|
|
25
|
-
|
|
26
|
-
absoluteStringNumber
|
|
40
|
+
if (newPadding) {
|
|
41
|
+
while (absoluteStringNumber.length < amount)
|
|
42
|
+
absoluteStringNumber = `0${absoluteStringNumber}`;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
let i = 0;
|
|
46
|
+
while (i < amount) {
|
|
47
|
+
absoluteStringNumber = `0${absoluteStringNumber}`;
|
|
48
|
+
i = i + 1;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
27
51
|
return `${sign}${absoluteStringNumber}`;
|
|
28
52
|
}
|
|
29
53
|
exports.padLeadingZeros = padLeadingZeros;
|
|
54
|
+
function isValidLeadingZerosParams(num, amount) {
|
|
55
|
+
return (0, isNaNV2_1.isNaNV2)(num) || (0, isNaNV2_1.isNaNV2)(amount);
|
|
56
|
+
}
|
|
30
57
|
/**
|
|
31
58
|
* Gets the absolute number of the giving parameters
|
|
32
59
|
* @param {number | string} num the number
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isNaNV2 } from "../../general/mix/isNaNV2";
|
|
2
|
+
import { getSign } from "../../general/numeric/getSign";
|
|
2
3
|
import { isValidISOOffset } from "../helpers/isValidISOOffset";
|
|
3
4
|
/**
|
|
4
5
|
* Gets the numeric offset (minutes) from the ISO offset (string)
|
|
@@ -13,5 +14,7 @@ export function ISOToNumericOffset(offset) {
|
|
|
13
14
|
if (!isValidISOOffset(offset))
|
|
14
15
|
return 0;
|
|
15
16
|
const [hours, minutes] = offset.split(":").map(Number);
|
|
16
|
-
|
|
17
|
+
const sign = getSign(hours);
|
|
18
|
+
const absoluteOffset = Math.abs(hours) * 60 + minutes;
|
|
19
|
+
return sign === "-" ? -1 * absoluteOffset : absoluteOffset;
|
|
17
20
|
}
|
|
@@ -19,12 +19,12 @@ export function numericOffsetToISO(offset, leadZeros = true) {
|
|
|
19
19
|
newOffset = ISOToNumericOffset(newOffset);
|
|
20
20
|
}
|
|
21
21
|
if (typeof newOffset !== "number" && isNaNV2(newOffset))
|
|
22
|
-
return leadZeros ? "+00:00" : "+0
|
|
22
|
+
return leadZeros ? "+00:00" : "+0";
|
|
23
23
|
if (typeof newOffset === "string" && !isNaNV2(newOffset))
|
|
24
24
|
newOffset = +newOffset;
|
|
25
25
|
const sign = getSign(newOffset);
|
|
26
26
|
newOffset = Math.abs(newOffset);
|
|
27
27
|
const hours = padLeadingZeros(Math.floor(newOffset / 60), leadZeroNumber);
|
|
28
|
-
const minutes = padLeadingZeros(newOffset % 60,
|
|
28
|
+
const minutes = padLeadingZeros(newOffset % 60, 2);
|
|
29
29
|
return `${sign}${hours}:${minutes}`;
|
|
30
30
|
}
|
|
@@ -6,7 +6,7 @@ import { numericOffsetToISO } from "../adapters/numericOffsetToISO";
|
|
|
6
6
|
*/
|
|
7
7
|
export function getUTCFormatByOffset(timezone) {
|
|
8
8
|
let newOffset = numericOffsetToISO(timezone === null || timezone === void 0 ? void 0 : timezone.offset, false);
|
|
9
|
-
if (newOffset.search(":
|
|
9
|
+
if (newOffset.search(":00") > -1) {
|
|
10
10
|
newOffset = newOffset.slice(0, newOffset.indexOf(":"));
|
|
11
11
|
}
|
|
12
12
|
return `UTC${newOffset}`;
|
|
@@ -23,5 +23,7 @@ export function isEmpty(variable, includeFalsy = false) {
|
|
|
23
23
|
(!(variable instanceof Date) &&
|
|
24
24
|
typeof variable !== "function" &&
|
|
25
25
|
isObject(variable) &&
|
|
26
|
+
Object.entries(variable).length === 0) ||
|
|
27
|
+
(getTag(variable) === "[object NodeList]" &&
|
|
26
28
|
Object.entries(variable).length === 0));
|
|
27
29
|
}
|
|
@@ -3,13 +3,26 @@
|
|
|
3
3
|
* @example
|
|
4
4
|
* num = 100
|
|
5
5
|
* size = 2
|
|
6
|
+
* padding = true
|
|
6
7
|
* => Returns "100"
|
|
7
8
|
* @example
|
|
8
9
|
* num = 100
|
|
9
10
|
* size = 5
|
|
11
|
+
* padding = true
|
|
10
12
|
* => Returns "00100"
|
|
11
|
-
* @
|
|
12
|
-
*
|
|
13
|
+
* @example
|
|
14
|
+
* num = 100
|
|
15
|
+
* size = 2
|
|
16
|
+
* padding = false
|
|
17
|
+
* => Returns "00100"
|
|
18
|
+
* @example
|
|
19
|
+
* num = 100
|
|
20
|
+
* size = 5
|
|
21
|
+
* padding = false
|
|
22
|
+
* => Returns "00000100"
|
|
23
|
+
* @param {number | string} num the number
|
|
24
|
+
* @param {number} size the quantity of zeros to add
|
|
25
|
+
* @param {number} padding True: to pad with missing zeros; depending on the num length. False: to fill zeros by the amount.
|
|
13
26
|
* @returns the string of the number with the prefixed zeros
|
|
14
27
|
*/
|
|
15
|
-
export declare function padLeadingZeros(num: number | string,
|
|
28
|
+
export declare function padLeadingZeros(num: number | string, amount?: number, padding?: boolean): string;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isEmpty } from "../mix";
|
|
1
2
|
import { isNaNV2 } from "../mix/isNaNV2";
|
|
2
3
|
import { getSign } from "../numeric/getSign";
|
|
3
4
|
/**
|
|
@@ -5,24 +6,50 @@ import { getSign } from "../numeric/getSign";
|
|
|
5
6
|
* @example
|
|
6
7
|
* num = 100
|
|
7
8
|
* size = 2
|
|
9
|
+
* padding = true
|
|
8
10
|
* => Returns "100"
|
|
9
11
|
* @example
|
|
10
12
|
* num = 100
|
|
11
13
|
* size = 5
|
|
14
|
+
* padding = true
|
|
12
15
|
* => Returns "00100"
|
|
13
|
-
* @
|
|
14
|
-
*
|
|
16
|
+
* @example
|
|
17
|
+
* num = 100
|
|
18
|
+
* size = 2
|
|
19
|
+
* padding = false
|
|
20
|
+
* => Returns "00100"
|
|
21
|
+
* @example
|
|
22
|
+
* num = 100
|
|
23
|
+
* size = 5
|
|
24
|
+
* padding = false
|
|
25
|
+
* => Returns "00000100"
|
|
26
|
+
* @param {number | string} num the number
|
|
27
|
+
* @param {number} size the quantity of zeros to add
|
|
28
|
+
* @param {number} padding True: to pad with missing zeros; depending on the num length. False: to fill zeros by the amount.
|
|
15
29
|
* @returns the string of the number with the prefixed zeros
|
|
16
30
|
*/
|
|
17
|
-
export function padLeadingZeros(num,
|
|
18
|
-
if (
|
|
31
|
+
export function padLeadingZeros(num, amount = 0, padding = true) {
|
|
32
|
+
if (isValidLeadingZerosParams(num, amount))
|
|
19
33
|
return num;
|
|
34
|
+
const newPadding = !isEmpty(padding, true);
|
|
20
35
|
const sign = getNumSign(num);
|
|
21
36
|
let absoluteStringNumber = getAbsoluteNum(num, sign);
|
|
22
|
-
|
|
23
|
-
absoluteStringNumber
|
|
37
|
+
if (newPadding) {
|
|
38
|
+
while (absoluteStringNumber.length < amount)
|
|
39
|
+
absoluteStringNumber = `0${absoluteStringNumber}`;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
let i = 0;
|
|
43
|
+
while (i < amount) {
|
|
44
|
+
absoluteStringNumber = `0${absoluteStringNumber}`;
|
|
45
|
+
i = i + 1;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
24
48
|
return `${sign}${absoluteStringNumber}`;
|
|
25
49
|
}
|
|
50
|
+
function isValidLeadingZerosParams(num, amount) {
|
|
51
|
+
return isNaNV2(num) || isNaNV2(amount);
|
|
52
|
+
}
|
|
26
53
|
/**
|
|
27
54
|
* Gets the absolute number of the giving parameters
|
|
28
55
|
* @param {number | string} num the number
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qrvey/utils",
|
|
3
|
-
"version": "1.10.0-
|
|
3
|
+
"version": "1.10.0-14",
|
|
4
4
|
"description": "Helper, Utils for all Qrvey Projects",
|
|
5
5
|
"homepage": "https://bitbucket.org/qrvey/qrvey_utils/wiki/Home",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -15,8 +15,9 @@
|
|
|
15
15
|
"publishing.beta.1": "node ./scripts/publishing --np-new-version=$npm_config_np_new_version --np-any-branch=$npm_config_np_any_branch --np-tag=$npm_config_np_tag",
|
|
16
16
|
"publishing": "node ./scripts/clean-build && np $npm_config_np_new_version --any-branch --tag=$npm_config_np_tag",
|
|
17
17
|
"publishing.win": "node ./scripts/clean-build && np %npm_config_np_new_version% --any-branch --tag=%npm_config_np_tag%",
|
|
18
|
-
"test": "jest test",
|
|
19
|
-
"test.
|
|
18
|
+
"test": "jest test --verbose",
|
|
19
|
+
"test.coverage": "jest test --collect-coverage",
|
|
20
|
+
"test.watch": "jest test --watch ",
|
|
20
21
|
"version": "auto-changelog -p && git add CHANGELOG.md"
|
|
21
22
|
},
|
|
22
23
|
"author": "Qrvey Inc",
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sets an amount of zeros as prefix of the given number
|
|
3
|
-
* @example
|
|
4
|
-
* num = 100
|
|
5
|
-
* size = 2
|
|
6
|
-
* => Returns "00100"
|
|
7
|
-
* @example
|
|
8
|
-
* num = 100
|
|
9
|
-
* size = 5
|
|
10
|
-
* => Returns "00000100"
|
|
11
|
-
* @param {number | string} num the number
|
|
12
|
-
* @param {number} amount the quantity of zeros to add
|
|
13
|
-
* @returns the string of the number with the prefixed zeros
|
|
14
|
-
*/
|
|
15
|
-
export declare function fillLeadingZeros(num: number | string, amount?: number): string;
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fillLeadingZeros = void 0;
|
|
4
|
-
const isNaNV2_1 = require("../mix/isNaNV2");
|
|
5
|
-
const getSign_1 = require("../numeric/getSign");
|
|
6
|
-
/**
|
|
7
|
-
* Sets an amount of zeros as prefix of the given number
|
|
8
|
-
* @example
|
|
9
|
-
* num = 100
|
|
10
|
-
* size = 2
|
|
11
|
-
* => Returns "00100"
|
|
12
|
-
* @example
|
|
13
|
-
* num = 100
|
|
14
|
-
* size = 5
|
|
15
|
-
* => Returns "00000100"
|
|
16
|
-
* @param {number | string} num the number
|
|
17
|
-
* @param {number} amount the quantity of zeros to add
|
|
18
|
-
* @returns the string of the number with the prefixed zeros
|
|
19
|
-
*/
|
|
20
|
-
function fillLeadingZeros(num, amount = 0) {
|
|
21
|
-
if ((0, isNaNV2_1.isNaNV2)(num) || (0, isNaNV2_1.isNaNV2)(amount))
|
|
22
|
-
return num;
|
|
23
|
-
const sign = getNumSign(num);
|
|
24
|
-
let absoluteStringNumber = getAbsoluteNum(num, sign);
|
|
25
|
-
let i = 0;
|
|
26
|
-
while (i < amount) {
|
|
27
|
-
absoluteStringNumber = `0${absoluteStringNumber}`;
|
|
28
|
-
i = i + 1;
|
|
29
|
-
}
|
|
30
|
-
return `${sign}${absoluteStringNumber}`;
|
|
31
|
-
}
|
|
32
|
-
exports.fillLeadingZeros = fillLeadingZeros;
|
|
33
|
-
/**
|
|
34
|
-
* Gets the absolute number of the giving parameters
|
|
35
|
-
* @param {number | string} num the number
|
|
36
|
-
* @param {string} sign the sign of the number
|
|
37
|
-
* @returns {string} the absolute number
|
|
38
|
-
*/
|
|
39
|
-
function getAbsoluteNum(num, sign = "") {
|
|
40
|
-
const stringNum = `${num}`;
|
|
41
|
-
return sign !== "" && `${stringNum}`.charAt(0) === sign
|
|
42
|
-
? stringNum.substring(1)
|
|
43
|
-
: stringNum;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Gets the number sign
|
|
47
|
-
* @param {number | string} num the number
|
|
48
|
-
* @returns {string} the number sign
|
|
49
|
-
*/
|
|
50
|
-
function getNumSign(num) {
|
|
51
|
-
const stringNum = `${num}`;
|
|
52
|
-
const sign = (0, getSign_1.getSign)(num);
|
|
53
|
-
if (stringNum.charAt(0) === sign)
|
|
54
|
-
return sign;
|
|
55
|
-
return "";
|
|
56
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sets an amount of zeros as prefix of the given number
|
|
3
|
-
* @example
|
|
4
|
-
* num = 100
|
|
5
|
-
* size = 2
|
|
6
|
-
* => Returns "00100"
|
|
7
|
-
* @example
|
|
8
|
-
* num = 100
|
|
9
|
-
* size = 5
|
|
10
|
-
* => Returns "00000100"
|
|
11
|
-
* @param {number | string} num the number
|
|
12
|
-
* @param {number} amount the quantity of zeros to add
|
|
13
|
-
* @returns the string of the number with the prefixed zeros
|
|
14
|
-
*/
|
|
15
|
-
export declare function fillLeadingZeros(num: number | string, amount?: number): string;
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { isNaNV2 } from "../mix/isNaNV2";
|
|
2
|
-
import { getSign } from "../numeric/getSign";
|
|
3
|
-
/**
|
|
4
|
-
* Sets an amount of zeros as prefix of the given number
|
|
5
|
-
* @example
|
|
6
|
-
* num = 100
|
|
7
|
-
* size = 2
|
|
8
|
-
* => Returns "00100"
|
|
9
|
-
* @example
|
|
10
|
-
* num = 100
|
|
11
|
-
* size = 5
|
|
12
|
-
* => Returns "00000100"
|
|
13
|
-
* @param {number | string} num the number
|
|
14
|
-
* @param {number} amount the quantity of zeros to add
|
|
15
|
-
* @returns the string of the number with the prefixed zeros
|
|
16
|
-
*/
|
|
17
|
-
export function fillLeadingZeros(num, amount = 0) {
|
|
18
|
-
if (isNaNV2(num) || isNaNV2(amount))
|
|
19
|
-
return num;
|
|
20
|
-
const sign = getNumSign(num);
|
|
21
|
-
let absoluteStringNumber = getAbsoluteNum(num, sign);
|
|
22
|
-
let i = 0;
|
|
23
|
-
while (i < amount) {
|
|
24
|
-
absoluteStringNumber = `0${absoluteStringNumber}`;
|
|
25
|
-
i = i + 1;
|
|
26
|
-
}
|
|
27
|
-
return `${sign}${absoluteStringNumber}`;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Gets the absolute number of the giving parameters
|
|
31
|
-
* @param {number | string} num the number
|
|
32
|
-
* @param {string} sign the sign of the number
|
|
33
|
-
* @returns {string} the absolute number
|
|
34
|
-
*/
|
|
35
|
-
function getAbsoluteNum(num, sign = "") {
|
|
36
|
-
const stringNum = `${num}`;
|
|
37
|
-
return sign !== "" && `${stringNum}`.charAt(0) === sign
|
|
38
|
-
? stringNum.substring(1)
|
|
39
|
-
: stringNum;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Gets the number sign
|
|
43
|
-
* @param {number | string} num the number
|
|
44
|
-
* @returns {string} the number sign
|
|
45
|
-
*/
|
|
46
|
-
function getNumSign(num) {
|
|
47
|
-
const stringNum = `${num}`;
|
|
48
|
-
const sign = getSign(num);
|
|
49
|
-
if (stringNum.charAt(0) === sign)
|
|
50
|
-
return sign;
|
|
51
|
-
return "";
|
|
52
|
-
}
|