就是在一次逛知乎看了一段全是下划线命名变量的代码 于是自己用js尝试着写了一下
效果如下图 也实现了反编译的
就图一乐 代码很简单 嘿嘿
// 使用
let key = 'queryyy';
let testdata = {};
let Keydata = { name: "shou", age: 12, set: "男" };
let keyValue = new rulercode();
testdata[keyValue.compiler('name')] = 'shou';
testdata[keyValue.compiler('age')] = '12';
testdata[keyValue.compiler('set')] = '男';
方法代码 接受一个布尔值 是否需要反编译
class rulercode {
searching = [];
codecharacter = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=_/.|';
constructor(data = false) {
this.iscode = data;
this.searchinginit();
}
searchinginit() {
for (let i = 0; i < this.codecharacter.length; i++) {
this.searching.push({
index: i + 1,
keyValue: this.codecharacter.charAt(i)
})
}
}
compiler(edata) {
let data = [];
this.searching.map(item => {
for (let i = 0; i < edata.length; i++) {
let _length = 0;
let _string = new String();
if (item.keyValue == edata[i]) {
for (let t = 0; t < item.index; t++) {
_string += '_'
}
data.push({
value: _string,
index: i
});
}
}
})
data = data.sort(this.cpmpare('index'));
let rundata = [];
data.map(item => {
rundata.push(item.value)
})
if (this.iscode) {
return rundata.join('-');
} else {
return rundata.join('');
}
}
cpmpare(property) {
return function (a, b) {
var value1 = a[property];
var value2 = b[property];
return value1 - value2;
}
}
reverse(data) {
let _stringdata = data.split('-');
let _codedata = new String();
_stringdata.map(item => {
let _stringketlength = 0;
for (let i = 0; i < item.length; i++) {
_stringketlength++;
}
this.searching.map(itemall => {
if (itemall.index == _stringketlength) {
_codedata += itemall.keyValue;
}
})
});
return _codedata;
}
}