-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add nextUntil subroutine (#17)
- Loading branch information
Showing
10 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// @flow | ||
|
||
export {default as nextUntilSubroutine} from './nextUntilSubroutine'; | ||
export {default as readSubroutine} from './readSubroutine'; | ||
export {default as selectSubroutine} from './selectSubroutine'; | ||
export {default as testSubroutine} from './testSubroutine'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// @flow | ||
|
||
import { | ||
FinalResultSentinel | ||
} from 'pianola'; | ||
import { | ||
createDebug | ||
} from '../utilities'; | ||
import { | ||
parseQuantifierExpression | ||
} from '../parsers'; | ||
import { | ||
SelectSubroutineUnexpectedResultCountError, | ||
SurgeonError | ||
} from '../errors'; | ||
import type { | ||
SelectSubroutineQuantifierType, | ||
SubroutineType | ||
} from '../types'; | ||
|
||
const debug = createDebug('subroutine:nextUntil'); | ||
|
||
const selectSubroutine: SubroutineType = (subject, [selectorExpression, filterExpression], {evaluator}) => { | ||
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); | ||
|
||
return matches; | ||
}; | ||
|
||
export default selectSubroutine; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// @flow | ||
|
||
import test from 'ava'; | ||
import sinon from 'sinon'; | ||
import { | ||
FinalResultSentinel | ||
} from 'pianola'; | ||
import nextUntilSubroutine from '../../../src/subroutines/nextUntilSubroutine'; | ||
|
||
test('returns array when expecting multiple results', (t): void => { | ||
const isElement = sinon.stub().returns(true); | ||
const nextUntil = sinon.stub().returns(['foo', 'bar']); | ||
|
||
const evaluator = { | ||
isElement, | ||
nextUntil | ||
}; | ||
|
||
const results = nextUntilSubroutine(null, ['.foo', '.bar'], {evaluator}); | ||
|
||
t.true(nextUntil.calledWith(null, '.foo', '.bar')); | ||
|
||
t.deepEqual(results, ['foo', 'bar']); | ||
}); |