merge-anything 5.1.6 → 6.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.
@@ -0,0 +1 @@
1
+ export declare function concatArrays<T>(originVal: unknown, newVal: T): T;
@@ -0,0 +1,8 @@
1
+ import { isArray } from 'is-what';
2
+ export function concatArrays(originVal, newVal) {
3
+ if (isArray(originVal) && isArray(newVal)) {
4
+ // concat logic
5
+ return originVal.concat(newVal);
6
+ }
7
+ return newVal; // always return newVal as fallback!!
8
+ }
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './extensions.js';
2
+ export * from './merge.js';
@@ -13,5 +13,5 @@ export type Merge<T, Ts extends unknown[]> = T extends Record<string | number |
13
13
  * Basic types overwrite objects or other basic types.
14
14
  */
15
15
  export declare function merge<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
16
- export declare function mergeAndCompare<T, Tn extends unknown[]>(compareFn: (prop1: any, prop2: any, propName: string | symbol) => any, object: T, ...otherObjects: Tn): Merge<T, Tn>;
16
+ export declare function mergeAndCompare<T, Tn extends unknown[]>(compareFn: (prop1: unknown, prop2: unknown, propName: string | symbol) => any, object: T, ...otherObjects: Tn): Merge<T, Tn>;
17
17
  export declare function mergeAndConcat<T, Tn extends unknown[]>(object: T, ...otherObjects: Tn): Merge<T, Tn>;
@@ -1,13 +1,5 @@
1
- import { isArray, isPlainObject, isSymbol } from 'is-what';
2
-
3
- function concatArrays(originVal, newVal) {
4
- if (isArray(originVal) && isArray(newVal)) {
5
- // concat logic
6
- return originVal.concat(newVal);
7
- }
8
- return newVal; // always return newVal as fallback!!
9
- }
10
-
1
+ import { isPlainObject, isSymbol } from 'is-what';
2
+ import { concatArrays } from './extensions.js';
11
3
  function assignProp(carry, key, newVal, originalObject) {
12
4
  const propType = {}.propertyIsEnumerable.call(originalObject, key)
13
5
  ? 'enumerable'
@@ -63,17 +55,17 @@ function mergeRecursively(origin, newComer, compareFn) {
63
55
  * Objects get merged, special objects (classes etc.) are re-assigned "as is".
64
56
  * Basic types overwrite objects or other basic types.
65
57
  */
66
- function merge(object, ...otherObjects) {
58
+ export function merge(object, ...otherObjects) {
67
59
  return otherObjects.reduce((result, newComer) => {
68
60
  return mergeRecursively(result, newComer);
69
61
  }, object);
70
62
  }
71
- function mergeAndCompare(compareFn, object, ...otherObjects) {
63
+ export function mergeAndCompare(compareFn, object, ...otherObjects) {
72
64
  return otherObjects.reduce((result, newComer) => {
73
65
  return mergeRecursively(result, newComer, compareFn);
74
66
  }, object);
75
67
  }
76
- function mergeAndConcat(object, ...otherObjects) {
68
+ export function mergeAndConcat(object, ...otherObjects) {
77
69
  return otherObjects.reduce((result, newComer) => {
78
70
  return mergeRecursively(result, newComer, concatArrays);
79
71
  }, object);
@@ -113,5 +105,3 @@ function mergeAndConcat(object, ...otherObjects) {
113
105
  // }
114
106
  // const ca = merge<Carguments, Carguments[]>({ key: 'value1' }, { key: 'value2' })
115
107
  // type P = Pop<Carguments[]>
116
-
117
- export { concatArrays, merge, mergeAndCompare, mergeAndConcat };
@@ -1,6 +1,6 @@
1
- import type { MergeDeep } from './MergeDeep.js';
2
- import type { Iteration, IterationOf, Pos, Next } from './Iteration.js';
1
+ import type { Iteration, IterationOf, Next, Pos } from './Iteration.js';
3
2
  import type { Length, List } from './List.js';
3
+ import type { MergeDeep } from './MergeDeep.js';
4
4
  /**
5
5
  * Ask TS to re-check that `A1` extends `A2`.
6
6
  * And if it fails, `A2` will be enforced anyway.
@@ -0,0 +1,12 @@
1
+ export {};
2
+ // type A1 = { arr: string[] }
3
+ // type A2 = { arr: number[] }
4
+ // type A3 = { arr: boolean[] }
5
+ // type TestA = Assign<A1, [A2, A3]>
6
+ // type B1 = { arr: number[] }
7
+ // type B2 = { arr?: number[] }
8
+ // type TestB = Assign<B1, [B2]>
9
+ // import { Timestamp } from '../../test/Timestamp'
10
+ // type T1 = { date: Timestamp }
11
+ // type T2 = { date: Timestamp }
12
+ // type TestT = Assign<T1, [T2]>
@@ -0,0 +1 @@
1
+ export {};
@@ -8,7 +8,7 @@
8
8
  * type list1 = number[]
9
9
  * ```
10
10
  */
11
- export type List<T = any> = readonly T[];
11
+ export type List<T = unknown> = readonly T[];
12
12
  /**
13
13
  * Get the length of `L`
14
14
  * @param L to get length
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ export {};
2
+ // import { PrettyPrint } from './PrettyPrint'
3
+ // type A1 = { arr: string[]; barr?: { b: number } }
4
+ // type A2 = { arr?: number[]; barr?: { b: number } }
5
+ // type TestA = PrettyPrint<MergeDeep<A1, A2>>
6
+ // type B1 = { a: number; b?: number; d?: number; e?: number; x: string; y?: number; z: string; } // prettier-ignore
7
+ // type B2 = { a?: number; c?: number; d?: number; e: number; x: number | undefined; y?: string; z?: number; } // prettier-ignore
8
+ // type TestB = PrettyPrint<MergeDeep<B1, B2>>
9
+ // type C1 = { info: { time: string; newDate: Date; very: { deep: { prop: boolean } } } }
10
+ // type C2 = { info: { date: string; very: { deep: { prop: boolean } } } }
11
+ // type TestC = PrettyPrint<MergeDeep<C1, C2>>
12
+ // type D1 = { [key in string]?: { cool: boolean } | null }
13
+ // type D2 = { [key in string]?: { notCool: boolean } | null }
14
+ // type TestD = PrettyPrint<MergeDeep<D1, D2>>
15
+ // import { Timestamp } from '../../test/Timestamp'
16
+ // type T1 = { date: Timestamp }
17
+ // type T2 = { date: Timestamp }
18
+ // type TestT = MergeDeep<T1, T2>
19
+ // interface I1 { date: Timestamp }
20
+ // interface I2 { date: Timestamp }
21
+ // type TestI = MergeDeep<I1, I2>
@@ -0,0 +1,4 @@
1
+ export {};
2
+ // import { Timestamp } from '../../test/Timestamp'
3
+ // type T1 = { b: string } & { nested: { props: number[] } } & { date: Timestamp[]; d: any[] }
4
+ // type Test1 = PrettyPrint<T1>
package/package.json CHANGED
@@ -1,35 +1,43 @@
1
1
  {
2
2
  "name": "merge-anything",
3
- "version": "5.1.6",
4
- "sideEffects": false,
5
- "type": "module",
3
+ "version": "6.0.0",
6
4
  "description": "Merge objects & other types recursively. A simple & small integration.",
7
- "module": "./dist/index.es.js",
8
- "main": "./dist/index.cjs",
9
- "types": "./dist/types/index.d.ts",
5
+ "type": "module",
6
+ "sideEffects": false,
10
7
  "exports": {
11
8
  ".": {
12
- "types": "./dist/types/index.d.ts",
13
- "import": "./dist/index.es.js",
14
- "require": "./dist/index.cjs"
9
+ "require": {
10
+ "types": "./dist/cjs/index.d.cts",
11
+ "default": "./dist/cjs/index.cjs"
12
+ },
13
+ "import": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ }
15
17
  }
16
18
  },
17
- "files": [
18
- "dist"
19
- ],
20
19
  "engines": {
21
- "node": ">=12.13"
20
+ "node": ">=18"
22
21
  },
23
22
  "scripts": {
24
- "lint": "tsc --noEmit && eslint ./src --ext .ts",
23
+ "lint": "tsc --noEmit && eslint ./src",
25
24
  "test": "vitest run",
26
- "build": "rollup --bundleConfigAsCjs -c ./scripts/build.js",
27
- "release": "npm run lint && del dist && npm run build && np"
25
+ "build": "del-cli dist && tsc",
26
+ "release": "npm run lint && npm run build && np"
28
27
  },
29
- "repository": {
30
- "type": "git",
31
- "url": "git+https://github.com/mesqueeb/merge-anything.git"
28
+ "dependencies": {
29
+ "is-what": "^4.1.8"
30
+ },
31
+ "devDependencies": {
32
+ "del-cli": "^5.1.0",
33
+ "np": "^10.0.5",
34
+ "vitest": "^1.6.0",
35
+ "@cycraft/eslint": "^0.3.0",
36
+ "@cycraft/tsconfig": "^0.1.2"
32
37
  },
38
+ "files": [
39
+ "dist"
40
+ ],
33
41
  "keywords": [
34
42
  "javascript",
35
43
  "merge",
@@ -52,59 +60,13 @@
52
60
  "merge-combine",
53
61
  "nested-combine"
54
62
  ],
55
- "author": "Luca Ban - Mesqueeb",
63
+ "author": "Luca Ban - Mesqueeb (https://cycraft.co)",
56
64
  "funding": "https://github.com/sponsors/mesqueeb",
57
65
  "license": "MIT",
58
- "bugs": {
59
- "url": "https://github.com/mesqueeb/merge-anything/issues"
66
+ "repository": {
67
+ "type": "git",
68
+ "url": "https://github.com/mesqueeb/merge-anything.git"
60
69
  },
61
70
  "homepage": "https://github.com/mesqueeb/merge-anything#readme",
62
- "dependencies": {
63
- "is-what": "^4.1.8"
64
- },
65
- "devDependencies": {
66
- "@typescript-eslint/eslint-plugin": "^5.59.2",
67
- "@typescript-eslint/parser": "^5.59.2",
68
- "del-cli": "^5.0.0",
69
- "eslint": "^8.40.0",
70
- "eslint-config-prettier": "^8.8.0",
71
- "eslint-plugin-tree-shaking": "^1.10.0",
72
- "np": "^7.7.0",
73
- "prettier": "^2.8.8",
74
- "rollup": "^3.21.5",
75
- "rollup-plugin-typescript2": "^0.34.1",
76
- "typescript": "^5.0.4",
77
- "vitest": "^0.31.0"
78
- },
79
- "np": {
80
- "yarn": false,
81
- "branch": "production"
82
- },
83
- "eslintConfig": {
84
- "ignorePatterns": [
85
- "node_modules",
86
- "dist",
87
- "scripts",
88
- "test"
89
- ],
90
- "root": true,
91
- "parser": "@typescript-eslint/parser",
92
- "plugins": [
93
- "@typescript-eslint",
94
- "tree-shaking"
95
- ],
96
- "extends": [
97
- "eslint:recommended",
98
- "plugin:@typescript-eslint/eslint-recommended",
99
- "plugin:@typescript-eslint/recommended",
100
- "prettier"
101
- ],
102
- "rules": {
103
- "@typescript-eslint/no-empty-function": "off",
104
- "@typescript-eslint/no-explicit-any": "off",
105
- "@typescript-eslint/ban-ts-ignore": "off",
106
- "tree-shaking/no-side-effects-in-initialization": "error",
107
- "@typescript-eslint/ban-ts-comment": "off"
108
- }
109
- }
71
+ "bugs": "https://github.com/mesqueeb/merge-anything/issues"
110
72
  }
package/dist/index.cjs DELETED
@@ -1,122 +0,0 @@
1
- 'use strict';
2
-
3
- var isWhat = require('is-what');
4
-
5
- function concatArrays(originVal, newVal) {
6
- if (isWhat.isArray(originVal) && isWhat.isArray(newVal)) {
7
- // concat logic
8
- return originVal.concat(newVal);
9
- }
10
- return newVal; // always return newVal as fallback!!
11
- }
12
-
13
- function assignProp(carry, key, newVal, originalObject) {
14
- const propType = {}.propertyIsEnumerable.call(originalObject, key)
15
- ? 'enumerable'
16
- : 'nonenumerable';
17
- if (propType === 'enumerable')
18
- carry[key] = newVal;
19
- if (propType === 'nonenumerable') {
20
- Object.defineProperty(carry, key, {
21
- value: newVal,
22
- enumerable: false,
23
- writable: true,
24
- configurable: true,
25
- });
26
- }
27
- }
28
- function mergeRecursively(origin, newComer, compareFn) {
29
- // always return newComer if its not an object
30
- if (!isWhat.isPlainObject(newComer))
31
- return newComer;
32
- // define newObject to merge all values upon
33
- let newObject = {};
34
- if (isWhat.isPlainObject(origin)) {
35
- const props = Object.getOwnPropertyNames(origin);
36
- const symbols = Object.getOwnPropertySymbols(origin);
37
- newObject = [...props, ...symbols].reduce((carry, key) => {
38
- const targetVal = origin[key];
39
- if ((!isWhat.isSymbol(key) && !Object.getOwnPropertyNames(newComer).includes(key)) ||
40
- (isWhat.isSymbol(key) && !Object.getOwnPropertySymbols(newComer).includes(key))) {
41
- assignProp(carry, key, targetVal, origin);
42
- }
43
- return carry;
44
- }, {});
45
- }
46
- // newObject has all properties that newComer hasn't
47
- const props = Object.getOwnPropertyNames(newComer);
48
- const symbols = Object.getOwnPropertySymbols(newComer);
49
- const result = [...props, ...symbols].reduce((carry, key) => {
50
- // re-define the origin and newComer as targetVal and newVal
51
- let newVal = newComer[key];
52
- const targetVal = isWhat.isPlainObject(origin) ? origin[key] : undefined;
53
- // When newVal is an object do the merge recursively
54
- if (targetVal !== undefined && isWhat.isPlainObject(newVal)) {
55
- newVal = mergeRecursively(targetVal, newVal, compareFn);
56
- }
57
- const propToAssign = compareFn ? compareFn(targetVal, newVal, key) : newVal;
58
- assignProp(carry, key, propToAssign, newComer);
59
- return carry;
60
- }, newObject);
61
- return result;
62
- }
63
- /**
64
- * Merge anything recursively.
65
- * Objects get merged, special objects (classes etc.) are re-assigned "as is".
66
- * Basic types overwrite objects or other basic types.
67
- */
68
- function merge(object, ...otherObjects) {
69
- return otherObjects.reduce((result, newComer) => {
70
- return mergeRecursively(result, newComer);
71
- }, object);
72
- }
73
- function mergeAndCompare(compareFn, object, ...otherObjects) {
74
- return otherObjects.reduce((result, newComer) => {
75
- return mergeRecursively(result, newComer, compareFn);
76
- }, object);
77
- }
78
- function mergeAndConcat(object, ...otherObjects) {
79
- return otherObjects.reduce((result, newComer) => {
80
- return mergeRecursively(result, newComer, concatArrays);
81
- }, object);
82
- }
83
- // import { Timestamp } from '../test/Timestamp'
84
- // type T1 = { date: Timestamp }
85
- // type T2 = [{ b: string[] }, { b: number[] }, { date: Timestamp }]
86
- // type TestT = Merge<T1, T2>
87
- // type A1 = { arr: string[] }
88
- // type A2 = { arr: number[] }
89
- // type A3 = { arr: boolean[] }
90
- // type TestA = Merge<A1, [A2, A3]>
91
- // interface I1 {
92
- // date: Timestamp
93
- // }
94
- // interface I2 {
95
- // date: Timestamp
96
- // }
97
- // const _a: I2 = { date: '' } as unknown as I2
98
- // type TestI = Merge<I1, [I2]>
99
- // // ReturnType<(typeof merge)<I1, I2>>
100
- // const a = merge(_a, [_a])
101
- // interface Arguments extends Record<string | number | symbol, unknown> {
102
- // key: string;
103
- // }
104
- // const aa1: Arguments = { key: "value1" }
105
- // const aa2: Arguments = { key: "value2" }
106
- // const aa = merge(a1, a2);
107
- // interface Barguments {
108
- // key: string
109
- // }
110
- // const ba1: Barguments = { key: 'value1' }
111
- // const ba2: Barguments = { key: 'value2' }
112
- // const ba = merge(ba1, ba2)
113
- // interface Carguments {
114
- // key: string
115
- // }
116
- // const ca = merge<Carguments, Carguments[]>({ key: 'value1' }, { key: 'value2' })
117
- // type P = Pop<Carguments[]>
118
-
119
- exports.concatArrays = concatArrays;
120
- exports.merge = merge;
121
- exports.mergeAndCompare = mergeAndCompare;
122
- exports.mergeAndConcat = mergeAndConcat;
@@ -1 +0,0 @@
1
- export declare function concatArrays(originVal: any, newVal: any): any | any[];
@@ -1,2 +1,2 @@
1
- export * from './merge.js';
2
1
  export * from './extensions.js';
2
+ export * from './merge.js';