diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2015-06-23 08:18:52 +0300 |
---|---|---|
committer | Rebecca Turner <me@re-becca.org> | 2015-06-26 03:27:33 +0300 |
commit | b50be6aff34a739ca43de65f546e743d1a9975b9 (patch) | |
tree | 3153fd3a271245b8a05a9be196286fbe31fc9d44 /lib/help-search.js | |
parent | 7c5ebe0552b7b5d0cdca34d28c4f16fe794ff2ff (diff) |
src: make the npm source comply with `standard`
This is a huge set of mostly mechanical changes. Going forward, all
changes to the npm source base are expected to comply with `standard`,
and it's been integrated into the test suite to enforce that. There are
a few notes below about specific classes of changes that need to be
handled specially for npm's code base.
standard: "Expected error to be handled."
`standard` only expects errors spelled "err" to be handled.
`npm-registry-mock` never actually invokes its callback with an error,
so in some cases I just changed it to be spelled "er" and called it
good.
standard: "Expected a "break" statement before 'case'."
This behavior is actually on purpose, and I don't feel like rewriting
the affected code right now (or, you know, ever). So I added code
comments disabling the checks in the three applicable changes.
standard: "x is a function."
Rebinding functions created via declarations (as opposed to expressions)
is a no-no?
PR-URL: https://github.com/npm/npm/pull/8668
Diffstat (limited to 'lib/help-search.js')
-rw-r--r-- | lib/help-search.js | 122 |
1 files changed, 61 insertions, 61 deletions
diff --git a/lib/help-search.js b/lib/help-search.js index d8553453b..16f389027 100644 --- a/lib/help-search.js +++ b/lib/help-search.js @@ -1,29 +1,29 @@ module.exports = helpSearch -var fs = require("graceful-fs") - , path = require("path") - , asyncMap = require("slide").asyncMap - , npm = require("./npm.js") - , glob = require("glob") - , color = require("ansicolors") +var fs = require('graceful-fs') +var path = require('path') +var asyncMap = require('slide').asyncMap +var npm = require('./npm.js') +var glob = require('glob') +var color = require('ansicolors') -helpSearch.usage = "npm help-search <text>" +helpSearch.usage = 'npm help-search <text>' function helpSearch (args, silent, cb) { - if (typeof cb !== "function") cb = silent, silent = false + if (typeof cb !== 'function') { + cb = silent + silent = false + } if (!args.length) return cb(helpSearch.usage) - var docPath = path.resolve(__dirname, "..", "doc") - return glob(docPath + "/*/*.md", function (er, files) { - if (er) - return cb(er) + var docPath = path.resolve(__dirname, '..', 'doc') + return glob(docPath + '/*/*.md', function (er, files) { + if (er) return cb(er) readFiles(files, function (er, data) { - if (er) - return cb(er) + if (er) return cb(er) searchFiles(args, data, function (er, results) { - if (er) - return cb(er) + if (er) return cb(er) formatResults(args, results, cb) }) }) @@ -52,8 +52,7 @@ function searchFiles (args, files, cb) { for (var a = 0, l = args.length; a < l && !match; a++) { match = data.toLowerCase().indexOf(args[a].toLowerCase()) !== -1 } - if (!match) - return + if (!match) return var lines = data.split(/\n+/) @@ -61,14 +60,14 @@ function searchFiles (args, files, cb) { // if the next line has a search term, then skip all 3 // otherwise, set the line to null. then remove the nulls. l = lines.length - for (var i = 0; i < l; i ++) { + for (var i = 0; i < l; i++) { var line = lines[i] - , nextLine = lines[i + 1] - , ll + var nextLine = lines[i + 1] + var ll match = false if (nextLine) { - for (a = 0, ll = args.length; a < ll && !match; a ++) { + for (a = 0, ll = args.length; a < ll && !match; a++) { match = nextLine.toLowerCase() .indexOf(args[a].toLowerCase()) !== -1 } @@ -80,12 +79,12 @@ function searchFiles (args, files, cb) { } match = false - for (a = 0, ll = args.length; a < ll && !match; a ++) { + for (a = 0, ll = args.length; a < ll && !match; a++) { match = line.toLowerCase().indexOf(args[a].toLowerCase()) !== -1 } if (match) { // skip over the next line - i ++ + i++ continue } @@ -94,7 +93,7 @@ function searchFiles (args, files, cb) { // now squish any string of nulls into a single null lines = lines.reduce(function (l, r) { - if (!(r === null && l[l.length-1] === null)) l.push(r) + if (!(r === null && l[l.length - 1] === null)) l.push(r) return l }, []) @@ -103,10 +102,10 @@ function searchFiles (args, files, cb) { // now see how many args were found at all. var found = {} - , totalHits = 0 + var totalHits = 0 lines.forEach(function (line) { args.forEach(function (arg) { - var hit = (line || "").toLowerCase() + var hit = (line || '').toLowerCase() .split(arg.toLowerCase()).length - 1 if (hit > 0) { found[arg] = (found[arg] || 0) + hit @@ -115,27 +114,28 @@ function searchFiles (args, files, cb) { }) }) - var cmd = "npm help " - if (path.basename(path.dirname(file)) === "api") { - cmd = "npm apihelp " + var cmd = 'npm help ' + if (path.basename(path.dirname(file)) === 'api') { + cmd = 'npm apihelp ' } - cmd += path.basename(file, ".md").replace(/^npm-/, "") - results.push({ file: file - , cmd: cmd - , lines: lines - , found: Object.keys(found) - , hits: found - , totalHits: totalHits - }) + cmd += path.basename(file, '.md').replace(/^npm-/, '') + results.push({ + file: file, + cmd: cmd, + lines: lines, + found: Object.keys(found), + hits: found, + totalHits: totalHits + }) }) // if only one result, then just show that help section. if (results.length === 1) { - return npm.commands.help([results[0].file.replace(/\.md$/, "")], cb) + return npm.commands.help([results[0].file.replace(/\.md$/, '')], cb) } if (results.length === 0) { - console.log("No results for " + args.map(JSON.stringify).join(" ")) + console.log('No results for ' + args.map(JSON.stringify).join(' ')) return cb() } @@ -161,25 +161,25 @@ function formatResults (args, results, cb) { var out = results.map(function (res) { var out = res.cmd - , r = Object.keys(res.hits).map(function (k) { - return k + ":" + res.hits[k] + var r = Object.keys(res.hits).map(function (k) { + return k + ':' + res.hits[k] }).sort(function (a, b) { return a > b ? 1 : -1 - }).join(" ") + }).join(' ') out += ((new Array(Math.max(1, cols - out.length - r.length))) - .join(" ")) + r + .join(' ')) + r - if (!npm.config.get("long")) return out + if (!npm.config.get('long')) return out - out = "\n\n" + out - + "\n" + (new Array(cols)).join("—") + "\n" - + res.lines.map(function (line, i) { - if (line === null || i > 3) return "" - for (var out = line, a = 0, l = args.length; a < l; a ++) { + out = '\n\n' + out + '\n' + + (new Array(cols)).join('—') + '\n' + + res.lines.map(function (line, i) { + if (line === null || i > 3) return '' + for (var out = line, a = 0, l = args.length; a < l; a++) { var finder = out.toLowerCase().split(args[a].toLowerCase()) - , newOut = "" - , p = 0 + var newOut = '' + var p = 0 finder.forEach(function (f) { newOut += out.substr(p, f.length) @@ -193,16 +193,16 @@ function formatResults (args, results, cb) { } return newOut - }).join("\n").trim() + }).join('\n').trim() return out - }).join("\n") - - if (results.length && !npm.config.get("long")) { - out = "Top hits for "+(args.map(JSON.stringify).join(" ")) - + "\n" + (new Array(cols)).join("—") + "\n" - + out - + "\n" + (new Array(cols)).join("—") + "\n" - + "(run with -l or --long to see more context)" + }).join('\n') + + if (results.length && !npm.config.get('long')) { + out = 'Top hits for ' + (args.map(JSON.stringify).join(' ')) + '\n' + + (new Array(cols)).join('—') + '\n' + + out + '\n' + + (new Array(cols)).join('—') + '\n' + + '(run with -l or --long to see more context)' } console.log(out.trim()) |