Skip to content

Commit

Permalink
add remove oxford comma working
Browse files Browse the repository at this point in the history
  • Loading branch information
spencermountain committed Feb 29, 2020
1 parent 8d12d7c commit a32e5dc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 10 additions & 0 deletions src/Subset/Lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/lists.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down

0 comments on commit a32e5dc

Please sign in to comment.