PDA

View Full Version : Code reduction/simplification using Ext.isEmpty() / Ext.value()



mystix
22 Jun 2009, 3:41 AM
Found a couple of spots in 2.x/3.x where code can be reduced and/or simplified using Ext.isEmpty() / Ext.value():
(note 1: Ext.value() is not defined in ext-core)
(note 2: all changes in red below apply to both the 2.x / 3.x branch except for those involving Ext.data.ArrayReader/JsonReader)


Ext.DomQuery


selectValue : function(path, root, defaultValue){
path = path.replace(trimRe, "");
if(!valueCache[path]){
valueCache[path] = Ext.DomQuery.compile(path, "select");
}
var n = valueCache[path](root),
v;
n = n[0] ? n[0] : n;
v = (n && n.firstChild ? n.firstChild.nodeValue : null);
return Ext.isEmpty(v) ? defaultValue : v; // note: can't use Ext.value() because it's not defined in ext-core
},



Ext.util.MixedCollection


add: function(key, o){
if(arguments.length == 1){
o = arguments[0];
key = this.getKey(o);
}
if(!Ext.isEmpty(key, true)){
var old = this.map[key];
if(typeof old != 'undefined'){
return this.replace(key, o);
}
this.map[key] = o;
}
this.length++;
this.items.push(o);
this.keys.push(key);
this.fireEvent('add', this.length-1, o, key);
return o;
},

replace : function(key, o){
if(arguments.length == 1){
o = arguments[0];
key = this.getKey(o);
}
var old = this.map[key];
if(Ext.isEmpty(key, true) || old === undefined){
return this.add(key, o);
}
var index = this.indexOfKey(key);
this.items[index] = o;
this.map[key] = o;
this.fireEvent("replace", key, old, o);
return o;
},

insert : function(index, key, o){
if(arguments.length == 2){
o = arguments[1];
key = this.getKey(o);
}
if(this.containsKey(key)){
this.suspendEvents();
this.removeKey(key);
this.resumeEvents();
}
if(index >= this.length){
return this.add(key, o);
}
this.length++;
this.items.splice(index, 0, o);
if(!Ext.isEmpty(key, true)){
this.map[key] = o;
}
this.keys.splice(index, 0, key);
this.fireEvent("add", index, o, key);
return o;
},



Ext.util.JSON


this.encode = isNative ? JSON.stringify : function(o){
if(Ext.isArray(o)){
return encodeArray(o);
} else if(Ext.isEmpty(o, true)){ // Ext.isEmpty() also checks for empty array,
return "null"; // so move check down for backwards compatibility
}
}else if(Object.prototype.toString.apply(o) === '[object Date]'){
return Ext.util.JSON.encodeDate(o);
}else if(typeof o == "string"){
return encodeString(o);
}else if(typeof o == "number"){
return isFinite(o) ? String(o) : "null";
}else if(typeof o == "boolean"){
return String(o);
}else {
var a = ["{"], b, i, v;
for (i in o) {
if(!useHasOwn || o.hasOwnProperty(i)) {
v = o[i];
switch (typeof v) {
case "undefined":
case "function":
case "unknown":
break;
default:
if(b){
a.push(',');
}
a.push(this.encode(i), ":",
v === null ? "null" : this.encode(v));
b = true;
}
}
}
a.push("}");
return a.join("");
}
};



Ext.util.Format


nl2br : function(v){
return Ext.isEmpty(v) ? '' : v.replace(/\n/g, '<br/>');
}



Ext.data.Field


// prebuilt conversion function for this field, instead of
// switching every time we're reading a value
if(!this.convert){
var cv, dateFormat = this.dateFormat;
switch(this.type){
case "":
case "auto":
case undefined:
cv = function(v){ return v; };
break;
case "string":
cv = function(v){ return String(Ext.value(v, '')); }; // Ext.value() calls Ext.isEmpty() implicitly
break;
case "int":
cv = function(v){ return Ext.num(parseInt(v, 10), ''); }
break;
case "float":
cv = function(v){ return Ext.num(v, ''); }
break;
case "bool":
case "boolean":
cv = function(v){ return v === true || v === "true" || v == 1; };
break;
case "date":
cv = function(v){
if(!v){
return '';
}
if(Ext.isDate(v)){
return v;
}
if(dateFormat){
if(dateFormat == "timestamp"){
return new Date(v*1000);
}
if(dateFormat == "time"){
return new Date(parseInt(v, 10));
}
return Date.parseDate(v, dateFormat);
}
var parsed = Date.parse(v);
return parsed ? new Date(parsed) : null;
};
break;

}
this.convert = cv;
}



Ext.data.JsonReader


buildExtractors : function() {
var s = this.meta, Record = this.recordType,
f = Record.prototype.fields, fi = f.items, fl = f.length;

if(s.totalProperty) {
this.getTotal = this.getJsonAccessor(s.totalProperty);
}
if(s.successProperty) {
this.getSuccess = this.getJsonAccessor(s.successProperty);
}
this.getRoot = s.root ? this.getJsonAccessor(s.root) : function(p){return p;};
if (s.id || s.idProperty) {
var g = this.getJsonAccessor(s.id || s.idProperty);
this.getId = function(rec) {
var r = g(rec);
return (r === undefined || r === "") ? null : r;
};
} else {
this.getId = function(){return null;};
}
var ef = [];
for(var i = 0; i < fl; i++){
f = fi[i];
var map = Ext.value(f.mapping, f.name, true);
ef.push(this.getJsonAccessor(map));
}
return ef;
},



Ext.data.ArrayReader


readRecords : function(o){
this.arrayData = o;
var s = this.meta,
sid = s ? Ext.num(s.idIndex, s.id) : null,
recordType = this.recordType,
fields = recordType.prototype.fields,
records = [],
v;

if(!this.getRoot) {
this.getRoot = s.root ? this.getJsonAccessor(s.root) : function(p) {return p;};
if(s.totalProperty) {
this.getTotal = this.getJsonAccessor(s.totalProperty);
}
}

var root = this.getRoot(o);

for(var i = 0; i < root.length; i++) {
var n = root[i],
values = {},
id = ((sid || sid === 0) && n[sid] !== undefined && n[sid] !== "" ? n[sid] : null);

for(var j = 0, jlen = fields.length; j < jlen; j++) {
var f = fields.items[j],
k = Ext.value(f.mapping, j, true);
v = n[k] !== undefined ? n[k] : f.defaultValue;
v = f.convert(v, n);
values[f.name] = v;
}
var record = new recordType(values, id);
record.json = n;
records[records.length] = record;
}

var totalRecords = records.length;

if(s.totalProperty) {
v = parseInt(this.getTotal(o), 10);
if(!isNaN(v)) {
totalRecords = v;
}
}

return {
records : records,
totalRecords : totalRecords
};
}



Ext.state.CookieProvider


set : function(name, value){
if(Ext.isEmpty(value, true)){
this.clear(name);
return;
}
this.setCookie(name, value);
Ext.state.CookieProvider.superclass.set.call(this, name, value);
},



Ext.form.ComboBox


selectByValue : function(v, scrollIntoView){
if(!Ext.isEmpty(v, true)){
var r = this.findRecord(this.valueField || this.displayField, v);
if(r){
this.select(this.store.indexOf(r), scrollIntoView);
return true;
}
}
return false;
},