string-magic-kit 0.0.0-stage → 1.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/README.md +7 -2
- package/index.js +68 -0
- package/package.json +15 -3
- package/test.js +30 -0
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# string-magic-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lightweight, zero-dependency Node.js string utility library for common text manipulation, inspection, and alternating merge operations.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install string-magic-kit
|
package/index.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
function assertString(value, label = 'input') {
|
|
2
|
+
if (typeof value !== 'string') {
|
|
3
|
+
throw new TypeError(`${label} must be a string`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function capitalize(str) {
|
|
8
|
+
assertString(str, 'str');
|
|
9
|
+
if (str.length === 0) return '';
|
|
10
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function reverse(str) {
|
|
14
|
+
assertString(str, 'str');
|
|
15
|
+
return [...str].reverse().join('');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function countWords(str) {
|
|
19
|
+
assertString(str, 'str');
|
|
20
|
+
const trimmed = str.trim();
|
|
21
|
+
if (!trimmed) return 0;
|
|
22
|
+
return trimmed.split(/\s+/).length;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function removeSpaces(str) {
|
|
26
|
+
assertString(str, 'str');
|
|
27
|
+
return str.replace(/\s+/g, '');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isPalindrome(str, { caseInsensitive = true, ignoreSpaces = true } = {}) {
|
|
31
|
+
assertString(str, 'str');
|
|
32
|
+
let normalized = str;
|
|
33
|
+
if (ignoreSpaces) {
|
|
34
|
+
normalized = normalized.replace(/\s+/g, '');
|
|
35
|
+
}
|
|
36
|
+
if (caseInsensitive) {
|
|
37
|
+
normalized = normalized.toLowerCase();
|
|
38
|
+
}
|
|
39
|
+
const chars = [...normalized];
|
|
40
|
+
const reversed = [...chars].reverse().join('');
|
|
41
|
+
return normalized === reversed;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function mergeAlternately(word1, word2) {
|
|
45
|
+
assertString(word1, 'word1');
|
|
46
|
+
assertString(word2, 'word2');
|
|
47
|
+
|
|
48
|
+
const chars1 = [...word1];
|
|
49
|
+
const chars2 = [...word2];
|
|
50
|
+
const maxLen = Math.max(chars1.length, chars2.length);
|
|
51
|
+
let merged = '';
|
|
52
|
+
|
|
53
|
+
for (let i = 0; i < maxLen; i += 1) {
|
|
54
|
+
if (i < chars1.length) merged += chars1[i];
|
|
55
|
+
if (i < chars2.length) merged += chars2[i];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return merged;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = {
|
|
62
|
+
capitalize,
|
|
63
|
+
reverse,
|
|
64
|
+
countWords,
|
|
65
|
+
removeSpaces,
|
|
66
|
+
isPalindrome,
|
|
67
|
+
mergeAlternately,
|
|
68
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "string-magic-kit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Lightweight zero-dependency Node.js string utilities for capitalization, reversal, word counting, whitespace cleanup, palindromes, and alternating merge.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"string",
|
|
11
|
+
"utilities",
|
|
12
|
+
"palindrome",
|
|
13
|
+
"reverse",
|
|
14
|
+
"merge-strings",
|
|
15
|
+
"text"
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT"
|
|
6
18
|
}
|
package/test.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const {
|
|
3
|
+
capitalize,
|
|
4
|
+
reverse,
|
|
5
|
+
countWords,
|
|
6
|
+
removeSpaces,
|
|
7
|
+
isPalindrome,
|
|
8
|
+
mergeAlternately,
|
|
9
|
+
} = require('./index');
|
|
10
|
+
|
|
11
|
+
assert.strictEqual(capitalize('hello world'), 'Hello world');
|
|
12
|
+
assert.strictEqual(capitalize(''), '');
|
|
13
|
+
|
|
14
|
+
assert.strictEqual(reverse('hello'), 'olleh');
|
|
15
|
+
|
|
16
|
+
assert.strictEqual(countWords('I love JavaScript'), 3);
|
|
17
|
+
assert.strictEqual(countWords(' '), 0);
|
|
18
|
+
|
|
19
|
+
assert.strictEqual(removeSpaces('hello world'), 'helloworld');
|
|
20
|
+
|
|
21
|
+
assert.strictEqual(isPalindrome('madam'), true);
|
|
22
|
+
assert.strictEqual(isPalindrome('Race car'), true);
|
|
23
|
+
assert.strictEqual(isPalindrome('hello'), false);
|
|
24
|
+
|
|
25
|
+
assert.strictEqual(mergeAlternately('abc', 'pqr'), 'apbqcr');
|
|
26
|
+
assert.strictEqual(mergeAlternately('ab', 'pqrs'), 'apbqrs');
|
|
27
|
+
|
|
28
|
+
assert.throws(() => capitalize(123), TypeError);
|
|
29
|
+
|
|
30
|
+
console.log('All string-magic-kit tests passed.');
|