Skip to content

Commit

Permalink
Add minLength support
Browse files Browse the repository at this point in the history
Closes #17.
  • Loading branch information
wooorm committed Mar 20, 2015
1 parent edcbe56 commit c90f51e
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 13 deletions.
8 changes: 6 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ franc.all('O Brasil caiu 26 posições em');
*/

/* "und" is returned for too-short input: */
franc.all(''); // [ [ 'und', 1 ] ]
franc('the'); // 'und'

/* You can change what’s too short (default: 10): */
franc('the', {'minLength': 3}); // 'sco'

/* Provide a whitelist: */
franc.all('O Brasil caiu 26 posições em', {
Expand Down Expand Up @@ -139,6 +142,7 @@ Options:
-h, --help output usage information
-v, --version output version number
-m, --min-length <number> minimum length to accept
-w, --whitelist <string> allow languages
-b, --blacklist <string> disallow languages
Expand All @@ -163,7 +167,7 @@ $ echo "Alle mennesker er født frie og" | franc --whitelist nob,dan

## Supported languages

**franc** supports 175 “languages”. For a complete list, check out [Supported-Languages.md](Supported-Languages.md).
**franc** supports 175 “languages”, by default. For a complete list, check out [Supported-Languages.md](Supported-Languages.md).

## Supporting more or less languages

Expand Down
15 changes: 15 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function help() {
'',
' -h, --help output usage information',
' -v, --version output version number',
' -m, --min-length <number> minimum length to accept',
' -w, --whitelist <string> allow languages',
' -b, --blacklist <string> disallow languages',
'',
Expand Down Expand Up @@ -75,6 +76,7 @@ function help() {
var index;
var blacklist;
var whitelist;
var minLength;

/**
* Log the language for `value`.
Expand All @@ -84,6 +86,7 @@ var whitelist;
function detect(value) {
if (value && value.length) {
console.log(franc(value, {
'minLength': minLength,
'whitelist': whitelist,
'blacklist': blacklist
}));
Expand All @@ -108,6 +111,18 @@ if (
) {
console.log(pack.version);
} else {
index = argv.indexOf('--min-length');

if (index === -1) {
index = argv.indexOf('-m');
}

if (index !== -1) {
minLength = Number(argv[index + 1] || '');

argv.splice(index, 2);
}

index = argv.indexOf('--blacklist');

if (index === -1) {
Expand Down
2 changes: 1 addition & 1 deletion dist/franc-all.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/franc-most.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/franc.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions lib/franc.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,15 @@ function singleLanguageTuples(language) {
* containing language--distance tuples.
*/
function detectAll(value, options) {
var settings = options || {};
var minLength = MIN_LENGTH;
var script;

if (!value || value.length < MIN_LENGTH) {
if (settings.minLength !== null && settings.minLength !== undefined) {
minLength = settings.minLength;
}

if (!value || value.length < minLength) {
return singleLanguageTuples('und');
}

Expand All @@ -300,7 +306,7 @@ function detectAll(value, options) {
*/

return normalize(value, getDistances(
utilities.asTuples(value), data[script[0]], options || {}
utilities.asTuples(value), data[script[0]], settings
));
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
"unicode-7.0.0": "^0.1.0"
},
"scripts": {
"test-browser": "_mocha --check-leaks -t 10000 test/browser.js",
"test-browser": "mocha --check-leaks -t 10000 test/browser.js",
"test-cli": "bash ./test/cli.sh",
"test-lib": "_mocha --check-leaks test/index.js",
"test-lib": "mocha --check-leaks test/index.js",
"test-coveralls": "istanbul cover _mocha --report lcovonly -- --check-leaks test/index.js",
"test-coverage": "istanbul cover _mocha -- -- test/index.js",
"test-travis": "npm run test-coveralls && npm run test-browser && npm run test-cli",
Expand Down
14 changes: 14 additions & 0 deletions test/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ it "Should accept \`-b\`"
result=`./cli.js -b por,glg "O Brasil caiu 26 posições em"` 2> /dev/null
assert $result "src"

it "Should accept \`--min-length\`"
result=`./cli.js --min-length 3 "the"` 2> /dev/null
assert $result "sco"

result=`./cli.js --min-length 4 "the"` 2> /dev/null
assert $result "und"

it "Should accept \`-m\`"
result=`./cli.js -m 3 "the"` 2> /dev/null
assert $result "sco"

result=`./cli.js -m 4 "the"` 2> /dev/null
assert $result "und"

it "Should accept \`--help\`"
code=0
./cli.js --help > /dev/null 2>&1 || code=$?
Expand Down
39 changes: 35 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('franc()', function () {
}
);

it('should accept blacklist parameter', function () {
it('should accept `blacklist`', function () {
var language = franc(fixtures[MAGIC_NUMBER]);

var result = franc(fixtures[MAGIC_NUMBER], {
Expand All @@ -87,14 +87,29 @@ describe('franc()', function () {
assert(result !== language);
});

it('should accept whitelist parameter', function () {
it('should accept `whitelist`', function () {
var result = franc(fixtures[MAGIC_NUMBER], {
'whitelist': [MAGIC_LANGUAGE]
});

assert(result === MAGIC_LANGUAGE);
});

it('should accept `minLength`', function () {
var result = franc('the', {
'minLength': 3
});

assert(result === 'sco');

result = franc('the', {
'minLength': 4
});

assert(result === 'und');
});
});

describe('franc.all()', function () {
it('should be of type `function`', function () {
assert(typeof franc.all === 'function');
Expand Down Expand Up @@ -137,7 +152,7 @@ describe('franc.all()', function () {
assert(result[1].length === 2);
});

it('should accept blacklist parameter', function () {
it('should accept `blacklist`', function () {
var shouldBeLanguage = franc(fixtures[MAGIC_NUMBER]);
var result = franc.all(fixtures[MAGIC_NUMBER], {
'blacklist': [shouldBeLanguage]
Expand All @@ -148,14 +163,30 @@ describe('franc.all()', function () {
});
});

it('should accept whitelist parameter', function () {
it('should accept `whitelist`', function () {
var result = franc.all(fixtures[MAGIC_NUMBER], {
'whitelist': [MAGIC_LANGUAGE]
});

assert(result.length === 1);
assert(result[0][0] === MAGIC_LANGUAGE);
});

it('should accept `minLength`', function () {
var result = franc.all('the', {
'minLength': 3
});

assert(result[0][0] === 'sco');
assert(result.length !== 1);

result = franc.all('the', {
'minLength': 4
});

assert(result[0][0] === 'und');
assert(result.length === 1);
});
});

describe('algorithm', function () {
Expand Down

0 comments on commit c90f51e

Please sign in to comment.