Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Created March 21, 2011 12:19
Show Gist options
  • Save aya-eiya/879376 to your computer and use it in GitHub Desktop.
Save aya-eiya/879376 to your computer and use it in GitHub Desktop.
JavaScript版KeyValueのアレ
<html>
<head>
<script>
WithAttributre = true;
//==================================
// Here is for non attributes xml =
//==================================
show = function (v){alert(v);return v;}
assert = function (a,b){ alert((a==b)?"OK":"unmatch"); };
// KeyValue
function KeyValue(k,v){
this.key = k;
this.value = v;
}
var format = {
"nil":function(k,v,as){ return "<${key}/>" .replace(/\$\{key\}/g,k); },
"val":function(k,v,as){ return "<${key}>${value}</${key}>".replace(/\$\{key\}/g,k).replace(/\$\{value\}/g,v); }
}
function convert(lst){
var str = "";
for(var i=0;i<lst.length;i++){
var kv = lst[i];
var f = ( kv.value ) ? format.val : format.nil;
var v = "";
if(kv.value) v = ( typeof("") == typeof(kv.value) ) ? kv.value : convert( kv.value ) ;
str += f( kv.key, v , kv.attributes );
}
return str;
}
var list = [
new KeyValue("key1","value1"),
new KeyValue("key2","value2"),
new KeyValue("key3", [
new KeyValue("key3-1","value3-1"),
new KeyValue("key3-2","value3-2")
])
];
assert(show(convert(list)),
"<key1>value1</key1><key2>value2</key2><key3><key3-1>value3-1</key3-1><key3-2>value3-2</key3-2></key3>");
//===============================
// Set Attributes =
//===============================
if(WithAttributre){
KeyValue = function (k,v,as){
this.key = k;
this.value = v;
this.attributes = (as)? as : new AttributeCollection();
}
format =
{
"nil":function(k,v,as){ return "<${key}${attributes}/>" .replace(/\$\{key\}/g,k).replace(/\$\{attributes\}/g,as.toListedStr()); },
"val":function(k,v,as){ return "<${key}${attributes}>${value}</${key}>".replace(/\$\{key\}/g,k).replace(/\$\{attributes\}/g,as.toListedStr()).replace(/\$\{value\}/g,v); }
}
Attribute = function(k,vs){
this.key = k; this.values = vs;
this.toListedStr = function(){
return ' ' + this.key + '="' + vs.join().replace(/"/g,"&quot;") + '"';
}
}
AttributeCollection = function (){
this.items = new Array();
for (var i = 0; i < arguments.length; i++) {
this.items[i]=arguments[i];
}
this.toListedStr = function(){
var ret = "";
for(var i=0;i<this.items.length;i++){
ret += this.items[i].toListedStr();
}
return ret;
}
}
// for shorten code.
$at = function (arr){
return new Attribute(arr[0],arr[1]);
}
$ats = function (arr){
var ret= new AttributeCollection();
for(var i = 0;i < arr.length;i++){
var vs = new Array();
for(var j=1;j<arr[i].length;j++) vs[j-1] = arr[i][j];
ret.items[i] = $at( [ arr[i][0] , vs ] );
}
return ret;
}
list = [
new KeyValue("key1","value1",$ats([ ["attr1","attrval1"] ])),
new KeyValue("key2","value2",$ats([ ["attr2","attrval2-1","attrval2-2"] ])),
new KeyValue("key3", [
new KeyValue("key3-1","value3-1"),
new KeyValue("key3-2","value3-2")
],$ats([ ["attr3_1","attrval3_1"],["attr3_2","attrval3_2"] ])),
new KeyValue("key4"),
new KeyValue("key5",null,$ats([ ["attr5","attrval5"] ])),
];
assert(show(convert(list)),
'<key1 attr1="attrval1">value1</key1>'+
'<key2 attr2="attrval2-1,attrval2-2">value2</key2>'+
'<key3 attr3_1="attrval3_1" attr3_2="attrval3_2"><key3-1>value3-1</key3-1><key3-2>value3-2</key3-2></key3>'+
'<key4/>'+
'<key5 attr5="attrval5"/>');
}
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment