Skip to content

Commit

Permalink
Add more punctuation marks to splitter
Browse files Browse the repository at this point in the history
Closes GH-7.

This commit adds comma (`,`), colon (`:`), semicolon (`;`), exclamation mark
(`!`), question mark (`?`), and parens (`(` and `)`) to the list of stops.
  • Loading branch information
wooorm committed Nov 21, 2022
1 parent 6585579 commit 99779c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ export function apStyleTitleCase(value, options) {

const stop = configuration.stopwords || defaults
const keep = configuration.keepSpaces
const splitter = /(\s+|[-‑–—])/
const splitter = /(\s+|[-‑–—,:;!?()])/

return value
.split(splitter)
.map((word, index, all) => {
if (/\s+/.test(word)) return keep ? word : ' '
if (splitter.test(word)) return word
// The splitter:
if (index % 2) {
if (/\s+/.test(word)) return keep ? word : ' '
return word
}

const lower = word.toLowerCase()

if (index !== 0 && index !== all.length - 1 && stop.includes(lower)) {
Expand Down
3 changes: 2 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ test('ap-style-title-case', function () {
[
'Observations of isolated pulsars and disk-fed X-ray binaries.',
'Observations of Isolated Pulsars and Disk-Fed X-Ray Binaries.'
]
],
['Shakspeare; Or, the Poet', 'Shakspeare; or, the Poet']
]

for (const pattern of patterns) {
Expand Down

0 comments on commit 99779c1

Please sign in to comment.