Subversion Repositories JSX

Compare Revisions

Last modification

Ignore whitespace Rev 434 → Rev 435

/trunk/test/object.js
1,5 → 1,6
function runTests ()
{
var assert = jsx.test.assert;
var assertFalse = jsx.test.assertFalse;
var assertTrue = jsx.test.assertTrue;
 
886,6 → 887,17
code: function () {
assertFalse(jsx.object.hasPropertyValue({x: 42}, "42", {strict: true}));
}
},
 
{
feature: 'jsx.object.findNewProperty(…)',
desc: 'Returns the correct value',
code: function () {
assert(jsx.object.findNewProperty() === "a_");
assert(jsx.object.findNewProperty({}) === "a_");
assert(jsx.object.findNewProperty({"a_": true}) === "b_");
assert(jsx.object.findNewProperty({"a_": true, "b_": true}) === "c_");
}
}
]
});
/trunk/object.js
1214,7 → 1214,56
}
 
return false;
},
 
/**
* Returns the name of an unused property for an object.
*
* @param {Object} obj
* @param {Number} iLength
* Maximum property name length up to which an unused name
* is searched. The default is 256.
* @return {string}
* The name of a non-existing property of <code>o</code> if
* {@link Object#prototype.hasOwnProperty()} is supported, or
* the name of a property with value <code>undefined</code>
* if it is not supported; the empty string
* if there is no such property.
*/
findNewProperty: function (obj, iLength) {
if (!obj)
{
obj = jsx_object;
}
 
if (arguments.length < 2)
{
iLength = 256;
}
else
{
iLength = parseInt(iLength, 10);
}
 
var prefix = "";
 
while (prefix.length < iLength)
{
for (var i = "a".charCodeAt(0), max = "z".charCodeAt(0); i <= max; ++i)
{
var ch = String.fromCharCode(i);
var newName = prefix + ch + "_";
if (!_hasOwnProperty(obj, newName))
{
return newName;
}
}
 
prefix += "a";
}
 
return "";
}
};
 
return jsx_object;
1329,61 → 1378,6
};
 
/**
* Returns the name of an unused property for an object.
*
* @function
*/
jsx.object.findNewProperty = (function () {
var _hasOwnProperty = jsx.object._hasOwnProperty;
 
/**
* @param {Object} obj
* @param {Number} iLength
* Maximum property name length up to which an unused name
* is searched. The default is 256.
* @return {string}
* The name of a non-existing property of <code>o</code> if
* {@link Object#prototype.hasOwnProperty()} is supported, or
* the name of a property with value <code>undefined</code>
* if it is not supported; the empty string
* if there is no such property.
*/
return function (obj, iLength) {
if (!obj)
{
obj = this;
}
 
if (arguments.length < 2)
{
iLength = 256;
}
else
{
iLength = parseInt(iLength, 10);
}
 
var newName = "";
 
while (newName.length < iLength)
{
for (var i = "a".charCodeAt(0), max = "z".charCodeAt(0); i <= max; ++i)
{
var ch = String.fromCharCode(i);
if (!_hasOwnProperty(obj, newName + ch + "_"))
{
return newName + ch + "_";
}
}
 
newName += "a";
}
 
return "";
};
}());
 
/**
* Clears the handler for the proprietary <code>error</code> event.
*
* NOTE: This method has previously been provided by {@link debug.js};