diff --git a/Readme.md b/Readme.md index c8c7556..7a5a969 100644 --- a/Readme.md +++ b/Readme.md @@ -41,6 +41,9 @@ levenshtein('saturday', 'sunday'); // 3 /* Case sensitive! */ levenshtein('DwAyNE', 'DUANE') !== levenshtein('dwayne', 'DuAnE'); // true +/* Insensitive: */ +levenshtein('DwAyNE', 'DUANE', true) === levenshtein('dwayne', 'DuAnE', true); // true + /* Order insensitive */ levenshtein('aarrgh', 'aargh') === levenshtein('aargh', 'aarrgh'); // true ``` @@ -64,6 +67,7 @@ Options: -h, --help output usage information -v, --version output version number + -i, --insensitive ignore casing Usage: diff --git a/cli.js b/cli.js index 4bc6c99..36f3997 100755 --- a/cli.js +++ b/cli.js @@ -37,6 +37,12 @@ var command; command = Object.keys(pack.bin)[0]; +/* + * Whether to ignore case. + */ + +var insensitive = false; + /** * Get the distance for words. * @@ -44,7 +50,7 @@ command = Object.keys(pack.bin)[0]; * @return {number} */ function distance(values) { - return levenshtein(values[0], values[1]); + return levenshtein(values[0], values[1], insensitive); } /** @@ -63,6 +69,7 @@ function help() { '', ' -h, --help output usage information', ' -v, --version output version number', + ' -i, --insensitive ignore casing', '', 'Usage:', '', @@ -101,17 +108,36 @@ function getDistance(value) { * Program. */ +var index = argv.indexOf('--insensitive'); + +if (index === -1) { + index = argv.indexOf('-i'); +} + if ( argv.indexOf('--help') !== -1 || argv.indexOf('-h') !== -1 ) { console.log(help()); -} else if ( + + return; +} + +if ( argv.indexOf('--version') !== -1 || argv.indexOf('-v') !== -1 ) { console.log(pack.version); -} else if (argv.length) { + + return; +} + +if (index !== -1) { + argv.splice(index, 1); + insensitive = true; +} + +if (argv.length) { getDistance(argv.join(' ')); } else if (!expextPipeIn) { getDistance(); diff --git a/index.js b/index.js index 64f9ec7..68a127b 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ var cache, cache = []; codes = []; -function levenshtein(value, other) { +function levenshtein(value, other, insensitive) { var length, lengthOther, code, @@ -29,6 +29,11 @@ function levenshtein(value, other) { return length; } + if (insensitive) { + value = value.toLowerCase(); + other = other.toLowerCase(); + } + index = 0; while (index < length) { diff --git a/test.js b/test.js index f50cec8..3276a0a 100644 --- a/test.js +++ b/test.js @@ -40,6 +40,13 @@ describe('levenshteinDistance()', function () { ); }); + it('should match case if `insensitive` is given', function () { + assert( + levenshteinDistance('DwAyNE', 'DUANE', true) === + levenshteinDistance('dwayne', 'DuAnE', true) + ); + }); + it('should not care about parameter order', function () { assert( levenshteinDistance('aarrgh', 'aargh') === diff --git a/test.sh b/test.sh index a6fb6e4..6a64883 100755 --- a/test.sh +++ b/test.sh @@ -21,6 +21,18 @@ it "Should accept space seperated values" result=`./cli.js "sitting" "kitten"` 2> /dev/null assert $result "3" +it "Should be case-sensitive by default" + result=`./cli.js "a" "A"` 2> /dev/null + assert $result "1" + +it "Should be case-insensitive when given \`-i\`" + result=`./cli.js -i "a" "A"` 2> /dev/null + assert $result "0" + +it "Should be case-insensitive when given \`--insensitive\`" + result=`./cli.js --insensitive "a" "A"` 2> /dev/null + assert $result "0" + it "Should accept comma seperated values" result=`./cli.js "sitting,kitten"` 2> /dev/null assert $result "3"