Skip to content

Commit

Permalink
fix(@uform/utils): fix bug of every and some (#88)
Browse files Browse the repository at this point in the history
* fix(@uform/utils): fix bug of every and some

* test(@uform/utils): add some test cases
  • Loading branch information
p0page authored and janryWang committed Jun 1, 2019
1 parent 5143c1b commit 36ab9da
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
53 changes: 52 additions & 1 deletion packages/utils/src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getIn, setIn, getPathSegments } from '../accessor'
import { Broadcast } from '../broadcast'
import { isEqual } from '../compare'
import { toArr } from '../array'
import { toArr, every, some, findIndex, find, includes } from '../array'
import { clone } from '../clone'
import { caculateSchemaInitialValues } from '../schema'

Expand Down Expand Up @@ -215,3 +215,54 @@ test('caculateSchemaInitialValues', () => {
test('getPathSegments', () => {
expect(isEqual(getPathSegments(0), [0])).toBeTruthy()
})

test('some', () => {
const values1 = [1, 2, 3, 4, 5]
const values2 = []
const values3 = { a: 1, b: 2, c: 3 }
const values4 = {}
expect(some(values1, item => item === 3)).toBeTruthy()
expect(some(values1, item => item === 6)).toBeFalsy()
expect(some(values2, () => true)).toBeFalsy()
expect(some(values2, () => false)).toBeFalsy()
expect(some(values3, item => item === 3)).toBeTruthy()
expect(some(values3, item => item === 6)).toBeFalsy()
expect(some(values4, () => true)).toBeFalsy()
expect(some(values4, () => false)).toBeFalsy()
})

test('every', () => {
const values1 = [1, 2, 3, 4, 5]
const values2 = []
const values3 = { a: 1, b: 2, c: 3 }
const values4 = {}
expect(every(values1, item => item < 6)).toBeTruthy()
expect(every(values1, item => item < 3)).toBeFalsy()
expect(every(values2, () => true)).toBeTruthy()
expect(every(values2, () => false)).toBeTruthy()
expect(every(values2, () => false)).toBeTruthy()
expect(every(values3, item => item < 6)).toBeTruthy()
expect(every(values3, item => item < 3)).toBeFalsy()
expect(every(values4, () => false)).toBeTruthy()
expect(every(values4, () => false)).toBeTruthy()
})

test('findIndex', () => {
const value = [1, 2, 3, 4, 5]
expect(isEqual(findIndex(value, item => item > 3), 3)).toBeTruthy()
expect(isEqual(findIndex(value, item => item < 3, true), 1)).toBeTruthy()
expect(isEqual(findIndex(value, item => item > 6), -1)).toBeTruthy()
})

test('find', () => {
const value = [1, 2, 3, 4, 5]
expect(isEqual(find(value, item => item > 3), 4)).toBeTruthy()
expect(isEqual(find(value, item => item < 3, true), 2)).toBeTruthy()
expect(isEqual(find(value, item => item > 6), void 0)).toBeTruthy()
})

test('includes', () => {
const value = [1, 2, 3, 4, 5]
expect(includes(value, 3)).toBeTruthy()
expect(includes(value, 6)).toBeFalsy()
})
8 changes: 2 additions & 6 deletions packages/utils/src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@ export const reduce = (val, callback, initial, revert) => {
}

export const every = (val, callback, revert) => {
let res = false
let res = true
each(
val,
(item, key) => {
if (!callback(item, key)) {
res = false
return false
} else {
res = true
}
},
revert
Expand All @@ -75,15 +73,13 @@ export const every = (val, callback, revert) => {
}

export const some = (val, callback, revert) => {
let res = true
let res = false
each(
val,
(item, key) => {
if (callback(item, key)) {
res = true
return false
} else {
res = false
}
},
revert
Expand Down

0 comments on commit 36ab9da

Please sign in to comment.