mielk-fn 1.0.13 → 1.0.14
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/lib/methods/objects.d.ts
CHANGED
|
@@ -2,9 +2,11 @@ type AnyObject = {
|
|
|
2
2
|
[key: string]: any;
|
|
3
3
|
};
|
|
4
4
|
type NumberStringFunction = (key: string, item: any) => number | string;
|
|
5
|
+
type InvertAllowedTypes = string | number;
|
|
6
|
+
declare function invert<T extends InvertAllowedTypes, U extends InvertAllowedTypes>(obj: Record<T, U>): Record<U, T>;
|
|
5
7
|
declare const _default: {
|
|
6
8
|
merge: (objects: AnyObject[], override?: boolean) => AnyObject;
|
|
7
|
-
invert:
|
|
9
|
+
invert: typeof invert;
|
|
8
10
|
modifyKeys: (obj: AnyObject, callback: NumberStringFunction, ignoreDuplicates?: boolean) => AnyObject;
|
|
9
11
|
};
|
|
10
12
|
export default _default;
|
package/lib/methods/objects.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const variables_1 = require("./variables");
|
|
4
|
+
//function invert<T extends AllowedTypes, U extends AllowedTypes>(obj: Record<T, U>): Record<U, T>
|
|
4
5
|
const merge = (objects, override = false) => {
|
|
5
6
|
const merged = {};
|
|
6
7
|
objects.forEach((obj) => {
|
|
@@ -14,19 +15,20 @@ const merge = (objects, override = false) => {
|
|
|
14
15
|
});
|
|
15
16
|
return merged;
|
|
16
17
|
};
|
|
17
|
-
|
|
18
|
+
function invert(obj) {
|
|
18
19
|
if (!(0, variables_1.isObject)(obj)) {
|
|
19
20
|
throw new Error('Invalid input: the input must be an object.');
|
|
20
21
|
}
|
|
21
22
|
const inverted = {};
|
|
22
|
-
for (const
|
|
23
|
+
for (const key in obj) {
|
|
24
|
+
const value = obj[key];
|
|
23
25
|
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
24
26
|
throw new Error('Invalid value type: the value must be a string or a number.');
|
|
25
27
|
}
|
|
26
28
|
inverted[value] = key;
|
|
27
29
|
}
|
|
28
30
|
return inverted;
|
|
29
|
-
}
|
|
31
|
+
}
|
|
30
32
|
const modifyKeys = (obj, callback, ignoreDuplicates = true) => {
|
|
31
33
|
if (!(0, variables_1.isPlainObject)(obj))
|
|
32
34
|
throw new TypeError(`Invalid type of ${obj === null || obj === void 0 ? void 0 : obj.constructor.name}. Expected JavaScript object`);
|
package/package.json
CHANGED
package/src/methods/objects.ts
CHANGED
|
@@ -2,6 +2,9 @@ import { isObject, isPlainObject } from './variables';
|
|
|
2
2
|
|
|
3
3
|
type AnyObject = { [key: string]: any };
|
|
4
4
|
type NumberStringFunction = (key: string, item: any) => number | string;
|
|
5
|
+
type InvertAllowedTypes = string | number;
|
|
6
|
+
|
|
7
|
+
//function invert<T extends AllowedTypes, U extends AllowedTypes>(obj: Record<T, U>): Record<U, T>
|
|
5
8
|
|
|
6
9
|
const merge = (objects: AnyObject[], override: boolean = false): AnyObject => {
|
|
7
10
|
const merged: AnyObject = {};
|
|
@@ -16,25 +19,24 @@ const merge = (objects: AnyObject[], override: boolean = false): AnyObject => {
|
|
|
16
19
|
return merged;
|
|
17
20
|
};
|
|
18
21
|
|
|
19
|
-
|
|
22
|
+
function invert<T extends InvertAllowedTypes, U extends InvertAllowedTypes>(obj: Record<T, U>): Record<U, T> {
|
|
20
23
|
if (!isObject(obj)) {
|
|
21
24
|
throw new Error('Invalid input: the input must be an object.');
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
const inverted
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
const inverted = {} as Record<U, T>;
|
|
28
|
+
for (const key in obj) {
|
|
29
|
+
const value = obj[key];
|
|
27
30
|
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
28
31
|
throw new Error('Invalid value type: the value must be a string or a number.');
|
|
29
32
|
}
|
|
30
|
-
|
|
31
33
|
inverted[value] = key;
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
return inverted;
|
|
35
|
-
}
|
|
37
|
+
}
|
|
36
38
|
|
|
37
|
-
const modifyKeys = (obj: AnyObject, callback: NumberStringFunction, ignoreDuplicates = true) => {
|
|
39
|
+
const modifyKeys = (obj: AnyObject, callback: NumberStringFunction, ignoreDuplicates = true): AnyObject => {
|
|
38
40
|
if (!isPlainObject(obj)) throw new TypeError(`Invalid type of ${obj?.constructor.name}. Expected JavaScript object`);
|
|
39
41
|
const entries = Object.entries(obj || {});
|
|
40
42
|
const result: AnyObject = {};
|
package/tests/objects.test.ts
CHANGED
|
@@ -77,6 +77,36 @@ describe('invert', () => {
|
|
|
77
77
|
const input = { key1: true } as unknown as Record<string | number, string | number>;
|
|
78
78
|
expect(() => invert(input)).toThrowError('Invalid value type: the value must be a string or a number.');
|
|
79
79
|
});
|
|
80
|
+
|
|
81
|
+
it('should invert object with string keys and number values', () => {
|
|
82
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
83
|
+
const result = invert(obj);
|
|
84
|
+
expect(result).toEqual({ '1': 'a', '2': 'b', '3': 'c' });
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should invert object with number keys and string values', () => {
|
|
88
|
+
const obj = { '1': 'a', '2': 'b', '3': 'c' };
|
|
89
|
+
const result = invert(obj);
|
|
90
|
+
expect(result).toEqual({ a: '1', b: '2', c: '3' });
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should invert object with string keys and string values', () => {
|
|
94
|
+
const obj = { a: '1', b: '2', c: '3' };
|
|
95
|
+
const result = invert(obj);
|
|
96
|
+
expect(result).toEqual({ '1': 'a', '2': 'b', '3': 'c' });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should invert object with number keys and number values', () => {
|
|
100
|
+
const obj = { '1': 2, '3': 4, '5': 6 };
|
|
101
|
+
const result = invert(obj);
|
|
102
|
+
expect(result).toEqual({ '2': '1', '4': '3', '6': '5' });
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('should invert object with unusual but valid keys or values', () => {
|
|
106
|
+
const obj = { '': 1, a: 0, b: -1 };
|
|
107
|
+
const result = invert(obj);
|
|
108
|
+
expect(result).toEqual({ '1': '', '0': 'a', '-1': 'b' });
|
|
109
|
+
});
|
|
80
110
|
});
|
|
81
111
|
|
|
82
112
|
describe('modifyKeys function', () => {
|