compare-anything 1.0.4 → 2.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,10 @@
1
+ import { ComparisonObjects } from './compareObjectsBasedOn.js';
2
+ export type ComparisonObjectArrays<ObjectType> = {
3
+ perIndex: ComparisonObjects<ObjectType>[];
4
+ equal: boolean;
5
+ };
6
+ export declare function compareObjectArraysBasedOn<ObjectType extends {
7
+ [key in string]: any;
8
+ } | any = {
9
+ [key in string]: any;
10
+ }>(propKeys: (keyof ObjectType)[], objectArrays: [[ObjectType, ...any[]], ...any[]]): ComparisonObjectArrays<ObjectType>;
@@ -0,0 +1,16 @@
1
+ import { compareObjectsBasedOn } from './compareObjectsBasedOn.js';
2
+ export function compareObjectArraysBasedOn(propKeys, objectArrays) {
3
+ const firstArray = objectArrays[0];
4
+ const comparison = {
5
+ perIndex: [],
6
+ equal: true,
7
+ };
8
+ firstArray.forEach((object, index) => {
9
+ const objectsToCompare = objectArrays.map((arr) => arr[index]);
10
+ const compared = compareObjectsBasedOn(propKeys, objectsToCompare);
11
+ if (!compared.equal)
12
+ comparison.equal = false;
13
+ comparison.perIndex.push(compared);
14
+ });
15
+ return comparison;
16
+ }
@@ -0,0 +1,10 @@
1
+ export type ComparisonObjects<ObjectType> = {
2
+ differentProps: (keyof ObjectType)[];
3
+ differentPropsPicked: Partial<ObjectType>[];
4
+ equal: boolean;
5
+ };
6
+ export declare function compareObjectsBasedOn<ObjectType extends {
7
+ [key in string]: any;
8
+ } | any = {
9
+ [key in string]: any;
10
+ }>(propKeys: (keyof ObjectType)[], objects: [ObjectType, ...any[]]): ComparisonObjects<ObjectType>;
@@ -0,0 +1,21 @@
1
+ import { pick } from 'filter-anything';
2
+ export function compareObjectsBasedOn(propKeys, objects) {
3
+ const baseProps = propKeys;
4
+ const baseObject = objects[0];
5
+ const differentPropsSet = objects.reduce((carry, object, index) => {
6
+ if (index === 0)
7
+ return carry;
8
+ for (const prop of baseProps) {
9
+ if (object[prop] !== baseObject[prop])
10
+ carry.add(prop);
11
+ }
12
+ return carry;
13
+ }, new Set());
14
+ const differentProps = [...differentPropsSet];
15
+ const differentPropsPicked = objects.map((object) => pick(object, differentProps));
16
+ return {
17
+ differentProps,
18
+ differentPropsPicked,
19
+ equal: differentProps.length === 0,
20
+ };
21
+ }
package/dist/index.d.ts CHANGED
@@ -1,29 +1,22 @@
1
- type ComparisonObjects<ObjectType> = {
2
- differentProps: (keyof ObjectType)[];
3
- differentPropsPicked: Partial<ObjectType>[];
4
- equal: boolean;
5
- };
6
- declare function compareObjectsBasedOn<ObjectType extends Record<string, any> | any = Record<string, any>>(propKeys: (keyof ObjectType)[], objects: [ObjectType, ...any[]]): ComparisonObjects<ObjectType>;
7
-
8
- type ComparisonObjectArrays<ObjectType> = {
9
- perIndex: ComparisonObjects<ObjectType>[];
10
- equal: boolean;
11
- };
12
- declare function compareObjectArraysBasedOn<ObjectType extends Record<string, any> | any = Record<string, any>>(propKeys: (keyof ObjectType)[], objectArrays: [[ObjectType, ...any[]], ...any[]]): ComparisonObjectArrays<ObjectType>;
13
-
14
- declare function compareObjectProps(...params: Record<string, any>[]): {
1
+ export { compareObjectArraysBasedOn } from './compareObjectArraysBasedOn.js';
2
+ export { compareObjectsBasedOn } from './compareObjectsBasedOn.js';
3
+ export declare function compareObjectProps(...params: {
4
+ [key in string]: any;
5
+ }[]): {
15
6
  props: string[];
16
7
  presentInAll: {
17
8
  [key in string]: boolean;
18
9
  };
19
10
  perProp: {
20
- [key in string]: Record<string, any>;
11
+ [key in string]: {
12
+ [key in string]: any;
13
+ };
21
14
  };
22
15
  presentIn: {
23
16
  [key in string]: number[];
24
17
  };
25
18
  };
26
- declare function compareArrays(...params: any[][]): {
19
+ export declare function compareArrays(...params: any[][]): {
27
20
  values: any[];
28
21
  infoPerValue: {
29
22
  [key in string]: {
@@ -33,5 +26,3 @@ declare function compareArrays(...params: any[][]): {
33
26
  };
34
27
  presentInAll: string[];
35
28
  };
36
-
37
- export { compareArrays, compareObjectArraysBasedOn, compareObjectProps, compareObjectsBasedOn };
package/dist/index.js CHANGED
@@ -1,107 +1,69 @@
1
1
  import { isAnyObject, isArray, isNumber, isString } from 'is-what';
2
- import { pick } from 'filter-anything';
3
-
4
- function compareObjectsBasedOn(propKeys, objects) {
5
- const baseProps = propKeys;
6
- const baseObject = objects[0];
7
- const differentPropsSet = objects.reduce((carry, object, index) => {
8
- if (index === 0)
9
- return carry;
10
- for (const prop of baseProps) {
11
- if (object[prop] !== baseObject[prop])
12
- carry.add(prop);
13
- }
14
- return carry;
15
- }, /* @__PURE__ */ new Set());
16
- const differentProps = [...differentPropsSet];
17
- const differentPropsPicked = objects.map(
18
- (object) => pick(object, differentProps)
19
- );
20
- return {
21
- differentProps,
22
- differentPropsPicked,
23
- equal: differentProps.length === 0
24
- };
25
- }
26
-
27
- function compareObjectArraysBasedOn(propKeys, objectArrays) {
28
- const firstArray = objectArrays[0];
29
- const comparison = {
30
- perIndex: [],
31
- equal: true
32
- };
33
- firstArray.forEach((object, index) => {
34
- const objectsToCompare = objectArrays.map((arr) => arr[index]);
35
- const compared = compareObjectsBasedOn(propKeys, objectsToCompare);
36
- if (!compared.equal)
37
- comparison.equal = false;
38
- comparison.perIndex.push(compared);
39
- });
40
- return comparison;
41
- }
42
-
43
- function compareObjectProps(...params) {
44
- const propsSet = /* @__PURE__ */ new Set();
45
- const res = {
46
- props: [],
47
- presentInAll: {},
48
- presentIn: {},
49
- perProp: {}
50
- };
51
- params.forEach((object, index) => {
52
- if (!isAnyObject(object)) {
53
- throw new Error("'compareObjectProps' can only compare objects");
54
- }
55
- Object.keys(object).forEach((prop) => {
56
- propsSet.add(prop);
57
- if (!(prop in res.presentIn))
58
- res.presentIn[prop] = [];
59
- res.presentIn[prop].push(index);
60
- if (!(prop in res.perProp))
61
- res.perProp[prop] = [];
62
- res.perProp[prop].push(object);
2
+ export { compareObjectArraysBasedOn } from './compareObjectArraysBasedOn.js';
3
+ export { compareObjectsBasedOn } from './compareObjectsBasedOn.js';
4
+ export function compareObjectProps(...params) {
5
+ const propsSet = new Set();
6
+ const res = {
7
+ props: [],
8
+ presentInAll: {},
9
+ presentIn: {},
10
+ perProp: {},
11
+ };
12
+ params.forEach((object, index) => {
13
+ if (!isAnyObject(object)) {
14
+ throw new Error("'compareObjectProps' can only compare objects");
15
+ }
16
+ Object.keys(object).forEach((prop) => {
17
+ propsSet.add(prop);
18
+ if (!(prop in res.presentIn))
19
+ res.presentIn[prop] = [];
20
+ res.presentIn[prop]?.push(index);
21
+ if (!(prop in res.perProp))
22
+ res.perProp[prop] = [];
23
+ res.perProp[prop]?.['push'](object);
24
+ });
63
25
  });
64
- });
65
- const paramCount = params.length;
66
- res.presentInAll = Object.keys(res.presentIn).reduce((carry, prop) => {
67
- const propCount = res.presentIn[prop].length;
68
- carry[prop] = propCount === paramCount;
69
- return carry;
70
- }, {});
71
- res.props = Array.from(propsSet);
72
- return res;
26
+ const paramCount = params.length;
27
+ res.presentInAll = Object.keys(res.presentIn).reduce((carry, prop) => {
28
+ const propCount = res.presentIn[prop]?.length;
29
+ carry[prop] = propCount === paramCount;
30
+ return carry;
31
+ }, {});
32
+ res.props = Array.from(propsSet);
33
+ return res;
73
34
  }
74
- function compareArrays(...params) {
75
- const valuesSet = /* @__PURE__ */ new Set();
76
- const res = {
77
- values: [],
78
- infoPerValue: {},
79
- presentInAll: []
80
- };
81
- const getEmptyArray = () => params.map(() => void 0);
82
- params.forEach((array, paramIndex) => {
83
- if (!isArray(array))
84
- throw new Error("'compareArrays' can only compare arrays");
85
- array.forEach((val, valIndex) => {
86
- valuesSet.add(val);
87
- const parsedVal = isNumber(val) || isString(val) ? String(val) : JSON.stringify(val);
88
- if (!(parsedVal in res.infoPerValue)) {
89
- res.infoPerValue[parsedVal] = {
90
- indexPerArray: getEmptyArray(),
91
- presentInAll: false
92
- };
93
- }
94
- res.infoPerValue[parsedVal].indexPerArray[paramIndex] = valIndex;
35
+ export function compareArrays(...params) {
36
+ const valuesSet = new Set();
37
+ const res = {
38
+ values: [],
39
+ infoPerValue: {},
40
+ presentInAll: [],
41
+ };
42
+ const getEmptyArray = () => params.map(() => undefined);
43
+ params.forEach((array, paramIndex) => {
44
+ if (!isArray(array))
45
+ throw new Error("'compareArrays' can only compare arrays");
46
+ array.forEach((val, valIndex) => {
47
+ valuesSet.add(val);
48
+ const parsedVal = isNumber(val) || isString(val) ? String(val) : JSON.stringify(val);
49
+ if (!(parsedVal in res.infoPerValue)) {
50
+ res.infoPerValue[parsedVal] = {
51
+ indexPerArray: getEmptyArray(),
52
+ presentInAll: false,
53
+ };
54
+ }
55
+ const v = res.infoPerValue[parsedVal];
56
+ if (v)
57
+ v.indexPerArray[paramIndex] = valIndex;
58
+ });
59
+ });
60
+ Object.entries(res.infoPerValue).forEach(([parsedVal, info]) => {
61
+ const isInAllArrays = info.indexPerArray.every((index) => isNumber(index) && index >= 0) &&
62
+ info.indexPerArray.length === params.length;
63
+ info.presentInAll = isInAllArrays;
64
+ if (isInAllArrays)
65
+ res.presentInAll.push(parsedVal);
95
66
  });
96
- });
97
- Object.entries(res.infoPerValue).forEach(([parsedVal, info]) => {
98
- const isInAllArrays = info.indexPerArray.every((index) => isNumber(index) && index >= 0) && info.indexPerArray.length === params.length;
99
- info.presentInAll = isInAllArrays;
100
- if (isInAllArrays)
101
- res.presentInAll.push(parsedVal);
102
- });
103
- res.values = Array.from(valuesSet);
104
- return res;
67
+ res.values = Array.from(valuesSet);
68
+ return res;
105
69
  }
106
-
107
- export { compareArrays, compareObjectArraysBasedOn, compareObjectProps, compareObjectsBasedOn };
package/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "compare-anything",
3
- "version": "1.0.4",
3
+ "version": "2.0.0",
4
4
  "description": "Compares objects and arrays and tells you which props or values are duplicates, and which are only present once.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
- "types": "./dist/index.d.ts",
8
- "module": "./dist/index.js",
9
- "main": "./dist/index.js",
10
7
  "exports": {
11
8
  ".": {
12
9
  "require": {
@@ -19,42 +16,30 @@
19
16
  }
20
17
  }
21
18
  },
22
- "files": [
23
- "dist"
24
- ],
25
19
  "engines": {
26
- "node": ">=12.13"
20
+ "node": ">=18"
27
21
  },
28
- "repository": "https://github.com/mesqueeb/compare-anything",
29
- "author": "Luca Ban - Mesqueeb",
30
- "funding": "https://github.com/sponsors/mesqueeb",
31
- "license": "MIT",
32
22
  "scripts": {
33
23
  "test": "vitest run",
34
- "lint": "tsc --noEmit && eslint ./src --ext .ts",
35
- "build": "rollup -c ./rollup.config.js",
36
- "release": "npm run lint && del dist && npm run build && np"
24
+ "lint": "tsc --noEmit && eslint ./src",
25
+ "build": "del-cli dist && tsc",
26
+ "release": "npm run lint && npm run build && np"
37
27
  },
38
28
  "dependencies": {
39
- "filter-anything": "^2.2.4",
40
- "is-what": "^4.1.8"
29
+ "filter-anything": "^4.0.0",
30
+ "is-what": "^5.0.0"
41
31
  },
42
32
  "devDependencies": {
43
- "@typescript-eslint/eslint-plugin": "^5.59.2",
44
- "@typescript-eslint/parser": "^5.59.2",
45
- "del-cli": "^5.0.0",
46
- "eslint": "^8.40.0",
47
- "eslint-config-prettier": "^8.8.0",
48
- "eslint-plugin-tree-shaking": "^1.10.0",
49
- "flatten-anything": "^2.0.7",
50
- "np": "^7.7.0",
51
- "prettier": "^2.8.8",
52
- "rollup": "^3.23.0",
53
- "rollup-plugin-dts": "^5.3.0",
54
- "rollup-plugin-esbuild": "^5.0.0",
55
- "typescript": "^4.9.5",
56
- "vitest": "^0.31.0"
33
+ "del-cli": "^5.1.0",
34
+ "flatten-anything": "^3.0.5",
35
+ "np": "^10.0.5",
36
+ "vitest": "^1.6.0",
37
+ "@cycraft/eslint": "^0.3.0",
38
+ "@cycraft/tsconfig": "^0.1.2"
57
39
  },
40
+ "files": [
41
+ "dist"
42
+ ],
58
43
  "keywords": [
59
44
  "typescript",
60
45
  "javascript",
@@ -67,39 +52,13 @@
67
52
  "find-duplicates",
68
53
  "remove-duplicates"
69
54
  ],
70
- "bugs": {
71
- "url": "https://github.com/mesqueeb/compare-anything/issues"
55
+ "author": "Luca Ban - Mesqueeb (https://cycraft.co)",
56
+ "funding": "https://github.com/sponsors/mesqueeb",
57
+ "license": "MIT",
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "https://github.com/mesqueeb/compare-anything.git"
72
61
  },
73
62
  "homepage": "https://github.com/mesqueeb/compare-anything#readme",
74
- "np": {
75
- "yarn": false,
76
- "branch": "production"
77
- },
78
- "eslintConfig": {
79
- "ignorePatterns": [
80
- "node_modules",
81
- "dist",
82
- "scripts",
83
- "test"
84
- ],
85
- "root": true,
86
- "parser": "@typescript-eslint/parser",
87
- "plugins": [
88
- "@typescript-eslint",
89
- "tree-shaking"
90
- ],
91
- "extends": [
92
- "eslint:recommended",
93
- "plugin:@typescript-eslint/eslint-recommended",
94
- "plugin:@typescript-eslint/recommended",
95
- "prettier"
96
- ],
97
- "rules": {
98
- "@typescript-eslint/no-empty-function": "off",
99
- "@typescript-eslint/no-explicit-any": "off",
100
- "@typescript-eslint/ban-ts-ignore": "off",
101
- "tree-shaking/no-side-effects-in-initialization": "error",
102
- "@typescript-eslint/ban-ts-comment": "off"
103
- }
104
- }
63
+ "bugs": "https://github.com/mesqueeb/compare-anything/issues"
105
64
  }
@@ -1,112 +0,0 @@
1
- 'use strict';
2
-
3
- const isWhat = require('is-what');
4
- const filterAnything = require('filter-anything');
5
-
6
- function compareObjectsBasedOn(propKeys, objects) {
7
- const baseProps = propKeys;
8
- const baseObject = objects[0];
9
- const differentPropsSet = objects.reduce((carry, object, index) => {
10
- if (index === 0)
11
- return carry;
12
- for (const prop of baseProps) {
13
- if (object[prop] !== baseObject[prop])
14
- carry.add(prop);
15
- }
16
- return carry;
17
- }, /* @__PURE__ */ new Set());
18
- const differentProps = [...differentPropsSet];
19
- const differentPropsPicked = objects.map(
20
- (object) => filterAnything.pick(object, differentProps)
21
- );
22
- return {
23
- differentProps,
24
- differentPropsPicked,
25
- equal: differentProps.length === 0
26
- };
27
- }
28
-
29
- function compareObjectArraysBasedOn(propKeys, objectArrays) {
30
- const firstArray = objectArrays[0];
31
- const comparison = {
32
- perIndex: [],
33
- equal: true
34
- };
35
- firstArray.forEach((object, index) => {
36
- const objectsToCompare = objectArrays.map((arr) => arr[index]);
37
- const compared = compareObjectsBasedOn(propKeys, objectsToCompare);
38
- if (!compared.equal)
39
- comparison.equal = false;
40
- comparison.perIndex.push(compared);
41
- });
42
- return comparison;
43
- }
44
-
45
- function compareObjectProps(...params) {
46
- const propsSet = /* @__PURE__ */ new Set();
47
- const res = {
48
- props: [],
49
- presentInAll: {},
50
- presentIn: {},
51
- perProp: {}
52
- };
53
- params.forEach((object, index) => {
54
- if (!isWhat.isAnyObject(object)) {
55
- throw new Error("'compareObjectProps' can only compare objects");
56
- }
57
- Object.keys(object).forEach((prop) => {
58
- propsSet.add(prop);
59
- if (!(prop in res.presentIn))
60
- res.presentIn[prop] = [];
61
- res.presentIn[prop].push(index);
62
- if (!(prop in res.perProp))
63
- res.perProp[prop] = [];
64
- res.perProp[prop].push(object);
65
- });
66
- });
67
- const paramCount = params.length;
68
- res.presentInAll = Object.keys(res.presentIn).reduce((carry, prop) => {
69
- const propCount = res.presentIn[prop].length;
70
- carry[prop] = propCount === paramCount;
71
- return carry;
72
- }, {});
73
- res.props = Array.from(propsSet);
74
- return res;
75
- }
76
- function compareArrays(...params) {
77
- const valuesSet = /* @__PURE__ */ new Set();
78
- const res = {
79
- values: [],
80
- infoPerValue: {},
81
- presentInAll: []
82
- };
83
- const getEmptyArray = () => params.map(() => void 0);
84
- params.forEach((array, paramIndex) => {
85
- if (!isWhat.isArray(array))
86
- throw new Error("'compareArrays' can only compare arrays");
87
- array.forEach((val, valIndex) => {
88
- valuesSet.add(val);
89
- const parsedVal = isWhat.isNumber(val) || isWhat.isString(val) ? String(val) : JSON.stringify(val);
90
- if (!(parsedVal in res.infoPerValue)) {
91
- res.infoPerValue[parsedVal] = {
92
- indexPerArray: getEmptyArray(),
93
- presentInAll: false
94
- };
95
- }
96
- res.infoPerValue[parsedVal].indexPerArray[paramIndex] = valIndex;
97
- });
98
- });
99
- Object.entries(res.infoPerValue).forEach(([parsedVal, info]) => {
100
- const isInAllArrays = info.indexPerArray.every((index) => isWhat.isNumber(index) && index >= 0) && info.indexPerArray.length === params.length;
101
- info.presentInAll = isInAllArrays;
102
- if (isInAllArrays)
103
- res.presentInAll.push(parsedVal);
104
- });
105
- res.values = Array.from(valuesSet);
106
- return res;
107
- }
108
-
109
- exports.compareArrays = compareArrays;
110
- exports.compareObjectArraysBasedOn = compareObjectArraysBasedOn;
111
- exports.compareObjectProps = compareObjectProps;
112
- exports.compareObjectsBasedOn = compareObjectsBasedOn;
@@ -1,37 +0,0 @@
1
- type ComparisonObjects<ObjectType> = {
2
- differentProps: (keyof ObjectType)[];
3
- differentPropsPicked: Partial<ObjectType>[];
4
- equal: boolean;
5
- };
6
- declare function compareObjectsBasedOn<ObjectType extends Record<string, any> | any = Record<string, any>>(propKeys: (keyof ObjectType)[], objects: [ObjectType, ...any[]]): ComparisonObjects<ObjectType>;
7
-
8
- type ComparisonObjectArrays<ObjectType> = {
9
- perIndex: ComparisonObjects<ObjectType>[];
10
- equal: boolean;
11
- };
12
- declare function compareObjectArraysBasedOn<ObjectType extends Record<string, any> | any = Record<string, any>>(propKeys: (keyof ObjectType)[], objectArrays: [[ObjectType, ...any[]], ...any[]]): ComparisonObjectArrays<ObjectType>;
13
-
14
- declare function compareObjectProps(...params: Record<string, any>[]): {
15
- props: string[];
16
- presentInAll: {
17
- [key in string]: boolean;
18
- };
19
- perProp: {
20
- [key in string]: Record<string, any>;
21
- };
22
- presentIn: {
23
- [key in string]: number[];
24
- };
25
- };
26
- declare function compareArrays(...params: any[][]): {
27
- values: any[];
28
- infoPerValue: {
29
- [key in string]: {
30
- indexPerArray: (number | undefined)[];
31
- presentInAll: boolean;
32
- };
33
- };
34
- presentInAll: string[];
35
- };
36
-
37
- export { compareArrays, compareObjectArraysBasedOn, compareObjectProps, compareObjectsBasedOn };