utilitish 0.0.5 → 0.0.7

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.
Files changed (33) hide show
  1. package/dist/array/array-constructor.d.ts +19 -13
  2. package/dist/array/array-constructor.js +5 -5
  3. package/dist/array/array-constructor.spec.d.ts +2 -0
  4. package/dist/array/array-constructor.spec.js +130 -0
  5. package/dist/array/array-prototype.d.ts +341 -84
  6. package/dist/array/array-prototype.js +38 -53
  7. package/dist/array/array-prototype.spec.d.ts +1 -0
  8. package/dist/array/array-prototype.spec.js +536 -0
  9. package/dist/index.d.ts +2 -0
  10. package/dist/index.js +2 -0
  11. package/dist/map/map-prototype.d.ts +69 -9
  12. package/dist/map/map-prototype.js +16 -3
  13. package/dist/map/map-prototype.spec.d.ts +1 -0
  14. package/dist/map/map-prototype.spec.js +261 -0
  15. package/dist/object/object-prototype.d.ts +61 -8
  16. package/dist/object/object-prototype.js +9 -0
  17. package/dist/object/object-prototype.spec.d.ts +1 -0
  18. package/dist/object/object-prototype.spec.js +110 -0
  19. package/dist/set/set-prototype.d.ts +83 -5
  20. package/dist/set/set-prototype.js +21 -6
  21. package/dist/set/set-prototype.spec.d.ts +1 -0
  22. package/dist/set/set-prototype.spec.js +122 -0
  23. package/dist/string/string-prototype.d.ts +143 -24
  24. package/dist/string/string-prototype.js +41 -11
  25. package/dist/string/string-prototype.spec.d.ts +1 -0
  26. package/dist/string/string-prototype.spec.js +115 -0
  27. package/dist/utils/core.utils.d.ts +103 -0
  28. package/dist/utils/core.utils.js +152 -0
  29. package/dist/utils/logic.utils.d.ts +3 -0
  30. package/dist/utils/logic.utils.js +41 -0
  31. package/package.json +1 -1
  32. package/dist/utils.d.ts +0 -28
  33. package/dist/utils.js +0 -80
@@ -53,26 +53,30 @@ declare global {
53
53
  *
54
54
  * @example
55
55
  * // 1D array with primitive values
56
- * Array.create('x', 5);
57
- * // => ['x', 'x', 'x', 'x', 'x']
56
+ * const arr1 = Array.create('x', 5);
57
+ * // type: string[]
58
+ * // value: ['x', 'x', 'x', 'x', 'x']
58
59
  *
59
60
  * @example
60
61
  * // 1D array with random numbers
61
- * Array.create(() => Math.random(), 5);
62
- * // => [0.12, 0.87, 0.45, 0.76, 0.33] (values will differ)
62
+ * const arr2 = Array.create(() => Math.random(), 5);
63
+ * // type: number[]
64
+ * // value: [0.12, 0.87, 0.45, 0.76, 0.33] (values will differ)
63
65
  *
64
66
  * @example
65
67
  * // 2D array (matrix)
66
- * Array.create(0, 2, 3);
67
- * // => [[0, 0, 0], [0, 0, 0]]
68
+ * const arr3 = Array.create(0, 2, 3);
69
+ * // type: number[][]
70
+ * // value: [[0, 0, 0], [0, 0, 0]]
68
71
  *
69
72
  * @example
70
73
  * // 2D array with distinct objects
71
- * Array.create(() => ({ id: 0 }), 2, 3);
72
- * // => [
73
- * // [{id:0}, {id:0}, {id:0}],
74
- * // [{id:0}, {id:0}, {id:0}]
75
- * // ]
74
+ * const arr4 = Array.create(() => ({ id: 0 }), 2, 3);
75
+ * // type: { id: number }[][]
76
+ * // value: [
77
+ * // [{ id: 0 }, { id: 0 }, { id: 0 }],
78
+ * // [{ id: 0 }, { id: 0 }, { id: 0 }]
79
+ * // ]
76
80
  *
77
81
  * @notes
78
82
  * - If `sizes.length === 0`, returns `[]`.
@@ -83,11 +87,13 @@ declare global {
83
87
  * @template T
84
88
  * @param {T | (() => T)} valueOrFactory The value to fill the array with, or a factory function producing values.
85
89
  * @param {...number} sizes Sizes for each dimension (1D => one number, 2D => two numbers, etc.).
86
- * @returns {any[]} A multi-dimensional array of depth `sizes.length` filled with values from `valueOrFactory`.
90
+ * @returns {Array} A multi-dimensional array of the specified depth filled with the given values.
87
91
  *
88
92
  * @remarks
89
93
  * - This method is **static** and must be called on `Array`, not on an instance.
90
94
  */
91
- create<T>(valueOrFactory: T | (() => T), ...sizes: number[]): any[];
95
+ create<T, Dims extends number[]>(valueOrFactory: T | (() => T), ...sizes: Dims): MultiDimensionalArray<WidenLiterals<T>, Dims>;
92
96
  }
93
97
  }
98
+ type MultiDimensionalArray<T, Dims extends number[]> = Dims extends [number, ...infer Rest extends number[]] ? MultiDimensionalArray<T[], Rest> : T;
99
+ type WidenLiterals<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const utils_1 = require("../utils");
3
+ const core_utils_1 = require("../utils/core.utils");
4
4
  /**
5
5
  * @see Array.zip
6
6
  */
7
- (0, utils_1.defineStaticIfNotExists)(Array, 'zip', function (...arrays) {
7
+ (0, core_utils_1.defineStaticIfNotExists)(Array, 'zip', function (...arrays) {
8
8
  if (arrays.length === 0)
9
9
  return [];
10
10
  const maxLen = Math.max(...arrays.map((arr) => arr.length));
@@ -13,7 +13,7 @@ const utils_1 = require("../utils");
13
13
  /**
14
14
  * @see Array.range
15
15
  */
16
- (0, utils_1.defineStaticIfNotExists)(Array, 'range', function (start, end, step = 1) {
16
+ (0, core_utils_1.defineStaticIfNotExists)(Array, 'range', function (start, end, step = 1) {
17
17
  if (end === undefined) {
18
18
  end = start;
19
19
  start = 0;
@@ -30,7 +30,7 @@ const utils_1 = require("../utils");
30
30
  /**
31
31
  * @see Array.repeat
32
32
  */
33
- (0, utils_1.defineStaticIfNotExists)(Array, 'repeat', function (length, value) {
33
+ (0, core_utils_1.defineStaticIfNotExists)(Array, 'repeat', function (length, value) {
34
34
  if (typeof length !== 'number' || !Number.isInteger(length) || length < 0) {
35
35
  throw new TypeError('Length must be a non-negative integer');
36
36
  }
@@ -39,7 +39,7 @@ const utils_1 = require("../utils");
39
39
  /**
40
40
  * @see Array.create
41
41
  */
42
- (0, utils_1.defineStaticIfNotExists)(Array, 'create', function (valueOrFactory, ...sizes) {
42
+ (0, core_utils_1.defineStaticIfNotExists)(Array, 'create', function (valueOrFactory, ...sizes) {
43
43
  if (sizes.length === 0)
44
44
  return [];
45
45
  if (!sizes.every((s) => Number.isInteger(s) && s >= 0)) {
@@ -0,0 +1,2 @@
1
+ import '../array/array-constructor';
2
+ import './array-constructor';
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("../array/array-constructor");
4
+ require("./array-constructor");
5
+ describe('Array', () => {
6
+ describe('range()', () => {
7
+ it('should generate a range from 0 to n-1 if only start is given', () => {
8
+ expect(Array.range(5)).toEqual([0, 1, 2, 3, 4]);
9
+ });
10
+ it('should generate a range from start to end-1', () => {
11
+ expect(Array.range(2, 6)).toEqual([2, 3, 4, 5]);
12
+ });
13
+ it('should support negative steps', () => {
14
+ expect(Array.range(5, 1, -1)).toEqual([5, 4, 3, 2]);
15
+ });
16
+ it('should return an empty array if start equals end', () => {
17
+ expect(Array.range(3, 3)).toEqual([]);
18
+ });
19
+ it('should return an empty array if step does not reach end', () => {
20
+ expect(Array.range(0, 5, -1)).toEqual([]);
21
+ expect(Array.range(5, 0, 1)).toEqual([]);
22
+ });
23
+ describe('error handling', () => {
24
+ it('should throw if step is 0', () => {
25
+ expect(() => Array.range(0, 5, 0)).toThrowError('step must not be 0');
26
+ });
27
+ });
28
+ });
29
+ describe('repeat()', () => {
30
+ it('should fill an array with the same value', () => {
31
+ expect(Array.repeat(3, 'a')).toEqual(['a', 'a', 'a']);
32
+ });
33
+ it('should fill an array with values from a function', () => {
34
+ let n = 0;
35
+ expect(Array.repeat(4, () => ++n)).toEqual([1, 2, 3, 4]);
36
+ });
37
+ it('should return an empty array if length is 0', () => {
38
+ expect(Array.repeat(0, 'x')).toEqual([]);
39
+ });
40
+ describe('error handling', () => {
41
+ it('should throw if length is negative or not an integer', () => {
42
+ expect(() => Array.repeat(-1, 'a')).toThrowError();
43
+ expect(() => Array.repeat(1.5, 'a')).toThrowError();
44
+ expect(() => Array.repeat('a', 'a')).toThrowError();
45
+ });
46
+ });
47
+ });
48
+ describe('zip()', () => {
49
+ it('should zip two arrays of equal length', () => {
50
+ expect(Array.zip([1, 2, 3], ['a', 'b', 'c'])).toEqual([
51
+ [1, 'a'],
52
+ [2, 'b'],
53
+ [3, 'c'],
54
+ ]);
55
+ });
56
+ it('should zip arrays of different lengths (fill with undefined)', () => {
57
+ expect(Array.zip([1, 2], ['a', 'b', 'c'], [true, false, true])).toEqual([
58
+ [1, 'a', true],
59
+ [2, 'b', false],
60
+ [undefined, 'c', true],
61
+ ]);
62
+ });
63
+ it('should work with a single array', () => {
64
+ expect(Array.zip([1, 2, 3])).toEqual([[1], [2], [3]]);
65
+ });
66
+ it('should fill with undefined for missing elements', () => {
67
+ expect(Array.zip([1], [2, 3, 4])).toEqual([
68
+ [1, 2],
69
+ [undefined, 3],
70
+ [undefined, 4],
71
+ ]);
72
+ });
73
+ describe('edge cases', () => {
74
+ it('should return an empty array if no arrays are given', () => {
75
+ expect(Array.zip()).toEqual([]);
76
+ });
77
+ it('should return an array of undefined if all arrays are empty', () => {
78
+ expect(Array.zip([], [])).toEqual([]);
79
+ });
80
+ });
81
+ });
82
+ describe('create()', () => {
83
+ it('should create a 1D array', () => {
84
+ expect(Array.create('x', 3)).toEqual(['x', 'x', 'x']);
85
+ });
86
+ it('should create a 2D array', () => {
87
+ expect(Array.create(0, 2, 3)).toEqual([
88
+ [0, 0, 0],
89
+ [0, 0, 0],
90
+ ]);
91
+ });
92
+ it('should create a 3D array', () => {
93
+ expect(Array.create(true, 2, 2, 2)).toEqual([
94
+ [
95
+ [true, true],
96
+ [true, true],
97
+ ],
98
+ [
99
+ [true, true],
100
+ [true, true],
101
+ ],
102
+ ]);
103
+ });
104
+ describe('edge cases', () => {
105
+ it('should return [] if no sizes are given', () => {
106
+ expect(Array.create('x')).toEqual([]);
107
+ });
108
+ it('should return [] if any size is 0', () => {
109
+ expect(Array.create('x', 0)).toEqual([]);
110
+ expect(Array.create('x', 2, 0)).toEqual([[], []]);
111
+ });
112
+ it('should return arrays with the same object reference for objects', () => {
113
+ const obj = { a: 1 };
114
+ const arr = Array.create(obj, 2);
115
+ expect(arr[0]).toBe(obj);
116
+ expect(arr[1]).toBe(obj);
117
+ });
118
+ });
119
+ describe('error handling', () => {
120
+ it('should throw if a size is negative', () => {
121
+ expect(() => Array.create('x', -1)).toThrow(TypeError);
122
+ expect(() => Array.create('x', 2, -3)).toThrow(TypeError);
123
+ });
124
+ it('should throw if a size is not an integer', () => {
125
+ expect(() => Array.create('x', 2.5)).toThrow(TypeError);
126
+ expect(() => Array.create('x', 2, 1.1)).toThrow(TypeError);
127
+ });
128
+ });
129
+ });
130
+ });