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