utilitish 0.0.6 → 0.0.8
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/array/array-constructor.d.ts +1 -1
- package/dist/array/array-constructor.js +5 -5
- package/dist/array/array-constructor.spec.d.ts +2 -0
- package/dist/array/array-constructor.spec.js +130 -0
- package/dist/array/array-prototype.d.ts +341 -85
- package/dist/array/array-prototype.js +38 -53
- package/dist/array/array-prototype.spec.d.ts +1 -0
- package/dist/array/array-prototype.spec.js +536 -0
- package/dist/map/map-prototype.d.ts +70 -10
- package/dist/map/map-prototype.js +16 -3
- package/dist/map/map-prototype.spec.d.ts +1 -0
- package/dist/map/map-prototype.spec.js +261 -0
- package/dist/object/object-prototype.d.ts +100 -9
- package/dist/object/object-prototype.js +43 -14
- package/dist/object/object-prototype.spec.d.ts +1 -0
- package/dist/object/object-prototype.spec.js +184 -0
- package/dist/set/set-prototype.d.ts +84 -6
- package/dist/set/set-prototype.js +21 -6
- package/dist/set/set-prototype.spec.d.ts +1 -0
- package/dist/set/set-prototype.spec.js +122 -0
- package/dist/string/string-prototype.d.ts +144 -25
- package/dist/string/string-prototype.js +41 -11
- package/dist/string/string-prototype.spec.d.ts +1 -0
- package/dist/string/string-prototype.spec.js +115 -0
- package/dist/utils/logic.utils.d.ts +3 -0
- package/dist/utils/logic.utils.js +41 -0
- package/package.json +1 -1
- /package/dist/{utils.d.ts → utils/core.utils.d.ts} +0 -0
- /package/dist/{utils.js → utils/core.utils.js} +0 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
require("../string/string-prototype");
|
|
4
|
+
describe('String.prototype', () => {
|
|
5
|
+
describe('capitalize()', () => {
|
|
6
|
+
it('should capitalize the first character', () => {
|
|
7
|
+
expect('hello'.capitalize()).toBe('Hello');
|
|
8
|
+
expect('Hello'.capitalize()).toBe('Hello');
|
|
9
|
+
expect(''.capitalize()).toBe('');
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
describe('splitWords()', () => {
|
|
13
|
+
it('should split camelCase, kebab-case, snake_case, and spaces', () => {
|
|
14
|
+
expect('helloWorld'.splitWords()).toEqual(['hello', 'World']);
|
|
15
|
+
expect('hello-world'.splitWords()).toEqual(['hello', 'world']);
|
|
16
|
+
expect('hello_world'.splitWords()).toEqual(['hello', 'world']);
|
|
17
|
+
expect('hello world'.splitWords()).toEqual(['hello', 'world']);
|
|
18
|
+
expect('HTMLParser'.splitWords()).toEqual(['HTML', 'Parser']);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
describe('camelCase()', () => {
|
|
22
|
+
it('should convert to camelCase', () => {
|
|
23
|
+
expect('hello world'.camelCase()).toBe('helloWorld');
|
|
24
|
+
expect('Hello_world-test'.camelCase()).toBe('helloWorldTest');
|
|
25
|
+
expect('helloWorldTest'.camelCase()).toBe('helloWorldTest');
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
describe('kebabCase()', () => {
|
|
29
|
+
it('should convert to kebab-case', () => {
|
|
30
|
+
expect('hello world'.kebabCase()).toBe('hello-world');
|
|
31
|
+
expect('Hello_worldTest'.kebabCase()).toBe('hello-world-test');
|
|
32
|
+
expect('hello-world-test'.kebabCase()).toBe('hello-world-test');
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe('snakeCase()', () => {
|
|
36
|
+
it('should convert to snake_case', () => {
|
|
37
|
+
expect('hello world'.snakeCase()).toBe('hello_world');
|
|
38
|
+
expect('Hello-worldTest'.snakeCase()).toBe('hello_world_test');
|
|
39
|
+
expect('hello_world_test'.snakeCase()).toBe('hello_world_test');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
describe('truncate()', () => {
|
|
43
|
+
it('should truncate and add ... if needed', () => {
|
|
44
|
+
expect('hello world'.truncate(5)).toBe('hello...');
|
|
45
|
+
expect('hello'.truncate(10)).toBe('hello');
|
|
46
|
+
});
|
|
47
|
+
it('should throw if n is not a non-negative integer', () => {
|
|
48
|
+
expect(() => 'abc'.truncate(-1)).toThrowError(TypeError);
|
|
49
|
+
expect(() => 'abc'.truncate(1.5)).toThrowError(TypeError);
|
|
50
|
+
expect(() => 'abc'.truncate('a')).toThrowError(TypeError);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe('reverse()', () => {
|
|
54
|
+
it('should reverse the string', () => {
|
|
55
|
+
expect('abc'.reverse()).toBe('cba');
|
|
56
|
+
expect('été'.reverse()).toBe('été');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
describe('isEmpty()', () => {
|
|
60
|
+
it('should return true for empty or whitespace strings', () => {
|
|
61
|
+
expect(''.isEmpty()).toBe(true);
|
|
62
|
+
expect(' '.isEmpty()).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
it('should return false for non-empty strings', () => {
|
|
65
|
+
expect('abc'.isEmpty()).toBe(false);
|
|
66
|
+
expect(' a '.isEmpty()).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
describe('slugify()', () => {
|
|
70
|
+
it('should slugify a string', () => {
|
|
71
|
+
expect('Hello World!'.slugify()).toBe('hello-world');
|
|
72
|
+
expect("Éléphant à l'été".slugify()).toBe('elephant-a-l-ete');
|
|
73
|
+
expect(' --Hello__World-- '.slugify()).toBe('hello-world');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe('replaceRange()', () => {
|
|
77
|
+
it('should replace a single character at the given index', () => {
|
|
78
|
+
expect('hello'.replaceRange(1, 2, 'a')).toBe('hallo');
|
|
79
|
+
});
|
|
80
|
+
it('should replace a range of characters', () => {
|
|
81
|
+
expect('abcdef'.replaceRange(2, 5, 'Z')).toBe('abZf');
|
|
82
|
+
});
|
|
83
|
+
it('should remove a range if replaceString is omitted', () => {
|
|
84
|
+
expect('abcdef'.replaceRange(1, 4)).toBe('aef');
|
|
85
|
+
});
|
|
86
|
+
it('should work when start > end (swaps automatically)', () => {
|
|
87
|
+
expect('abcdef'.replaceRange(5, 2, 'X')).toBe('abXf');
|
|
88
|
+
});
|
|
89
|
+
it('should insert at the end if start and end are equal to length', () => {
|
|
90
|
+
expect('abc'.replaceRange(3, 3, 'Z')).toBe('abcZ');
|
|
91
|
+
});
|
|
92
|
+
describe('error handling', () => {
|
|
93
|
+
it('should throw if start or end is negative', () => {
|
|
94
|
+
expect(() => 'abc'.replaceRange(-1, 2)).toThrow(RangeError);
|
|
95
|
+
expect(() => 'abc'.replaceRange(1, -2)).toThrow(RangeError);
|
|
96
|
+
});
|
|
97
|
+
it('should throw if start or end is out of bounds', () => {
|
|
98
|
+
expect(() => 'abc'.replaceRange(0, 4)).toThrow(RangeError);
|
|
99
|
+
expect(() => 'abc'.replaceRange(5, 1)).toThrow(RangeError);
|
|
100
|
+
});
|
|
101
|
+
it('should throw if start or end is not an integer', () => {
|
|
102
|
+
expect(() => 'abc'.replaceRange(1.5, 2)).toThrow(TypeError);
|
|
103
|
+
expect(() => 'abc'.replaceRange(1, 2.2)).toThrow(TypeError);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
describe('edge cases', () => {
|
|
107
|
+
it('should default replaceString to empty string', () => {
|
|
108
|
+
expect('hello'.replaceRange(1, 4)).toBe('ho');
|
|
109
|
+
});
|
|
110
|
+
it('should support empty string', () => {
|
|
111
|
+
expect(''.replaceRange(0, 0, 'x')).toBe('x');
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mapToObject = mapToObject;
|
|
4
|
+
exports.sortBy = sortBy;
|
|
5
|
+
const core_utils_1 = require("./core.utils");
|
|
6
|
+
function mapToObject(map) {
|
|
7
|
+
const obj = {};
|
|
8
|
+
for (const [key, value] of map) {
|
|
9
|
+
// Validate that key is not null or undefined
|
|
10
|
+
if (key === null || key === undefined) {
|
|
11
|
+
throw new TypeError(`Invalid key: key cannot be null or undefined. Key received: ${String(key)}`);
|
|
12
|
+
}
|
|
13
|
+
const keyType = typeof key;
|
|
14
|
+
// Only allow string, number, or symbol
|
|
15
|
+
if (keyType !== 'string' && keyType !== 'number' && keyType !== 'symbol') {
|
|
16
|
+
throw new TypeError(`Invalid key type: keys must be string, number, or symbol, received ${keyType}. Key value: ${String(key)}`);
|
|
17
|
+
}
|
|
18
|
+
obj[key] = value;
|
|
19
|
+
}
|
|
20
|
+
return obj;
|
|
21
|
+
}
|
|
22
|
+
function sortBy(arr, direction, selector) {
|
|
23
|
+
if (arr.length === 0)
|
|
24
|
+
return arr.slice();
|
|
25
|
+
const getValue = (0, core_utils_1.resolveSelector)(selector, (item) => item);
|
|
26
|
+
if (!selector && !arr.every((item) => (0, core_utils_1.isNumberOrString)(item))) {
|
|
27
|
+
throw new TypeError('Array elements must be number or string if no selector is provided');
|
|
28
|
+
}
|
|
29
|
+
return arr.slice().sort((a, b) => {
|
|
30
|
+
const valA = getValue(a);
|
|
31
|
+
const valB = getValue(b);
|
|
32
|
+
if (!(0, core_utils_1.isNumberOrString)(valA) || !(0, core_utils_1.isNumberOrString)(valB)) {
|
|
33
|
+
throw new TypeError('Selector must return number or string');
|
|
34
|
+
}
|
|
35
|
+
if (valA > valB)
|
|
36
|
+
return direction === 'asc' ? 1 : -1;
|
|
37
|
+
if (valA < valB)
|
|
38
|
+
return direction === 'asc' ? -1 : 1;
|
|
39
|
+
return 0;
|
|
40
|
+
});
|
|
41
|
+
}
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|