compare-anything 0.3.3 → 0.3.7

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/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Compare anything 🛰
2
2
 
3
+ <a href="https://www.npmjs.com/package/compare-anything"><img src="https://img.shields.io/npm/v/compare-anything.svg" alt="Total Downloads"></a>
4
+ <a href="https://www.npmjs.com/package/compare-anything"><img src="https://img.shields.io/npm/dw/compare-anything.svg" alt="Latest Stable Version"></a>
5
+
3
6
  ```
4
7
  npm i compare-anything
5
8
  ```
package/dist/index.cjs ADDED
@@ -0,0 +1,113 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var isWhat = require('is-what');
6
+ var filterAnything = require('filter-anything');
7
+
8
+ function compareObjectsBasedOn(propKeys, objects) {
9
+ const baseProps = propKeys;
10
+ const baseObject = objects[0];
11
+ const differentPropsSet = objects.reduce((carry, object, index) => {
12
+ if (index === 0)
13
+ return carry;
14
+ for (const prop of baseProps) {
15
+ if (object[prop] !== baseObject[prop])
16
+ carry.add(prop);
17
+ }
18
+ return carry;
19
+ }, new Set());
20
+ const differentProps = [...differentPropsSet];
21
+ const differentPropsPicked = objects.map((object) => filterAnything.pick(object, differentProps));
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 = 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 = new Set();
78
+ const res = {
79
+ values: [],
80
+ infoPerValue: {},
81
+ presentInAll: [],
82
+ };
83
+ const getEmptyArray = () => params.map(() => undefined);
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) &&
101
+ info.indexPerArray.length === params.length;
102
+ info.presentInAll = isInAllArrays;
103
+ if (isInAllArrays)
104
+ res.presentInAll.push(parsedVal);
105
+ });
106
+ res.values = Array.from(valuesSet);
107
+ return res;
108
+ }
109
+
110
+ exports.compareArrays = compareArrays;
111
+ exports.compareObjectArraysBasedOn = compareObjectArraysBasedOn;
112
+ exports.compareObjectProps = compareObjectProps;
113
+ exports.compareObjectsBasedOn = compareObjectsBasedOn;
@@ -0,0 +1,106 @@
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
+ };
23
+ }
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;
39
+ }
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 };
@@ -1,7 +1,6 @@
1
1
  import { ComparisonObjects } from './compareObjectsBasedOn';
2
- import { PlainObject } from './types';
3
2
  export declare type ComparisonObjectArrays<ObjectType> = {
4
3
  perIndex: ComparisonObjects<ObjectType>[];
5
4
  equal: boolean;
6
5
  };
7
- export declare function compareObjectArraysBasedOn<ObjectType extends PlainObject | any = PlainObject>(propKeys: (keyof ObjectType)[], objectArrays: [[ObjectType, ...any[]], ...any[]]): ComparisonObjectArrays<ObjectType>;
6
+ export declare function compareObjectArraysBasedOn<ObjectType extends Record<string, any> | any = Record<string, any>>(propKeys: (keyof ObjectType)[], objectArrays: [[ObjectType, ...any[]], ...any[]]): ComparisonObjectArrays<ObjectType>;
@@ -1,7 +1,6 @@
1
- import { PlainObject } from './types';
2
1
  export declare type ComparisonObjects<ObjectType> = {
3
2
  differentProps: (keyof ObjectType)[];
4
3
  differentPropsPicked: Partial<ObjectType>[];
5
4
  equal: boolean;
6
5
  };
7
- export declare function compareObjectsBasedOn<ObjectType extends PlainObject | any = PlainObject>(propKeys: (keyof ObjectType)[], objects: [ObjectType, ...any[]]): ComparisonObjects<ObjectType>;
6
+ export declare function compareObjectsBasedOn<ObjectType extends Record<string, any> | any = Record<string, any>>(propKeys: (keyof ObjectType)[], objects: [ObjectType, ...any[]]): ComparisonObjects<ObjectType>;
@@ -1,27 +1,24 @@
1
1
  export { compareObjectsBasedOn } from './compareObjectsBasedOn';
2
2
  export { compareObjectArraysBasedOn } from './compareObjectArraysBasedOn';
3
- declare type plainObject = {
4
- [key: string]: any;
5
- };
6
- export declare function compareObjectProps(...params: plainObject[]): {
3
+ export declare function compareObjectProps(...params: Record<string, any>[]): {
7
4
  props: string[];
8
5
  presentInAll: {
9
- [prop: string]: boolean;
6
+ [key in string]: boolean;
10
7
  };
11
8
  perProp: {
12
- [prop: string]: plainObject;
9
+ [key in string]: Record<string, any>;
13
10
  };
14
11
  presentIn: {
15
- [prop: string]: number[];
12
+ [key in string]: number[];
16
13
  };
17
14
  };
18
15
  export declare function compareArrays(...params: any[][]): {
19
16
  values: any[];
20
17
  infoPerValue: {
21
- [prop: string]: {
18
+ [key in string]: {
22
19
  indexPerArray: (number | undefined)[];
23
20
  presentInAll: boolean;
24
21
  };
25
22
  };
26
- presentInAll: any[];
23
+ presentInAll: string[];
27
24
  };
package/package.json CHANGED
@@ -1,37 +1,54 @@
1
1
  {
2
2
  "name": "compare-anything",
3
3
  "sideEffects": false,
4
- "version": "0.3.3",
4
+ "type": "module",
5
+ "version": "0.3.7",
5
6
  "description": "Compares objects and arrays and tells you which props or values are duplicates, and which are only present once.",
6
- "main": "dist/index.cjs.js",
7
- "module": "dist/index.esm.js",
8
- "typings": "types/index.d.ts",
7
+ "module": "./dist/index.es.js",
8
+ "main": "./dist/index.cjs",
9
+ "types": "./dist/types/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./dist/index.es.js",
13
+ "require": "./dist/index.cjs",
14
+ "types": "./dist/types/index.d.ts"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=12.13",
22
+ "npm": ">=7"
23
+ },
9
24
  "repository": "https://github.com/mesqueeb/compare-anything",
10
25
  "author": "Luca Ban - Mesqueeb",
26
+ "funding": "https://github.com/sponsors/mesqueeb",
11
27
  "license": "MIT",
12
28
  "scripts": {
13
- "test": "ava",
14
- "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
15
- "rollup": "rollup -c build/rollup.js",
16
- "build": "npm run lint && npm run rollup && npm run test"
29
+ "test": "vitest run",
30
+ "lint": "tsc --noEmit && eslint ./src --ext .ts",
31
+ "build": "rollup -c ./scripts/build.js",
32
+ "release": "npm run lint && del dist && npm run build && np"
17
33
  },
18
34
  "dependencies": {
19
- "filter-anything": "^2.1.5",
20
- "is-what": "^3.11.2"
35
+ "filter-anything": "^2.2.1",
36
+ "is-what": "^4.1.1"
21
37
  },
22
38
  "devDependencies": {
23
- "@typescript-eslint/eslint-plugin": "^2.34.0",
24
- "@typescript-eslint/parser": "^2.34.0",
25
- "ava": "^3.13.0",
26
- "eslint": "^6.8.0",
27
- "eslint-config-prettier": "^6.13.0",
28
- "eslint-plugin-tree-shaking": "^1.8.0",
29
- "flatten-anything": "^2.0.3",
30
- "rollup": "^2.32.0",
31
- "rollup-plugin-typescript2": "^0.28.0",
32
- "tsconfig-paths": "^3.9.0",
33
- "ts-node": "^8.10.2",
34
- "typescript": "^3.9.7"
39
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
40
+ "@typescript-eslint/parser": "^5.10.1",
41
+ "del-cli": "^4.0.1",
42
+ "eslint": "^8.7.0",
43
+ "eslint-config-prettier": "^8.3.0",
44
+ "eslint-plugin-tree-shaking": "^1.10.0",
45
+ "flatten-anything": "^2.0.4",
46
+ "np": "^7.6.0",
47
+ "prettier": "^2.5.1",
48
+ "rollup": "^2.66.0",
49
+ "rollup-plugin-typescript2": "^0.31.1",
50
+ "typescript": "^4.5.5",
51
+ "vitest": "^0.2.1"
35
52
  },
36
53
  "keywords": [
37
54
  "typescript",
@@ -49,13 +66,35 @@
49
66
  "url": "https://github.com/mesqueeb/compare-anything/issues"
50
67
  },
51
68
  "homepage": "https://github.com/mesqueeb/compare-anything#readme",
52
- "ava": {
53
- "extensions": [
54
- "ts"
69
+ "np": {
70
+ "yarn": false,
71
+ "branch": "production"
72
+ },
73
+ "eslintConfig": {
74
+ "ignorePatterns": [
75
+ "node_modules",
76
+ "dist",
77
+ "scripts",
78
+ "test"
79
+ ],
80
+ "root": true,
81
+ "parser": "@typescript-eslint/parser",
82
+ "plugins": [
83
+ "@typescript-eslint",
84
+ "tree-shaking"
85
+ ],
86
+ "extends": [
87
+ "eslint:recommended",
88
+ "plugin:@typescript-eslint/eslint-recommended",
89
+ "plugin:@typescript-eslint/recommended",
90
+ "prettier"
55
91
  ],
56
- "require": [
57
- "tsconfig-paths/register",
58
- "ts-node/register"
59
- ]
92
+ "rules": {
93
+ "@typescript-eslint/no-empty-function": "off",
94
+ "@typescript-eslint/no-explicit-any": "off",
95
+ "@typescript-eslint/ban-ts-ignore": "off",
96
+ "tree-shaking/no-side-effects-in-initialization": "error",
97
+ "@typescript-eslint/ban-ts-comment": "off"
98
+ }
60
99
  }
61
100
  }
package/.eslintignore DELETED
@@ -1,10 +0,0 @@
1
- # don't ever lint node_modules
2
- node_modules
3
- # don't lint build output (make sure it's set to your correct build folder name)
4
- dist
5
- # don't lint nyc coverage output
6
- coverage
7
-
8
- test
9
-
10
- .eslintrc.js
package/.eslintrc.js DELETED
@@ -1,17 +0,0 @@
1
- // npm i -D @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-prettier eslint-plugin-tree-shaking
2
- module.exports = {
3
- root: true,
4
- parser: '@typescript-eslint/parser',
5
- plugins: ['@typescript-eslint', 'tree-shaking'],
6
- extends: [
7
- 'eslint:recommended',
8
- 'plugin:@typescript-eslint/eslint-recommended',
9
- 'plugin:@typescript-eslint/recommended',
10
- 'prettier/@typescript-eslint',
11
- ],
12
- rules: {
13
- '@typescript-eslint/no-explicit-any': 'off',
14
- '@typescript-eslint/ban-ts-ignore': 'off',
15
- 'tree-shaking/no-side-effects-in-initialization': 'error',
16
- },
17
- }
@@ -1,12 +0,0 @@
1
- # These are supported funding model platforms
2
-
3
- github: mesqueeb
4
- patreon: # Replace with a single Patreon username
5
- open_collective: # Replace with a single Open Collective username
6
- ko_fi: # Replace with a single Ko-fi username
7
- tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8
- community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
- liberapay: # Replace with a single Liberapay username
10
- issuehunt: # Replace with a single IssueHunt username
11
- otechie: # Replace with a single Otechie username
12
- custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
package/build/rollup.js DELETED
@@ -1,64 +0,0 @@
1
- /* eslint-disable */
2
-
3
- // npm install rollup-plugin-typescript2 typescript --save-dev
4
- import typescript from 'rollup-plugin-typescript2'
5
- // import { terser } from 'rollup-plugin-terser'
6
- // import resolve from 'rollup-plugin-node-resolve'
7
-
8
- // ------------------------------------------------------------------------------------------
9
- // formats
10
- // ------------------------------------------------------------------------------------------
11
- // amd – Asynchronous Module Definition, used with module loaders like RequireJS
12
- // cjs – CommonJS, suitable for Node and Browserify/Webpack
13
- // esm – Keep the bundle as an ES module file
14
- // iife – A self-executing function, suitable for inclusion as a <script> tag. (If you want to create a bundle for your application, you probably want to use this, because it leads to smaller file sizes.)
15
- // umd – Universal Module Definition, works as amd, cjs and iife all in one
16
- // system – Native format of the SystemJS loader
17
-
18
- // ------------------------------------------------------------------------------------------
19
- // setup
20
- // ------------------------------------------------------------------------------------------
21
- const pkg = require('../package.json')
22
- const name = pkg.name
23
- const className = name.replace(/(^\w|-\w)/g, c =>
24
- c.replace('-', '').toUpperCase()
25
- )
26
- const external = Object.keys(pkg.dependencies || [])
27
- const plugins = [
28
- typescript({
29
- useTsconfigDeclarationDir: true,
30
- tsconfigOverride: { exclude: ['test/**/*'] }
31
- })
32
- ]
33
-
34
- // ------------------------------------------------------------------------------------------
35
- // Builds
36
- // ------------------------------------------------------------------------------------------
37
- function defaults (config) {
38
- // defaults
39
- const defaults = {
40
- plugins,
41
- external
42
- }
43
- // defaults.output
44
- config.output = config.output.map(output => {
45
- return Object.assign(
46
- {
47
- sourcemap: false,
48
- name: className
49
- },
50
- output
51
- )
52
- })
53
- return Object.assign(defaults, config)
54
- }
55
-
56
- export default [
57
- defaults({
58
- input: 'src/index.ts',
59
- output: [
60
- { file: 'dist/index.cjs.js', format: 'cjs' },
61
- { file: 'dist/index.esm.js', format: 'esm' }
62
- ]
63
- })
64
- ]
package/dist/index.cjs.js DELETED
@@ -1,185 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var isWhat = require('is-what');
6
- var filterAnything = require('filter-anything');
7
-
8
- /*! *****************************************************************************
9
- Copyright (c) Microsoft Corporation.
10
-
11
- Permission to use, copy, modify, and/or distribute this software for any
12
- purpose with or without fee is hereby granted.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
- PERFORMANCE OF THIS SOFTWARE.
21
- ***************************************************************************** */
22
-
23
- function __values(o) {
24
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
25
- if (m) return m.call(o);
26
- if (o && typeof o.length === "number") return {
27
- next: function () {
28
- if (o && i >= o.length) o = void 0;
29
- return { value: o && o[i++], done: !o };
30
- }
31
- };
32
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
33
- }
34
-
35
- function __read(o, n) {
36
- var m = typeof Symbol === "function" && o[Symbol.iterator];
37
- if (!m) return o;
38
- var i = m.call(o), r, ar = [], e;
39
- try {
40
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
41
- }
42
- catch (error) { e = { error: error }; }
43
- finally {
44
- try {
45
- if (r && !r.done && (m = i["return"])) m.call(i);
46
- }
47
- finally { if (e) throw e.error; }
48
- }
49
- return ar;
50
- }
51
-
52
- function __spread() {
53
- for (var ar = [], i = 0; i < arguments.length; i++)
54
- ar = ar.concat(__read(arguments[i]));
55
- return ar;
56
- }
57
-
58
- function compareObjectsBasedOn(propKeys, objects) {
59
- var baseProps = propKeys;
60
- var baseObject = objects[0];
61
- var differentPropsSet = objects.reduce(function (carry, object, index) {
62
- var e_1, _a;
63
- if (index === 0)
64
- return carry;
65
- try {
66
- for (var baseProps_1 = __values(baseProps), baseProps_1_1 = baseProps_1.next(); !baseProps_1_1.done; baseProps_1_1 = baseProps_1.next()) {
67
- var prop = baseProps_1_1.value;
68
- if (object[prop] !== baseObject[prop])
69
- carry.add(prop);
70
- }
71
- }
72
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
73
- finally {
74
- try {
75
- if (baseProps_1_1 && !baseProps_1_1.done && (_a = baseProps_1.return)) _a.call(baseProps_1);
76
- }
77
- finally { if (e_1) throw e_1.error; }
78
- }
79
- return carry;
80
- }, new Set());
81
- var differentProps = __spread(differentPropsSet);
82
- var differentPropsPicked = objects.map(function (object) {
83
- return filterAnything.pick(object, differentProps);
84
- });
85
- return {
86
- differentProps: differentProps,
87
- differentPropsPicked: differentPropsPicked,
88
- equal: differentProps.length === 0
89
- };
90
- }
91
-
92
- function compareObjectArraysBasedOn(propKeys, objectArrays) {
93
- var firstArray = objectArrays[0];
94
- var comparison = {
95
- perIndex: [],
96
- equal: true
97
- };
98
- firstArray.forEach(function (object, index) {
99
- var objectsToCompare = objectArrays.map(function (arr) { return arr[index]; });
100
- var compared = compareObjectsBasedOn(propKeys, objectsToCompare);
101
- if (!compared.equal)
102
- comparison.equal = false;
103
- comparison.perIndex.push(compared);
104
- });
105
- return comparison;
106
- }
107
-
108
- function compareObjectProps() {
109
- var params = [];
110
- for (var _i = 0; _i < arguments.length; _i++) {
111
- params[_i] = arguments[_i];
112
- }
113
- var propsSet = new Set();
114
- var res = {
115
- props: null,
116
- presentInAll: null,
117
- presentIn: {},
118
- perProp: {}
119
- };
120
- params.forEach(function (object, index) {
121
- if (!isWhat.isAnyObject(object)) {
122
- throw new Error("'compareObjectProps' can only compare objects");
123
- }
124
- Object.keys(object).forEach(function (prop) {
125
- propsSet.add(prop);
126
- if (!(prop in res.presentIn))
127
- res.presentIn[prop] = [];
128
- res.presentIn[prop].push(index);
129
- if (!(prop in res.perProp))
130
- res.perProp[prop] = [];
131
- res.perProp[prop].push(object);
132
- });
133
- });
134
- var paramCount = params.length;
135
- res.presentInAll = Object.keys(res.presentIn).reduce(function (carry, prop) {
136
- var propCount = res.presentIn[prop].length;
137
- carry[prop] = propCount === paramCount;
138
- return carry;
139
- }, {});
140
- res.props = Array.from(propsSet);
141
- return res;
142
- }
143
- function compareArrays() {
144
- var params = [];
145
- for (var _i = 0; _i < arguments.length; _i++) {
146
- params[_i] = arguments[_i];
147
- }
148
- var valuesSet = new Set();
149
- var res = {
150
- values: null,
151
- infoPerValue: {},
152
- presentInAll: []
153
- };
154
- var getEmptyArray = function () { return params.map(function () { return undefined; }); };
155
- params.forEach(function (array, paramIndex) {
156
- if (!isWhat.isArray(array))
157
- throw new Error("'compareArrays' can only compare arrays");
158
- array.forEach(function (val, valIndex) {
159
- valuesSet.add(val);
160
- var parsedVal = isWhat.isNumber(val) || isWhat.isString(val) ? String(val) : JSON.stringify(val);
161
- if (!(parsedVal in res.infoPerValue)) {
162
- res.infoPerValue[parsedVal] = {
163
- indexPerArray: getEmptyArray(),
164
- presentInAll: false
165
- };
166
- }
167
- res.infoPerValue[parsedVal].indexPerArray[paramIndex] = valIndex;
168
- });
169
- });
170
- Object.entries(res.infoPerValue).forEach(function (_a) {
171
- var _b = __read(_a, 2), parsedVal = _b[0], info = _b[1];
172
- var isInAllArrays = info.indexPerArray.every(function (index) { return isWhat.isNumber(index) && index >= 0; }) &&
173
- info.indexPerArray.length === params.length;
174
- info.presentInAll = isInAllArrays;
175
- if (isInAllArrays)
176
- res.presentInAll.push(parsedVal);
177
- });
178
- res.values = Array.from(valuesSet);
179
- return res;
180
- }
181
-
182
- exports.compareArrays = compareArrays;
183
- exports.compareObjectArraysBasedOn = compareObjectArraysBasedOn;
184
- exports.compareObjectProps = compareObjectProps;
185
- exports.compareObjectsBasedOn = compareObjectsBasedOn;
package/dist/index.esm.js DELETED
@@ -1,178 +0,0 @@
1
- import { isAnyObject, isArray, isNumber, isString } from 'is-what';
2
- import { pick } from 'filter-anything';
3
-
4
- /*! *****************************************************************************
5
- Copyright (c) Microsoft Corporation.
6
-
7
- Permission to use, copy, modify, and/or distribute this software for any
8
- purpose with or without fee is hereby granted.
9
-
10
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
- PERFORMANCE OF THIS SOFTWARE.
17
- ***************************************************************************** */
18
-
19
- function __values(o) {
20
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
21
- if (m) return m.call(o);
22
- if (o && typeof o.length === "number") return {
23
- next: function () {
24
- if (o && i >= o.length) o = void 0;
25
- return { value: o && o[i++], done: !o };
26
- }
27
- };
28
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
29
- }
30
-
31
- function __read(o, n) {
32
- var m = typeof Symbol === "function" && o[Symbol.iterator];
33
- if (!m) return o;
34
- var i = m.call(o), r, ar = [], e;
35
- try {
36
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
37
- }
38
- catch (error) { e = { error: error }; }
39
- finally {
40
- try {
41
- if (r && !r.done && (m = i["return"])) m.call(i);
42
- }
43
- finally { if (e) throw e.error; }
44
- }
45
- return ar;
46
- }
47
-
48
- function __spread() {
49
- for (var ar = [], i = 0; i < arguments.length; i++)
50
- ar = ar.concat(__read(arguments[i]));
51
- return ar;
52
- }
53
-
54
- function compareObjectsBasedOn(propKeys, objects) {
55
- var baseProps = propKeys;
56
- var baseObject = objects[0];
57
- var differentPropsSet = objects.reduce(function (carry, object, index) {
58
- var e_1, _a;
59
- if (index === 0)
60
- return carry;
61
- try {
62
- for (var baseProps_1 = __values(baseProps), baseProps_1_1 = baseProps_1.next(); !baseProps_1_1.done; baseProps_1_1 = baseProps_1.next()) {
63
- var prop = baseProps_1_1.value;
64
- if (object[prop] !== baseObject[prop])
65
- carry.add(prop);
66
- }
67
- }
68
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
69
- finally {
70
- try {
71
- if (baseProps_1_1 && !baseProps_1_1.done && (_a = baseProps_1.return)) _a.call(baseProps_1);
72
- }
73
- finally { if (e_1) throw e_1.error; }
74
- }
75
- return carry;
76
- }, new Set());
77
- var differentProps = __spread(differentPropsSet);
78
- var differentPropsPicked = objects.map(function (object) {
79
- return pick(object, differentProps);
80
- });
81
- return {
82
- differentProps: differentProps,
83
- differentPropsPicked: differentPropsPicked,
84
- equal: differentProps.length === 0
85
- };
86
- }
87
-
88
- function compareObjectArraysBasedOn(propKeys, objectArrays) {
89
- var firstArray = objectArrays[0];
90
- var comparison = {
91
- perIndex: [],
92
- equal: true
93
- };
94
- firstArray.forEach(function (object, index) {
95
- var objectsToCompare = objectArrays.map(function (arr) { return arr[index]; });
96
- var compared = compareObjectsBasedOn(propKeys, objectsToCompare);
97
- if (!compared.equal)
98
- comparison.equal = false;
99
- comparison.perIndex.push(compared);
100
- });
101
- return comparison;
102
- }
103
-
104
- function compareObjectProps() {
105
- var params = [];
106
- for (var _i = 0; _i < arguments.length; _i++) {
107
- params[_i] = arguments[_i];
108
- }
109
- var propsSet = new Set();
110
- var res = {
111
- props: null,
112
- presentInAll: null,
113
- presentIn: {},
114
- perProp: {}
115
- };
116
- params.forEach(function (object, index) {
117
- if (!isAnyObject(object)) {
118
- throw new Error("'compareObjectProps' can only compare objects");
119
- }
120
- Object.keys(object).forEach(function (prop) {
121
- propsSet.add(prop);
122
- if (!(prop in res.presentIn))
123
- res.presentIn[prop] = [];
124
- res.presentIn[prop].push(index);
125
- if (!(prop in res.perProp))
126
- res.perProp[prop] = [];
127
- res.perProp[prop].push(object);
128
- });
129
- });
130
- var paramCount = params.length;
131
- res.presentInAll = Object.keys(res.presentIn).reduce(function (carry, prop) {
132
- var propCount = res.presentIn[prop].length;
133
- carry[prop] = propCount === paramCount;
134
- return carry;
135
- }, {});
136
- res.props = Array.from(propsSet);
137
- return res;
138
- }
139
- function compareArrays() {
140
- var params = [];
141
- for (var _i = 0; _i < arguments.length; _i++) {
142
- params[_i] = arguments[_i];
143
- }
144
- var valuesSet = new Set();
145
- var res = {
146
- values: null,
147
- infoPerValue: {},
148
- presentInAll: []
149
- };
150
- var getEmptyArray = function () { return params.map(function () { return undefined; }); };
151
- params.forEach(function (array, paramIndex) {
152
- if (!isArray(array))
153
- throw new Error("'compareArrays' can only compare arrays");
154
- array.forEach(function (val, valIndex) {
155
- valuesSet.add(val);
156
- var parsedVal = isNumber(val) || isString(val) ? String(val) : JSON.stringify(val);
157
- if (!(parsedVal in res.infoPerValue)) {
158
- res.infoPerValue[parsedVal] = {
159
- indexPerArray: getEmptyArray(),
160
- presentInAll: false
161
- };
162
- }
163
- res.infoPerValue[parsedVal].indexPerArray[paramIndex] = valIndex;
164
- });
165
- });
166
- Object.entries(res.infoPerValue).forEach(function (_a) {
167
- var _b = __read(_a, 2), parsedVal = _b[0], info = _b[1];
168
- var isInAllArrays = info.indexPerArray.every(function (index) { return isNumber(index) && index >= 0; }) &&
169
- info.indexPerArray.length === params.length;
170
- info.presentInAll = isInAllArrays;
171
- if (isInAllArrays)
172
- res.presentInAll.push(parsedVal);
173
- });
174
- res.values = Array.from(valuesSet);
175
- return res;
176
- }
177
-
178
- export { compareArrays, compareObjectArraysBasedOn, compareObjectProps, compareObjectsBasedOn };
@@ -1,30 +0,0 @@
1
- import {
2
- ComparisonObjects,
3
- compareObjectsBasedOn
4
- } from './compareObjectsBasedOn'
5
- import { PlainObject } from './types'
6
-
7
- export type ComparisonObjectArrays<ObjectType> = {
8
- perIndex: ComparisonObjects<ObjectType>[]
9
- equal: boolean
10
- }
11
-
12
- export function compareObjectArraysBasedOn<
13
- ObjectType extends PlainObject | any = PlainObject
14
- > (
15
- propKeys: (keyof ObjectType)[],
16
- objectArrays: [[ObjectType, ...any[]], ...any[]]
17
- ): ComparisonObjectArrays<ObjectType> {
18
- const firstArray = objectArrays[0]
19
- const comparison: ComparisonObjectArrays<ObjectType> = {
20
- perIndex: [],
21
- equal: true
22
- }
23
- firstArray.forEach((object, index) => {
24
- const objectsToCompare = objectArrays.map(arr => arr[index])
25
- const compared = compareObjectsBasedOn(propKeys, objectsToCompare as any)
26
- if (!compared.equal) comparison.equal = false
27
- comparison.perIndex.push(compared)
28
- })
29
- return comparison
30
- }
@@ -1,38 +0,0 @@
1
- import { pick } from 'filter-anything'
2
- import { PlainObject } from './types'
3
-
4
- export type ComparisonObjects<ObjectType> = {
5
- differentProps: (keyof ObjectType)[]
6
- differentPropsPicked: Partial<ObjectType>[]
7
- equal: boolean
8
- }
9
-
10
- export function compareObjectsBasedOn<
11
- ObjectType extends PlainObject | any = PlainObject
12
- > (
13
- propKeys: (keyof ObjectType)[],
14
- objects: [ObjectType, ...any[]]
15
- ): ComparisonObjects<ObjectType> {
16
- type PropKeys = keyof ObjectType
17
- const baseProps = propKeys
18
- const baseObject = objects[0]
19
- const differentPropsSet: Set<PropKeys> = objects.reduce(
20
- (carry, object, index) => {
21
- if (index === 0) return carry
22
- for (const prop of baseProps) {
23
- if (object[prop] !== baseObject[prop]) carry.add(prop)
24
- }
25
- return carry
26
- },
27
- new Set() as Set<PropKeys>
28
- )
29
- const differentProps = [...differentPropsSet]
30
- const differentPropsPicked = objects.map(object =>
31
- pick(object, differentProps as string[])
32
- ) as Partial<ObjectType>[]
33
- return {
34
- differentProps,
35
- differentPropsPicked,
36
- equal: differentProps.length === 0
37
- }
38
- }
package/src/index.ts DELETED
@@ -1,98 +0,0 @@
1
- import { isAnyObject, isArray, isString, isNumber } from 'is-what'
2
-
3
- export { compareObjectsBasedOn } from './compareObjectsBasedOn'
4
- export { compareObjectArraysBasedOn } from './compareObjectArraysBasedOn'
5
-
6
- type plainObject = { [key: string]: any }
7
-
8
- export function compareObjectProps (
9
- ...params: plainObject[]
10
- ): {
11
- props: string[]
12
- presentInAll: { [prop: string]: boolean }
13
- perProp: { [prop: string]: plainObject }
14
- presentIn: { [prop: string]: number[] }
15
- } {
16
- const propsSet = new Set()
17
- const res = {
18
- props: null,
19
- presentInAll: null,
20
- presentIn: {},
21
- perProp: {}
22
- }
23
- params.forEach((object, index) => {
24
- if (!isAnyObject(object)) {
25
- throw new Error("'compareObjectProps' can only compare objects")
26
- }
27
- Object.keys(object).forEach(prop => {
28
- propsSet.add(prop)
29
- if (!(prop in res.presentIn)) res.presentIn[prop] = []
30
- res.presentIn[prop].push(index)
31
- if (!(prop in res.perProp)) res.perProp[prop] = []
32
- res.perProp[prop].push(object)
33
- })
34
- })
35
- const paramCount = params.length
36
- res.presentInAll = Object.keys(res.presentIn).reduce((carry, prop) => {
37
- const propCount = res.presentIn[prop].length
38
- carry[prop] = propCount === paramCount
39
- return carry
40
- }, {})
41
- res.props = Array.from(propsSet)
42
- return res
43
- }
44
-
45
- export function compareArrays (
46
- ...params: any[][]
47
- ): {
48
- values: any[]
49
- infoPerValue: {
50
- [prop: string]: {
51
- indexPerArray: (number | undefined)[]
52
- presentInAll: boolean
53
- }
54
- }
55
- presentInAll: any[]
56
- } {
57
- const valuesSet = new Set()
58
- const res: {
59
- values: null | any[]
60
- infoPerValue: {
61
- [val: string]: {
62
- indexPerArray: (number | undefined)[]
63
- presentInAll: boolean
64
- }
65
- }
66
- presentInAll: string[]
67
- } = {
68
- values: null,
69
- infoPerValue: {},
70
- presentInAll: []
71
- }
72
- const getEmptyArray = (): undefined[] => params.map(() => undefined)
73
- params.forEach((array, paramIndex) => {
74
- if (!isArray(array))
75
- throw new Error("'compareArrays' can only compare arrays")
76
- array.forEach((val, valIndex) => {
77
- valuesSet.add(val)
78
- const parsedVal =
79
- isNumber(val) || isString(val) ? String(val) : JSON.stringify(val)
80
- if (!(parsedVal in res.infoPerValue)) {
81
- res.infoPerValue[parsedVal] = {
82
- indexPerArray: getEmptyArray(),
83
- presentInAll: false
84
- }
85
- }
86
- res.infoPerValue[parsedVal].indexPerArray[paramIndex] = valIndex
87
- })
88
- })
89
- Object.entries(res.infoPerValue).forEach(([parsedVal, info]) => {
90
- const isInAllArrays =
91
- info.indexPerArray.every(index => isNumber(index) && index >= 0) &&
92
- info.indexPerArray.length === params.length
93
- info.presentInAll = isInAllArrays
94
- if (isInAllArrays) res.presentInAll.push(parsedVal)
95
- })
96
- res.values = Array.from(valuesSet)
97
- return res
98
- }
package/src/types.ts DELETED
@@ -1,4 +0,0 @@
1
- export type PlainObject = {
2
- [prop: string]: any
3
- [prop: number]: never
4
- }
@@ -1,21 +0,0 @@
1
- // @ts-check
2
- import test from 'ava'
3
- import { compareObjectsBasedOn } from '../src'
4
-
5
- test('compareObjectsBasedOn', t => {
6
- const base = { a: 1, b: true, c: 'CC' }
7
- const check = { d: 1 }
8
- const result = compareObjectsBasedOn(['a', 'b'], [base, check])
9
- t.deepEqual(result.differentProps, ['a', 'b'])
10
- t.deepEqual(result.differentPropsPicked, [{ a: 1, b: true }, {}])
11
- t.deepEqual(result.equal, false)
12
- })
13
-
14
- test('compareObjectsBasedOn - ok', t => {
15
- const base = { a: 1, b: true, c: 'CC' }
16
- const check = { a: 1, c: 'CC', d: 1 }
17
- const result = compareObjectsBasedOn(['a', 'c'], [base, check])
18
- t.deepEqual(result.differentProps, [])
19
- t.deepEqual(result.differentPropsPicked, [{}, {}])
20
- t.deepEqual(result.equal, true)
21
- })
package/test/index.ts DELETED
@@ -1,95 +0,0 @@
1
- import test from 'ava'
2
- import { flatten } from 'flatten-anything'
3
- import { compareObjectProps, compareArrays } from '../src/index'
4
-
5
- test('compareObjectProps', t => {
6
- let res, objectA, objectB
7
- objectA = { a: '', b: '', c: '', d: '' }
8
- objectB = { b: '', c: '', e: '', f: '' }
9
-
10
- res = compareObjectProps(objectA, objectB)
11
- t.deepEqual(res, {
12
- props: ['a', 'b', 'c', 'd', 'e', 'f'],
13
- presentInAll: { a: false, b: true, c: true, d: false, e: false, f: false },
14
- presentIn: { a: [0], b: [0, 1], c: [0, 1], d: [0], e: [1], f: [1] },
15
- perProp: {
16
- a: [objectA],
17
- b: [objectA, objectB],
18
- c: [objectA, objectB],
19
- d: [objectA],
20
- e: [objectB],
21
- f: [objectB]
22
- }
23
- })
24
- })
25
-
26
- test('compareObjectProps 3 or more', t => {
27
- let res, objectA, objectB, objectC, objectD
28
- objectA = { a: '', b: '', c: '', d: '' }
29
- objectB = { b: '', c: '', e: '', f: '' }
30
- objectC = { b: '1', e: '', d: '' }
31
- objectD = { b: '2', c: '', a: '', f: '' }
32
-
33
- res = compareObjectProps(objectA, objectB, objectC, objectD)
34
- t.deepEqual(res, {
35
- props: ['a', 'b', 'c', 'd', 'e', 'f'],
36
- presentInAll: { a: false, b: true, c: false, d: false, e: false, f: false },
37
- presentIn: {
38
- a: [0, 3],
39
- b: [0, 1, 2, 3],
40
- c: [0, 1, 3],
41
- d: [0, 2],
42
- e: [1, 2],
43
- f: [1, 3]
44
- },
45
- perProp: {
46
- a: [objectA, objectD],
47
- b: [objectA, objectB, objectC, objectD],
48
- c: [objectA, objectB, objectD],
49
- d: [objectA, objectC],
50
- e: [objectB, objectC],
51
- f: [objectB, objectD]
52
- }
53
- })
54
- })
55
-
56
- test('compareObjectProps flat', t => {
57
- let res
58
- const objectA = { nested: { a: '🃏️', b: '🎴' } }
59
- const objectB = { nested: { a: '🃏', c: '🀄️' } }
60
- const flatA = flatten(objectA)
61
- // → {'nested.a': '🃏️', 'nested.b': '🎴'}
62
- const flatB = flatten(objectB)
63
- // → {'nested.a': '🃏️', 'nested.c': '🀄️'}
64
- res = compareObjectProps(flatA, flatB)
65
- t.deepEqual(res, {
66
- props: ['nested.a', 'nested.b', 'nested.c'],
67
- presentInAll: { 'nested.a': true, 'nested.b': false, 'nested.c': false },
68
- presentIn: { 'nested.a': [0, 1], 'nested.b': [0], 'nested.c': [1] },
69
- perProp: {
70
- 'nested.a': [flatA, flatB],
71
- 'nested.b': [flatA],
72
- 'nested.c': [flatB]
73
- }
74
- })
75
- })
76
-
77
- test('compareArrays', t => {
78
- let res, arrayA, arrayB
79
- arrayA = ['a', 'b', 'c', 'd']
80
- arrayB = ['b', 'c', 'e', 'f']
81
-
82
- res = compareArrays(arrayA, arrayB)
83
- t.deepEqual(res, {
84
- values: ['a', 'b', 'c', 'd', 'e', 'f'],
85
- infoPerValue: {
86
- a: { indexPerArray: [0, undefined], presentInAll: false },
87
- b: { indexPerArray: [1, 0], presentInAll: true },
88
- c: { indexPerArray: [2, 1], presentInAll: true },
89
- d: { indexPerArray: [3, undefined], presentInAll: false },
90
- e: { indexPerArray: [undefined, 2], presentInAll: false },
91
- f: { indexPerArray: [undefined, 3], presentInAll: false }
92
- },
93
- presentInAll: ['b', 'c']
94
- })
95
- })
package/tsconfig.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "baseUrl": ".",
4
- "downlevelIteration": true,
5
- "outDir": "./dist",
6
- "declaration": true,
7
- "declarationDir": "./types/",
8
- "target": "es5",
9
- "lib": ["es5", "es2015", "es2016", "es2017", "dom"]
10
- },
11
- "include": [
12
- "src/**/*",
13
- "test/**/*"
14
- ]
15
- }
package/types/types.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export declare type PlainObject = {
2
- [prop: string]: any;
3
- [prop: number]: never;
4
- };