Skip to content

Commit

Permalink
Update removeNonWord pattern to cover more chars. closes mout#156. cl…
Browse files Browse the repository at this point in the history
…oses mout#163
  • Loading branch information
conradz authored and millermedeiros committed Jan 30, 2014
1 parent 68c2f43 commit ecb1f2a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
19 changes: 19 additions & 0 deletions _build/pattern-removeNonWord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Run this script with Node to generate the pattern that is included in
// src/string/removeNonWord.js.
//
// node _build/pattern-removeNonWord
//

var regenerate = require('regenerate');

var pattern = regenerate()
.addRange('a', 'z')
.addRange('A', 'Z')
.addRange('0', '9')
.addRange(0xC0, 0xFF)
.add(' ', '-', '_')
.remove(0xF7, 0xD7)
.toString();

console.log('// Pattern for string/removeNonWord:');
console.log('/' + pattern + '/g');
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"commander": "~1.0.5",
"rocambole": "~0.2.3",
"jshint": "2.x",
"rimraf": "~2.2.2"
"rimraf": "~2.2.2",
"regenerate": "~0.5.4"
},
"testling": {
"preprocess": "node build testling",
Expand Down Expand Up @@ -95,4 +96,4 @@
"tests/runner.js"
]
}
}
}
5 changes: 4 additions & 1 deletion src/string/removeNonWord.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
define(['../lang/toString'], function(toString){
// This pattern is generated by the _build/pattern-removeNonWord.js script
var PATTERN = /[^\x20\x2D0-9A-Z\x5Fa-z\xC0-\xD6\xD8-\xF6\xF8-\xFF]/g;

/**
* Remove non-word chars.
*/
function removeNonWord(str){
str = toString(str);
return str.replace(/[^0-9a-zA-Z\xC0-\xFF \-_]/g, '');
return str.replace(PATTERN, '');
}

return removeNonWord;
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/string/spec-removeNonWord.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ define(['mout/string/removeNonWord'], function (removeNonWord) {
describe('string/removeNonWord()', function(){

it('should remove non word chars', function(){
var str = 'lorem ~!@#$%^&*()_+`-={}[]|\\:";\'/?><., ipsum';
var str = 'lorem ~!@#$%^&*()_+`-={}[]|\\:";\'/?><., ipsum\xD7';
expect( removeNonWord(str) ).toEqual('lorem _- ipsum');
});

Expand Down

0 comments on commit ecb1f2a

Please sign in to comment.