Permalink
Browse files

Refactored result to separate object

  • Loading branch information...
tstone committed Jun 24, 2012
1 parent 9b90521 commit 02b833907c6ecb59aa2e8521101176d42923f6ad
Showing with 55 additions and 13 deletions.
  1. +1 −1 package.json
  2. +4 −12 src/{spell.js → checker.js}
  3. +3 −0 src/index.js
  4. +47 −0 src/result.js
View
@@ -23,7 +23,7 @@
"url": "http://github.com/tstone/googlespell.git",
"web": "http://github.com/tstone/googlespell"
},
- "main": "src/spell.js",
+ "main": "src/index.js",
"engines": {
"node": "0.6.x",
"npm": "1.0.x"
@@ -2,7 +2,8 @@
var xml2js = require('xml2js'),
request = require('request'),
_ = require('underscore'),
- lib = require('./lib');
+ lib = require('./lib'),
+ Result = require('./result');
var Checker = function(config){
this.config = _.extend({
@@ -30,20 +31,11 @@ Checker.prototype.check = function(s, callback){
} else {
var parser = new xml2js.Parser({ attrkey: 'attrs', charkey: 'chars' });
parser.parseString(body, function(err, result){
- result = lib.formatResult(result, text);
-
- // Remove suggestions below confidence threshold
- var threshold = this.config.threshold;
- result.suggestions = result.suggestions.reduce(function(acc, x){
- if (x.confidence >= threshold) { acc.push(x); }
- return acc;
- }, []);
-
- if (typeof callback === 'function') { callback(undefined, result); }
+ if (typeof callback === 'function') { callback(undefined, new Result(result, text, this.config)); }
}.bind(this));
}
}.bind(this));
};
-module.exports.Checker = exports.Checker = Checker;
+module.exports = Checker;
View
@@ -0,0 +1,3 @@
+
+exports.Checker = require('./checker');
+exports.Result = require('./result');
View
@@ -0,0 +1,47 @@
+
+
+var _ = require('underscore');
+
+
+var Result = function(base, source, config) { this.__init(base, source, config); };
+(function(cls){
+
+ cls.__init = function(base, source, config) {
+
+ this.config = config;
+ this.source = source;
+
+ if (base.c) {
+ this.suggestions = this.__parseSuggestions(base.c);
+ } else {
+ this.suggestions = [];
+ }
+
+ };
+
+ cls.__parseSuggestions = function(xs){
+ var suggestions = [];
+ var threshold = this.config.threshold;
+
+ var sugObj = function(o) {
+ return {
+ offset: o.attrs.o,
+ confidence: o.attrs.s,
+ word: this.source.substr(o.attrs.o - 1, o.attrs.l),
+ words: o.chars.split('\t')
+ };
+ }.bind(this);
+ var addSug = function(x) {
+ var sug = sugObj(x);
+ if (sug.confidence >= threshold) {
+ suggestions.push(sug);
+ }
+ };
+
+ Array.isArray(xs) ? xs.forEach(addSug) : addSug(xs);
+ this.suggestions = suggestions;
+ };
+
+}(Result.prototype));
+
+module.exports = Result;

0 comments on commit 02b8339

Please sign in to comment.