mielk-fn 1.0.2 → 1.0.4
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/index.d.ts +3 -0
- package/lib/index.js +10 -0
- package/lib/methods/arrays.d.ts +8 -0
- package/lib/methods/arrays.js +30 -0
- package/lib/methods/objects.d.ts +12 -0
- package/lib/methods/objects.js +44 -0
- package/package.json +2 -2
- package/tests/objects.test.ts +1 -1
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.objects = exports.arrays = void 0;
|
|
7
|
+
const arrays_1 = __importDefault(require("./methods/arrays"));
|
|
8
|
+
exports.arrays = arrays_1.default;
|
|
9
|
+
const objects_1 = __importDefault(require("./methods/objects"));
|
|
10
|
+
exports.objects = objects_1.default;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type NumberFunction = (item: any) => number;
|
|
2
|
+
type StringNumberFunction = (item: any) => string | number;
|
|
3
|
+
type AnyFunction = (item: any) => any;
|
|
4
|
+
declare const _default: {
|
|
5
|
+
toMap: (items: any[], keyCallback: StringNumberFunction, valueFn?: AnyFunction, ignoreDuplicates?: boolean) => Map<any, any>;
|
|
6
|
+
toIndexedArray: (items: any[], callback: NumberFunction) => any[];
|
|
7
|
+
};
|
|
8
|
+
export default _default;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const toMap = (items, keyCallback, valueFn = (item) => item, ignoreDuplicates = true) => {
|
|
4
|
+
const map = new Map();
|
|
5
|
+
items.forEach((item) => {
|
|
6
|
+
const key = keyCallback(item);
|
|
7
|
+
const value = valueFn ? valueFn(item) : item;
|
|
8
|
+
if (ignoreDuplicates && map.has(key)) {
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
map.set(key, value);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
return map;
|
|
15
|
+
};
|
|
16
|
+
const toIndexedArray = (items, callback) => {
|
|
17
|
+
const arr = [];
|
|
18
|
+
items.forEach((item) => {
|
|
19
|
+
// Check if item is an object and has a function named callback
|
|
20
|
+
if (typeof item === 'object' && item !== null) {
|
|
21
|
+
const index = callback(item);
|
|
22
|
+
if (typeof index !== 'number') {
|
|
23
|
+
throw new TypeError('Callback should return a number.');
|
|
24
|
+
}
|
|
25
|
+
arr[index] = item;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return arr;
|
|
29
|
+
};
|
|
30
|
+
exports.default = { toMap, toIndexedArray };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type AnyObject = {
|
|
2
|
+
[key: string]: any;
|
|
3
|
+
};
|
|
4
|
+
type NumberStringFunction = (key: string, item: any) => number | string;
|
|
5
|
+
declare const _default: {
|
|
6
|
+
isObject: (value: unknown) => boolean;
|
|
7
|
+
isPlainObject: (value: unknown) => boolean;
|
|
8
|
+
merge: (objects: AnyObject[], override?: boolean) => AnyObject;
|
|
9
|
+
invert: (obj: Record<string | number, string | number>) => Record<string | number, string | number>;
|
|
10
|
+
modifyKeys: (obj: AnyObject, callback: NumberStringFunction, ignoreDuplicates?: boolean) => AnyObject;
|
|
11
|
+
};
|
|
12
|
+
export default _default;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const isObject = (value) => typeof value === 'object' && !Array.isArray(value) && value !== null;
|
|
4
|
+
const isPlainObject = (value) => (value === null || value === void 0 ? void 0 : value.constructor.name) === 'Object';
|
|
5
|
+
const merge = (objects, override = false) => {
|
|
6
|
+
const merged = {};
|
|
7
|
+
objects.forEach((obj) => {
|
|
8
|
+
if (isObject(obj)) {
|
|
9
|
+
const entries = Object.entries(obj);
|
|
10
|
+
entries.forEach(([key, value]) => {
|
|
11
|
+
if (!merged[key] || override)
|
|
12
|
+
merged[key] = value;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
return merged;
|
|
17
|
+
};
|
|
18
|
+
const invert = (obj) => {
|
|
19
|
+
if (!isObject(obj)) {
|
|
20
|
+
throw new Error('Invalid input: the input must be an object.');
|
|
21
|
+
}
|
|
22
|
+
const inverted = {};
|
|
23
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
24
|
+
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
25
|
+
throw new Error('Invalid value type: the value must be a string or a number.');
|
|
26
|
+
}
|
|
27
|
+
inverted[value] = key;
|
|
28
|
+
}
|
|
29
|
+
return inverted;
|
|
30
|
+
};
|
|
31
|
+
const modifyKeys = (obj, callback, ignoreDuplicates = true) => {
|
|
32
|
+
if (!isPlainObject(obj))
|
|
33
|
+
throw new TypeError(`Invalid type of ${obj === null || obj === void 0 ? void 0 : obj.constructor.name}. Expected JavaScript object`);
|
|
34
|
+
const entries = Object.entries(obj || {});
|
|
35
|
+
const result = {};
|
|
36
|
+
entries.forEach(([key, value]) => {
|
|
37
|
+
const newKey = callback(key, value);
|
|
38
|
+
if (!result.hasOwnProperty(newKey) || !ignoreDuplicates) {
|
|
39
|
+
result[newKey] = value;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
exports.default = { isObject, isPlainObject, merge, invert, modifyKeys };
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mielk-fn",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Set of helpful functions",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"
|
|
8
|
+
"prepublishOnly": "npm test",
|
|
9
9
|
"test": "jest",
|
|
10
10
|
"build": "tsc"
|
|
11
11
|
},
|
package/tests/objects.test.ts
CHANGED
|
@@ -50,7 +50,7 @@ describe('isObject', () => {
|
|
|
50
50
|
describe('isPlainObject', () => {
|
|
51
51
|
const { isPlainObject } = objects;
|
|
52
52
|
it('should return true for plain JavaScript objects', () => {
|
|
53
|
-
expect(isPlainObject({})).toBe(
|
|
53
|
+
expect(isPlainObject({})).toBe(true);
|
|
54
54
|
expect(isPlainObject({ key: 'value' })).toBe(true);
|
|
55
55
|
});
|
|
56
56
|
|