@trenskow/equals 0.1.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.
- package/LICENSE +9 -0
- package/README.md +25 -0
- package/eslint.config.js +55 -0
- package/index.js +9 -0
- package/lib/index.js +54 -0
- package/package.json +32 -0
- package/test/index.js +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Copyright 2025 Kristian Trenskow
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
@trenskow/equals
|
|
2
|
+
----
|
|
3
|
+
|
|
4
|
+
A very simple JavaScript library to test equality between two (or more) values.
|
|
5
|
+
|
|
6
|
+
# Usage
|
|
7
|
+
|
|
8
|
+
Below is an example of usage.
|
|
9
|
+
|
|
10
|
+
````javascript
|
|
11
|
+
import equals from '@trenskow/equals';
|
|
12
|
+
|
|
13
|
+
equals(1, 1); // true
|
|
14
|
+
equals(1, 0); // false
|
|
15
|
+
equals([1, 2, 3], [1, 2, 3]); // true
|
|
16
|
+
equals([1, 2, 3], [3, 2, 1]); // false
|
|
17
|
+
equals({ a: 1, b: 2}, { a: 1, b: 2 }); // true
|
|
18
|
+
equals({ a: 1, b: 2}, { a: 2, b: 1 }); // false
|
|
19
|
+
equals(true, 1); // false
|
|
20
|
+
equals(false, 0); // false
|
|
21
|
+
````
|
|
22
|
+
|
|
23
|
+
# License
|
|
24
|
+
|
|
25
|
+
See license in LICENSE
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import js from '@eslint/js';
|
|
5
|
+
import { FlatCompat } from '@eslint/eslintrc';
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
const compat = new FlatCompat({
|
|
10
|
+
baseDirectory: __dirname,
|
|
11
|
+
recommendedConfig: js.configs.recommended,
|
|
12
|
+
allConfig: js.configs.all
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export default [...compat.extends('eslint:recommended'), {
|
|
16
|
+
languageOptions: {
|
|
17
|
+
globals: {
|
|
18
|
+
...globals.node,
|
|
19
|
+
...globals.mocha,
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
ecmaVersion: 'latest',
|
|
23
|
+
sourceType: 'module',
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
rules: {
|
|
27
|
+
indent: ['error', 'tab', {
|
|
28
|
+
SwitchCase: 1,
|
|
29
|
+
}],
|
|
30
|
+
|
|
31
|
+
'linebreak-style': ['error', 'unix'],
|
|
32
|
+
quotes: ['error', 'single'],
|
|
33
|
+
semi: ['error', 'always'],
|
|
34
|
+
|
|
35
|
+
'no-console': ['error', {
|
|
36
|
+
allow: ['warn', 'error', 'info'],
|
|
37
|
+
}],
|
|
38
|
+
|
|
39
|
+
'no-unused-vars': ['error', {
|
|
40
|
+
argsIgnorePattern: '^_',
|
|
41
|
+
}],
|
|
42
|
+
|
|
43
|
+
'no-empty': ['error', {
|
|
44
|
+
allowEmptyCatch: true,
|
|
45
|
+
}],
|
|
46
|
+
|
|
47
|
+
'no-trailing-spaces': ['error', {
|
|
48
|
+
ignoreComments: true,
|
|
49
|
+
}],
|
|
50
|
+
|
|
51
|
+
'require-atomic-updates': 'off',
|
|
52
|
+
'no-implicit-globals': ['error'],
|
|
53
|
+
'eol-last': ['error', 'always'],
|
|
54
|
+
},
|
|
55
|
+
}];
|
package/index.js
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//
|
|
2
|
+
// index.js
|
|
3
|
+
// @trenskow/equals
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2025/12/19
|
|
6
|
+
// For license see LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
const equals = (a, b) => {
|
|
10
|
+
|
|
11
|
+
if (a === b) return true;
|
|
12
|
+
if (typeof a !== typeof b) return false;
|
|
13
|
+
|
|
14
|
+
if (a && b && typeof a === 'object') {
|
|
15
|
+
|
|
16
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
17
|
+
return equals.array(a, b);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return equals.object(a, b);
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return false;
|
|
25
|
+
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
equals.array = (a, b) => {
|
|
29
|
+
|
|
30
|
+
if (a.length !== b.length) return false;
|
|
31
|
+
|
|
32
|
+
for (let idx = 0; idx < a.length; idx++) {
|
|
33
|
+
if (!equals(a[idx], b[idx])) return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return true;
|
|
37
|
+
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
equals.object = (a, b) => {
|
|
41
|
+
return equals.array(Object.keys(a), Object.keys(b)) && equals.array(Object.values(a), Object.values(b));
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default (...args) => {
|
|
45
|
+
|
|
46
|
+
if (args.length < 2) return false;
|
|
47
|
+
|
|
48
|
+
for (let idx = 1; idx < args.length; idx++) {
|
|
49
|
+
return equals(args[idx - 1], args[idx]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return true;
|
|
53
|
+
|
|
54
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trenskow/equals",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A very simple JavaScript library to test equality between two (or more) values.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"equals",
|
|
7
|
+
"value"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/trenskow/equals#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/trenskow/equals/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/trenskow/equals.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "BSD-2-Clause",
|
|
18
|
+
"author": "Kristian Trenskow <this.is@kristians.email>",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "index.js",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "node ./node_modules/mocha/bin/mocha.js ./test/index.js"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@eslint/eslintrc": "^3.3.3",
|
|
26
|
+
"@eslint/js": "^9.39.2",
|
|
27
|
+
"chai": "^6.2.1",
|
|
28
|
+
"eslint": "^9.39.2",
|
|
29
|
+
"globals": "^16.5.0",
|
|
30
|
+
"mocha": "^11.7.5"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/test/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//
|
|
2
|
+
// index.js
|
|
3
|
+
// @trenskow/equals
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2025/12/19
|
|
6
|
+
// For license see LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
import { expect } from 'chai';
|
|
10
|
+
|
|
11
|
+
import equals from '../lib/index.js';
|
|
12
|
+
|
|
13
|
+
describe('@trenskow/equals', () => {
|
|
14
|
+
|
|
15
|
+
describe('equals', () => {
|
|
16
|
+
|
|
17
|
+
it('should return true for equal primitive values.', () => {
|
|
18
|
+
expect(equals(1, 1)).to.be.true;
|
|
19
|
+
expect(equals('test', 'test')).to.be.true;
|
|
20
|
+
expect(equals(true, true)).to.be.true;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should return false for different primitive values.', () => {
|
|
24
|
+
expect(equals(1, 2)).to.be.false;
|
|
25
|
+
expect(equals('test', 'TEST')).to.be.false;
|
|
26
|
+
expect(equals(true, false)).to.be.false;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should return true for equal arrays.', () => {
|
|
30
|
+
expect(equals([1, 2, 3], [1, 2, 3])).to.be.true;
|
|
31
|
+
expect(equals(['a', 'b'], ['a', 'b'])).to.be.true;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should return false for different arrays.', () => {
|
|
35
|
+
expect(equals([1, 2, 3], [1, 2, 4])).to.be.false;
|
|
36
|
+
expect(equals(['a', 'b'], ['a', 'c'])).to.be.false;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should return true for equal objects.', () => {
|
|
40
|
+
expect(equals({ a: 1, b: 2 }, { a: 1, b: 2 })).to.be.true;
|
|
41
|
+
expect(equals({ x: 'x', y: 'y' }, { x: 'x', y: 'y' })).to.be.true;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return false for different objects.', () => {
|
|
45
|
+
expect(equals({ a: 1, b: 2 }, { a: 1, b: 3 })).to.be.false;
|
|
46
|
+
expect(equals({ x: 'x', y: 'y' }, { x: 'x', y: 'z' })).to.be.false;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should return false for different types.', () => {
|
|
50
|
+
expect(equals(1, '1')).to.be.false;
|
|
51
|
+
expect(equals([1, 2], { a: 1, b: 2 })).to.be.false;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
});
|