Skip to content

Commit

Permalink
feat: ignoreMLClassifyErrors flag
Browse files Browse the repository at this point in the history
  • Loading branch information
julianpoy committed Jan 4, 2024
1 parent f70689e commit adaeeaa
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ RecipeClipper.clipRecipe({
window: window, // Optional: Pass a custom window object - very useful if you want to use this library with JSDOM
mlDisable: false, // Optional: Disable the machine learning part of this project
mlClassifyEndpoint: '', // Optional: Provide the endpoint for the machine learning classification server documented below
mlModelEndpoint: '' // Optional: Provide the machine learning model endpoint if using local in-browser machine learning
mlModelEndpoint: '', // Optional: Provide the machine learning model endpoint if using local in-browser machine learning
ignoreMLClassifyErrors: false, // Optional: Do not throw an error if machine learning classification fails, just return an empty string for that field instead
})...
```

Expand Down
2 changes: 2 additions & 0 deletions src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const generateConfig = (options) => {
mlDisable: null,
mlModelEndpoint: null,
mlClassifyEndpoint: null,
ignoreMLClassifyErrors: null,
},
};

Expand All @@ -19,6 +20,7 @@ export const generateConfig = (options) => {
config.options.mlDisable = window.RC_ML_DISABLE || null;
config.options.mlModelEndpoint = window.RC_ML_MODEL_ENDPOINT || null;
config.options.mlClassifyEndpoint = window.RC_ML_CLASSIFY_ENDPOINT || null;
config.options.ignoreMLClassifyErrors = window.RC_IGNORE_ML_CLASSIFY_ERRORS || null;
}
} catch (_) {
// Do nothing
Expand Down
4 changes: 4 additions & 0 deletions src/utils/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('generateConfig', () => {
mlDisable: null,
mlModelEndpoint: null,
mlClassifyEndpoint: null,
ignoreMLClassifyErrors: null,
},
});
});
Expand All @@ -19,6 +20,7 @@ describe('generateConfig', () => {
mlDisable: true,
mlModelEndpoint: 'example',
mlClassifyEndpoint: 'example2',
ignoreMLClassifyErrors: true,
});
expect(config).toEqual({
window,
Expand All @@ -27,6 +29,7 @@ describe('generateConfig', () => {
mlDisable: true,
mlModelEndpoint: 'example',
mlClassifyEndpoint: 'example2',
ignoreMLClassifyErrors: true,
},
});
});
Expand All @@ -43,6 +46,7 @@ describe('generateConfig', () => {
mlDisable: null,
mlModelEndpoint: null,
mlClassifyEndpoint: null,
ignoreMLClassifyErrors: null,
},
});
});
Expand Down
20 changes: 16 additions & 4 deletions src/utils/ml.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,25 @@ export const findByHeader = async (config, type) => {
.reduce((a, b) => (a.length > b.length ? a : b), '');
};

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

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

return result;
};

// Type 1 for ingredients
// Type 2 for instructions
// Others to be implemented in future...
export const grabByMl = async (config, type) => {
try {
return await self.find(config, type);
} catch (e) {
if (config.options.ignoreMLClassifyErrors) {
return '';
}

throw e;
}
};
53 changes: 50 additions & 3 deletions src/utils/ml.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as element from './element';
import * as ml from './ml';
import {
grabByMl,
find,
findByHeader,
findFullSearch,
mlFilter,
Expand All @@ -26,9 +27,55 @@ describe('ml', () => {
afterEach(() => {
jest.restoreAllMocks();
config.options.mlDisable = undefined;
config.options.ignoreMLClassifyErrors = undefined;
});

describe('grabByMl', () => {
let findMock;
beforeEach(async () => {
findMock = jest.spyOn(ml, 'find');
});

describe('success', () => {
let result;
beforeEach(async () => {
findMock.mockResolvedValue('example');
result = await grabByMl(config, 1);
});

it('calls find', () => {
expect(findMock).toHaveBeenCalledWith(config, 1);
});

it('returns results from find', () => {
expect(result).toEqual('example');
});
});

describe('failure', () => {
beforeEach(async () => {
findMock.mockRejectedValue(new Error('example'));
});

describe('when ignoreMLClassifyErrors is true', () => {
beforeEach(() => {
config.options.ignoreMLClassifyErrors = true;
});

it('returns an empty string', async () => {
expect(await grabByMl(config, 1)).toEqual('');
});
});

describe('when ignoreMLClassifyErrors is not enabled', () => {
it('throws an error', () => {
expect(grabByMl(config, 1)).rejects.toThrow('example');
});
});
});
});

describe('find', () => {
let findByHeaderMock;
let findFullSearchMock;

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

beforeEach(async () => {
config.options.mlDisable = true;
result = await grabByMl(config, 1);
result = await find(config, 1);
});

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

result = await grabByMl(config, 1);
result = await find(config, 1);
});

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

result = await grabByMl(config, 1);
result = await find(config, 1);
});

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

0 comments on commit adaeeaa

Please sign in to comment.