├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.js ├── readme.md ├── index.d.ts ├── package.json ├── license └── test.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function arrayEqual(array1, array2) { 2 | if (array1 === array2) { 3 | return true; 4 | } 5 | 6 | const {length} = array1; 7 | 8 | if (length !== array2.length) { 9 | return false; 10 | } 11 | 12 | for (let index = 0; index < length; index++) { 13 | if (array1[index] !== array2[index]) { 14 | return false; 15 | } 16 | } 17 | 18 | return true; 19 | } 20 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # array-equal 2 | 3 | > Check if two arrays are equal 4 | 5 | It checks that the elements and order are the same. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install array-equal 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import arrayEqual from 'array-equal'; 17 | 18 | arrayEqual([1, 2, 3], [1, 2, 3]); 19 | //=> true 20 | 21 | arrayEqual([1, 2, 3], [1, 2, 3, 4]); 22 | //=> false 23 | ``` 24 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 20 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Check if two arrays are equal. 3 | 4 | It checks that the elements and order are the same. 5 | 6 | @param array1 - The first array to compare. 7 | @param array2 - The second array to compare. 8 | @returns A boolean indicating whether the arrays are equal. 9 | 10 | @example 11 | ``` 12 | import arrayEqual from 'array-equal'; 13 | 14 | arrayEqual([1, 2, 3], [1, 2, 3]); 15 | //=> true 16 | 17 | arrayEqual([1, 2, 3], [1, 2, 3, 4]); 18 | //=> false 19 | ``` 20 | */ 21 | export default function arrayEqual(array1: T[], array2: U[]): array1 is T[]; 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "array-equal", 3 | "version": "2.0.0", 4 | "description": "Check if two arrays are equal", 5 | "license": "MIT", 6 | "repository": "sindresorhus/array-equal", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "type": "module", 9 | "exports": { 10 | "types": "./index.d.ts", 11 | "default": "./index.js" 12 | }, 13 | "sideEffects": false, 14 | "engines": { 15 | "node": ">=20" 16 | }, 17 | "scripts": { 18 | "test": "xo && ava" 19 | }, 20 | "files": [ 21 | "index.js", 22 | "index.d.ts" 23 | ], 24 | "keywords": [ 25 | "array", 26 | "equal", 27 | "equals", 28 | "same", 29 | "identical", 30 | "equality", 31 | "compare", 32 | "validate" 33 | ], 34 | "devDependencies": { 35 | "ava": "^6.2.0", 36 | "xo": "^0.60.0" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Jonathan Ong 4 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import arrayEqual from './index.js'; 3 | 4 | test('compare arrays with same numeric elements', t => { 5 | t.true(arrayEqual([1, 2, 3], [1, 2, 3])); 6 | }); 7 | 8 | test('compare arrays with different numeric elements', t => { 9 | t.false(arrayEqual([1, 2, 3], [4, 5, 6])); 10 | }); 11 | 12 | test('compare arrays of different lengths', t => { 13 | t.false(arrayEqual([1, 2, 3], [1, 2, 3, 4])); 14 | }); 15 | 16 | test('compare arrays with same string elements', t => { 17 | t.true(arrayEqual(['a', 'b', 'c'], ['a', 'b', 'c'])); 18 | }); 19 | 20 | test('compare arrays with different string elements', t => { 21 | t.false(arrayEqual(['a', 'b', 'c'], ['d', 'e', 'f'])); 22 | }); 23 | 24 | test('compare arrays with mixed types', t => { 25 | t.false(arrayEqual(['1', 2, 3], [1, 2, 3])); 26 | }); 27 | 28 | test('compare empty arrays', t => { 29 | t.true(arrayEqual([], [])); 30 | }); 31 | 32 | test('compare arrays with same objects', t => { 33 | const object = {key: 'value'}; 34 | t.true(arrayEqual([object, object], [object, object])); 35 | }); 36 | 37 | test('compare arrays with different objects', t => { 38 | t.false(arrayEqual([{key: 'value'}], [{key: 'value'}])); 39 | }); 40 | --------------------------------------------------------------------------------