Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2011-12-03 06:19:58 +0400
committerisaacs <i@izs.me>2011-12-03 06:19:58 +0400
commit7d5fc664d501ae854e7c66b3dbf1700bd4baeed0 (patch)
tree3d33bb7607cdc2d7d5c4352ccb9bb4baa5f1642d /lib/search.js
parentbeeb7fd82e3cc543a9911fe87ec096f8eed29e7a (diff)
Fix #1673 Support for regexps in search
Diffstat (limited to 'lib/search.js')
-rw-r--r--lib/search.js27
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/search.js b/lib/search.js
index 45e436a0c..71888835c 100644
--- a/lib/search.js
+++ b/lib/search.js
@@ -108,16 +108,22 @@ function getWords (data) {
function filterWords (data, args, notArgs) {
var words = data.words
for (var i = 0, l = args.length; i < l; i ++) {
- if (words.indexOf(args[i]) === -1) {
- return false
- }
+ if (!match(words, args[i])) return false
}
for (var i = 0, l = notArgs.length; i < l; i ++) {
- if (words.indexOf(notArgs[i]) !== -1) return false
+ if (match(words, notArgs[i])) return false
}
return true
}
+function match (words, arg) {
+ if (arg.charAt(0) === "/" && arg.slice(-1) === "/") {
+ arg = new RegExp(arg.substr(1, arg.length - 2))
+ return words.match(arg)
+ }
+ return words.indexOf(arg) !== -1
+}
+
function prettify (data, args) {
try {
var tty = require("tty")
@@ -201,8 +207,17 @@ function addColorMarker (str, arg, i) {
var m = i % cl + 1
, markStart = String.fromCharCode(m)
, markEnd = String.fromCharCode(0)
- , pieces = str.toLowerCase().split(arg.toLowerCase())
+
+ if (arg.charAt(0) === "/" && arg.slice(-1) === "/") {
+ return str.replace( new RegExp(arg.substr(1, arg.length - 2), "gi")
+ , function (bit) { return markStart + bit + markEnd } )
+
+ }
+
+ // just a normal string, do the split/map thing
+ var pieces = str.toLowerCase().split(arg.toLowerCase())
, p = 0
+
return pieces.map(function (piece, i) {
piece = str.substr(p, piece.length)
var mark = markStart
@@ -211,8 +226,8 @@ function addColorMarker (str, arg, i) {
p += piece.length + arg.length
return piece + mark
}).join("")
- return str.split(arg).join(mark)
}
+
function colorize (line) {
for (var i = 0; i < cl; i ++) {
var m = i + 1