Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35addee
commit aa9ba65
Showing
2 changed files
with
23 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import quotesDB from 'typographic-quotes-l10n-db'; | ||
|
||
export default function typographicQuotes(input = '', {locale = 'en-us'} = {}) { | ||
let separator = ''; | ||
const localeQuotes = quotesDB[locale]; | ||
const pattern = /(^|\s)(?:"(.*?)"|'(.*?)')(\s|$|\.|,|\?|!)/gim; | ||
const handleQuotes = (quotes, cb) => | ||
(match, before='', part1='', part2='', after='') => { | ||
let text = (part1 + part2); | ||
if (cb) { text = text.replace(pattern, cb); } | ||
if (locale === 'fr') { separator = ' '; } | ||
return `${before}${quotes[0]}${separator}${text}${separator}${quotes[1]}${after}`; | ||
} | ||
|
||
const primary = (match, before='', part1='', part2='', after='')=> { | ||
const quotes = [localeQuotes[0], localeQuotes[1]]; | ||
const text = (part1 + part2).replace(pattern, secondary); | ||
return `${before}${quotes[0]}${text}${quotes[1]}${after}`; | ||
} | ||
|
||
const secondary = (match, before='', part1='', part2='', after='')=> { | ||
const quotes = [localeQuotes[2], localeQuotes[3]]; | ||
let text = part1 + part2; | ||
return `${before}${quotes[0]}${text}${quotes[1]}${after}`; | ||
} | ||
|
||
return input.replace(pattern, primary); | ||
return input.replace(pattern, | ||
handleQuotes( | ||
localeQuotes.slice(0, 2), | ||
handleQuotes(localeQuotes.slice(2, 4)))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters