Skip to content

Commit bb17ab9

Browse files
committed
refactor: move lang detection to library module
1 parent 343d532 commit bb17ab9

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

source/library/ensure-language.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import franc from 'franc';
2+
3+
export default (input, allowed) => {
4+
const detected = franc.all(input)
5+
.filter(lang => lang[1] >= 0.45)
6+
.map(lang => lang[0]);
7+
8+
// franc spits out ['und'] when unable to
9+
// guess any languages, let it through in this case
10+
const matches = detected[0] === 'und' ||
11+
detected.indexOf(allowed) > -1;
12+
13+
return {
14+
matches,
15+
detected
16+
};
17+
};

source/rules/lang.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
import franc from 'franc';
1+
import ensureLanguage from '../library/ensure-language';
22

33
export default (parsed, when, value) => {
44
const negated = when === 'never';
5-
const detected = franc.all(parsed.subject)
6-
.filter(lang => lang[1] >= 0.45)
7-
.map(lang => lang[0]);
8-
9-
// franc spits out ['und'] when unable to
10-
// guess any languages, let it through in this case
11-
const matches = detected[0] === 'und' ||
12-
detected.indexOf(value) > -1;
5+
const {matches, detected} = ensureLanguage(parsed.subject, value);
136

147
return [
158
negated ? !matches : matches,

0 commit comments

Comments
 (0)