JSFiddle - React, Tailwind, and code Playground
by Cheng Long Xiao
HTML
<div id="displayer"></div>
JavaScript
<!--
/**
*
* 调换两个元素的位置
*
*
* <b>Example:</b>
* <pre>
* in parser.exchange('{"A","B" "C","D" "E","F" "J","K"}');
* out {"C","A" "D","C" "F","E" "K","J"}
* </pre>
*
* @ti 2012/04/20 12:34:29 GMT + 08:00
*
*/
var parser = window.parser || {
/**
* 用于转义的反斜线
*
* @type {String}
*/
BACKLASH : '\\'
,
/**
*
*
* @type {String}
*/
DOUBLE_QUOT : '"'
,
/**
* @type {String}
*/
SINGLE_QUOT : "'"
,
/**
* 用于 Pair 的间隔符
*
* @type {String}
*/
PAIR_SEPARATOR : ','
,
/**
* 用于 Token 的间隔符
* @type {String}
*/
TOKEN_SEPARATOR : ' '
,
/**
* Exchange
*
* @param {String} input
*/
exchange : function(input) {
try {
return this._process(input);
} catch ( error ) {
debug.log(error);
throw error;
alert('数据不符合规范: ' + input + ' Error: ' + error);
} finally {
}
}
,
_process : function(input) {
// 用于统计执行时间
var begin = new Date().getTime();
//
var output = [];
input = input || '{"A","B" "C","D" "E","F" "J","K" "A","B" "C","D" "E","F" "J","K" "A","B" "C","D" "E","F" "J","K" "A","B" "C","D" "E","F" "J","K" "A","B" "C","D"}';
// Raw Data
var raw = this.extract(input);
// var raw = this.encode( this.extract(input) );
console.log(raw);
var pair;
var pairArray = this.strSplit(raw, this.TOKEN_SEPARATOR);
var index;
for ( index in pairArray )
{
debug.log(pairArray[index]);
pair = this.strSplit(pairArray[index], this.PAIR_SEPARATOR);
// 调换 Pair 元素的位置,并重组
output.push(pair[1], this.PAIR_SEPARATOR, pair[0]);
// 添加 Token 分隔符
if ( pairArray.length - 1 != index ) output.push(this.TOKEN_SEPARATOR);
debug.log(pair);
debug.log(pair[0] + "<>" + pair[1]);
}
debug.log( output.join('') );
debug.log( this.enwrap( output.join('') ) );
var result = this.enwrap( output.join('') );
//alert(result);
debug.log('Consumed: ' + (new Date().getTime() - begin) + '...