GithubHelp home page GithubHelp logo

gex's Introduction

gex

If you're using this library, feel free to contact me on twitter if you have any questions! :) @rjrodger

This module works on both Node.js and browsers.

Current Version: 0.1.4

Tested on: Node.js 0.10.26, Chrome 34, Firefox 28, Safari 5.1, Opera 12.16

Build Status

Glob expressions for JavaScript

"When regular expressions are just too hard!"

Match glob expressions using * and ? against any JavaScript data type. The character * means match anything of any length, the character ? means match exactly one of any character, and all other characters match themselves.

var gex = require('gex')

gex('a*').on( 'abc' ) // returns 'abc'
gex('a*c').on( 'abbbc' ) // returns 'abbbc'
gex('a?c').on( 'abc' ) // returns 'abc'

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 testing, gex can help you ignore them when you use JSON.stringify:

var entity = {created: new Date().getTime(), name:'foo' }
assert.ok( gex('{"created":*,"name":"foo"}').on( JSON.stringify(entity) ) )

If you need to use globbing on files, here's how apply a glob to a list of files in a folder:

var fs = require('fs')
fs.readdir('.',function(err,files){ 
  var pngs = gex('*.png').on(files) 
})

And that's it!

Installation

npm install gex

And in your code:

var gex = require('gex')

Or clone the git repository: git clone git://github.com/rjrodger/gex.git

This library depends on the excellent underscore module: underscore

Usage

The gex object is a function that takes a single argument, the glob expression. This returns a Gex object that has only one function itself: on. The on function accepts any JavaScript data type, and operates as follows:

  • strings, numbers, booleans, dates, regexes: converted to string form for matching, returned as themselves
  • arrays: return a new array with all the elements that matched. Elements are not modified, but are converted to strings for matching. Does not recurse into elements.
  • objects: return a new object with with all the property names that matched. Values are copied by reference.
  • null, NAN, undefined: never match anything

Testing

The unit tests use mocha

npm install mocha

Run with:

mocha test/gex.test.js

gex's People

Contributors

bitdeli-chef avatar rjrodger avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.