Skip to content

Commit

Permalink
feat: replace debug with roarr
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 19, 2018
1 parent 0d30bb1 commit c714906
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,6 @@ Return `InvalidValueSentinel` from a subroutine to force Surgeon throw `InvalidD

## Debugging

Surgeon is using [`debug`](https://www.npmjs.com/package/debug) to log debugging information.
Surgeon is using [`roarr`](https://www.npmjs.com/package/roarr) to log debugging information.

Export `DEBUG=surgeon:*` environment variable to enable Surgeon debug log.
Export `ROARR_LOG=TRUE` environment variable to enable Surgeon debug log.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
},
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"debug": "^3.1.0",
"es6-error": "^4.1.1",
"pianola": "^1.2.0",
"regex-parser": "^2.2.9"
"regex-parser": "^2.2.9",
"roarr": "^2.3.0"
},
"description": "Declarative DOM extraction expression evaluator.",
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions src/Logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @flow

import Roarr from 'roarr';

const Logger = Roarr
.child({
package: 'roarr'
});

export default Logger;
12 changes: 6 additions & 6 deletions src/subroutines/nextUntilSubroutine.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// @flow

import {
createDebug
} from '../utilities';
import {
SurgeonError
} from '../errors';
import type {
SubroutineType
} from '../types';
import Logger from '../Logger';

const debug = createDebug('subroutine:nextUntil');
const log = Logger.child({
namespace: 'subroutine:nextUntil'
});

const nextUntilSubroutine: SubroutineType = (subject, [selectorExpression, filterExpression], {evaluator}) => {
debug('selecting following siblings matching "%s" until "%s"', filterExpression, selectorExpression);
log.debug('selecting following siblings matching "%s" until "%s"', filterExpression, selectorExpression);

if (!evaluator.isElement(subject)) {
throw new SurgeonError('Unexpected value. Value must be an element.');
}

const matches = evaluator.nextUntil(subject, selectorExpression, filterExpression);

debug('matched %d node(s)', matches.length);
log.debug('matched %d node(s)', matches.length);

return matches;
};
Expand Down
16 changes: 9 additions & 7 deletions src/subroutines/readSubroutine.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// @flow

import {
createDebug
} from '../utilities';
import {
SurgeonError
} from '../errors';
import type {
SubroutineType
} from '../types';
import Logger from '../Logger';

const debug = createDebug('subroutine:read');
const log = Logger.child({
namespace: 'subroutine:read'
});

const readSubroutine: SubroutineType = (subject, [target, name], {evaluator}) => {
if (!evaluator.isElement(subject)) {
Expand All @@ -20,18 +20,20 @@ const readSubroutine: SubroutineType = (subject, [target, name], {evaluator}) =>
let value;

if (target === 'attribute') {
debug('reading attribute "%s"', name);
log.debug('reading attribute "%s"', name);

value = evaluator.getAttributeValue(subject, name);
} else if (target === 'property') {
debug('reading property "%s"', name);
log.debug('reading property "%s"', name);

value = evaluator.getPropertyValue(subject, name);
} else {
throw new SurgeonError('Unexpected read target.');
}

debug('read value', value);
log.debug({
value
}, 'read value');

return value;
};
Expand Down
10 changes: 5 additions & 5 deletions src/subroutines/removeSubroutine.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import {
FinalResultSentinel
} from 'pianola';
import {
createDebug
} from '../utilities';
import type {
SubroutineType
} from '../types';
import {
SurgeonError
} from '../errors';
import Logger from '../Logger';
import selectSubroutine from './selectSubroutine';

const debug = createDebug('subroutine:remove');
const log = Logger.child({
namespace: 'subroutine:remove'
});

const removeSubroutine: SubroutineType = (subject, [cssSelector, quantifierExpression], {evaluator}) => {
debug('selecting "%s" for removal', cssSelector);
log.debug('selecting "%s" for removal', cssSelector);

if (!evaluator.isElement(subject)) {
throw new SurgeonError('Unexpected value. Value must be an element.');
Expand Down
14 changes: 7 additions & 7 deletions src/subroutines/selectSubroutine.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import {
import {
createQuantifier
} from '../factories';
import {
createDebug
} from '../utilities';
import {
SelectSubroutineUnexpectedResultCountError,
SurgeonError
} from '../errors';
import type {
SubroutineType
} from '../types';
import Logger from '../Logger';

const debug = createDebug('subroutine:select');
const log = Logger.child({
namespace: 'subroutine:select'
});

const selectSubroutine: SubroutineType = (subject, [cssSelector, quantifierExpression], {evaluator}) => {
debug('selecting "%s"', cssSelector);
log.debug('selecting "%s"', cssSelector);

if (!evaluator.isElement(subject)) {
throw new SurgeonError('Unexpected value. Value must be an element.');
Expand All @@ -30,10 +30,10 @@ const selectSubroutine: SubroutineType = (subject, [cssSelector, quantifierExpre

const quantifier = createQuantifier(quantifierExpression);

debug('selector "%s" matched %d node(s)', cssSelector, matches.length);
log.debug('selector "%s" matched %d node(s)', cssSelector, matches.length);

if (matches.length < quantifier.min || matches.length > quantifier.max) {
debug('expected to match between %d and %s matches', quantifier.min, quantifier.max === Infinity ? 'infinity' : quantifier.max);
log.debug('expected to match between %d and %s matches', quantifier.min, quantifier.max === Infinity ? 'infinity' : quantifier.max);

throw new SelectSubroutineUnexpectedResultCountError(matches.length, quantifier);
}
Expand Down
7 changes: 0 additions & 7 deletions src/utilities/createDebug.js

This file was deleted.

1 change: 0 additions & 1 deletion src/utilities/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow

export {default as createDebug} from './createDebug';
export {default as isEnvironmentBrowser} from './isEnvironmentBrowser';

0 comments on commit c714906

Please sign in to comment.