doubleplus-numbers
Advanced tools
Comparing version 0.0.2 to 0.1.0
100
index.js
@@ -1,11 +0,87 @@ | ||
// TODO: Build this list programmatically | ||
var expressions = [ | ||
/one \(1\)/, | ||
/two \(2\)/, | ||
/three \(3\)/, | ||
/four \(4\)/, | ||
/five \(5\)/ ] | ||
var range = require('array-range'); | ||
var numberStringRepresentation = require('number-string-representation'); | ||
var RANGEMAX = 100; | ||
// Regex for detecting numerals: | ||
// \d+ First numerals | ||
// (,\d+)* Optionally, a comma and some more numerals (may be repeated) | ||
// (\.\d+)? Optionally, a decimal point followed by more numerals | ||
var reNumerals = /\d+(,\d+)*(\.\d+)?/i; | ||
function deDollarize(dollary) { | ||
// Unfortunately, number-string-representation alawys returns stuff about | ||
// dollars at the end of its output. This function trims it off. | ||
// TODO: Account for decimals instead of lopping them off. | ||
// Example `dollary` input string: "two thousand five hundred twenty-three and 00/100 dollars" | ||
var reNoDollars = /(.+)(\sand \d\d\/100 dollars)/i; | ||
if (reNoDollars.test(dollary)) { | ||
return reNoDollars.exec(dollary)[1]; | ||
} else { | ||
return dollary; | ||
} | ||
} | ||
function getAllNumerals(input) { | ||
/* | ||
returns a list of all numerals (as strings) in the input string | ||
good test string: "one 234.56 three 456 five 67.89 foo" | ||
more test strings: | ||
two (2) things | ||
three (3) more | ||
five dollars and twenty cents (5.20)! | ||
four thousand (4,000)!!! | ||
some more (1,234.567) | ||
billions and billions (1,234,567,890.10) | ||
one 234.56 three 456 five 67.89 foo | ||
Test at http://regexpal.com/ | ||
*/ | ||
r = []; | ||
var matches = input.match(reNumerals); | ||
console.log("reNumerals: " + reNumerals); | ||
console.log("matches: " + matches); | ||
for (i=0; i < matches.length; i++) { | ||
var match = matches[i]; | ||
console.log("i=" + i + "; match: " + match); | ||
r.push(match); | ||
console.log("r: " +r); | ||
} | ||
return r; | ||
} | ||
function getNumberWords(input) { | ||
/* | ||
Returns a string containing words for the numeral `input` | ||
*/ | ||
dollarWords = numberStringRepresentation(input); | ||
words = deDollarize(dollarWords); | ||
return words.toLowerCase(); | ||
} | ||
function getRedundancy(input) { | ||
/* | ||
Given an integer, makes a redundant string like "two (2)" | ||
*/ | ||
return getNumberWords(input) + " (" + input + ")"; | ||
} | ||
function escapeRegExp(str) { | ||
// Source: http://stackoverflow.com/a/17326679 | ||
return String(str).replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); | ||
} | ||
function makeRegExp(input) { | ||
// use this to map() onto an array of redundancies | ||
return new RegExp(escapeRegExp(input)); | ||
} | ||
var integerRange = range(1,RANGEMAX+1); | ||
var redundancies = integerRange.map(getRedundancy); | ||
var expressions = redundancies.map(makeRegExp); | ||
function message(form, path, expression, match) { | ||
var word = match[0] | ||
var word = match[0]; | ||
return { | ||
@@ -16,6 +92,8 @@ message: '"' + word + '" repeats a written number and numeral, which is redundant and error-prone', | ||
source: 'doubleplus-numbers', | ||
url: null } } | ||
url: null | ||
}; | ||
} | ||
var annotator = require('commonform-regexp-annotator')(expressions, message) | ||
var annotator = require('commonform-regexp-annotator')(expressions, message); | ||
module.exports = annotator | ||
module.exports = annotator; |
{ | ||
"name": "doubleplus-numbers", | ||
"description": "Common Form annotator to find and identify redundant and repeated number(s) and numeral(s)", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"author": "Ansel Halliburton <github@anseljh.com> (http://anseljh.com)", | ||
@@ -16,8 +16,6 @@ "bugs": "https://github.com/anseljh/doubleplus-numbers/issues", | ||
"dependencies": { | ||
"numeral": "1.5.x", | ||
"number-string-representation": "0.1.9" | ||
"commonform-regexp-annotator": ">=1.0.3", | ||
"number-string-representation": "0.1.9", | ||
"array-range": ">=1.0" | ||
}, | ||
"peerDependencies": { | ||
"commonform-regexp-annotator": ">=1.0.3" | ||
}, | ||
"devDependencies": { | ||
@@ -29,4 +27,4 @@ "defence-cli": "~1.0.1", | ||
"scripts": { | ||
"test": "defence README.md | replace-require-self | node" | ||
} | ||
"test": "defence README.md | replace-require-self | node" | ||
} | ||
} |
@@ -0,0 +0,0 @@ # doubleplus-numbers |
Sorry, the diff of this file is not supported yet
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
16009
83
+ Addedarray-range@>=1.0
+ Addedarray-range@1.0.1(transitive)
- Removednumeral@1.5.x
- Removednumeral@1.5.6(transitive)