@eugeniouglov/string-manipulator 0.0.0-stage → 1.0.2

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,3 +1,190 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # string-manipulator
2
+
3
+ Utility package for string manipulation in JavaScript/Node.js.
4
+
5
+ Supports two connection methods:
6
+
7
+ 1. **Regular import** in another file (`require` / `import`)
8
+ 2. **Run as module** (`node string-manipulator.js`)
9
+
10
+ ---
11
+
12
+ ## Installation
13
+
14
+ If the package is published on npm:
15
+
16
+ ```bash
17
+ npm install string-manipulator
18
+ ```
19
+
20
+ Or use it locally by connecting the folder as a package.
21
+
22
+ ---
23
+
24
+ ## Quick Start
25
+
26
+ ### ESM (import)
27
+
28
+ ```javascript
29
+ import StringManipulator from 'string-manipulator';
30
+
31
+ const sm = new StringManipulator();
32
+ sm.splitText('hello world'); // ["hello", "world"]
33
+ ```
34
+
35
+ ### CommonJS (require)
36
+
37
+ ```javascript
38
+ const StringManipulator = require('string-manipulator');
39
+
40
+ const sm = new StringManipulator();
41
+ sm.splitText('hello world'); // ["hello", "world"]
42
+ ```
43
+
44
+ ### Run as module (demonstration)
45
+
46
+ ```bash
47
+ node string-manipulator.js
48
+ ```
49
+
50
+ Outputs examples of all methods.
51
+
52
+ ---
53
+
54
+ ## API
55
+
56
+ ### `new StringManipulator()`
57
+
58
+ Creates a string manipulator instance.
59
+
60
+ ### `splitText(stringToSplit, separator = " ")` → `string[]`
61
+
62
+ Splits a string by the delimiter and removes empty elements.
63
+
64
+ ```javascript
65
+ sm.splitText('hello world'); // ["hello", "world"]
66
+ sm.splitText('a,b,c', ','); // ["a", "b", "c"]
67
+ ```
68
+
69
+ ### `getCuttedText(string, startSymbol?, endSymbol?)` → `string`
70
+
71
+ Extracts a substring between the first occurrences of `startSymbol` and `endSymbol`.
72
+
73
+ ```javascript
74
+ sm.getCuttedText('Hello [world]!', '[', ']'); // "world"
75
+ sm.getCuttedText('abc def', null, ' '); // "abc"
76
+ ```
77
+
78
+ ### `isSame(text1, text2)` → `boolean`
79
+
80
+ Compares two strings case-insensitively.
81
+
82
+ ```javascript
83
+ sm.isSame('Hello', 'hello'); // true
84
+ sm.isSame('abc', 'abd'); // false
85
+ ```
86
+
87
+ ### `getArrayByText(text, symbolSplit = ",")` → `string[]`
88
+
89
+ Splits a string into an array by the specified delimiter.
90
+
91
+ ```javascript
92
+ sm.getArrayByText('apple,banana', ','); // ["apple", "banana"]
93
+ ```
94
+
95
+ ### `getTextInOneLine(text)` → `string`
96
+
97
+ Removes all line breaks, replacing them with a space.
98
+
99
+ ```javascript
100
+ sm.getTextInOneLine('line1\nline2\r\nline3'); // "line1 line2 line3"
101
+ ```
102
+
103
+ ### `getConvertedTextToHTML(text)` → `string`
104
+
105
+ Converts `\n` to HTML `<br>` tag.
106
+
107
+ ```javascript
108
+ sm.getConvertedTextToHTML('Hello\nWorld'); // "Hello<br>World"
109
+ ```
110
+
111
+ ### `replaceSymbols(text, find, replace)` → `string`
112
+
113
+ Replaces all occurrences of a substring, safely escaping regex special characters.
114
+
115
+ ```javascript
116
+ sm.replaceSymbols('foo.bar.foo', '.', '|'); // "foo|bar|foo"
117
+ ```
118
+
119
+ ### `getTextWithoutLastSymbol(text_to_edit)` → `string`
120
+
121
+ Removes the last character of the string.
122
+
123
+ ```javascript
124
+ sm.getTextWithoutLastSymbol('Hello!'); // "Hello"
125
+ ```
126
+
127
+ ### `getLastWord(text, symbolBeforeWord = " ")` → `string`
128
+
129
+ Returns the last word of the string after trimming spaces.
130
+
131
+ ```javascript
132
+ sm.getLastWord('hello world foo'); // "foo"
133
+ ```
134
+
135
+ ### `getWords(str, firstIndexWord, lastIndexWord?)` → `string`
136
+
137
+ Returns a range of words by indices.
138
+
139
+ ```javascript
140
+ sm.getWords('one two three four', 1, 3); // "two three"
141
+ ```
142
+
143
+ ### `getFirstLine(str)` → `string`
144
+
145
+ Returns the first line of multiline text.
146
+
147
+ ```javascript
148
+ sm.getFirstLine('First\nSecond\nThird'); // "First"
149
+ ```
150
+
151
+ ### `getSeparatedWordsByCamelCaseString(str)` → `string`
152
+
153
+ Splits a camelCase string into words separated by spaces.
154
+
155
+ ```javascript
156
+ sm.getSeparatedWordsByCamelCaseString('myVariableName'); // "my Variable Name"
157
+ ```
158
+
159
+ ### `getTextWithoutSpecialCharactes(str, symbolInsted = " ")` → `string`
160
+
161
+ Replaces all non-alphanumeric characters (except spaces) with the specified character.
162
+
163
+ ```javascript
164
+ sm.getTextWithoutSpecialCharactes('user@example.com!', '_'); // "user example com _"
165
+ ```
166
+
167
+ ---
168
+
169
+ ## Package Structure
170
+
171
+ ```
172
+ string-manipulator/
173
+ ├── package.json # Package manifest (ESM + CJS export)
174
+ ├── string-manipulator.js # Main module (ESM, can be run directly)
175
+ ├── string-manipulator.cjs # CommonJS wrapper for require()
176
+ ├── example.js # Usage examples
177
+ └── README.md # Documentation
178
+ ```
179
+
180
+ ---
181
+
182
+ ## Support ☕🤗
183
+
184
+ If you find this package useful, you can
185
+ [![Buy Me A Coffee](https://img.shields.io/badge/Buy%20Me%20a%20Coffee-ffdd00?style=for-the-badge&logo=buy-me-a-coffee&logoColor=black)](https://buymeacoffee.com/eugeniouglb)
186
+
187
+
188
+ ## License
189
+
190
+ MIT
package/package.json CHANGED
@@ -1,6 +1,38 @@
1
- {
2
- "name": "@eugeniouglov/string-manipulator",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
1
+ {
2
+ "name": "@eugeniouglov/string-manipulator",
3
+ "version": "1.0.2",
4
+ "description": "Utility package for string manipulation. Supports regular import (require/import) and running as a module (node string-manipulator.js).",
5
+ "main": "string-manipulator.cjs",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./string-manipulator.js",
10
+ "require": "./string-manipulator.cjs"
11
+ }
12
+ },
13
+ "files": [
14
+ "string-manipulator.js",
15
+ "string-manipulator.cjs",
16
+ "README.md"
17
+ ],
18
+ "scripts": {
19
+ "start": "node string-manipulator.js",
20
+ "demo": "node string-manipulator.js",
21
+ "test": "node --test"
22
+ },
23
+ "keywords": [
24
+ "string",
25
+ "manipulator",
26
+ "utilities",
27
+ "text-processing"
28
+ ],
29
+ "author": "",
30
+ "license": "MIT",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/eugeniouglov/string-manipulator.git"
34
+ },
35
+ "engines": {
36
+ "node": ">=14.0.0"
37
+ }
6
38
  }
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * CommonJS wrapper for require() compatibility.
5
+ * Allows importing the package in a regular CJS environment:
6
+ * const StringManipulator = require('string-manipulator');
7
+ */
8
+
9
+ const { createRequire } = require('module');
10
+ const createReq = createRequire(__filename);
11
+
12
+ const esmMod = createReq('./string-manipulator.js');
13
+
14
+ const StringManipulator = esmMod.default || esmMod;
15
+
16
+ module.exports = StringManipulator;
17
+ module.exports.StringManipulator = esmMod.StringManipulator || esmMod.default || esmMod;
@@ -0,0 +1,200 @@
1
+ /**
2
+ * StringManipulator — utility class for string manipulation.
3
+ *
4
+ * Can be used in two ways:
5
+ * 1. Regular import in another file:
6
+ * - ESM: import StringManipulator from 'string-manipulator'
7
+ * - CJS: const StringManipulator = require('string-manipulator')
8
+ * 2. Run as module/script:
9
+ * node string-manipulator.js
10
+ */
11
+
12
+ import { pathToFileURL } from 'url';
13
+
14
+ class StringManipulator {
15
+ /**
16
+ * Splits a string by the specified separator and removes empty elements.
17
+ *
18
+ * @param {string} stringToSplit - The input string to split.
19
+ * @param {string} [separator=" "] - The delimiter.
20
+ * @returns {string[]} Array of non-empty substrings.
21
+ */
22
+ splitText(stringToSplit, separator = " ") {
23
+ const array = [];
24
+ const splitedText = stringToSplit.split(separator);
25
+
26
+ for (const word of splitedText) {
27
+ if (word) {
28
+ array.push(word);
29
+ }
30
+ }
31
+
32
+ return array;
33
+ }
34
+
35
+ /**
36
+ * Extracts a substring between the first occurrences of startSymbol and endSymbol.
37
+ *
38
+ * @param {string} string - The source text.
39
+ * @param {string} [startSymbol] - Starting delimiter (excluded from result).
40
+ * @param {string} [endSymbol] - Ending delimiter (excluded from result).
41
+ * @returns {string} The extracted substring.
42
+ */
43
+ getCuttedText(string, startSymbol, endSymbol) {
44
+ let from = 0;
45
+ let to = string.length;
46
+
47
+ if (startSymbol) from = string.indexOf(startSymbol) + 1;
48
+ if (endSymbol) to = string.indexOf(endSymbol);
49
+
50
+ return string.slice(from, to);
51
+ }
52
+
53
+ /**
54
+ * Performs a case-insensitive comparison between two strings.
55
+ *
56
+ * @param {string} text1 - The first string.
57
+ * @param {string} text2 - The second string.
58
+ * @returns {boolean} true if strings are identical ignoring case.
59
+ */
60
+ isSame(text1, text2) {
61
+ return text1.toLowerCase() === text2.toLowerCase();
62
+ }
63
+
64
+ /**
65
+ * Splits a string into an array using the specified delimiter.
66
+ *
67
+ * @param {string} text - The input text.
68
+ * @param {string} [symbolSplit=","] - The delimiter.
69
+ * @returns {string[]} Array of substrings.
70
+ */
71
+ getArrayByText(text, symbolSplit = ",") {
72
+ return text.split(symbolSplit);
73
+ }
74
+
75
+ /**
76
+ * Replaces all line breaks in a text with a single space, converting it into a single line.
77
+ *
78
+ * @param {string} text - The multiline input text.
79
+ * @returns {string} A single-line string without line breaks.
80
+ */
81
+ getTextInOneLine(text) {
82
+ return text.replace(/(\r\n|\n|\r)/gm, " ");
83
+ }
84
+
85
+ /**
86
+ * Converts newline characters (\n) into HTML line break tags (<br>).
87
+ *
88
+ * @param {string} text - The plain text containing newline characters.
89
+ * @returns {string} The HTML-formatted string.
90
+ */
91
+ getConvertedTextToHTML(text) {
92
+ return this.replaceSymbols(text, "\n", "<br>");
93
+ }
94
+
95
+ /**
96
+ * Replaces all occurrences of a specific substring within a text, safely escaping regex special characters.
97
+ *
98
+ * @param {string} text - The source text.
99
+ * @param {string} find - The substring to search for.
100
+ * @param {string} replace - The replacement string.
101
+ * @returns {string} The updated text with all matches replaced.
102
+ */
103
+ replaceSymbols(text, find, replace) {
104
+ const escapedFind = find.replace(/([.*+?^=!:${}()|[\]\\/])/g, "\\$1");
105
+ return text.replace(new RegExp(escapedFind, "g"), replace);
106
+ }
107
+
108
+ /**
109
+ * Removes the last character from a given string.
110
+ *
111
+ * @param {string} text_to_edit - The input string.
112
+ * @returns {string} The string without its final character.
113
+ */
114
+ getTextWithoutLastSymbol(text_to_edit) {
115
+ return text_to_edit.substring(0, text_to_edit.length - 1);
116
+ }
117
+
118
+ /**
119
+ * Extracts the last word from a string after trimming leading and trailing whitespace.
120
+ *
121
+ * @param {string} text - The input text.
122
+ * @param {string} [symolBeforeWord=" "] - The delimiter separating words.
123
+ * @returns {string} The last word in the string.
124
+ */
125
+ getLastWord(text, symbolBeforeWord = " ") {
126
+ text = text.trim();
127
+ const separatedWords = text.split(symbolBeforeWord);
128
+ return separatedWords[separatedWords.length - 1];
129
+ }
130
+
131
+ /**
132
+ * Extracts a specific range of words from a string based on start and end word indices.
133
+ *
134
+ * @param {string} str - The input text.
135
+ * @param {number} firstIndexWord - The zero-based starting word index (inclusive).
136
+ * @param {number} [lastIndexWord] - The zero-based ending word index (exclusive).
137
+ * @returns {string} A space-separated string containing the selected range of words.
138
+ */
139
+ getWords(str, firstIndexWord, lastIndexWord) {
140
+ return str.split(/\s+/).slice(firstIndexWord, lastIndexWord).join(" ");
141
+ }
142
+
143
+ /**
144
+ * Retrieves the first line of a multiline string.
145
+ *
146
+ * @param {string} str - The input text.
147
+ * @returns {string} The first line of the text.
148
+ */
149
+ getFirstLine(str) {
150
+ return str.split("\n")[0];
151
+ }
152
+
153
+ /**
154
+ * Splits a camelCase string into separate space-delimited words.
155
+ *
156
+ * @param {string} str - The camelCase string (e.g., "camelCaseText").
157
+ * @returns {string} The space-separated string (e.g., "camel Case Text").
158
+ */
159
+ getSeparatedWordsByCamelCaseString(str) {
160
+ return str.replace(/([a-z])([A-Z])/g, '$1 $2');
161
+ }
162
+
163
+ /**
164
+ * Replaces all non-alphanumeric characters in a string with a specified replacement character.
165
+ *
166
+ * @param {string} str - The input text containing special characters.
167
+ * @param {string} [symbolInsted=" "] - The character used to replace special characters.
168
+ * @returns {string} The sanitized string containing only letters, numbers, and the replacement character.
169
+ */
170
+ getTextWithoutSpecialCharactes(str, symbolInsted = " ") {
171
+ return str.replace(/[^\p{L}\p{N} ]/gu, symbolInsted);
172
+ }
173
+ }
174
+
175
+ export default StringManipulator;
176
+ export { StringManipulator };
177
+
178
+ /**
179
+ * If the file is run directly (node string-manipulator.js),
180
+ * outputs a demonstration of all methods.
181
+ */
182
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
183
+ const sm = new StringManipulator();
184
+
185
+ console.log('=== StringManipulator — demonstration ===\n');
186
+
187
+ console.log('1. splitText("hello world foo"):', sm.splitText('hello world foo'));
188
+ console.log('2. getCuttedText("Hello [world]!", "[", "]"):', sm.getCuttedText('Hello [world]!', '[', ']'));
189
+ console.log('3. isSame("Hello", "hello"):', sm.isSame('Hello', 'hello'));
190
+ console.log('4. getArrayByText("a,b,c", ","):', sm.getArrayByText('a,b,c', ','));
191
+ console.log('5. getTextInOneLine("line1\\nline2\\r\\nline3"):', sm.getTextInOneLine('line1\nline2\r\nline3'));
192
+ console.log('6. getConvertedTextToHTML("hello\\nworld"):', sm.getConvertedTextToHTML('hello\nworld'));
193
+ console.log('7. replaceSymbols("foo bar foo", "foo", "baz"):', sm.replaceSymbols('foo bar foo', 'foo', 'baz'));
194
+ console.log('8. getTextWithoutLastSymbol("hello!"):', sm.getTextWithoutLastSymbol('hello!'));
195
+ console.log('9. getLastWord("hello world foo"):', sm.getLastWord('hello world foo'));
196
+ console.log('10. getWords("one two three four", 1, 3):', sm.getWords('one two three four', 1, 3));
197
+ console.log('11. getFirstLine("first\\nsecond\\nthird"):', sm.getFirstLine('first\nsecond\nthird'));
198
+ console.log('12. getSeparatedWordsByCamelCaseString("camelCaseText"):', sm.getSeparatedWordsByCamelCaseString('camelCaseText'));
199
+ console.log('13. getTextWithoutSpecialCharactes("hello@world! 123"):', sm.getTextWithoutSpecialCharactes('hello@world! 123'));
200
+ }