我已经创建了这组功能,希望它们可以帮助您(我认为这transformIntoSingleElement
就是您要寻找的):
function stackTrace() {
var err = new Error();
console.log(err.stack);
return err;
}
function transformIntoArray(element, mandatory){
mandatory = (mandatory!==undefined)?mandatory:true;
if(element === undefined && mandatory){
stackTrace();
return element;
}
if(element === undefined)
return [];
if(element.constructor.name != "Array")
return [element];
return element;
}
function transformIntoSingleElement(element, index, mandatory){
mandatory = (mandatory!==undefined)?mandatory:true;
if(element === undefined && mandatory){
stackTrace();
return element;
}
if(element === undefined){
return element;
}
if(element.constructor.name == "Array"){
if(element.length >= index)
return element[index];
return element[0];
}
return element;
}
function getElementValue(element, dflt, mandatory){
dflt = dflt || "";
mandatory = (mandatory!==undefined)?mandatory:true;
if(element === undefined && mandatory){
stackTrace();
return element;
}
if(element === undefined)
return dflt;
if(element._ === undefined)
return element;
return element._;
}
function getAttributeValue(element, attribute, dflt, mandatory){
dflt = dflt || "";
mandatory = (mandatory!==undefined)?mandatory:true;
if(element === undefined && mandatory){
stackTrace();
return element;
}
if(element.$ !== undefined && element.$[attribute] !== undefined && (element.$[attribute] !== "" || (element.$[attribute] === "" && mandatory))){
return element.$[attribute];
}
return dflt;
}