Skip to content

Commit

Permalink
Add utility for tokenizing objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Harding committed Mar 10, 2014
1 parent a49d6d1 commit 255c3c6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var semver = require('semver'),
],
bloodhound: [
'src/bloodhound/version.js',
'src/bloodhound/tokenizers.js',
'src/bloodhound/lru_cache.js',
'src/bloodhound/persistent_storage.js',
'src/bloodhound/transport.js',
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ files = [
'test/vendor/**/*',
'src/common/utils.js',
'src/bloodhound/version.js',
'src/bloodhound/tokenizers.js',
'src/bloodhound/lru_cache.js',
'src/bloodhound/persistent_storage.js',
'src/bloodhound/transport.js',
Expand Down
10 changes: 1 addition & 9 deletions src/bloodhound/bloodhound.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,7 @@
return Bloodhound;
};

Bloodhound.tokenizers = {
whitespace: function whitespaceTokenizer(s) {
return s.split(/\s+/);
},

nonword: function nonwordTokenizer(s) {
return s.split(/\W+/);
}
};
Bloodhound.tokenizers = tokenizers;

// instance methods
// ----------------
Expand Down
27 changes: 27 additions & 0 deletions src/bloodhound/tokenizers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* typeahead.js
* https://github.com/twitter/typeahead.js
* Copyright 2013-2014 Twitter, Inc. and other contributors; Licensed MIT
*/

var tokenizers = (function(root) {

return {
nonword: nonword,
whitespace: whitespace,
obj: {
nonword: getObjTokenizer(nonword),
whitespace: getObjTokenizer(whitespace)
}
};

function whitespace(s) { return s.split(/\s+/); }

function nonword(s) { return s.split(/\W+/); }

function getObjTokenizer(tokenizer) {
return function setKey(key) {
return function tokenize(o) { return tokenizer(o[key]); };
};
}
})();
26 changes: 26 additions & 0 deletions test/tokenizers_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe('tokenizers', function() {

it('.whitespace should tokenize on whitespace', function() {
var tokens = tokenizers.whitespace('big-deal ok');
expect(tokens).toEqual(['big-deal', 'ok']);
});

it('.nonword should tokenize on non-word characters', function() {
var tokens = tokenizers.nonword('big-deal ok');
expect(tokens).toEqual(['big', 'deal', 'ok']);
});

it('.obj.whitespace should tokenize on whitespace', function() {
var t = tokenizers.obj.whitespace('val');
var tokens = t({ val: 'big-deal ok' });

expect(tokens).toEqual(['big-deal', 'ok']);
});

it('.obj.nonword should tokenize on non-word characters', function() {
var t = tokenizers.obj.nonword('val');
var tokens = t({ val: 'big-deal ok' });

expect(tokens).toEqual(['big', 'deal', 'ok']);
});
});

0 comments on commit 255c3c6

Please sign in to comment.