stringzy 3.0.0 → 4.0.0
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/.prettierignore +4 -0
- package/.prettierrc +7 -0
- package/CONTRIBUTING.md +41 -29
- package/README.md +397 -160
- package/dist/analyzing/characterCount.d.ts +19 -0
- package/dist/analyzing/characterCount.js +21 -2
- package/dist/analyzing/characterFrequency.d.ts +19 -0
- package/dist/analyzing/characterFrequency.js +22 -3
- package/dist/analyzing/complexity.d.ts +33 -0
- package/dist/analyzing/complexity.js +35 -2
- package/dist/analyzing/index.d.ts +18 -12
- package/dist/analyzing/index.js +10 -2
- package/dist/analyzing/patternCount.d.ts +10 -0
- package/dist/analyzing/patternCount.js +52 -0
- package/dist/analyzing/stringSimilarity.js +1 -1
- package/dist/analyzing/vowelConsonantCount.d.ts +22 -0
- package/dist/analyzing/vowelConsonantCount.js +38 -0
- package/dist/analyzing/wordCount.d.ts +22 -0
- package/dist/analyzing/wordCount.js +24 -2
- package/dist/formatting/capitalize.d.ts +21 -0
- package/dist/formatting/capitalize.js +22 -1
- package/dist/formatting/index.d.ts +6 -6
- package/dist/formatting/number.d.ts +23 -0
- package/dist/formatting/number.js +24 -1
- package/dist/formatting/phone.d.ts +23 -0
- package/dist/formatting/phone.js +23 -0
- package/dist/index.d.ts +9 -4
- package/dist/tests/analyzing/patternCount.test.d.ts +1 -0
- package/dist/tests/analyzing/patternCount.test.js +34 -0
- package/dist/tests/analyzing/readingDuration.test.js +12 -12
- package/dist/tests/analyzing/vowelConsonantCount.test.d.ts +1 -0
- package/dist/tests/analyzing/vowelConsonantCount.test.js +25 -0
- package/dist/tests/transformations/numberToText.test.d.ts +1 -0
- package/dist/tests/transformations/numberToText.test.js +60 -0
- package/dist/tests/transformations/splitChunks.test.d.ts +1 -0
- package/dist/tests/transformations/splitChunks.test.js +31 -0
- package/dist/tests/validations/isCoordinates.test.d.ts +1 -0
- package/dist/tests/validations/isCoordinates.test.js +18 -0
- package/dist/tests/validations/isEmail.smtpUTF8.test.d.ts +1 -0
- package/dist/tests/validations/isEmail.smtpUTF8.test.js +16 -0
- package/dist/tests/validations/isEmail.test.js +56 -6
- package/dist/tests/validations/isHexColor.test.js +21 -21
- package/dist/tests/validations/isPalindrome.test.d.ts +1 -0
- package/dist/tests/validations/isPalindrome.test.js +39 -0
- package/dist/tests/validations/isTypeOf.test.d.ts +1 -0
- package/dist/tests/validations/isTypeOf.test.js +28 -0
- package/dist/transformations/camelCase.d.ts +24 -0
- package/dist/transformations/camelCase.js +24 -0
- package/dist/transformations/capitalizeWords.d.ts +21 -0
- package/dist/transformations/capitalizeWords.js +23 -2
- package/dist/transformations/constantCase.d.ts +26 -0
- package/dist/transformations/constantCase.js +26 -0
- package/dist/transformations/escapeHTML.d.ts +23 -0
- package/dist/transformations/escapeHTML.js +24 -2
- package/dist/transformations/index.d.ts +3 -0
- package/dist/transformations/index.js +6 -2
- package/dist/transformations/initials.d.ts +27 -0
- package/dist/transformations/initials.js +38 -8
- package/dist/transformations/kebabCase.d.ts +26 -0
- package/dist/transformations/kebabCase.js +26 -0
- package/dist/transformations/maskSegment.js +4 -6
- package/dist/transformations/numberToText/helpers.d.ts +10 -0
- package/dist/transformations/numberToText/helpers.js +31 -0
- package/dist/transformations/numberToText/implementation_EN.d.ts +10 -0
- package/dist/transformations/numberToText/implementation_EN.js +45 -0
- package/dist/transformations/numberToText/implementation_PL.d.ts +10 -0
- package/dist/transformations/numberToText/implementation_PL.js +79 -0
- package/dist/transformations/numberToText/main.d.ts +19 -0
- package/dist/transformations/numberToText/main.js +67 -0
- package/dist/transformations/numberToText/types.d.ts +3 -0
- package/dist/transformations/numberToText/types.js +82 -0
- package/dist/transformations/pascalCase.d.ts +25 -0
- package/dist/transformations/pascalCase.js +25 -0
- package/dist/transformations/removeDuplicates.d.ts +21 -0
- package/dist/transformations/removeDuplicates.js +25 -4
- package/dist/transformations/removeSpecialChars.d.ts +22 -0
- package/dist/transformations/removeSpecialChars.js +26 -4
- package/dist/transformations/removeWords.d.ts +27 -0
- package/dist/transformations/removeWords.js +31 -4
- package/dist/transformations/snakeCase.d.ts +26 -0
- package/dist/transformations/snakeCase.js +26 -0
- package/dist/transformations/splitChunks.d.ts +8 -0
- package/dist/transformations/splitChunks.js +24 -0
- package/dist/transformations/titleCase.d.ts +25 -0
- package/dist/transformations/titleCase.js +25 -0
- package/dist/transformations/toSlug.d.ts +24 -0
- package/dist/transformations/toSlug.js +28 -4
- package/dist/transformations/truncateText.d.ts +25 -0
- package/dist/transformations/truncateText.js +28 -3
- package/dist/validations/index.d.ts +6 -0
- package/dist/validations/index.js +10 -2
- package/dist/validations/isCoordinates.d.ts +8 -0
- package/dist/validations/isCoordinates.js +19 -0
- package/dist/validations/isDate.d.ts +1 -1
- package/dist/validations/isDate.js +6 -8
- package/dist/validations/isEmail.d.ts +13 -1
- package/dist/validations/isEmail.js +176 -3
- package/dist/validations/isEmpty.d.ts +9 -0
- package/dist/validations/isEmpty.js +9 -0
- package/dist/validations/isHexColor.js +1 -1
- package/dist/validations/isIPv4.d.ts +21 -0
- package/dist/validations/isIPv4.js +22 -2
- package/dist/validations/isPalindrome.d.ts +10 -0
- package/dist/validations/isPalindrome.js +21 -0
- package/dist/validations/isSlug.d.ts +27 -0
- package/dist/validations/isSlug.js +27 -0
- package/dist/validations/isTypeOf.d.ts +9 -0
- package/dist/validations/isTypeOf.js +30 -0
- package/dist/validations/isURL.d.ts +21 -0
- package/dist/validations/isURL.js +21 -0
- package/package.json +5 -3
|
@@ -1 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a given string to CONSTANT_CASE format.
|
|
3
|
+
*
|
|
4
|
+
* The transformation includes:
|
|
5
|
+
* - Trimming whitespace from both ends
|
|
6
|
+
* - Replacing spaces and hyphens with underscores
|
|
7
|
+
* - Inserting underscores between lowercase and uppercase letter boundaries (e.g., `fooBar` → `FOO_BAR`)
|
|
8
|
+
* - Replacing all non-word characters with underscores
|
|
9
|
+
* - Converting the entire string to uppercase
|
|
10
|
+
* - Collapsing multiple underscores into one
|
|
11
|
+
* - Removing leading and trailing underscores
|
|
12
|
+
*
|
|
13
|
+
* If the input is `null` or `undefined`, it returns an empty string.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} text - The input string to convert.
|
|
16
|
+
* @returns {string} The transformed CONSTANT_CASE string.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* constantCase("hello world"); // "HELLO_WORLD"
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* constantCase("camelCaseText"); // "CAMEL_CASE_TEXT"
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* constantCase(" convert-to_CONSTANT.case! "); // "CONVERT_TO_CONSTANT_CASE"
|
|
26
|
+
*/
|
|
1
27
|
export declare function constantCase(text: string): string;
|
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.constantCase = constantCase;
|
|
4
|
+
/**
|
|
5
|
+
* Converts a given string to CONSTANT_CASE format.
|
|
6
|
+
*
|
|
7
|
+
* The transformation includes:
|
|
8
|
+
* - Trimming whitespace from both ends
|
|
9
|
+
* - Replacing spaces and hyphens with underscores
|
|
10
|
+
* - Inserting underscores between lowercase and uppercase letter boundaries (e.g., `fooBar` → `FOO_BAR`)
|
|
11
|
+
* - Replacing all non-word characters with underscores
|
|
12
|
+
* - Converting the entire string to uppercase
|
|
13
|
+
* - Collapsing multiple underscores into one
|
|
14
|
+
* - Removing leading and trailing underscores
|
|
15
|
+
*
|
|
16
|
+
* If the input is `null` or `undefined`, it returns an empty string.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} text - The input string to convert.
|
|
19
|
+
* @returns {string} The transformed CONSTANT_CASE string.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* constantCase("hello world"); // "HELLO_WORLD"
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* constantCase("camelCaseText"); // "CAMEL_CASE_TEXT"
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* constantCase(" convert-to_CONSTANT.case! "); // "CONVERT_TO_CONSTANT_CASE"
|
|
29
|
+
*/
|
|
4
30
|
function constantCase(text) {
|
|
5
31
|
if (text == null)
|
|
6
32
|
return '';
|
|
@@ -1 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes special HTML characters in a string to their corresponding HTML entities.
|
|
3
|
+
*
|
|
4
|
+
* This function replaces the following characters:
|
|
5
|
+
* - `&` with `&`
|
|
6
|
+
* - `<` with `<`
|
|
7
|
+
* - `>` with `>`
|
|
8
|
+
* - `"` with `"`
|
|
9
|
+
* - `'` with `'`
|
|
10
|
+
*
|
|
11
|
+
* This is useful to prevent HTML injection or XSS attacks when inserting user input into HTML.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} str - The string to escape.
|
|
14
|
+
* @returns {string} The escaped string with HTML entities.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* escapeHtml('<div class="test">Hello & Welcome</div>');
|
|
18
|
+
* // "<div class="test">Hello & Welcome</div>"
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* escapeHtml("It's a test!");
|
|
22
|
+
* // "It's a test!"
|
|
23
|
+
*/
|
|
1
24
|
export declare function escapeHtml(str: string): string;
|
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.escapeHtml = escapeHtml;
|
|
4
|
+
/**
|
|
5
|
+
* Escapes special HTML characters in a string to their corresponding HTML entities.
|
|
6
|
+
*
|
|
7
|
+
* This function replaces the following characters:
|
|
8
|
+
* - `&` with `&`
|
|
9
|
+
* - `<` with `<`
|
|
10
|
+
* - `>` with `>`
|
|
11
|
+
* - `"` with `"`
|
|
12
|
+
* - `'` with `'`
|
|
13
|
+
*
|
|
14
|
+
* This is useful to prevent HTML injection or XSS attacks when inserting user input into HTML.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} str - The string to escape.
|
|
17
|
+
* @returns {string} The escaped string with HTML entities.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* escapeHtml('<div class="test">Hello & Welcome</div>');
|
|
21
|
+
* // "<div class="test">Hello & Welcome</div>"
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* escapeHtml("It's a test!");
|
|
25
|
+
* // "It's a test!"
|
|
26
|
+
*/
|
|
4
27
|
function escapeHtml(str) {
|
|
5
28
|
const htmlEscapes = {
|
|
6
29
|
'&': '&',
|
|
7
30
|
'<': '<',
|
|
8
31
|
'>': '>',
|
|
9
32
|
'"': '"',
|
|
10
|
-
"'": '''
|
|
33
|
+
"'": ''',
|
|
11
34
|
};
|
|
12
35
|
return str.replace(/[&<>"']/g, (match) => htmlEscapes[match]);
|
|
13
36
|
}
|
|
14
|
-
;
|
|
@@ -13,6 +13,7 @@ export { toSlug } from './toSlug';
|
|
|
13
13
|
export { truncateText } from './truncateText';
|
|
14
14
|
export { escapeHtml } from './escapeHTML';
|
|
15
15
|
export { maskSegment } from './maskSegment';
|
|
16
|
+
export { numberToText } from './numberToText/main';
|
|
16
17
|
import { camelCase } from './camelCase';
|
|
17
18
|
import { capitalizeWords } from './capitalizeWords';
|
|
18
19
|
import { constantCase } from './constantCase';
|
|
@@ -29,6 +30,7 @@ import { truncateText } from './truncateText';
|
|
|
29
30
|
import { escapeHtml } from './escapeHTML';
|
|
30
31
|
import { maskSegment } from './maskSegment';
|
|
31
32
|
import { deburr } from './deburr';
|
|
33
|
+
import { numberToText } from './numberToText/main';
|
|
32
34
|
export declare const transformations: {
|
|
33
35
|
camelCase: typeof camelCase;
|
|
34
36
|
capitalizeWords: typeof capitalizeWords;
|
|
@@ -46,4 +48,5 @@ export declare const transformations: {
|
|
|
46
48
|
escapeHtml: typeof escapeHtml;
|
|
47
49
|
maskSegment: typeof maskSegment;
|
|
48
50
|
deburr: typeof deburr;
|
|
51
|
+
numberToText: typeof numberToText;
|
|
49
52
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformations = exports.maskSegment = exports.escapeHtml = exports.truncateText = exports.toSlug = exports.titleCase = exports.snakeCase = exports.removeWords = exports.removeSpecialChars = exports.removeDuplicates = exports.pascalCase = exports.kebabCase = exports.initials = exports.constantCase = exports.capitalizeWords = exports.camelCase = void 0;
|
|
3
|
+
exports.transformations = exports.numberToText = exports.maskSegment = exports.escapeHtml = exports.truncateText = exports.toSlug = exports.titleCase = exports.snakeCase = exports.removeWords = exports.removeSpecialChars = exports.removeDuplicates = exports.pascalCase = exports.kebabCase = exports.initials = exports.constantCase = exports.capitalizeWords = exports.camelCase = void 0;
|
|
4
4
|
var camelCase_1 = require("./camelCase");
|
|
5
5
|
Object.defineProperty(exports, "camelCase", { enumerable: true, get: function () { return camelCase_1.camelCase; } });
|
|
6
6
|
var capitalizeWords_1 = require("./capitalizeWords");
|
|
@@ -31,6 +31,8 @@ var escapeHTML_1 = require("./escapeHTML");
|
|
|
31
31
|
Object.defineProperty(exports, "escapeHtml", { enumerable: true, get: function () { return escapeHTML_1.escapeHtml; } });
|
|
32
32
|
var maskSegment_1 = require("./maskSegment");
|
|
33
33
|
Object.defineProperty(exports, "maskSegment", { enumerable: true, get: function () { return maskSegment_1.maskSegment; } });
|
|
34
|
+
var main_1 = require("./numberToText/main");
|
|
35
|
+
Object.defineProperty(exports, "numberToText", { enumerable: true, get: function () { return main_1.numberToText; } });
|
|
34
36
|
const camelCase_2 = require("./camelCase");
|
|
35
37
|
const capitalizeWords_2 = require("./capitalizeWords");
|
|
36
38
|
const constantCase_2 = require("./constantCase");
|
|
@@ -47,6 +49,7 @@ const truncateText_2 = require("./truncateText");
|
|
|
47
49
|
const escapeHTML_2 = require("./escapeHTML");
|
|
48
50
|
const maskSegment_2 = require("./maskSegment");
|
|
49
51
|
const deburr_1 = require("./deburr");
|
|
52
|
+
const main_2 = require("./numberToText/main");
|
|
50
53
|
exports.transformations = {
|
|
51
54
|
camelCase: camelCase_2.camelCase,
|
|
52
55
|
capitalizeWords: capitalizeWords_2.capitalizeWords,
|
|
@@ -63,5 +66,6 @@ exports.transformations = {
|
|
|
63
66
|
truncateText: truncateText_2.truncateText,
|
|
64
67
|
escapeHtml: escapeHTML_2.escapeHtml,
|
|
65
68
|
maskSegment: maskSegment_2.maskSegment,
|
|
66
|
-
deburr: deburr_1.deburr
|
|
69
|
+
deburr: deburr_1.deburr,
|
|
70
|
+
numberToText: main_2.numberToText
|
|
67
71
|
};
|
|
@@ -1 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the initials from a given string.
|
|
3
|
+
*
|
|
4
|
+
* Splits the input text by whitespace, takes the first character of each word,
|
|
5
|
+
* and joins them together. Optionally limits the number of initials returned.
|
|
6
|
+
*
|
|
7
|
+
* Throws an error if the input is not a string or if the `limit` parameter
|
|
8
|
+
* is provided but not a non-negative number.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} text - The input string from which to extract initials.
|
|
11
|
+
* @param {number} [limit] - Optional maximum number of initials to return.
|
|
12
|
+
* @returns {string} A string containing the initials.
|
|
13
|
+
* @throws {TypeError} If `text` is not a string.
|
|
14
|
+
* @throws {Error} If `limit` is provided and is not a non-negative number.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* initials("John Doe"); // "JD"
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* initials("John Ronald Reuel Tolkien", 2); // "JR"
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* initials(" singleWord "); // "s"
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* initials(""); // ""
|
|
27
|
+
*/
|
|
1
28
|
export declare function initials(text: string, limit?: number): string;
|
|
@@ -1,19 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.initials = initials;
|
|
4
|
+
/**
|
|
5
|
+
* Extracts the initials from a given string.
|
|
6
|
+
*
|
|
7
|
+
* Splits the input text by whitespace, takes the first character of each word,
|
|
8
|
+
* and joins them together. Optionally limits the number of initials returned.
|
|
9
|
+
*
|
|
10
|
+
* Throws an error if the input is not a string or if the `limit` parameter
|
|
11
|
+
* is provided but not a non-negative number.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} text - The input string from which to extract initials.
|
|
14
|
+
* @param {number} [limit] - Optional maximum number of initials to return.
|
|
15
|
+
* @returns {string} A string containing the initials.
|
|
16
|
+
* @throws {TypeError} If `text` is not a string.
|
|
17
|
+
* @throws {Error} If `limit` is provided and is not a non-negative number.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* initials("John Doe"); // "JD"
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* initials("John Ronald Reuel Tolkien", 2); // "JR"
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* initials(" singleWord "); // "s"
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* initials(""); // ""
|
|
30
|
+
*/
|
|
4
31
|
function initials(text, limit) {
|
|
5
|
-
if (typeof text !==
|
|
6
|
-
throw new
|
|
32
|
+
if (typeof text !== 'string') {
|
|
33
|
+
throw new TypeError('Input must be a string');
|
|
7
34
|
}
|
|
8
|
-
if (limit !== undefined && (typeof limit !==
|
|
9
|
-
throw new Error(
|
|
35
|
+
if (limit !== undefined && (typeof limit !== 'number' || isNaN(limit))) {
|
|
36
|
+
throw new Error('Limit must be a valid number');
|
|
10
37
|
}
|
|
11
38
|
if (limit !== undefined && limit < 0) {
|
|
12
|
-
throw new Error(
|
|
39
|
+
throw new Error('Limit must be a non-negative number');
|
|
13
40
|
}
|
|
14
|
-
const words = text
|
|
41
|
+
const words = text
|
|
42
|
+
.trim()
|
|
43
|
+
.split(/\s+/)
|
|
44
|
+
.filter((word) => word.length > 0);
|
|
15
45
|
if (words.length === 0)
|
|
16
|
-
return
|
|
46
|
+
return '';
|
|
17
47
|
const initialsArray = words.map((word) => word.charAt(0)).slice(0, limit !== null && limit !== void 0 ? limit : words.length);
|
|
18
|
-
return initialsArray.join(
|
|
48
|
+
return initialsArray.join('');
|
|
19
49
|
}
|
|
@@ -1 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a given string to kebab-case format.
|
|
3
|
+
*
|
|
4
|
+
* The conversion process includes:
|
|
5
|
+
* - Trimming whitespace from both ends.
|
|
6
|
+
* - Replacing spaces and underscores with hyphens.
|
|
7
|
+
* - Inserting hyphens between lowercase and uppercase letter boundaries (e.g., `fooBar` → `foo-bar`).
|
|
8
|
+
* - Replacing all non-word characters (except hyphens) with hyphens.
|
|
9
|
+
* - Converting the entire string to lowercase.
|
|
10
|
+
* - Collapsing multiple consecutive hyphens into a single one.
|
|
11
|
+
* - Removing leading and trailing hyphens.
|
|
12
|
+
*
|
|
13
|
+
* If the input is `null` or `undefined`, an empty string is returned.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} text - The input string to convert.
|
|
16
|
+
* @returns {string} The kebab-case formatted string.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* kebabCase("Hello World"); // "hello-world"
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* kebabCase("camelCaseText"); // "camel-case-text"
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* kebabCase(" convert_to--kebab.case! "); // "convert-to-kebab-case"
|
|
26
|
+
*/
|
|
1
27
|
export declare function kebabCase(text: string): string;
|
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.kebabCase = kebabCase;
|
|
4
|
+
/**
|
|
5
|
+
* Converts a given string to kebab-case format.
|
|
6
|
+
*
|
|
7
|
+
* The conversion process includes:
|
|
8
|
+
* - Trimming whitespace from both ends.
|
|
9
|
+
* - Replacing spaces and underscores with hyphens.
|
|
10
|
+
* - Inserting hyphens between lowercase and uppercase letter boundaries (e.g., `fooBar` → `foo-bar`).
|
|
11
|
+
* - Replacing all non-word characters (except hyphens) with hyphens.
|
|
12
|
+
* - Converting the entire string to lowercase.
|
|
13
|
+
* - Collapsing multiple consecutive hyphens into a single one.
|
|
14
|
+
* - Removing leading and trailing hyphens.
|
|
15
|
+
*
|
|
16
|
+
* If the input is `null` or `undefined`, an empty string is returned.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} text - The input string to convert.
|
|
19
|
+
* @returns {string} The kebab-case formatted string.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* kebabCase("Hello World"); // "hello-world"
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* kebabCase("camelCaseText"); // "camel-case-text"
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* kebabCase(" convert_to--kebab.case! "); // "convert-to-kebab-case"
|
|
29
|
+
*/
|
|
4
30
|
function kebabCase(text) {
|
|
5
31
|
if (text == null)
|
|
6
32
|
return '';
|
|
@@ -9,17 +9,15 @@
|
|
|
9
9
|
*/
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
11
|
exports.maskSegment = maskSegment;
|
|
12
|
-
function maskSegment(text, maskStart = 0, maskEnd = text.length, maskChar =
|
|
12
|
+
function maskSegment(text, maskStart = 0, maskEnd = text.length, maskChar = '*') {
|
|
13
13
|
if (maskStart < 0 || maskEnd > text.length || maskStart >= maskEnd) {
|
|
14
|
-
throw new Error(
|
|
14
|
+
throw new Error('Invalid mask segment range');
|
|
15
15
|
}
|
|
16
16
|
if (maskChar.length !== 1) {
|
|
17
|
-
throw new Error(
|
|
17
|
+
throw new Error('Mask character must be a single character');
|
|
18
18
|
}
|
|
19
19
|
if (maskStart === 0 && maskEnd === text.length) {
|
|
20
20
|
return maskChar.repeat(text.length);
|
|
21
21
|
}
|
|
22
|
-
return
|
|
23
|
-
maskChar.repeat(maskEnd - maskStart) +
|
|
24
|
-
text.slice(maskEnd));
|
|
22
|
+
return text.slice(0, maskStart) + maskChar.repeat(maskEnd - maskStart) + text.slice(maskEnd);
|
|
25
23
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a number is negative
|
|
3
|
+
*/
|
|
4
|
+
export declare function isNegative(num: number): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Gets the highest order of magnitude for a number
|
|
7
|
+
* @example getOrderOfMagnitude(1234) returns 1000
|
|
8
|
+
* @example getOrderOfMagnitude(567) returns 100
|
|
9
|
+
*/
|
|
10
|
+
export declare function getOrderOfMagnitude(num: number): number;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNegative = isNegative;
|
|
4
|
+
exports.getOrderOfMagnitude = getOrderOfMagnitude;
|
|
5
|
+
/**
|
|
6
|
+
* Checks if a number is negative
|
|
7
|
+
*/
|
|
8
|
+
function isNegative(num) {
|
|
9
|
+
return num < 0;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Gets the highest order of magnitude for a number
|
|
13
|
+
* @example getOrderOfMagnitude(1234) returns 1000
|
|
14
|
+
* @example getOrderOfMagnitude(567) returns 100
|
|
15
|
+
*/
|
|
16
|
+
function getOrderOfMagnitude(num) {
|
|
17
|
+
const absNum = Math.abs(num);
|
|
18
|
+
if (absNum >= 1000000000000)
|
|
19
|
+
return 1000000000000;
|
|
20
|
+
if (absNum >= 1000000000)
|
|
21
|
+
return 1000000000;
|
|
22
|
+
if (absNum >= 1000000)
|
|
23
|
+
return 1000000;
|
|
24
|
+
if (absNum >= 1000)
|
|
25
|
+
return 1000;
|
|
26
|
+
if (absNum >= 100)
|
|
27
|
+
return 100;
|
|
28
|
+
if (absNum >= 10)
|
|
29
|
+
return 10;
|
|
30
|
+
return 1;
|
|
31
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a number to its English textual representation.
|
|
3
|
+
*
|
|
4
|
+
* Handles negative numbers, numbers from 0 to 20, tens, hundreds, thousands, and larger magnitudes.
|
|
5
|
+
*
|
|
6
|
+
* @param num The number to convert.
|
|
7
|
+
* @returns The textual representation of the number in English.
|
|
8
|
+
*/
|
|
9
|
+
declare function numberToTextEn(num: number): string;
|
|
10
|
+
export default numberToTextEn;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const types_1 = require("./types");
|
|
4
|
+
const helpers_1 = require("./helpers");
|
|
5
|
+
/**
|
|
6
|
+
* Converts a number to its English textual representation.
|
|
7
|
+
*
|
|
8
|
+
* Handles negative numbers, numbers from 0 to 20, tens, hundreds, thousands, and larger magnitudes.
|
|
9
|
+
*
|
|
10
|
+
* @param num The number to convert.
|
|
11
|
+
* @returns The textual representation of the number in English.
|
|
12
|
+
*/
|
|
13
|
+
function numberToTextEn(num) {
|
|
14
|
+
if ((0, helpers_1.isNegative)(num)) {
|
|
15
|
+
return `minus ${numberToTextEn(Math.abs(num))}`;
|
|
16
|
+
}
|
|
17
|
+
// 0-20
|
|
18
|
+
if (num <= 20 && types_1.NUMBERS_EN[num]) {
|
|
19
|
+
return types_1.NUMBERS_EN[num];
|
|
20
|
+
}
|
|
21
|
+
// 21-99
|
|
22
|
+
if (num < 100) {
|
|
23
|
+
const tens = Math.floor(num / 10) * 10;
|
|
24
|
+
const ones = num % 10;
|
|
25
|
+
if (ones === 0) {
|
|
26
|
+
return types_1.NUMBERS_EN[tens];
|
|
27
|
+
}
|
|
28
|
+
return `${types_1.NUMBERS_EN[tens]}-${types_1.NUMBERS_EN[ones]}`;
|
|
29
|
+
}
|
|
30
|
+
// 100+
|
|
31
|
+
const magnitude = (0, helpers_1.getOrderOfMagnitude)(num);
|
|
32
|
+
const quotient = Math.floor(num / magnitude);
|
|
33
|
+
const remainder = num % magnitude;
|
|
34
|
+
// Special handling for hundreds
|
|
35
|
+
if (magnitude === 100) {
|
|
36
|
+
const result = `${numberToTextEn(quotient)} hundred`;
|
|
37
|
+
return remainder === 0 ? result : `${result} ${numberToTextEn(remainder)}`;
|
|
38
|
+
}
|
|
39
|
+
// 1000+
|
|
40
|
+
const magnitudeName = types_1.NUMBERS_EN[magnitude];
|
|
41
|
+
const quotientText = numberToTextEn(quotient);
|
|
42
|
+
const result = `${quotientText} ${magnitudeName}`;
|
|
43
|
+
return remainder === 0 ? result : `${result} ${numberToTextEn(remainder)}`;
|
|
44
|
+
}
|
|
45
|
+
exports.default = numberToTextEn;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a number to its Polish textual representation.
|
|
3
|
+
*
|
|
4
|
+
* Handles negative numbers, numbers from 0 to billions, and applies correct Polish grammatical forms.
|
|
5
|
+
*
|
|
6
|
+
* @param num The number to convert.
|
|
7
|
+
* @returns The textual representation of the number in Polish.
|
|
8
|
+
*/
|
|
9
|
+
declare function numberToTextPl(num: number): string;
|
|
10
|
+
export default numberToTextPl;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const types_1 = require("./types");
|
|
4
|
+
const helpers_1 = require("./helpers");
|
|
5
|
+
/**
|
|
6
|
+
* Converts a number to its Polish textual representation.
|
|
7
|
+
*
|
|
8
|
+
* Handles negative numbers, numbers from 0 to billions, and applies correct Polish grammatical forms.
|
|
9
|
+
*
|
|
10
|
+
* @param num The number to convert.
|
|
11
|
+
* @returns The textual representation of the number in Polish.
|
|
12
|
+
*/
|
|
13
|
+
function numberToTextPl(num) {
|
|
14
|
+
if ((0, helpers_1.isNegative)(num)) {
|
|
15
|
+
return `minus ${numberToTextPl(Math.abs(num))}`;
|
|
16
|
+
}
|
|
17
|
+
// 0-20
|
|
18
|
+
if (num <= 20 && types_1.NUMBERS_PL[num]) {
|
|
19
|
+
return types_1.NUMBERS_PL[num];
|
|
20
|
+
}
|
|
21
|
+
// 21-99
|
|
22
|
+
if (num < 100) {
|
|
23
|
+
const tens = Math.floor(num / 10) * 10;
|
|
24
|
+
const ones = num % 10;
|
|
25
|
+
if (ones === 0) {
|
|
26
|
+
return types_1.NUMBERS_PL[tens];
|
|
27
|
+
}
|
|
28
|
+
return `${types_1.NUMBERS_PL[tens]} ${types_1.NUMBERS_PL[ones]}`;
|
|
29
|
+
}
|
|
30
|
+
// 100+
|
|
31
|
+
const magnitude = (0, helpers_1.getOrderOfMagnitude)(num);
|
|
32
|
+
const quotient = Math.floor(num / magnitude);
|
|
33
|
+
const remainder = num % magnitude;
|
|
34
|
+
// Special handling for hundreds
|
|
35
|
+
if (magnitude === 100) {
|
|
36
|
+
const hundredsForm = quotient * 100;
|
|
37
|
+
const result = types_1.NUMBERS_PL[hundredsForm];
|
|
38
|
+
return remainder === 0 ? result : `${result} ${numberToTextPl(remainder)}`;
|
|
39
|
+
}
|
|
40
|
+
// 1000+
|
|
41
|
+
let prefix = '';
|
|
42
|
+
if (quotient !== 1) {
|
|
43
|
+
prefix = numberToTextPl(quotient) + ' ';
|
|
44
|
+
}
|
|
45
|
+
const magnitudeForm = getPolishGrammaticalForm(magnitude, quotient);
|
|
46
|
+
const result = `${prefix}${magnitudeForm}`.trim();
|
|
47
|
+
return remainder === 0 ? result : `${result} ${numberToTextPl(remainder)}`;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Gets the correct grammatical form for Polish magnitude words
|
|
51
|
+
* based on the number (singular, few, many)
|
|
52
|
+
*/
|
|
53
|
+
function getPolishGrammaticalForm(magnitude, count) {
|
|
54
|
+
const forms = {
|
|
55
|
+
1000: ['tysiąc', 'tysiące', 'tysięcy'],
|
|
56
|
+
1000000: ['milion', 'miliony', 'milionów'],
|
|
57
|
+
1000000000: ['miliard', 'miliardy', 'miliardów'],
|
|
58
|
+
1000000000000: ['bilion', 'biliony', 'bilionów']
|
|
59
|
+
};
|
|
60
|
+
const formArray = forms[magnitude];
|
|
61
|
+
if (!formArray) {
|
|
62
|
+
throw new Error(`No grammatical forms defined for magnitude ${magnitude}`);
|
|
63
|
+
}
|
|
64
|
+
const lastTwoDigits = count % 100;
|
|
65
|
+
const lastDigit = count % 10;
|
|
66
|
+
if (lastTwoDigits >= 11 && lastTwoDigits <= 19) {
|
|
67
|
+
return formArray[2];
|
|
68
|
+
}
|
|
69
|
+
if (lastDigit === 1) {
|
|
70
|
+
return formArray[0];
|
|
71
|
+
}
|
|
72
|
+
else if (lastDigit >= 2 && lastDigit <= 4) {
|
|
73
|
+
return formArray[1];
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
return formArray[2];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.default = numberToTextPl;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NumberToTextLanguage } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a number to its text representation in the specified language.
|
|
4
|
+
*
|
|
5
|
+
* @param num - The number to convert (integer between -1,000,000,000,000 and 1,000,000,000,000)
|
|
6
|
+
* @param lang - The target language ('en' for English, 'pl' for Polish)
|
|
7
|
+
* @returns The text representation of the number
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* numberToText(123) // "one hundred twenty-three"
|
|
11
|
+
* numberToText(123, 'pl') // "sto dwadzieścia trzy"
|
|
12
|
+
* numberToText(-45) // "minus forty-five"
|
|
13
|
+
* numberToText(1000000) // "one million"
|
|
14
|
+
* numberToText(1000000, 'pl') // "milion"
|
|
15
|
+
*
|
|
16
|
+
* @throws {TypeError} If num is not a valid integer or lang is not supported
|
|
17
|
+
* @throws {RangeError} If num is outside the supported range
|
|
18
|
+
*/
|
|
19
|
+
export declare function numberToText(num: number, lang?: NumberToTextLanguage): string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.numberToText = numberToText;
|
|
7
|
+
const implementation_PL_1 = __importDefault(require("./implementation_PL"));
|
|
8
|
+
const implementation_EN_1 = __importDefault(require("./implementation_EN"));
|
|
9
|
+
/**
|
|
10
|
+
* Converts a number to its text representation in the specified language.
|
|
11
|
+
*
|
|
12
|
+
* @param num - The number to convert (integer between -1,000,000,000,000 and 1,000,000,000,000)
|
|
13
|
+
* @param lang - The target language ('en' for English, 'pl' for Polish)
|
|
14
|
+
* @returns The text representation of the number
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* numberToText(123) // "one hundred twenty-three"
|
|
18
|
+
* numberToText(123, 'pl') // "sto dwadzieścia trzy"
|
|
19
|
+
* numberToText(-45) // "minus forty-five"
|
|
20
|
+
* numberToText(1000000) // "one million"
|
|
21
|
+
* numberToText(1000000, 'pl') // "milion"
|
|
22
|
+
*
|
|
23
|
+
* @throws {TypeError} If num is not a valid integer or lang is not supported
|
|
24
|
+
* @throws {RangeError} If num is outside the supported range
|
|
25
|
+
*/
|
|
26
|
+
function numberToText(num, lang = 'en') {
|
|
27
|
+
validateNumber(num);
|
|
28
|
+
validateLanguage(lang);
|
|
29
|
+
const langMap = {
|
|
30
|
+
en: implementation_EN_1.default,
|
|
31
|
+
pl: implementation_PL_1.default,
|
|
32
|
+
};
|
|
33
|
+
if (!langMap[lang]) {
|
|
34
|
+
throw new TypeError('Unsupported language');
|
|
35
|
+
}
|
|
36
|
+
return langMap[lang](num);
|
|
37
|
+
}
|
|
38
|
+
/////////////////////////////////////////
|
|
39
|
+
//// Validation
|
|
40
|
+
/////////////////////////////////////////
|
|
41
|
+
/**
|
|
42
|
+
* Checks if the provided value is an integer within the allowed range.
|
|
43
|
+
*
|
|
44
|
+
* @param num - The value to check
|
|
45
|
+
* @throws {TypeError} If num is not an integer
|
|
46
|
+
* @throws {RangeError} If num is outside the range -1,000,000,000,000 to 1,000,000,000,000
|
|
47
|
+
*/
|
|
48
|
+
function validateNumber(num) {
|
|
49
|
+
if (typeof num !== 'number' || !Number.isInteger(num) || isNaN(num)) {
|
|
50
|
+
throw new TypeError('Num param should be number');
|
|
51
|
+
}
|
|
52
|
+
if (num < -1000000000000 || num > 1000000000000) {
|
|
53
|
+
throw new RangeError('Number must be between -1,000,000,000,000 and 1,000,000,000,000');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Checks if the provided language is supported.
|
|
58
|
+
*
|
|
59
|
+
* @param lang - The language code to check
|
|
60
|
+
* @throws {TypeError} If lang is not one of the supported languages
|
|
61
|
+
*/
|
|
62
|
+
function validateLanguage(lang) {
|
|
63
|
+
const validLanguages = ['en', 'pl'];
|
|
64
|
+
if (!validLanguages.includes(lang)) {
|
|
65
|
+
throw new TypeError(`Language must be one of: ${validLanguages.join(', ')}`);
|
|
66
|
+
}
|
|
67
|
+
}
|