-
Notifications
You must be signed in to change notification settings - Fork 11
/
arraySet.ts
37 lines (31 loc) · 901 Bytes
/
arraySet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export function arraySetDelete<T>( array: Array<T>, value: T ): boolean {
const index = array.indexOf( value );
if ( index === -1 ) { return false; }
array.splice( index, 1 );
return true;
}
export function arraySetHas<T>( array: Array<T>, value: T ): boolean {
return array.indexOf( value ) !== -1;
}
export function arraySetAdd<T>( array: Array<T>, value: T ): boolean {
const index = array.indexOf( value );
if ( index !== -1 ) { return false; }
array.push( value );
return true;
}
export function arraySetUnion<T>( a: Array<T>, b: Array<T> ): Array<T> {
const out = [ ...a ];
b.forEach( ( v ) => {
if ( !arraySetHas( out, v ) ) {
out.push( v );
}
} );
return out;
}
export function arraySetDiff<T>( from: Array<T>, diff: Array<T> ): Array<T> {
const out = [ ...from ];
diff.forEach( ( v ) => {
arraySetDelete( out, v );
} );
return out;
}