-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add atStart() and atEnd() convenience methods to MatchingContext.
- Loading branch information
1 parent
5ebf0d1
commit 8555ce6
Showing
2 changed files
with
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const UnionReplacer = require('../dist/union-replacer.cjs'); | ||
|
||
describe('MatchingContext', () => { | ||
function startEndReplace(mctx) { | ||
const l = mctx.match[0].length; | ||
const s = mctx.atStart() ? 'S' : '_'; | ||
const e = mctx.atEnd() ? 'E' : '_'; | ||
return `[${s}${e}:${l}]`; | ||
} | ||
it('recognizes start and end', () => { | ||
const r = new UnionReplacer([[/^x|y|z$/, startEndReplace, true]]); | ||
expect(r.replace('xyz')).toBe('[S_:1][__:1][_E:1]'); | ||
}); | ||
it('recognizes start = end match and empty end', () => { | ||
const r = new UnionReplacer([[/x|$/, startEndReplace, true]]); | ||
expect(r.replace('x')).toBe('[SE:1][_E:0]'); | ||
}); | ||
it('recognizes empty start and empty end', () => { | ||
const r = new UnionReplacer([[/^|$/, startEndReplace, true]]); | ||
expect(r.replace('x')).toBe('[S_:0]x[_E:0]'); | ||
}); | ||
it('recognizes empty start and empty on empty string', () => { | ||
const r = new UnionReplacer([[/^|$/, startEndReplace, true]]); | ||
expect(r.replace('')).toBe('[SE:0]'); | ||
}); | ||
}); |