Skip to content

Commit

Permalink
Add support for insensitive comparison
Browse files Browse the repository at this point in the history
On the CLI, `-i` or `--insensitive` turns insensitive comparison
on. On the API, the third arguments (default: false), turns
insensitive comparison on when `true`.

Closes GH-1.
  • Loading branch information
wooorm committed Jun 23, 2015
1 parent 6f4af86 commit 16d4b02
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -64,6 +67,7 @@ Options:
-h, --help output usage information
-v, --version output version number
-i, --insensitive ignore casing
Usage:
Expand Down
32 changes: 29 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@ var command;

command = Object.keys(pack.bin)[0];

/*
* Whether to ignore case.
*/

var insensitive = false;

/**
* Get the distance for words.
*
* @param {Array.<string>} values
* @return {number}
*/
function distance(values) {
return levenshtein(values[0], values[1]);
return levenshtein(values[0], values[1], insensitive);
}

/**
Expand All @@ -63,6 +69,7 @@ function help() {
'',
' -h, --help output usage information',
' -v, --version output version number',
' -i, --insensitive ignore casing',
'',
'Usage:',
'',
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var cache,
cache = [];
codes = [];

function levenshtein(value, other) {
function levenshtein(value, other, insensitive) {
var length,
lengthOther,
code,
Expand All @@ -29,6 +29,11 @@ function levenshtein(value, other) {
return length;
}

if (insensitive) {
value = value.toLowerCase();
other = other.toLowerCase();
}

index = 0;

while (index < length) {
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') ===
Expand Down
12 changes: 12 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 16d4b02

Please sign in to comment.