diff --git a/CHANGELOG b/CHANGELOG index 74f4cc4..0ca90ad 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* For consistency: use Object.isUndefined where possible. [Tobie Langel] + * Make String#isJSON return false for empty or blank strings. Make Ajax.Response#responseJSON null when Ajax.Response#responseText is empty or blank. [Andrew Dupont, Thomas Fuchs, Tobie Langel] * Make Ajax.Response#_getResponseJSON use Ajax.Response#responseText. [Tobie Langel] diff --git a/src/ajax.js b/src/ajax.js index c18c529..d7790b0 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -242,7 +242,7 @@ Ajax.Response = Class.create({ if(readyState == 4) { var xml = transport.responseXML; - this.responseXML = xml === undefined ? null : xml; + this.responseXML = Object.isUndefined(xml) ? null : xml; this.responseJSON = this._getResponseJSON(); } }, diff --git a/src/array.js b/src/array.js index 41fc895..245ad13 100644 --- a/src/array.js +++ b/src/array.js @@ -100,7 +100,7 @@ Object.extend(Array.prototype, { var results = []; this.each(function(object) { var value = Object.toJSON(object); - if (value !== undefined) results.push(value); + if (!Object.isUndefined(value)) results.push(value); }); return '[' + results.join(', ') + ']'; } diff --git a/src/base.js b/src/base.js index cda0435..6629914 100644 --- a/src/base.js +++ b/src/base.js @@ -69,7 +69,7 @@ Object.extend = function(destination, source) { Object.extend(Object, { inspect: function(object) { try { - if (object === undefined) return 'undefined'; + if (Object.isUndefined(object)) return 'undefined'; if (object === null) return 'null'; return object.inspect ? object.inspect() : object.toString(); } catch (e) { @@ -94,7 +94,7 @@ Object.extend(Object, { var results = []; for (var property in object) { var value = Object.toJSON(object[property]); - if (value !== undefined) + if (!Object.isUndefined(value)) results.push(property.toJSON() + ': ' + value); } @@ -163,7 +163,7 @@ Object.extend(Function.prototype, { }, bind: function() { - if (arguments.length < 2 && arguments[0] === undefined) return this; + if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this; var __method = this, args = $A(arguments), object = args.shift(); return function() { return __method.apply(object, args.concat($A(arguments))); diff --git a/src/dom.js b/src/dom.js index b480015..49cc145 100644 --- a/src/dom.js +++ b/src/dom.js @@ -286,7 +286,7 @@ Element.Methods = { var attributes = { }, t = Element._attributeTranslations.write; if (typeof name == 'object') attributes = name; - else attributes[name] = value === undefined ? true : value; + else attributes[name] = Object.isUndefined(value) ? true : value; for (var attr in attributes) { var name = t.names[attr] || attr, value = attributes[attr]; @@ -412,7 +412,7 @@ Element.Methods = { if (property == 'opacity') element.setOpacity(styles[property]); else elementStyle[(property == 'float' || property == 'cssFloat') ? - (elementStyle.styleFloat === undefined ? 'cssFloat' : 'styleFloat') : + (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') : property] = styles[property]; return element; diff --git a/src/enumerable.js b/src/enumerable.js index 50d09d2..45ace8e 100644 --- a/src/enumerable.js +++ b/src/enumerable.js @@ -102,7 +102,7 @@ var Enumerable = { }, inGroupsOf: function(number, fillWith) { - fillWith = fillWith === undefined ? null : fillWith; + fillWith = Object.isUndefined(fillWith) ? null : fillWith; return this.eachSlice(number, function(slice) { while(slice.length < number) slice.push(fillWith); return slice; @@ -129,7 +129,7 @@ var Enumerable = { var result; this.each(function(value, index) { value = iterator(value, index); - if (result == undefined || value >= result) + if (result == null || value >= result) result = value; }); return result; @@ -140,7 +140,7 @@ var Enumerable = { var result; this.each(function(value, index) { value = iterator(value, index); - if (result == undefined || value < result) + if (result == null || value < result) result = value; }); return result; diff --git a/src/form.js b/src/form.js index 0d823e8..0fd2ef3 100644 --- a/src/form.js +++ b/src/form.js @@ -6,7 +6,7 @@ var Form = { serializeElements: function(elements, options) { if (typeof options != 'object') options = { hash: !!options }; - else if (options.hash === undefined) options.hash = true; + else if (Object.isUndefined(options.hash)) options.hash = true; var key, value, submitted = false, submit = options.submit; var data = elements.inject({ }, function(result, element) { @@ -204,17 +204,17 @@ Form.Element.Serializers = { }, inputSelector: function(element, value) { - if (value === undefined) return element.checked ? element.value : null; + if (Object.isUndefined(value)) return element.checked ? element.value : null; else element.checked = !!value; }, textarea: function(element, value) { - if (value === undefined) return element.value; + if (Object.isUndefined(value)) return element.value; else element.value = value; }, select: function(element, index) { - if (index === undefined) + if (Object.isUndefined(index)) return this[element.type == 'select-one' ? 'selectOne' : 'selectMany'](element); else { diff --git a/src/string.js b/src/string.js index 58ef999..30d0ffb 100644 --- a/src/string.js +++ b/src/string.js @@ -31,7 +31,7 @@ Object.extend(String.prototype, { sub: function(pattern, replacement, count) { replacement = this.gsub.prepareReplacement(replacement); - count = count === undefined ? 1 : count; + count = Object.isUndefined(count) ? 1 : count; return this.gsub(pattern, function(match) { if (--count < 0) return match[0]; @@ -46,7 +46,7 @@ Object.extend(String.prototype, { truncate: function(length, truncation) { length = length || 30; - truncation = truncation === undefined ? '...' : truncation; + truncation = Object.isUndefined(truncation) ? '...' : truncation; return this.length > length ? this.slice(0, length - truncation.length) + truncation : String(this); },