Skip to content

Commit

Permalink
Use relevant search in step docs
Browse files Browse the repository at this point in the history
  • Loading branch information
schipiga committed Mar 15, 2018
1 parent 5c9b76c commit 0f49bbe
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 14 deletions.
85 changes: 74 additions & 11 deletions lib/tools.js
Expand Up @@ -11,12 +11,18 @@ var util = require("util");
require("colors");
var _ = require("lodash");
var highlight = require("cli-highlight").highlight;
var natural = require("natural");
var U = require("glace-utils");

var d = U.switchColor();

var self = module.exports;

var classifier = new natural.BayesClassifier();
classifier.isTrained = false;

var all_steps = [];
all_steps.isFilled = false;

self.listSteps = (filter, namesOnly) => {
namesOnly = namesOnly || false;

Expand All @@ -36,28 +42,56 @@ self.listSteps = (filter, namesOnly) => {
.filter(i => /^\w+$/.test(i))
.filter(i => /^\D/.test(i));

var i = 0;
var filtered_steps = [];

for (var s of steps) {
if (all_steps.isFilled && !filter) break;

var func = SS[s];

if (!util.isFunction(func)) continue;

var doc = getDoc(func);

var tmp = {
name: s,
description: ` ${func.toString().split("{")[0]}{...}`,
doc: doc,
};

if (!all_steps.isFilled) all_steps.push(tmp);

if (doc && !classifier.isTrained) {
classifier.addDocument(doc, s);
}

if (namesOnly) {
if (!U.textContains(s, filter)) continue;
} else {
if (!(U.textContains(s, filter) || U.textContains(doc, filter))) continue;
}

console.log(d(`${++i}. ${s}:`));
console.log(d(` ${func.toString().split("{")[0]}{...}`));

if (doc) {
doc = ` /**\n${doc}\n */`;
console.log(highlight(doc, { language: "js" }));
}
filtered_steps.push(tmp);
};
all_steps.isFilled = true;

if (!classifier.isTrained) {
classifier.train();
classifier.isTrained = true;
}

if (filter && !namesOnly) {
filtered_steps = getRelevant(
all_steps, filtered_steps,
classifier.getClassifications(filter));
}

var i = 0;
for (s of (filter ? filtered_steps : all_steps)) {
console.log(d(`${++i}. ${s.name}:`));
console.log(d(s.description));
if (s.doc) console.log(highlight(s.doc, { language: "js" }));
}

if (i === 0) {
console.log("No steps are found".yellow);
Expand Down Expand Up @@ -100,7 +134,6 @@ self.listFixtures = (filter, namesOnly) => {
console.log(d(`${++i}. ${fx}`));

if (doc) {
doc = ` /**\n${doc}\n */`;
console.log(highlight(doc, { language: "js" }));
}
};
Expand All @@ -110,14 +143,44 @@ self.listFixtures = (filter, namesOnly) => {
};
};

var getRelevant = (all, filtered, classified) => {
if (classified.map(i => i.value).reduce((j, i) => j === i ? j : NaN)) {
return filtered;
}

classified = classified.slice(0, 3);

var result = [];

for (var cls of classified) {
result.push(getStep(all, cls.label));
};

for (var f of filtered) {
if (getStep(result, f.name)) continue;
result.push(f);
};

return result.reverse();
};

var getStep = (bucket, name) => {
for (var b of bucket) {
if (b.name === name) return b;
}
return null;
};

var getDoc = func => {
return func
var doc = func
.__doc__
.split("\n")
.map(i => i.trim())
.filter(i => i)
.map(i => ` ${i}`)
.join("\n");
if (!doc) return doc;
return ` /**\n${doc}\n */`;
};

var load = () => {
Expand Down
51 changes: 48 additions & 3 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -39,6 +39,7 @@
"glace-utils": "^1.1.7",
"lodash": "^4.17.5",
"mocha": "^4.0.1",
"natural": "^0.5.6",
"rewire": "^3.0.2",
"sinon": "^4.4.5",
"sinon-chai": "^2.14.0",
Expand Down

0 comments on commit 0f49bbe

Please sign in to comment.