From 2d0ff6fa5297b9554f61d99dab22533b187ffec4 Mon Sep 17 00:00:00 2001 From: Artur Adib Date: Fri, 2 Mar 2012 09:58:21 -0500 Subject: [PATCH] grep() --- shell.js | 19 ++++++++++++------- test/grep.js | 30 +++++++++++++++--------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/shell.js b/shell.js index 4a6ab7a..c12f380 100644 --- a/shell.js +++ b/shell.js @@ -482,7 +482,7 @@ function ShellString(str) { //@ + `sed(/.*DELETE_THIS_LINE.*\n/, '', 'source.js')` //@ //@ Reads an input string from `file` and performs a JavaScript `replace()` on the input -//@ using the given search regex and replacement string. Returns the modified string. +//@ using the given search regex and replacement string. Returns the replaced `String()` object. function _sed(options, regex, replacement, file) { options = parseOptions(options, { 'i': 'inplace' @@ -510,14 +510,19 @@ function _sed(options, regex, replacement, file) { exports.sed = wrap('sed', _sed); //@ -//@ #### grep(regex_filter, 'file [file ...]') -//@ Reads input string from given files and returns a string containing all lines of the -//@ file that match the given `regex_filter`. Wildcards are accepted for file names. -function _grep(options, regex, filesStr) { - if (!filesStr) +//@ #### grep([options ,] regex_filter, file [, file ...]') +//@ +//@ Examples: +//@ +//@ + `grep('GLOBAL_VARIABLE', '*.js')` +//@ +//@ Reads input string from given files and returns a `String()` containing all lines of the +//@ file that match the given `regex_filter`. Wildcard `*` accepted. +function _grep(options, regex, file) { + if (!file) error('no file given'); - var files = parsePaths(filesStr); + var files = [].slice.call(arguments, 2); files = expand(files); var grep = ''; diff --git a/test/grep.js b/test/grep.js index 76d3265..6d25da2 100644 --- a/test/grep.js +++ b/test/grep.js @@ -1,4 +1,4 @@ -require('../maker'); +var shell = require('..'); var assert = require('assert'), path = require('path'), @@ -7,7 +7,7 @@ var assert = require('assert'), // Node shims for < v0.7 fs.existsSync = fs.existsSync || path.existsSync; -silent(); +shell.silent(); function numLines(str) { return typeof str === 'string' ? str.match(/\n/g).length : 0; @@ -20,30 +20,30 @@ shell.mkdir('tmp') // Invalids // -grep(); -assert.ok(error()); +shell.grep(); +assert.ok(shell.error()); -grep(/asdf/g); // too few args -assert.ok(error()); +shell.grep(/asdf/g); // too few args +assert.ok(shell.error()); assert.equal(fs.existsSync('/asdfasdf'), false); // sanity check -grep(/asdf/g, '/asdfasdf'); // no such file -assert.ok(error()); +shell.grep(/asdf/g, '/asdfasdf'); // no such file +assert.ok(shell.error()); // // Valids // -var result = grep('line', 'resources/a.txt'); -assert.equal(error(), null); +var result = shell.grep('line', 'resources/a.txt'); +assert.equal(shell.error(), null); assert.equal(result.split('\n').length - 1, 4); -var result = grep('line one', 'resources/a.txt'); -assert.equal(error(), null); +var result = shell.grep('line one', 'resources/a.txt'); +assert.equal(shell.error(), null); assert.equal(result, 'This is line one\n'); -var result = grep(/line one/, 'resources/a.txt'); -assert.equal(error(), null); +var result = shell.grep(/line one/, 'resources/a.txt'); +assert.equal(shell.error(), null); assert.equal(result, 'This is line one\n'); -exit(123); +shell.exit(123);