Permalink
Browse files

updated README and tests

  • Loading branch information...
rjrodger committed Mar 11, 2013
1 parent f776c04 commit 0d46bb6dde05d9f6ace92ce8360016b8948ce1b1
Showing with 66 additions and 14 deletions.
  1. +13 −4 README.md
  2. +37 −10 lib/gex.js
  3. +16 −0 test/gex.test.js
View
@@ -6,7 +6,8 @@ Current Version: 0.1.1
Tested on: node 0.8
-Glob expressions for JavaScript
+
+## Glob expressions for JavaScript
*"When regular expressions are just too hard!"*
@@ -25,6 +26,12 @@ You can also match against objects and arrays:
gex('a*').on( ['ab','zz','ac'] ) // returns ['ab','ac']
gex('a*').on( {ab:1,zz:2,ac:3} ) // returns {ab:1,ac:3}
+And also match against multiple globs:
+
+ gex(['a*','b*']).on( 'bx' ) // returns 'bx'
+ gex(['a*','b*']).on( ['ax','zz','bx'] ) // returns ['ax','bx']
+
+
One of the most useful things you can do with this library is quick
assertions in unit tests. For example if your objects contain dates,
randomly generated unique identifiers, or other data irrelevant for
@@ -71,10 +78,12 @@ itself: `on`. The `on` function accepts any JavaScript data type, and operates a
## Testing
-The unit tests use [expresso](https://github.com/visionmedia/expresso)
+The unit tests use [mocha](https://github.com/visionmedia/mocha)
+
+ npm install mocha
- npm install expresso
+Run with:
-The tests are in test/gex.test.js
+ mocha test/gex.test.js
View
@@ -43,6 +43,19 @@ function Gex(gexspec) {
}
+ function match(str) {
+ str = ''+str
+ var hasmatch = false
+ var gexstrs = _.keys(gexmap)
+ //console.log(gexstrs)
+ for(var i = 0; i < gexstrs.length && !hasmatch; i++ ) {
+ hasmatch = !!gexmap[gexstrs[i]].exec(str)
+ //console.log(hasmatch+' '+gexstrs[i]+' '+str)
+ }
+ return hasmatch
+ }
+
+
self.on = function(obj) {
if( _.isString(obj)
|| _.isNumber(obj)
@@ -51,15 +64,17 @@ function Gex(gexspec) {
|| _.isRegExp(obj)
)
{
- return (!!re.exec(''+obj)) ? obj : null
+ //return (!!re.exec(''+obj)) ? obj : null
+ return match(obj) ? obj : null
}
else if( _.isArray(obj)
|| _.isArguments(obj)
) {
var out = []
for( var i = 0; i < obj.length; i++ ) {
- if( !dodgy(obj[i]) && !!re.exec(''+obj[i]) ) {
+ //if( !dodgy(obj[i]) && !!re.exec(''+obj[i]) ) {
+ if( !dodgy(obj[i]) && match(obj[i]) ) {
out.push(obj[i])
}
}
@@ -70,11 +85,12 @@ function Gex(gexspec) {
return null
}
- else if( 'object' == typeof(obj) ) {
+ else if( _.isObject(obj) ) {
var out = {}
for( var p in obj ) {
if( obj.hasOwnProperty(p) ) {
- if( !!re.exec(p) ) {
+ //if( !!re.exec(p) ) {
+ if( match(p) ) {
out[p] = obj[p]
}
}
@@ -114,7 +130,8 @@ function Gex(gexspec) {
return new RegExp(gs)
}
else {
- return re
+ var gexstrs = _.keys(gexmap)
+ return 1 == gexstrs.length ? gexmap[gexstrs[0]] : _.clone(gexmap)
}
}
@@ -123,16 +140,26 @@ function Gex(gexspec) {
}
self.toString = function() {
- return str
+ return ''+_.keys(gexmap)
}
- var str = gexstr(gexspec)
- var re = self.re(str)
+
+ var gexstrs = (null==gexspec||_.isNaN(gexspec)) ? [] : _.isArray(gexspec) ? gexspec : [gexspec]
+ //console.log(gexstrs)
+
+ var gexmap = {}
+
+ _.each( gexstrs, function(str) {
+ var re = self.re(str)
+ gexmap[str]=re
+ })
+
+ //console.dir(gexmap)
}
-function gex(gexexp) {
- var gex = new Gex(gexexp)
+function gex(gexspec) {
+ var gex = new Gex(gexspec)
return gex
}
gex.Gex = Gex
View
@@ -6,6 +6,7 @@
var assert = require('assert')
+var util = require('util')
var gex = require('..')
@@ -123,4 +124,19 @@ describe('gex', function() {
assert.equal( 'a\nb', g.on('a\nb') )
})
+
+ it('multi', function() {
+ var g = gex(['a','b'])
+ assert.equal('a',g.on('a'))
+ assert.equal('b',g.on('b'))
+ assert.equal('{ a: /^a$/, b: /^b$/ }',util.inspect(g.re()))
+
+ g = gex(['a*','b'])
+ assert.equal('ax',g.on('ax'))
+ assert.equal('b',g.on('b'))
+ assert.equal("{ 'a*': /^a[\\s\\S]*$/, b: /^b$/ }",util.inspect(g.re()))
+
+ assert.equal( 'bx', ''+gex(['a*','b*']).on( 'bx' ) )
+ assert.equal( 'ax,bx', ''+gex(['a*','b*']).on( ['ax','zz','bx'] ) )
+ })
})

0 comments on commit 0d46bb6

Please sign in to comment.