1+ import { isDef } from '../is/isDef'
2+ import { isUndef } from '../is/isUndef'
3+
14export function quickFind ( array : any [ ] , id : string | number ) {
25 const indexMap = new Map ( )
36 array . forEach ( ( item , i ) => indexMap . set ( item [ id ] , i ) )
@@ -17,23 +20,23 @@ class QuickFind {
1720
1821 find ( id : any ) {
1922 const index = this . indexMap . get ( id )
20- if ( index === undefined )
23+ if ( isUndef ( index ) )
2124 return undefined
2225 return this . array [ index ]
2326 }
2427
2528 _update ( id : any , key : any , value : any ) {
26- if ( key === undefined ) {
29+ if ( isUndef ( key ) ) {
2730 const index = this . indexMap . get ( id )
28- if ( index === undefined )
31+ if ( isUndef ( index ) )
2932 throw new Error ( '当前id不存在' )
3033 if ( value [ this . id ] !== id )
3134 throw new Error ( '不可修改唯一id' )
3235 this . array [ index ] = value
3336 }
3437 else {
3538 const target = this . find ( id )
36- if ( target === undefined )
39+ if ( isUndef ( target ) )
3740 return
3841 target [ key ] = value
3942 }
@@ -42,7 +45,7 @@ class QuickFind {
4245
4346 delete ( id : any ) {
4447 const index = this . indexMap . get ( id )
45- if ( index === undefined )
48+ if ( isUndef ( index ) )
4649 return
4750 this . array . splice ( index , 1 )
4851 this . indexMap . delete ( id )
@@ -51,17 +54,17 @@ class QuickFind {
5154
5255 set ( id : any , key : any , value ?: any ) {
5356 const index = this . indexMap . get ( id )
54- if ( value === undefined ) {
55- if ( key === undefined )
57+ if ( isUndef ( value ) ) {
58+ if ( isUndef ( key ) )
5659 return
5760 value = key
5861 key = undefined
5962 }
60- if ( index !== undefined ) {
63+ if ( isDef ( index ) ) {
6164 return this . _update ( id , key , value )
6265 }
6366 else {
64- if ( value [ this . id ] === undefined )
67+ if ( isUndef ( value [ this . id ] ) )
6568 throw new Error ( '新增的数据必须包含唯一id' )
6669 if ( value [ this . id ] !== id )
6770 throw new Error ( '新增的数据id必须与当前id一致' )
0 commit comments