Skip to content

Commit

Permalink
feat: add isEqualArray
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 14, 2018
1 parent a6e99c8 commit 4ccbdab
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export { default as inBrowser } from './inBrowser'
export { default as isArray } from './isArray'
export { default as isBoolean } from './isBoolean'
export { default as isDate } from './isDate'
export { default as isEqualArray } from './isEqualArray'
export { default as isFinite } from './isFinite'
export { default as isFunction } from './isFunction'
export { default as isInteger } from './isInteger'
Expand Down
21 changes: 21 additions & 0 deletions src/isEqualArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 检查给定的数组是否相等。
*
* @param arr1 数组1
* @param arr2 数组2
* @returns 是(true)或否(false)
*/
export default function isEqualArray(arr1: any[], arr2: any[]): boolean {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
return false
}
if (arr1.length !== arr2.length) {
return false
}
for (let i = 0, len = arr1.length; i < len; i++) {
if (arr1[i] !== arr2[i]) {
return false
}
}
return true
}
16 changes: 16 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,19 @@ describe('parseCSSValue', () => {
})
})
})

describe('isEqualArray', () => {
test('数组 == 数组', () => {
expect(vtils.isEqualArray([], [])).toBeTruthy()
expect(vtils.isEqualArray([1, now, '+'], [1, now, '+'])).toBeTruthy()
})
test('数组 != 数组', () => {
expect(vtils.isEqualArray([], [1])).toBeFalsy()
expect(vtils.isEqualArray([1, now, '+'], [1, now, '-'])).toBeFalsy()
expect(vtils.isEqualArray([1, now, '+', '-'], [1, now, '+'])).toBeFalsy()
})
test('数组 != 其他', () => {
expect(vtils.isEqualArray([], false as any)).toBeFalsy()
expect(vtils.isEqualArray(['1', '2', '3'], '123' as any)).toBeFalsy()
})
})

0 comments on commit 4ccbdab

Please sign in to comment.