Skip to content

Commit de63238

Browse files
committed
fix: add tests for smart quotes helper functions
1 parent 023d73d commit de63238

File tree

2 files changed

+88
-6
lines changed

2 files changed

+88
-6
lines changed

spec/smartQuotes.spec.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {expect} from 'chai'
2+
import {isDoubleQuote, isSingleQuote, isWhitespace, isSeparatorOrWhitespace, isApostrophe} from '../src/smartQuotes'
3+
4+
const allSingleQuotes = ['‘', '’', '‹', '›', '‚', '‘', '›', '‹', `'`, `‘`]
5+
const allDoubleQuotes = ['«', '»', '»', '«', '"', '"', '“', '”', '”', '”', '“', '“', '„', '“']
6+
const charValues = ['', '*', '<', 'b', 'ab']
7+
const nonStringValues = [undefined, null, true, 123, NaN]
8+
const whitespaceChars = [' ', '\t', '\n', '\r', '\v', '\f']
9+
const separatorValues = ['>', '-', '–—']
10+
11+
describe('Smart Quotes Helper Functions', () => {
12+
describe('isDoubleQuote', () => {
13+
it('Should return false for non double quote values', () => {
14+
[...charValues, ...separatorValues, ...nonStringValues, ...allSingleQuotes].forEach(value => {
15+
expect(isDoubleQuote(value)).to.equal(false, `Failed for value: ${value}`)
16+
})
17+
})
18+
19+
it('Should return true for double quote values', () => {
20+
allDoubleQuotes.forEach(value => {
21+
expect(isDoubleQuote(value)).to.equal(true, `Failed for value: ${value}`)
22+
})
23+
})
24+
})
25+
26+
describe('isSingleQuote', () => {
27+
it('Should return false for non single quote values', () => {
28+
[...charValues, ...separatorValues, ...nonStringValues, ...allDoubleQuotes].forEach(value => {
29+
expect(isSingleQuote(value)).to.equal(false, `Failed for value: ${value}`)
30+
})
31+
})
32+
33+
it('Should return true for single quote values', () => {
34+
allSingleQuotes.forEach(value => {
35+
expect(isSingleQuote(value)).to.equal(true, `Failed for value: ${value}`)
36+
})
37+
})
38+
})
39+
40+
describe('isWhiteSpace', () => {
41+
it('should return false for non whitespace characters', () => {
42+
[...charValues, ...nonStringValues].forEach(value => {
43+
expect(isWhitespace(value)).to.equal(false, `Failed for: ${value}`)
44+
})
45+
})
46+
47+
it('should return true for whitespace characters', () => {
48+
[...whitespaceChars ].forEach(value => {
49+
expect(isWhitespace(value)).to.equal(true, `Failed for: ${value}`)
50+
})
51+
})
52+
})
53+
54+
describe('isSeparatorOrWhitespace', () => {
55+
it('should return false for non whitespace / separator characters', () => {
56+
[...charValues, ...nonStringValues ].forEach(value => {
57+
expect(isSeparatorOrWhitespace(value)).to.equal(false, `Failed for: ${value}`)
58+
})
59+
})
60+
61+
it('should return true for whitespace/ separator characters', () => {
62+
[...whitespaceChars, ...separatorValues].forEach(value => {
63+
expect(isSeparatorOrWhitespace(value)).to.equal(true, `Failed for: ${value}`)
64+
})
65+
})
66+
})
67+
68+
describe('isApostrophe', () => {
69+
it('should return false for non apostrophe characters', () => {
70+
[...charValues, ...nonStringValues, ...allDoubleQuotes, `'f`, '’j', '‘', '‹', '›', '‚', '‘', '›', '‹', `‘`].forEach(value => {
71+
expect(isApostrophe(value)).to.equal(false, `Failed for: ${value}`)
72+
})
73+
})
74+
75+
it('should return true for apostrophe characters', () => {
76+
[`'`, '’'].forEach(value => {
77+
expect(isApostrophe(value)).to.equal(true, `Failed for: ${value}`)
78+
})
79+
})
80+
})
81+
})
82+

src/smartQuotes.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ export const shouldApplySmartQuotes = (config, target) => {
44
const {smartQuotes, quotes, singleQuotes} = config
55
return !!smartQuotes && isValidQuotePairConfig(quotes) && isValidQuotePairConfig(singleQuotes) && target.isContentEditable
66
}
7-
8-
const isDoubleQuote = (char) => /^[«»"]$/.test(char)
9-
const isSingleQuote = (char) => /^[']$/.test(char)
10-
const isApostrophe = (char) => /^[']$/.test(char)
11-
const isWhitespace = (char) => /^\s$/.test(char)
12-
const isSeparatorOrWhitespace = (char) => /\s|[>\-]/.test(char)
7+
// TODO: isDoubleQuote, isSingleQuote, isApostrophe, isWhitespace accept more than one char
8+
export const isDoubleQuote = (char) => /^[«»"]$/.test(char)
9+
export const isSingleQuote = (char) => /^[']$/.test(char)
10+
export const isApostrophe = (char) => /^[']$/.test(char)
11+
export const isWhitespace = (char) => /^\s$/.test(char)
12+
export const isSeparatorOrWhitespace = (char) => /\s|[>\-]/.test(char)
1313

1414
const shouldBeOpeningQuote = (text, indexCharBefore) => indexCharBefore < 0 || isSeparatorOrWhitespace(text[indexCharBefore])
1515
const shouldBeClosingQuote = (text, indexCharBefore) => !!text[indexCharBefore] && !isSeparatorOrWhitespace(text[indexCharBefore])

0 commit comments

Comments
 (0)