Skip to content

Commit adaeeaa

Browse files
committed
feat: ignoreMLClassifyErrors flag
1 parent f70689e commit adaeeaa

5 files changed

Lines changed: 74 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ RecipeClipper.clipRecipe({
5151
window: window, // Optional: Pass a custom window object - very useful if you want to use this library with JSDOM
5252
mlDisable: false, // Optional: Disable the machine learning part of this project
5353
mlClassifyEndpoint: '', // Optional: Provide the endpoint for the machine learning classification server documented below
54-
mlModelEndpoint: '' // Optional: Provide the machine learning model endpoint if using local in-browser machine learning
54+
mlModelEndpoint: '', // Optional: Provide the machine learning model endpoint if using local in-browser machine learning
55+
ignoreMLClassifyErrors: false, // Optional: Do not throw an error if machine learning classification fails, just return an empty string for that field instead
5556
})...
5657
```
5758

src/utils/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const generateConfig = (options) => {
77
mlDisable: null,
88
mlModelEndpoint: null,
99
mlClassifyEndpoint: null,
10+
ignoreMLClassifyErrors: null,
1011
},
1112
};
1213

@@ -19,6 +20,7 @@ export const generateConfig = (options) => {
1920
config.options.mlDisable = window.RC_ML_DISABLE || null;
2021
config.options.mlModelEndpoint = window.RC_ML_MODEL_ENDPOINT || null;
2122
config.options.mlClassifyEndpoint = window.RC_ML_CLASSIFY_ENDPOINT || null;
23+
config.options.ignoreMLClassifyErrors = window.RC_IGNORE_ML_CLASSIFY_ERRORS || null;
2224
}
2325
} catch (_) {
2426
// Do nothing

src/utils/config.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('generateConfig', () => {
1010
mlDisable: null,
1111
mlModelEndpoint: null,
1212
mlClassifyEndpoint: null,
13+
ignoreMLClassifyErrors: null,
1314
},
1415
});
1516
});
@@ -19,6 +20,7 @@ describe('generateConfig', () => {
1920
mlDisable: true,
2021
mlModelEndpoint: 'example',
2122
mlClassifyEndpoint: 'example2',
23+
ignoreMLClassifyErrors: true,
2224
});
2325
expect(config).toEqual({
2426
window,
@@ -27,6 +29,7 @@ describe('generateConfig', () => {
2729
mlDisable: true,
2830
mlModelEndpoint: 'example',
2931
mlClassifyEndpoint: 'example2',
32+
ignoreMLClassifyErrors: true,
3033
},
3134
});
3235
});
@@ -43,6 +46,7 @@ describe('generateConfig', () => {
4346
mlDisable: null,
4447
mlModelEndpoint: null,
4548
mlClassifyEndpoint: null,
49+
ignoreMLClassifyErrors: null,
4650
},
4751
});
4852
});

src/utils/ml.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,25 @@ export const findByHeader = async (config, type) => {
162162
.reduce((a, b) => (a.length > b.length ? a : b), '');
163163
};
164164

165-
// Type 1 for ingredients
166-
// Type 2 for instructions
167-
// Others to be implemented in future...
168-
export const grabByMl = async (config, type) => {
165+
export const find = async (config, type) => {
169166
if (config.options.mlDisable) return '';
170167

171168
const result = await self.findByHeader(config, type) || await self.findFullSearch(config, type);
172169

173170
return result;
174171
};
172+
173+
// Type 1 for ingredients
174+
// Type 2 for instructions
175+
// Others to be implemented in future...
176+
export const grabByMl = async (config, type) => {
177+
try {
178+
return await self.find(config, type);
179+
} catch (e) {
180+
if (config.options.ignoreMLClassifyErrors) {
181+
return '';
182+
}
183+
184+
throw e;
185+
}
186+
};

src/utils/ml.spec.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as element from './element';
22
import * as ml from './ml';
33
import {
44
grabByMl,
5+
find,
56
findByHeader,
67
findFullSearch,
78
mlFilter,
@@ -26,9 +27,55 @@ describe('ml', () => {
2627
afterEach(() => {
2728
jest.restoreAllMocks();
2829
config.options.mlDisable = undefined;
30+
config.options.ignoreMLClassifyErrors = undefined;
2931
});
3032

3133
describe('grabByMl', () => {
34+
let findMock;
35+
beforeEach(async () => {
36+
findMock = jest.spyOn(ml, 'find');
37+
});
38+
39+
describe('success', () => {
40+
let result;
41+
beforeEach(async () => {
42+
findMock.mockResolvedValue('example');
43+
result = await grabByMl(config, 1);
44+
});
45+
46+
it('calls find', () => {
47+
expect(findMock).toHaveBeenCalledWith(config, 1);
48+
});
49+
50+
it('returns results from find', () => {
51+
expect(result).toEqual('example');
52+
});
53+
});
54+
55+
describe('failure', () => {
56+
beforeEach(async () => {
57+
findMock.mockRejectedValue(new Error('example'));
58+
});
59+
60+
describe('when ignoreMLClassifyErrors is true', () => {
61+
beforeEach(() => {
62+
config.options.ignoreMLClassifyErrors = true;
63+
});
64+
65+
it('returns an empty string', async () => {
66+
expect(await grabByMl(config, 1)).toEqual('');
67+
});
68+
});
69+
70+
describe('when ignoreMLClassifyErrors is not enabled', () => {
71+
it('throws an error', () => {
72+
expect(grabByMl(config, 1)).rejects.toThrow('example');
73+
});
74+
});
75+
});
76+
});
77+
78+
describe('find', () => {
3279
let findByHeaderMock;
3380
let findFullSearchMock;
3481

@@ -42,7 +89,7 @@ describe('ml', () => {
4289

4390
beforeEach(async () => {
4491
config.options.mlDisable = true;
45-
result = await grabByMl(config, 1);
92+
result = await find(config, 1);
4693
});
4794

4895
it('returns empty string', () => {
@@ -65,7 +112,7 @@ describe('ml', () => {
65112
beforeEach(async () => {
66113
findByHeaderMock.mockResolvedValue(mockHeaderResult);
67114

68-
result = await grabByMl(config, 1);
115+
result = await find(config, 1);
69116
});
70117

71118
it('returns match from findByHeader', () => {
@@ -89,7 +136,7 @@ describe('ml', () => {
89136
findByHeaderMock.mockReturnValue(null);
90137
findFullSearchMock.mockReturnValue(mockFullTextResult);
91138

92-
result = await grabByMl(config, 1);
139+
result = await find(config, 1);
93140
});
94141

95142
it('returns match from findFullSearch', () => {

0 commit comments

Comments
 (0)