Simplify code for reading of named arguments, removing the need for filtering arguments by using 'in' checks instead for the presence of properties / setters.

This commit is contained in:
Jürg Lehni 2013-03-01 14:06:04 -08:00
parent 06e33ba412
commit 7405858f32
3 changed files with 41 additions and 48 deletions

View file

@ -60,27 +60,17 @@ this.Base = Base.inject(/** @lends Base# */{
}, },
/** /**
* Sets all the properties of the passed object literal to their values on * #_set() is part of the mechanism for constructors which take one object
* the item it is called on, if the item has property of the given name (or * literal describing all the properties to be set on the created instance.
* a setter defined for it), annd returns the item itself. * @return {Boolean} {@true if the object is a plain object}
*/ */
set: function(props) { _set: function(props) {
if (props) { if (Base.isPlainObject(props)) {
for (var key in props) for (var key in props)
if (props.hasOwnProperty(key) && key in this) if (props.hasOwnProperty(key) && key in this)
this[key] = props[key]; this[key] = props[key];
return true;
} }
return this;
},
/**
* #_set() is part of the mechanism for constructors which take one object
* literal describing all the properties to be set on the created instance.
* It behaves the same as #set(), but only if the provided object is a plain
* object. It returns undefined otherwise.
*/
_set: function(props) {
return Base.isPlainObject(props) && this.set(props);
}, },
statics: /** @lends Base */{ statics: /** @lends Base */{
@ -238,21 +228,12 @@ this.Base = Base.inject(/** @lends Base# */{
* or a normal array. * or a normal array.
* @param {Number} start the index at which to start reading in the list * @param {Number} start the index at which to start reading in the list
* @param {String} name the property name to read from. * @param {String} name the property name to read from.
* @param {Boolean} [filter=true] controls wether a clone of the passed
* object should be kept in list._filtered, of which the consumed
* properties are removed. This can be passed on e.g. to Item#set().
*/ */
readNamed: function(list, name, filter) { readNamed: function(list, name) {
var value = this.getNamed(list, name), var value = this.getNamed(list, name);
// value is undefined if there is no arguments object, and null // value is undefined if there is no arguments object, and null
// if there is one, but no value is defined. // if there is one, but no value is defined.
hasObject = value !== undefined; return this.read(value !== undefined ? [value] : list);
if (hasObject && filter !== false) {
if (!list._filtered)
list._filtered = Base.merge(list[0]);
delete list._filtered[name];
}
return this.read(hasObject ? [value] : list);
}, },
/** /**
@ -280,7 +261,7 @@ this.Base = Base.inject(/** @lends Base# */{
* named arguments. * named arguments.
*/ */
hasNamed: function(list, name) { hasNamed: function(list, name) {
return !name && list._hasObject || !!this.getNamed(list, name); return !!this.getNamed(list, name);
}, },
/** /**

View file

@ -207,6 +207,17 @@ var Item = this.Item = Base.extend(Callback, {
} }
}, },
/**
* Sets those properties of the passed object literal on this item to
* the values defined in the object literal, if the item has property of the
* given name (or a setter defined for it).
* @return {Item} the item itself.
*/
set: function(props) {
this._set(props);
return this;
},
/** /**
* The unique id of the item. * The unique id of the item.
* *

View file

@ -13,15 +13,16 @@
Path.inject({ statics: new function() { Path.inject({ statics: new function() {
function readRectangle(list) { function readRectangle(list) {
var rect; var props = Base.getNamed(list),
if (Base.hasNamed(list, 'from')) { rect;
if (props) {
if ('from' in props) {
rect = new Rectangle(Point.readNamed(list, 'from'), rect = new Rectangle(Point.readNamed(list, 'from'),
Point.readNamed(list, 'to')); Point.readNamed(list, 'to'));
} else if (Base.hasNamed(list)) { } else {
rect = Base.each(Base.getNamed(list), function(value, key) { rect = new Rectangle();
if (key in this) rect._set(Base.getNamed(list));
this[key] = Base.readNamed(list, key); }
}, new Rectangle());
} else { } else {
rect = Rectangle.read(list); rect = Rectangle.read(list);
} }
@ -34,7 +35,7 @@ Path.inject({ statics: new function() {
top = rect.getTop(), top = rect.getTop(),
right = rect.getRight(), right = rect.getRight(),
bottom = rect.getBottom(), bottom = rect.getBottom(),
path = new Path(arguments._filtered); path = new Path(Base.getNamed(arguments));
path._add([ path._add([
new Segment(Point.create(left, bottom)), new Segment(Point.create(left, bottom)),
new Segment(Point.create(left, top)), new Segment(Point.create(left, top)),
@ -57,7 +58,7 @@ Path.inject({ statics: new function() {
function createEllipse(/* rect */) { function createEllipse(/* rect */) {
var rect = readRectangle(arguments), var rect = readRectangle(arguments),
path = new Path(arguments._filtered), path = new Path(Base.getNamed(arguments)),
point = rect.getPoint(true), point = rect.getPoint(true),
size = rect.getSize(true), size = rect.getSize(true),
segments = new Array(4); segments = new Array(4);
@ -94,7 +95,7 @@ Path.inject({ statics: new function() {
return new Path( return new Path(
Point.readNamed(arguments, 'from'), Point.readNamed(arguments, 'from'),
Point.readNamed(arguments, 'to') Point.readNamed(arguments, 'to')
).set(arguments._filtered); ).set(Base.getNamed(arguments));
}, },
/** /**
@ -169,7 +170,7 @@ Path.inject({ statics: new function() {
tr = rect.getTopRight(true), tr = rect.getTopRight(true),
br = rect.getBottomRight(true), br = rect.getBottomRight(true),
h = radius.multiply(kappa * 2), // handle vector h = radius.multiply(kappa * 2), // handle vector
path = new Path(arguments._filtered); path = new Path(Base.getNamed(arguments));
path._add([ path._add([
new Segment(bl.add(radius.width, 0), null, [-h.width, 0]), new Segment(bl.add(radius.width, 0), null, [-h.width, 0]),
new Segment(bl.subtract(0, radius.height), [0, h.height], null), new Segment(bl.subtract(0, radius.height), [0, h.height], null),
@ -226,7 +227,7 @@ Path.inject({ statics: new function() {
radius = Base.readNamed(arguments, 'radius'); radius = Base.readNamed(arguments, 'radius');
return createEllipse(new Rectangle(center.subtract(radius), return createEllipse(new Rectangle(center.subtract(radius),
Size.create(radius * 2, radius * 2))) Size.create(radius * 2, radius * 2)))
.set(arguments._filtered); .set(Base.getNamed(arguments));
}, },
/** /**
@ -248,7 +249,7 @@ Path.inject({ statics: new function() {
var from = Point.readNamed(arguments, 'from'), var from = Point.readNamed(arguments, 'from'),
through = Point.readNamed(arguments, 'through'), through = Point.readNamed(arguments, 'through'),
to = Point.readNamed(arguments, 'to'), to = Point.readNamed(arguments, 'to'),
path = new Path(arguments._filtered); path = new Path(Base.getNamed(arguments));
path.moveTo(from); path.moveTo(from);
path.arcTo(through, to); path.arcTo(through, to);
return path; return path;
@ -282,7 +283,7 @@ Path.inject({ statics: new function() {
var center = Point.readNamed(arguments, 'center'), var center = Point.readNamed(arguments, 'center'),
numSides = Base.readNamed(arguments, 'numSides'), numSides = Base.readNamed(arguments, 'numSides'),
radius = Base.readNamed(arguments, 'radius'), radius = Base.readNamed(arguments, 'radius'),
path = new Path(arguments._filtered), path = new Path(Base.getNamed(arguments)),
step = 360 / numSides, step = 360 / numSides,
three = !(numSides % 3), three = !(numSides % 3),
vector = new Point(0, three ? -radius : radius), vector = new Point(0, three ? -radius : radius),
@ -323,7 +324,7 @@ Path.inject({ statics: new function() {
numPoints = Base.readNamed(arguments, 'numPoints') * 2, numPoints = Base.readNamed(arguments, 'numPoints') * 2,
radius1 = Base.readNamed(arguments, 'radius1'), radius1 = Base.readNamed(arguments, 'radius1'),
radius2 = Base.readNamed(arguments, 'radius2'), radius2 = Base.readNamed(arguments, 'radius2'),
path = new Path(arguments._filtered), path = new Path(Base.getNamed(arguments)),
step = 360 / numPoints, step = 360 / numPoints,
vector = new Point(0, -1), vector = new Point(0, -1),
segments = new Array(numPoints); segments = new Array(numPoints);