diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-11-19 12:11:22 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-11-19 12:11:22 +0000 |
| commit | f3676789408dfaf898823c1fbe3cb7d6888a1fd4 (patch) | |
| tree | 5129ae6ac59ae7349f64f35bb05c24d79e4c6a34 | |
| parent | 6bef85a8fde03b50de4aa78064ab4c32ad54041c (diff) | |
Add startsWith style name matcher
| -rw-r--r-- | lib/styles/document-matchers.js | 16 | ||||
| -rw-r--r-- | test/styles/document-matchers.tests.js | 27 |
2 files changed, 34 insertions, 9 deletions
diff --git a/lib/styles/document-matchers.js b/lib/styles/document-matchers.js index 38ac049..3f7fbdd 100644 --- a/lib/styles/document-matchers.js +++ b/lib/styles/document-matchers.js @@ -6,6 +6,7 @@ exports.underline = new Matcher("underline"); exports.strikethrough = new Matcher("strikethrough"); exports.commentReference = new Matcher("commentReference"); exports.equalTo = equalTo; +exports.startsWith = startsWith; function paragraph(options) { @@ -42,11 +43,22 @@ function isList(element, levelIndex, isOrdered) { function equalTo(value) { return { - operator: equals, + operator: operatorEqualTo, operand: value }; } -function equals(first, second) { +function startsWith(value) { + return { + operator: operatorStartsWith, + operand: value + }; +} + +function operatorEqualTo(first, second) { return first.toUpperCase() === second.toUpperCase(); } + +function operatorStartsWith(first, second) { + return second.toUpperCase().indexOf(first.toUpperCase()) === 0; +} diff --git a/test/styles/document-matchers.tests.js b/test/styles/document-matchers.tests.js index 9f2864b..fb2025d 100644 --- a/test/styles/document-matchers.tests.js +++ b/test/styles/document-matchers.tests.js @@ -25,13 +25,6 @@ test("paragraph style name only matches paragraphs with that style name", functi assert.ok(!matcher.matches(paragraphWithStyle("Heading2", "Heading 2"))); }); -test("paragraph style name matching is case insensitive", function() { - var matcher = documentMatchers.paragraph({styleName: documentMatchers.equalTo("heading 1")}); - assert.ok(!matcher.matches(new Paragraph())); - assert.ok(matcher.matches(paragraphWithStyle("Heading1", "heaDING 1"))); - assert.ok(!matcher.matches(paragraphWithStyle("Heading2", "heaDING 2"))); -}); - test("ordered-list(index) matches an ordered list with specified level index", function() { var matcher = documentMatchers.paragraph({list: {isOrdered: true, levelIndex: 1}}); assert.ok(!matcher.matches(new Paragraph())); @@ -55,3 +48,23 @@ test("matchers for lists with index 0 do not match elements that are not lists", function paragraphWithStyle(styleId, styleName) { return new Paragraph([], {styleId: styleId, styleName: styleName}); } + + +test("equalTo matcher is case insensitive", function() { + var matcher = documentMatchers.equalTo("Heading 1"); + assert.ok(matcher.operator(matcher.operand, "heaDING 1")); + assert.ok(!matcher.operator(matcher.operand, "heaDING 2")); +}); + +test("startsWith matches strings with prefix", function() { + var matcher = documentMatchers.startsWith("Heading"); + assert.ok(matcher.operator(matcher.operand, "Heading 1")); + assert.ok(!matcher.operator(matcher.operand, "Custom Heading")); + assert.ok(!matcher.operator(matcher.operand, "Head")); + assert.ok(!matcher.operator(matcher.operand, "Header 2")); +}); + +test("startsWith matcher is case insensitive", function() { + var matcher = documentMatchers.startsWith("Heading"); + assert.ok(matcher.operator(matcher.operand, "heaDING")); +}); |
