text : function(options) {
		if(!(check.object(options) || check.undefined(options))) {
			throw new TypeError("Incorrect argument type, requires Object.");
		}
		
		//validated arguments: color, font, x, y, text, textAlign
		var validated_options;
		try {
			validated_options = argument_check(options);
		} catch(error) {
			throw error;
		}
		
		//text handler
		//if text is set, then verify it's either a createjs Text() object or something we can String()
		if(!check.undefined(validated_options.text)) {
			if (!(check.instance(validated_options.text, this._createjs.Text))) {
				validated_options.text = String(validated_options.text);
			}
		}
		
		//append / overwrite any options passed in
		for (variable in validated_options) {
			this._text_options[variable] = validated_options[variable];
		}
	},
Exemple #2
0
/**
 * text being set indicates the query was for an address
 * check if house number was specified and found in result
 *
 * @param {object|undefined} text
 * @param {object} hit
 * @returns {number}
 */
function checkQueryType(text, hit) {
  if (check.assigned(text) && check.assigned(text.number) &&
      (check.undefined(hit.address) ||
      (check.assigned(hit.address) && check.undefined(hit.address.number)))) {
    return 0;
  }
  return 1;
}
	arrow : function(options) {
		if(!(check.object(options) || check.undefined(options))) {
			throw new TypeError("Incorrect argument type, requires Object or undefined.");
		}
		
		//validated options: type, id
		var validated_options;
		try {
			validated_options = argument_check(options);
		} catch(error) {
			throw error;
		}
		
		//check arguments unique to arrow construction
		if(!(check.number(validated_options.angle) || check.undefined(validated_options.angle))) {
			throw new TypeError("Variable: " + "angle" + " was " + type_of(validated_options.angle) + ", should be: " + "number");
		}
		if(!(check.number(validated_options.length) || check.undefined(validated_options.length))) {
			throw new TypeError("Variable: " + "length" + " was " + type_of(validated_options.length) + ", should be: " + "number");
		} else {
			if (check.number(validated_options.length) && validated_options.length <= 0) {
				throw new TypeError("Variable: " + "length" + " was " + validated_options.length + ", should be greater than zero.");
			}
		}
		if(!(check.string(validated_options.direction) || check.undefined(validated_options.direction))) {
			throw new TypeError("Variable: " + "direction" + " was " + type_of(validated_options.direction) + ", should be: " + "string");
		} else {
			if (check.string(validated_options.direction) && !(validated_options.direction in this._default_directions)) {
				throw new TypeError("Variable: " + "direction" + " was " + validated_options.direction + ", should be: " 
				+ "one of these: " + "'left', 'right', 'up' or 'down'");
			}
		}
		
		//it's an error to have both angle and direction passed
		if (check.number(validated_options.angle) && check.string(validated_options.direction)) {
			throw new Error('Cannot have options "angle" and "direction" defined at the same time.');
		}
		
		//translate normal angle notation to special angle notation used by canvas
		if (check.number(validated_options.angle)) {
			validated_options.angle = -validated_options.angle;
		}
		
		//if direction is set, then translate that into an angle and unset direction before setting options on _arrow_options
		if(check.string(validated_options.direction)) {
			validated_options.angle = this._default_directions[validated_options.direction];
			delete validated_options.direction;
		}
		
		//copy settings for arrow creation on get()
		for (option in validated_options) {
			this._arrow_options[option] = validated_options[option];
		}
	},
  iterate(res.results, req.clean, function(r, clean, i) {
    if(check.undefined(r.data) || check.undefined(r.meta) || check.undefined(clean)) {
      return;
    }
    // compute standard deviation and mean from all scores
    var scores = r.meta.scores;
    var stdev = computeStandardDeviation(scores);
    var mean = stats.mean(scores);

    // loop through data items and determine confidence scores
    r.data = r.data.map(computeConfidenceScore.bind(null, clean, mean, stdev));
  });
	_decorate : function(container) {
		var append_to = require('lib/util/append_to');
		container._.append_to = append_to(container);
		
		//TODO, setup place commands on container for easy positioning
		//	the DOMElement can't calculate its own bounds, so we have no reliable way to getting the bounds of the HTML object
		//	the Element interface DOES have some ways of calculating the dimensions of the elements, but they're marked as experimental at this time:
		//	https://developer.mozilla.org/en-US/docs/Web/API/Element
		/* var place = require('lib/util/place');
		container._.place = place(container); */
		
		//add add_to_stage functionality
		var add_to_stage = require('lib/util/add_to_stage');
		container._.add_to_stage = add_to_stage(container);
		
		//add optional name
		//if name is unset, attempt to attach 'label' property as name on container
		//otherwise 'name' is ''
		if (this._name.length === 0) {
			if (!(check.undefined(this._html_options.label))) container._.name = String(this._html_options.label);
		} else {
			container._.name = this._name;
		}
		
		return container;
	},
function html_factory(options) {
	//get an instance if we just call it
	if(!(this instanceof html_factory)) return new html_factory(options);
	
	//is createjs already registered?
	this._createjs = primitives.get('createjs');
	if (check.undefined(this._createjs) || check.not.object(this._createjs)) {
		throw new ReferenceError("'createjs' undefined or not object in primitives object.");
	}
	
	//create the text factory instance for getting Text for our Shapes
	var text_factory = require('lib/factory/text_factory');
	this._text_factory = text_factory();
	//default text should be empty string
	this._text_factory.text({text:''});
	
	//options for the raw HTML element
	this._html_options = {};
	
	//options for the container
	this._container_options = {x:0, y:0};
	
	//default HTML types
	this._default_types = ['radio', 'text', 'button'];
	
	this._name = '';
};
function shape_factory(options) {
	//get an instance if we just call it
	if(!(this instanceof shape_factory)) return new shape_factory(options);
	
	//is createjs already registered?
	this._createjs = primitives.get('createjs');
	if (check.undefined(this._createjs) || check.not.object(this._createjs)) {
		throw new ReferenceError("'createjs' undefined or not object in primitives object.");
	}
	
	//configure defaults for color of Shape
	this._color_options = {fill:undefined, stroke:null}
	
	//create the text factory instance for getting Text for our Shapes
	var text_factory = require('lib/factory/text_factory');
	this._text_factory = text_factory();
	
	//default sizing and positioning options for the Shape
	//	default h and w for rect shapes come from LaFore defaults
	//TODO: radius = 1, is a placeholder until we can measure the radius of circles used in LaFore
	this._shape_options = {radius: 1, height:18, width:36, x:0, y:0},
	
	//default options for the container (that will hold both the Shape and Text)
	//users can set persistent options for the container by passing options to get()
	this._container_options = {x:0, y:0};
	
	//configure default shapes the factory knows about
	//'rectangle' and 'box' are aliases for 'rect'
	this._default_shapes = ["circle", "rect", "rectangle", "box"];
	
	//configured in shape() then used in _get_shape() to return the Shape object
	this._shape_type = undefined;
};
	_get_text : function() {
		var value;
		//if default, then create a random number to assign to the Text
		if (check.undefined(this._text_options.text)) {
			var random_integer = require('lib/util/random_integer');
			value = String(random_integer());
		}
		
		//if user defined, then assign it (always a String)
		if (check.string(this._text_options.text)) {
			value = this._text_options.text;
		}
		
		//if a Text object, then just return that
		if (check.instance(this._text_options.text, this._createjs.Text)) {
			return this._text_options.text;
		}
		
		//...otherwise create new Text object, initialize to value
		var text = new this._createjs.Text(value);
		
		//...and copy options that was set in text() to the Text object
		for (text_option in this._text_options) {
			//skipping text, as we don't want to overwrite what we initialized it to
			if (text_option === 'text') continue;
			text[text_option] = this._text_options[text_option];
		}
		
		return text;
	},
	color : function(options) {
		if(!(check.object(options) || check.undefined(options))) {
			throw new TypeError("Incorrect argument type, requires Object.");
		}
		
		//validated options: color (alias for fill), fill, stroke
		var validated_options;
		try {
			validated_options = argument_check(options);
		} catch(error) {
			throw error;
		}
		
		if ('fill' in validated_options) {
			this._color_options['fill'] = validated_options['fill'];
		}
		
		if ('color' in validated_options) {
			this._color_options['fill'] = validated_options['color'];
		}
		
		if ('stroke' in validated_options) {
			this._color_options['stroke'] = validated_options['stroke'];
		}
	},
		array._.iterator = function(name) {
			if(check.undefined(name) || check.emptyString(name)) {
				throw new TypeError("Required argument 'name' missing, should be non-empty String");
			}
			
			return instance._iterator(array, name);
		}
	setTimeout(function() {
		t.notOk(move.is_moving(), "should be false after the tween is complete");
		t.ok(check.undefined(move.whats_moving()), "the reference to what was moving should not be undefined, post move()");
		
		TEARDOWN();
		t.end();
	}, 1100);
Exemple #12
0
function computeScores(req, res, next) {
  // do nothing if no result data set
  if (check.undefined(req.clean) || check.undefined(res) ||
      check.undefined(res.data) || check.undefined(res.meta)) {
    return next();
  }

  // compute standard deviation and mean from all scores
  var scores = res.meta.scores;
  var stdev = computeStandardDeviation(scores);
  var mean = stats.mean(scores);

  // loop through data items and determine confidence scores
  res.data = res.data.map(computeConfidenceScore.bind(null, req, mean, stdev));

  next();
}
test('[validate._.options] correct', function (t) {
	t.plan(2);
	
	//TEST
	
	var result = validate._.options({});
	t.ok(result.valid, "arguments are correct");
	t.ok(check.undefined(result.msg));
});
	var intervalID = setInterval(function() {
		var result = l.next();
		
		if (result.done) {
			console.log("DONE");
			
			t.ok(check.undefined(l._step_state));
			t.equal(l._state, l._READY, "in READY state after completely traversing a lesson");
			t.notOk(ui.input.self._.htmlElement.disabled, "text input is no longer disabled");
			t.ok(result.done);
			t.ok(check.undefined(l._search));
			t.ok(check.undefined(l._input_tmp));
			
			//CLEANUP
			TEARDOWN();
			t.end();
			clearInterval(intervalID);
		}
	}, 1500);
	function not_found_case() {
		var result = l.next();
		
		if (result.done) {
			t.ok(check.undefined(l._step_state));
			t.equal(l._state, l._READY, "in READY state after completely traversing a lesson");
			t.notOk(ui.input.self._.htmlElement.disabled, "text input is no longer disabled");
			t.ok(result.done);
			t.ok(check.undefined(l._search));
			
			//CLEANUP
			clearInterval(not_found_case_interval_ID);
			
			//setup new input for next part of the lesson
			ui.input.set("0");
			
			found_case_interval_ID = setInterval(found_case, 1500);
		}
	};
test('[validate._.UI] correct', function (t) {
	t.plan(2);
	
	var ui = SETUP();
	
	//TEST
	var result = validate._.UI(ui);
	t.ok(result.valid, "arguments are correct");
	t.ok(check.undefined(result.msg));
	
	TEARDOWN();
});
function text_factory(options) {
	//get an instance if we just call it
	if(!(this instanceof text_factory)) return new text_factory(options);
	
	//is createjs already registered?
	this._createjs = primitives.get('createjs');
	if (check.undefined(this._createjs) || check.not.object(this._createjs)) {
		throw new ReferenceError("'createjs' undefined or not object in primitives object.");
	}
	
	//configure defaults for Text
	this._text_options = {color:"black", font:"18px Arial", text:undefined};
};
	_get_color : function() {
		var fill, stroke;
		
		//if the fill is undefined (the default) then get a random color with good constrast with the text color
		if (check.undefined(this._color_options.fill)) {
			fill = (require('lib/util/random_color'))(this._text_factory.color());
		} else {
			fill = this._color_options.fill;
		}
		stroke = this._color_options.stroke;
		
		return {fill:fill, stroke:stroke};
	},
	_get_shape : function(shape_color) {
		if (check.undefined(this._shape_type)) {
			throw new TypeError("Unknown type of Shape to create, run shape() first before calling get()");
		}
		
		var shape = new createjs.Shape();
		
		//assign color (stroke and fill) BEFORE applying shape for some reason (createjs idiom)
		shape.graphics.beginFill(shape_color.fill);
		shape.graphics.beginStroke(shape_color.stroke);
		
		//assign (x, y) coordinates to the Shape in _get_shape() rather than on the Graphic (createjs idiom)
		shape.x = this._shape_options.x;
		shape.y = this._shape_options.y;
		
		//circle specific Shape handling
		if (this._shape_type === 'circle') {
			shape.graphics.drawCircle(0, 0, this._shape_options.radius);
			
			//set bounds on the Shape for later
			//Note: we set the (x, y) coordinates to the top left hand corner of the "bounding box"
			//	as such the (x, y) coordinates are always negative, as we are always above and to the left of center
			//	where the (x, y) coordinates of how the Shape is placed is figured by createjs
			shape.setBounds(-this._shape_options.radius, 
				-this._shape_options.radius, 
				2*this._shape_options.radius,
				2*this._shape_options.radius
				);
		}
		
		//rectangle specific Shape handling
		if (this._shape_type === 'rect') {
			shape.graphics.drawRect(0, 0, this._shape_options.width, this._shape_options.height);
			
			//set bounds on the Shape for later
			shape.setBounds(0, 0,
				this._shape_options.width,
				this._shape_options.height
				);
		}
		
		//assign any other user options to the Shape
		for (shape_option in this._shape_options) {
			//...except for those we've already covered
			if (shape_option in {radius:null, height:null, width:null, x:null, y:null}) continue;
			shape[shape_option] = this._shape_options[shape_option];
		}
		
		//return the Shape
		return shape;
	},
	get : function (options) {
		if(!(check.object(options) || check.undefined(options))) {
			throw new TypeError("Incorrect argument type, requires Object.");
		}
		
		//template pattern, build the shape object
		//where options can be positioning and transform options as applied to the container
		var shape_color = this._get_color();				//get the color string used in shape construction
		var shape_text = this._get_text();					//the text value to display with the shape
		var shape = this._get_shape(shape_color);			//get the shape itself
		var container = this._bundle(shape, shape_text);	//bundle the shape and text into a createjs container
		container = this._decorate(container, shape_color);	//add additional convenience methods specific to the Shape to the container
		return this._set_props(container, options);			//add properties user propertier to container
	},
	function found_case() {
		var result = l.next();
		
		if (result.done) {
			console.log("DONE");
			
			t.ok(check.undefined(l._delete));
			t.ok(check.undefined(l._delete_index));
			t.ok(check.undefined(l._step_state));
			t.ok(check.undefined(l._input_tmp));
			t.equal(l._state, l._READY);
			t.notOk(ui.input.self._.htmlElement.disabled, "text input is no longer disabled");
			
			var output = ui.output.get();
			t.ok(output.match(/smaller/));
			t.ok(output.match(/removing the selected value/));
			
			//CLEANUP
			TEARDOWN();
			t.end();
			clearInterval(found_case_interval_ID);
		}
	};
Exemple #22
0
/*
 * Check for clearly mismatching properties in a result
 * zip code and state (admin1) are currently checked if present
 *
 * @param {object|undefined} text
 * @param {object} hit
 * @returns {bool}
 */
function checkForDealBreakers(req, hit) {
  if (check.undefined(req.clean.parsed_text)) {
    return false;
  }

  if (check.assigned(req.clean.parsed_text.state) && req.clean.parsed_text.state !== hit.admin1_abbr) {
    logger.debug('[confidence][deal-breaker]: state !== admin1_abbr');
    return true;
  }

  if (check.assigned(req.clean.parsed_text.postalcode) && check.assigned(hit.address) &&
      req.clean.parsed_text.postalcode !== hit.address.zip) {
    logger.debug('[confidence][deal-breaker]: postalcode !== zip (' + req.clean.parsed_text.postalcode + ' !== ' + hit.address.zip + ')');
    return true;
  }
}
test('[shape_factory.get][place] correct', function (t) {
	t.plan(1);
	
	SETUP();
	
	var sf = shape_factory();
	sf.shape({type:"box"});
	sf.color({color:null, stroke:"black"});
	sf.text({text:0, font:"16px Arial", x:-22, y:1, textAlign:"right"});
	
	var result_1 = sf.get();
	
	t.ok(!check.undefined(result_1._.place), 'place namespace is correctly applied to the container');
	
	TEARDOWN();
});
test('[pre_lesson_check] correct, bad user input', function (t) {
	//SETUP
	var ui = SETUP();
	ui.input.set(undefined);
	
	//TEST
	var result = pre_lesson_check(ui);
	t.ok(check.undefined(result), "undefined on bad user input");
	
	var output = ui.output.get();
	t.ok(output.match(/not a number/), 'shows what was wrong');
	t.ok(output.match(/Please enter a number/), 'tells you what to enter');
	t.ok(output.match(/the 'Next' button/), '...and what to do');
	
	TEARDOWN();
	t.end();
});
test('[validate] correct', function (t) {
	//SETUP
	var ui = SETUP();
	
	var array_UI = require('lib/UI/array_UI');
	var array = array_UI({length:1});
	var arrow = array._.arrow;
	
	var result = validate({UI:ui, array:array, arrow:arrow});
	
	//TEST
	t.ok(result.valid, "arguments are correct");
	t.ok(check.undefined(result.msg));
	
	TEARDOWN();
	t.end();
});
/*
 * Check for clearly mismatching properties in a result
 * zip code and state (region) are currently checked if present
 *
 * @param {object|undefined} text
 * @param {object} hit
 * @returns {bool}
 */
function checkForDealBreakers(clean, hit) {
  if (check.undefined(clean.parsed_text)) {
    return false;
  }

  if (check.assigned(clean.parsed_text.state) && clean.parsed_text.state !== hit.parent.region_a[0]) {
    logger.debug('[confidence][deal-breaker]: state !== region_a');
    return true;
  }


  if (check.assigned(clean.parsed_text.postalcode) && check.assigned(hit.address_parts) &&
      clean.parsed_text.postalcode !== hit.address_parts.zip) {
    logger.debug('[confidence][deal-breaker]: postalcode !== zip (' + clean.parsed_text.postalcode +
      ' !== ' + hit.address_parts.zip + ')');
    return true;
  }
}
	get : function(options) {
		if(!(check.object(options) || check.undefined(options))) {
			throw new TypeError("Incorrect argument type, requires Object.");
		}
		
		//validated options: x, y
		var validated_options;
		try {
			validated_options = argument_check(options);
		} catch(error) {
			throw error;
		}
		
		var tip = this._get_tip();
		var line = this._get_line();
		var container = this._bundle(tip, line);
		container = this._decorate(container);
		return this._set_props(container, validated_options);
	},
Exemple #28
0
 getCore: (corePath, res) => {
   if (check.undefined(global.TesseractCore)) {
     res.progress({ status: 'loading tesseract core', progress: 0 });
     global.importScripts(corePath);
     /*
      * Depending on whether the browser supports WebAssembly,
      * the version of the TesseractCore will be different.
      */
     if (check.not.undefined(global.TesseractCoreWASM) && typeof WebAssembly === 'object') {
       global.TesseractCore = global.TesseractCoreWASM;
     } else if (check.not.undefined(global.TesseractCoreASM)){
       global.TesseractCore = global.TesseractCoreASM;
     } else {
       throw Error('Failed to load TesseractCore');
     }
     res.progress({ status: 'loading tesseract core', progress: 1 });
   }
   return global.TesseractCore;
 },
	html : function(options) {
		if(!check.object(options)) {
			throw new TypeError("Incorrect argument type, requires Object.");
		}
		
		//validated options: type, id
		var validated_options;
		try {
			validated_options = argument_check(options);
		} catch(error) {
			throw error;
		}
		
		//if type isn't passed...
		if (!("type" in validated_options)) {
			throw new TypeError("argument 'type' is required, should be non-empty String");
		}
		
		//type was passed, but unsupported type
		if (this._default_types.indexOf(validated_options.type) === -1) {
			throw new TypeError("argument 'type' was unsupported: " + String(validated_options.type));
		}
		
		//generate an id if we need one
		if(!("id" in validated_options)) {
			var unique_id = require('lib/util/unique_id');
			var id_str = unique_id(validated_options.type);
			validated_options.id = id_str;
		} else {
			//...else if the user passed us one, just String it
			validated_options.id = String(validated_options.id);
		}
		
		//if the type was 'button', then we can take an optional 'label' argument, but not allowed on any other HTML type
		if (validated_options.type !== 'button' && !(check.undefined(validated_options.label))) {
			throw new TypeError("argument 'label' is only used with 'button' HTML type");
		}
			
		//set HTML options for producing HTML in _get_html() later		
		for (option in validated_options) {
			this._html_options[option] = validated_options[option];
		}
	},
	shape : function(options) {
		if(!(check.object(options) || check.undefined(options))) {
			throw new TypeError("Incorrect argument type, requires Object.");
		}
		
		//validated options:
		//	height, width (for rect shapes)
		//	radius (for circle shapes)
		var validated_options;
		try {
			validated_options = argument_check(options);
		} catch(error) {
			throw error;
		}
		
		//if type isn't passed...
		if (!("type" in validated_options)) {
			throw new TypeError("argument 'type' is required, should be non-empty String");
		}
		
		if(this._default_shapes.indexOf(validated_options.type) === -1) {
			throw new TypeError("argument 'type' was unknown shape type: " + String(validated_options.type));
		}
		
		if (validated_options.type === "circle") {
			//if there are configuration options, then use them, otherwise the defaults
			if(validated_options.radius) this._shape_options.radius = validated_options.radius;
			
			this._shape_type = "circle"
			return;
		}
		
		if (validated_options.type === "rectangle" || validated_options.type === "rect" || validated_options.type === "box") {
			//if there are confgiuration options, then use them, otherwise the defaults
			if(validated_options.width) this._shape_options.width = validated_options.width;
			if(validated_options.height) this._shape_options.height = validated_options.height;
			
			this._shape_type = "rect"
			return;
		}
	},