Skip to content

Commit b8ea835

Browse files
authored
Merge pull request #52 from eliotw/master
Keyword triggers for fl oz instead of oz conversions
2 parents 563f335 + 1a2bdb0 commit b8ea835

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/conversion_helper.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const metricDistanceUnits = [/km/, /light-?years?/,
7878
const metricWeightUnits = [/kgs?/, /grams?/, /kilograms?/];
7979
const metricVolumeUnits = [/(?:milli|centi|deca|kilo)?lit(?:er|re)s?/, /(?:deca|kilo)?m(?:eters?)?(?:\^3| cubed?)/];
8080
const metricForceUnits = [/newtons?/, /dynes?/];
81+
const liquidKeywords = ['liquid', 'water', 'tea', 'beer', 'soda', 'cider', 'juice', 'coffee', 'liquor', 'milk', 'bottle', 'spirits', 'rum', 'vodka', 'tequila', 'wine', 'oil'];
8182

8283
const ukSubreddits = ["britain", "british", "england", "english", "scotland", "scottish", "wales", "welsh", "ireland", "irish", "london", "uk"];
8384

@@ -322,7 +323,27 @@ const unitLookupList = [
322323
"isWeaklyInvalidInput" : isHyperbole,
323324
"conversionFunction" : (i) => volumeMap(i * 0.0295735295625),
324325
"ignoredUnits" : metricVolumeUnits,
325-
"ignoredKeywords" : ukSubreddits
326+
"ignoredKeywords" : ukSubreddits,
327+
"preprocess" : (comment) => {
328+
const input = comment['body'];
329+
const ozRegex = new RegExp(( rh.startRegex
330+
+ rh.numberRegex
331+
+ "[- ]?"
332+
+ rh.regexJoinToString([/oz/, /ounces?/])
333+
),'gi');
334+
const ozAndLiquidRegex = new RegExp(( ozRegex.source
335+
+ ".+?\\b"
336+
+ rh.regexJoinToString(liquidKeywords)
337+
),'i');
338+
339+
if (!ozAndLiquidRegex.test(input)) {
340+
return input;
341+
}
342+
343+
return input.replace(ozRegex, (oz, offset, string) => {
344+
return " " + oz + " fl. oz";
345+
});
346+
}
326347
},
327348
{
328349
"imperialUnits" : [/oz/, /ounces?/],

test/converter-test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ describe('Converter', () => {
5858
testConvert(
5959
[
6060
"10 fl oz",
61-
"1001 oz. liquid"
61+
"1001 oz. liquid",
62+
"20 oz of water"
6263
],
6364
{
6465
"10 fl. oz." : "300 mL",
65-
"1,001 fl. oz." : "30 L"
66+
"1,001 fl. oz." : "30 L",
67+
"20 fl. oz." : "600 mL",
6668
}
6769
);
6870
});

0 commit comments

Comments
 (0)