From a32e5dc0dc61cb50f58640e0c4ac02371550be82 Mon Sep 17 00:00:00 2001 From: spencer kelly Date: Sat, 29 Feb 2020 12:56:34 -0500 Subject: [PATCH] add remove oxford comma working --- scratch.js | 6 +++--- src/Subset/Lists.js | 10 ++++++++++ tests/lists.test.js | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/scratch.js b/scratch.js index aa176c8e0..0983e0e8d 100644 --- a/scratch.js +++ b/scratch.js @@ -10,8 +10,8 @@ let txt = require('./scripts/test/speed/_sotu-text.js') // // console.log(doc._cache) // console.log(doc.has('#Person')) -let doc = nlp('i saw red, blue and green.') -doc.lists().remove('blue') -console.log(doc.text()) +let doc = nlp('i saw red, blue, and green.') // doc.lists().addOxfordComma() +doc.lists().removeOxfordComma() +console.log(doc.text()) // console.log(arr) diff --git a/src/Subset/Lists.js b/src/Subset/Lists.js index 4132493ab..d5153b211 100644 --- a/src/Subset/Lists.js +++ b/src/Subset/Lists.js @@ -49,9 +49,19 @@ const addMethod = function(Doc) { return this.filter(doc => parse(doc).hasOxford) } addOxfordComma() { + let items = this.items() + let needsComma = items.eq(items.length - 2) + if (needsComma.found && needsComma.has('@hasComma') === false) { + needsComma.post(', ') + } return this } removeOxfordComma() { + let items = this.items() + let needsComma = items.eq(items.length - 2) + if (needsComma.found && needsComma.has('@hasComma') === true) { + needsComma.post(' ') + } return this } } diff --git a/tests/lists.test.js b/tests/lists.test.js index 3dd803334..f591abc20 100644 --- a/tests/lists.test.js +++ b/tests/lists.test.js @@ -1,6 +1,26 @@ const test = require('tape') const nlp = require('./_lib') +test('comma-remove', function(t) { + let doc = nlp('i saw red, blue, and green.') + doc.lists().removeOxfordComma() + t.equal(doc.text(), 'i saw red, blue and green.', 'remove comma') + + doc = nlp('i saw red, blue, and green.') + doc.lists().addOxfordComma() + t.equal(doc.text(), 'i saw red, blue, and green.', 'add comma') + + doc.lists().addOxfordComma() + doc.lists().addOxfordComma() + doc.lists().addOxfordComma() + t.equal(doc.text(), 'i saw red, blue, and green.', 'just one comma') + doc.lists().removeOxfordComma() + doc.lists().removeOxfordComma() + doc.lists().removeOxfordComma() + t.equal(doc.text(), 'i saw red, blue and green.', 'still no commas') + t.end() +}) + test('list-remove', function(t) { let doc = nlp('i saw red, blue and green.') doc.lists().remove('asdf')