Skip to content

Commit

Permalink
feat: make evaluator config depend on the running env
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jan 17, 2017
1 parent 910bf27 commit 173f818
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Have you got suggestions for improvement? [I am all ears](https://github.com/gaj

|Name|Description|Default value|
|---|---|---|
|`evaluator`|HTML parser and selector engine. Possible values: `cheerio`, `browser`. Use `cheerio` if you are running Surgeon in Node.js. Use `browser` if you are running Surgeon in a browser or headless browser (e.g. PhantomJS).|`cheerio`|
|`evaluator`|HTML parser and selector engine. Possible values: `cheerio`, `browser`. Use `cheerio` if you are running Surgeon in Node.js. Use `browser` if you are running Surgeon in a browser or a headless browser (e.g. PhantomJS).|`browser` if `window` and `document` variables are present, `cheerio` otherwise.|

## Cookbook

Expand Down
15 changes: 13 additions & 2 deletions src/factories/createConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ import {
browserEvaluator,
cheerioEvaluator
} from '../evaluators';
import {
isEnvironmentBrowser
} from '../utilities';

export default (userConfiguration: UserConfigurationType = {}): ConfigurationType => {
let evaluator;

if (!userConfiguration.evaluator || userConfiguration.evaluator === 'cheerio') {
let evaluatorName = userConfiguration.evaluator;

if (!evaluatorName) {
const environmentIsBrowser = isEnvironmentBrowser();

evaluatorName = environmentIsBrowser ? 'browser' : 'cheerio';
}

if (evaluatorName === 'cheerio') {
evaluator = cheerioEvaluator();
} else if (userConfiguration.evaluator === 'browser') {
} else if (evaluatorName === 'browser') {
evaluator = browserEvaluator();
} else {
throw new Error('Unknown adapter.');
Expand Down
4 changes: 3 additions & 1 deletion src/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import getQuantifier from './getQuantifier';
import hasAttributeSelector from './hasAttributeSelector';
import hasPropertySelector from './hasPropertySelector';
import hasQuantifier from './hasQuantifier';
import isEnvironmentBrowser from './isEnvironmentBrowser';

export {
getAttributeSelector,
getPropertySelector,
getQuantifier,
hasAttributeSelector,
hasPropertySelector,
hasQuantifier
hasQuantifier,
isEnvironmentBrowser
};
5 changes: 5 additions & 0 deletions src/utilities/isEnvironmentBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow

export default (): boolean => {
return typeof window === 'object' && typeof document === 'object';
};

0 comments on commit 173f818

Please sign in to comment.