JavaScript
语言:
JaveScriptBabelCoffeeScript
确定
new Vue({
el: '#app',
data: {
selected: 'json',
src_array: '',
options: [{
text: 'JSON Array',
value: 'json'
}, {
text: 'PHP Array',
value: 'php'
}, ]
},
computed: {
// a computed getter
dst_array: function() {
if (this.src_array === "") {
return;
}
var cdata = '';
if (this.selected === 'php') {
var temp = this.src_array;
temp = temp.replace(/^\$[a-zA-Z0-9-_]+\s*=\s*\[/, '');
temp = temp.replace(/\[/g, '{');
temp = temp.replace(/]/g, '}');
temp = temp.replace(/\s*=>\s*"/g, ': "');
temp = temp.replace(/\s*=>\s*'/g, ': \'');
temp = temp.replace(/\s*=>\s*{'/g, ': {');
cdata = "[";
cdata += temp;
cdata += "]";
return cdata;
} else {
cdata = "public $params = [\n";
var temp = this.src_array;
if (temp.substr(0, 1) === "[" || temp.substr(0, 1) === "{") {
temp = temp.substr(1);
}
if (temp.substring(temp.length - 1, temp.length) === "]" ||
temp.substring(temp.length - 1, temp.length) === "}") {
temp = temp.substr(0, temp.length - 1).trim();
}
temp = temp.replace(/\{/g, '[');
temp = temp.replace(/\}/g, ']');
temp = temp.replace(/\:\s*"/g, ' => "');
temp = temp.replace(/\:\s*'/g, ' => \'');
temp = temp.replace(/\:\s*\[/g, ' => [');
cdata += temp;
cdata += "\n];";
return cdata;
}
}
}
});