commonform-regexp-annotator
Advanced tools
Comparing version 1.1.0 to 1.1.1
92
index.js
var predicate = require('commonform-predicate') | ||
function rule(expressions, annotator, form, path) | ||
{ return form.content | ||
.reduce( | ||
function(annotations, element, index) { | ||
if (predicate.text(element)) { | ||
var elementPath = path.concat([ 'content', index ]) | ||
expressions | ||
.forEach(function(expression) { | ||
var match | ||
if (expression.global) { | ||
while ((match = expression.exec(element)) !== null) { | ||
annotations.push( | ||
annotator(form, elementPath, expression, match)) } } | ||
else { | ||
match = expression.exec(element) | ||
if (match !== null) { | ||
annotations.push( | ||
annotator(form, elementPath, expression, match)) } } }) } | ||
return annotations }, | ||
[ ]) } | ||
module.exports = function (expressions, annotator) { | ||
return function (form) { | ||
return recurse(expressions, annotator, form, [], []) | ||
} | ||
} | ||
function recurse(expressions, annotator, form, path, annotations ) | ||
{ return annotations | ||
.concat(rule(expressions, annotator, form, path)) | ||
.concat( | ||
form.content | ||
.reduce( | ||
function(annotations, element, index) { | ||
if (predicate.child(element)) { | ||
var childForm = element.form | ||
var childPath = path.concat([ 'content', index, 'form' ]) | ||
return annotations.concat( | ||
recurse( | ||
expressions, annotator, | ||
childForm, childPath, [ ])) } | ||
else { | ||
return annotations } }, | ||
[ ])) } | ||
function rule (expressions, annotator, form, path) { | ||
return form.content.reduce(function (annotations, element, index) { | ||
if (predicate.text(element)) { | ||
var elementPath = path.concat(['content', index]) | ||
expressions.forEach(function (expression) { | ||
var match | ||
if (expression.global) { | ||
while ((match = expression.exec(element)) !== null) { | ||
annotations.push( | ||
annotator(form, elementPath, expression, match) | ||
) | ||
} | ||
} else { | ||
match = expression.exec(element) | ||
if (match !== null) { | ||
annotations.push( | ||
annotator(form, elementPath, expression, match) | ||
) | ||
} | ||
} | ||
}) | ||
} | ||
return annotations | ||
}, []) | ||
} | ||
function commonformRegExpAnnotator(expressions, annotator) | ||
{ return function(form) { | ||
return recurse(expressions, annotator, form, [ ], [ ]) } } | ||
module.exports = commonformRegExpAnnotator | ||
function recurse (expressions, annotator, form, path, annotations) { | ||
return annotations | ||
.concat(rule(expressions, annotator, form, path)) | ||
.concat( | ||
form.content.reduce(function (annotations, element, index) { | ||
if (predicate.child(element)) { | ||
var childForm = element.form | ||
var childPath = path.concat(['content', index, 'form']) | ||
return annotations.concat( | ||
recurse( | ||
expressions, annotator, | ||
childForm, childPath, [] | ||
) | ||
) | ||
} else { | ||
return annotations | ||
} | ||
}, []) | ||
) | ||
} |
{ | ||
"name": "commonform-regexp-annotator", | ||
"description": "create Common Form annotators from lists of regular expressions", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"author": "Kyle E. Mitchell <kyle@kemitchell.com> (http://kemitchell.com)", | ||
"devDependencies": { | ||
"defence-cli": "1.x", | ||
"replace-require-self": "1.x" | ||
"replace-require-self": "1.x", | ||
"standard": "^8.0.0" | ||
}, | ||
@@ -17,6 +18,10 @@ "keywords": [ | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"license": "Apache-2.0", | ||
"repository": "commonform/commonform-regexp-annotator", | ||
"scripts": { | ||
"test": "defence README.md | replace-require-self | node" | ||
"test": "defence README.md | replace-require-self | node", | ||
"lint": "standard" | ||
}, | ||
@@ -23,0 +28,0 @@ "dependencies": { |
@@ -1,4 +0,7 @@ | ||
The module exports a single function that takes an array of `RegExp` and a function for generating annotations and returns an annotator function to apply to Common Forms. | ||
The module exports a single function that takes an array of `RegExp` | ||
and a function for generating annotations and returns an annotator | ||
function to apply to Common Forms. | ||
Include capture groups and flags in your `RegExp` as needed to match and generate annotation messages. | ||
Include capture groups and flags in your `RegExp` as needed to match | ||
and generate annotation messages. | ||
@@ -8,7 +11,13 @@ ```javascript | ||
new RegExp('\\b(apple(s?))\\b', 'gi'), | ||
/\b(thereof)\b/ ] | ||
/\b(thereof)\b/ | ||
] | ||
``` | ||
The annotation function receives the form in which a `RegExp` was found, its path within the overall form, the `RegExp` that matches, and the match data from `RegeExp.prototype.exec`. It must return a [Common Form Annotations](https://npmjs.com/packages/commonform-annotation). | ||
The annotation function receives the form in which a `RegExp` was | ||
found, its path within the overall form, the `RegExp` that matches, | ||
and the match data from `RegeExp.prototype.exec`. It must return a | ||
[Common Form Annotations][annotation] | ||
[annotation]: https://npmjs.com/packages/commonform-annotation) | ||
```javascript | ||
@@ -19,8 +28,11 @@ function message(form, path, expression, match) { | ||
message: ( | ||
( match[1].indexOf('apple') > -1 ) ? | ||
( '"' + word + '" is fruity' ) : | ||
( '"' + word + '" is archaic' ) ), | ||
match[1].indexOf('apple') > -1 | ||
? ('"' + word + '" is fruity') | ||
: ('"' + word + '" is archaic') | ||
), | ||
path: path, | ||
source: 'example-annotator', | ||
url: null } } | ||
url: null | ||
} | ||
} | ||
@@ -38,15 +50,26 @@ var reAnnotator = require('commonform-regexp-annotator') | ||
assert.deepEqual( | ||
annotator({ content: [ 'Drop them apples and the apple stem thereof!' ] }), | ||
[ { message: '"apples" is fruity', | ||
path: [ 'content', 0 ], | ||
annotator({ | ||
content: ['Drop them apples and the apple stem thereof!'] | ||
}), | ||
[ | ||
{ | ||
message: '"apples" is fruity', | ||
path: ['content', 0], | ||
source: 'example-annotator', | ||
url: null }, | ||
{ message: '"apple" is fruity', | ||
path: [ 'content', 0 ], | ||
url: null | ||
}, | ||
{ | ||
message: '"apple" is fruity', | ||
path: ['content', 0], | ||
source: 'example-annotator', | ||
url: null }, | ||
{ message: '"thereof" is archaic', | ||
path: [ 'content', 0 ], | ||
url: null | ||
}, | ||
{ | ||
message: '"thereof" is archaic', | ||
path: ['content', 0], | ||
source: 'example-annotator', | ||
url: null } ]) | ||
url: null | ||
} | ||
] | ||
) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51
73
3986
3
3