Skip to content
This repository was archived by the owner on Mar 6, 2025. It is now read-only.

Commit db41a68

Browse files
author
ChrisTerBeke
authored
Merge pull request #222 from Ultimaker/add-array-intersect-util
Add array intersect util
2 parents b033750 + e6e8ebb commit db41a68

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,4 @@ export * from './utils/json_matching';
145145
export * from './utils/layout_constants';
146146
export * from './utils/range';
147147
export * from './utils/split_text_by_new_line';
148+
export * from './utils/array_intersection';

src/stories/utils.stories.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import styles from '@sambego/storybook-styles';
1313
import copyToClipboard from '../utils/copy_to_clipboard';
1414
import downloadFile from '../utils/download_file';
1515
import splitTextByNewLine from '../utils/split_text_by_new_line';
16+
import arrayIntersection from '../utils/array_intersection';
1617

1718
// inputs
1819
import TextareaField from '../components/input_fields/textarea_field';
@@ -91,3 +92,41 @@ stories.add('Split text by new line', withState({ value: null })(withInfo('Text
9192
<StaticField value={splitTextByNewLine(store.state.value)} id="splitResult" label="Result" />
9293
</div>
9394
))));
95+
96+
stories.add('Array intersection', withState({
97+
array1: ['1', '2', '3', '4', '5'],
98+
array2: ['4', '5', '6', '7', '8'],
99+
result: ['4', '5'],
100+
})(withInfo('Array intersection')(({ store }) => {
101+
const { array1, array2, result } = store.state;
102+
return (
103+
<div style={{ width: 350 }}>
104+
<TextField
105+
value={array1}
106+
id="array1"
107+
onChangeHandler={(id, value) => store.set({
108+
array1: value.split(','),
109+
result: arrayIntersection(value.split(','), array2),
110+
})}
111+
label="array1"
112+
/>
113+
<TextField
114+
value={array2}
115+
id="array2"
116+
onChangeHandler={(id, value) => store.set({
117+
array2: value.split(','),
118+
result: arrayIntersection(array1, value.split(',')),
119+
})}
120+
label="array2"
121+
/>
122+
<br />
123+
<TextField
124+
value={result}
125+
id="result"
126+
onChangeHandler={() => null}
127+
label="result"
128+
/>
129+
<br />
130+
</div>
131+
);
132+
})));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2019 Ultimaker B.V.
2+
import arrayIntersection from '../array_intersection';
3+
4+
describe('The arrayIntersection function', () => {
5+
const array1 = [
6+
1,
7+
2,
8+
'cat',
9+
'dog',
10+
5,
11+
];
12+
const array2 = [
13+
'bird',
14+
2,
15+
'cat',
16+
'fish',
17+
55,
18+
];
19+
const expectedIntersection = [
20+
2,
21+
'cat',
22+
];
23+
24+
it('should return the given error code if its not recognized as related to the air manager', () => {
25+
expect(arrayIntersection(array1, array2)).toEqual(expectedIntersection);
26+
});
27+
28+
it('should return an empty array if one of the arguments is null', () => {
29+
expect(arrayIntersection(array1, null)).toEqual([]);
30+
});
31+
});

src/utils/array_intersection.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// returns an array containing the common elements of two given arrays
2+
export function arrayIntersection(firstArray: any[], secondArray: any[]): any[] {
3+
if (Array.isArray(firstArray) && Array.isArray(secondArray)) {
4+
return firstArray.filter(firstArrayElement => (
5+
secondArray.includes(firstArrayElement)));
6+
}
7+
return [];
8+
}
9+
10+
export default arrayIntersection;

0 commit comments

Comments
 (0)