Skip to content

Commit

Permalink
fix french quotes close #3
Browse files Browse the repository at this point in the history
  • Loading branch information
iamstarkov committed Dec 3, 2015
1 parent 35addee commit aa9ba65
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
25 changes: 12 additions & 13 deletions index.js
@@ -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))))
}
11 changes: 11 additions & 0 deletions test.js
Expand Up @@ -6,6 +6,11 @@ it('should fix simple quotes', ()=> {
equal(quotes(`foo "foo" bar`), `foo “foo” bar`);
});

it('should fix simple quotes for French', ()=> {
let locale = 'fr';
equal(quotes(`foo 'foo' bar`, { locale }), `foo « foo » bar`);
});

it('should support locale', ()=> {
let locale = 'ru';
equal(quotes(`foo 'foo' bar`, {locale}), `foo «foo» bar`);
Expand Down Expand Up @@ -50,6 +55,12 @@ it('should fix nested quotes', ()=> {
equal(quotes(`foo 'foo "inside" bar' bar`), `foo “foo ‘inside’ bar” bar`);
});

it('should fix nested quotes for French', ()=> {
const locale = 'fr';
equal(quotes(`foo "foo 'inside' bar" bar`, { locale }), `foo « foo “ inside ” bar » bar`);
equal(quotes(`foo 'foo "inside" bar' bar`, { locale }), `foo « foo “ inside ” bar » bar`);
});

it('should fix nested quotes in start', ()=> {
equal(quotes(`"foo 'inside' bar" bar`), `“foo ‘inside’ bar” bar`);
equal(quotes(`'foo "inside" bar' bar`), `“foo ‘inside’ bar” bar`);
Expand Down

0 comments on commit aa9ba65

Please sign in to comment.