Skip to content

Commit

Permalink
now using jasmine, runs in browser too
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Sep 26, 2013
1 parent 0d46bb6 commit 5012cca
Show file tree
Hide file tree
Showing 15 changed files with 3,899 additions and 326 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "0.11"
- "0.10"
- "0.8"
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -2,9 +2,12 @@


If you're using this library, feel free to contact me on twitter if you have any questions! :) [@rjrodger](http://twitter.com/rjrodger) If you're using this library, feel free to contact me on twitter if you have any questions! :) [@rjrodger](http://twitter.com/rjrodger)


Current Version: 0.1.1 This module works on both Node.js and browsers.


Tested on: node 0.8
Current Version: 0.1.2

Tested on: Node.js 0.10.19, Chrome 29, Firefox 23, Safari 5.1, Opera 12.11




## Glob expressions for JavaScript ## Glob expressions for JavaScript
Expand Down
3 changes: 3 additions & 0 deletions gex-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gex-min.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

199 changes: 199 additions & 0 deletions gex.js
@@ -0,0 +1,199 @@
/* Copyright (c) 2011-2013 Richard Rodger, MIT License, https://github.com/rjrodger/patrun */
"use strict";


(function() {
var root = this
var previousGex = root.gex

var has_require = typeof require !== 'undefined'

var _ = root._

if( typeof _ === 'undefined' ) {
if( has_require ) {
_ = require('underscore')
}
else throw new Error('gex requires underscore, see http://underscorejs.org');
}


function Gex(gexspec) {
var self = this

function dodgy(obj) {
return ( _.isNull(obj)
|| _.isNaN(obj)
|| _.isUndefined(obj) )
}

function clean(gexexp) {
var gexstr = ''+gexexp
if( _.isNull(gexexp)
|| _.isNaN(gexexp)
|| _.isUndefined(gexexp) ) {
gexstr = ''
}
return gexstr
}

/*
function gexstr(gexexp) {
var gs = []
if( _.isArray(gexexp) || _.isArguments(gexexp) ) {
for( var i = 0; i < gexexp.length; i++ ){
var s = clean(gexexp[i])
gexexp.push(
( '*' == s || '?' == s ) ? s
: self.esc(s)
)
}
gs = gs.join('')
}
else {
gs = clean(gexexp)
}
return gs
}
*/

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
}


_.noConflict = function() {
root._ = previousGex;
return self;
}


self.on = function(obj) {
if( _.isString(obj)
|| _.isNumber(obj)
|| _.isBoolean(obj)
|| _.isDate(obj)
|| _.isRegExp(obj)
)
{
//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]) && match(obj[i]) ) {
out.push(obj[i])
}
}
return out
}

else if( dodgy(obj) ) {
return null
}

else if( _.isObject(obj) ) {
var out = {}
for( var p in obj ) {
if( obj.hasOwnProperty(p) ) {
//if( !!re.exec(p) ) {
if( match(p) ) {
out[p] = obj[p]
}
}
}
return out
}

else {
return null
}
}

self.esc = function(gexexp) {
var gexstr = clean(gexexp)
gexstr = gexstr.replace(/\*/g,'**')
gexstr = gexstr.replace(/\?/g,'*?')
return gexstr
}


self.re = function(gs) {
if( '' == gs || gs ) {
var gs = self.escregexp(gs)

// use [\s\S] instead of . to match newlines
gs = gs.replace(/\\\*/g,'[\\s\\S]*')
gs = gs.replace(/\\\?/g,'[\\s\\S]')
//gs = gs.replace(/\\\*/g,'.*')
//gs = gs.replace(/\\\?/g,'.')

// escapes ** and *?
gs = gs.replace(/\[\\s\\S\]\*\[\\s\\S\]\*/g,'\\\*')
gs = gs.replace(/\[\\s\\S\]\*\[\\s\\S\]/g,'\\\?')

gs = '^'+gs+'$'

return new RegExp(gs)
}
else {
var gexstrs = _.keys(gexmap)
return 1 == gexstrs.length ? gexmap[gexstrs[0]] : _.clone(gexmap)
}
}

self.escregexp = function(restr) {
return restr ? (''+restr).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") : ''
}

self.toString = function() {
return ''+_.keys(gexmap)
}


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(gexspec) {
var gex = new Gex(gexspec)
return gex
}
gex.Gex = Gex



if( typeof exports !== 'undefined' ) {
if( typeof module !== 'undefined' && module.exports ) {
exports = module.exports = gex
}
exports.gex = gex
}
else {
root.gex = gex
}

}).call(this);
168 changes: 0 additions & 168 deletions lib/gex.js

This file was deleted.

0 comments on commit 5012cca

Please sign in to comment.