Add files via upload
This commit is contained in:
parent
b6512aa351
commit
0e6ce73e0f
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,189 @@
|
|||
// drT.js
|
||||
// 2022/09/30 write by hjdhnx
|
||||
// Licensed under the MIT license.
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
var drT = {
|
||||
name: "drT",
|
||||
version: "1.0.0",
|
||||
templateSettings: {
|
||||
evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
|
||||
interpolate: /\{\{([\s\S]+?)\}\}/g, // 变量渲染
|
||||
encode: /\{\{@([\s\S]+?)\}\}/g, // 变量自动url编码
|
||||
use: /\{\{#([\s\S]+?)\}\}/g,
|
||||
useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
|
||||
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
|
||||
defineParams:/^\s*([\w$]+):([\s\S]+)/,
|
||||
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g, // ? if ?? else if ?? else
|
||||
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
|
||||
varname: "fl",
|
||||
strip: true,
|
||||
append: true,
|
||||
selfcontained: false,
|
||||
doNotSkipEncoded: false
|
||||
},
|
||||
template: undefined, //fn, compile template
|
||||
compile: undefined, //fn, for express
|
||||
log: true
|
||||
}, _globals;
|
||||
|
||||
drT.encodeHTMLSource = function(doNotSkipEncoded) {
|
||||
var encodeHTMLRules = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'", "/": "/" },
|
||||
matchHTML = doNotSkipEncoded ? /[&<>"'\/]/g : /&(?!#?\w+;)|<|>|"|'|\//g;
|
||||
return function(code) {
|
||||
return code ? code.toString().replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : "";
|
||||
};
|
||||
};
|
||||
|
||||
_globals = (function(){ return this || (0,eval)("this"); }());
|
||||
|
||||
/* istanbul ignore else */
|
||||
if (typeof module !== "undefined" && module.exports) {
|
||||
module.exports = drT;
|
||||
} else if (typeof define === "function" && define.amd) {
|
||||
define(function(){return drT;});
|
||||
} else {
|
||||
_globals.drT = drT;
|
||||
}
|
||||
|
||||
var startend = {
|
||||
append: { start: "'+(", end: ")+'", startencode: "'+encodeHTML(" },
|
||||
split: { start: "';out+=(", end: ");out+='", startencode: "';out+=encodeHTML(" }
|
||||
}, skip = /$^/;
|
||||
|
||||
function resolveDefs(c, block, def) {
|
||||
return ((typeof block === "string") ? block : block.toString())
|
||||
.replace(c.define || skip, function(m, code, assign, value) {
|
||||
if (code.indexOf("def.") === 0) {
|
||||
code = code.substring(4);
|
||||
}
|
||||
if (!(code in def)) {
|
||||
if (assign === ":") {
|
||||
if (c.defineParams) value.replace(c.defineParams, function(m, param, v) {
|
||||
def[code] = {arg: param, text: v};
|
||||
});
|
||||
if (!(code in def)) def[code]= value;
|
||||
} else {
|
||||
new Function("def", "def['"+code+"']=" + value)(def);
|
||||
}
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.replace(c.use || skip, function(m, code) {
|
||||
if (c.useParams) code = code.replace(c.useParams, function(m, s, d, param) {
|
||||
if (def[d] && def[d].arg && param) {
|
||||
var rw = (d+":"+param).replace(/'|\\/g, "_");
|
||||
def.__exp = def.__exp || {};
|
||||
def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2");
|
||||
return s + "def.__exp['"+rw+"']";
|
||||
}
|
||||
});
|
||||
var v = new Function("def", "return " + code)(def);
|
||||
return v ? resolveDefs(c, v, def) : v;
|
||||
});
|
||||
}
|
||||
|
||||
function unescape(code) {
|
||||
return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
|
||||
}
|
||||
|
||||
drT.template = function(tmpl, c, def) {
|
||||
c = c || drT.templateSettings;
|
||||
var cse = c.append ? startend.append : startend.split, needhtmlencode, sid = 0, indv,
|
||||
str = (c.use || c.define) ? resolveDefs(c, tmpl, def || {}) : tmpl;
|
||||
|
||||
// console.log(str);
|
||||
let beforeCode = '';
|
||||
if(str.match(c.interpolate || skip)){
|
||||
let inter_codes = str.match(c.interpolate || skip);
|
||||
let inter_dict = {};
|
||||
inter_codes.forEach(item=>{
|
||||
item.replace(c.interpolate || skip,function (m,code) {
|
||||
let varname = code.split('.')[0];
|
||||
if(!inter_dict.hasOwnProperty(varname)){
|
||||
let beginCode = `if(typeof(${varname})==='undefined'){${varname}={}}`;
|
||||
inter_dict[varname] = beginCode;
|
||||
}if(!inter_dict.hasOwnProperty(code)){
|
||||
let beginCode = `if(typeof(${code})==='undefined'){${code}=''};`;
|
||||
inter_dict[code] = beginCode;
|
||||
}
|
||||
});
|
||||
});
|
||||
let beginCode = Object.values(inter_dict).join('\n');
|
||||
// console.log(beginCode);
|
||||
beforeCode += beginCode;
|
||||
}
|
||||
str = beforeCode+("var out='" + (c.strip ? str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g," ")
|
||||
.replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,""): str)
|
||||
.replace(/'|\\/g, "\\$&")
|
||||
.replace(c.encode || skip, function(m, code) {
|
||||
needhtmlencode = true;
|
||||
return cse.startencode + unescape(code) + cse.end;
|
||||
})
|
||||
.replace(c.interpolate || skip, function(m, code) {
|
||||
let varname = code.split('.')[0];
|
||||
// console.log(varname === code);
|
||||
// console.log(`varname:${varname},code:${code}`);
|
||||
if(varname === code){
|
||||
let res = cse.start + `JSON.stringify(${unescape(code)})` + cse.end;
|
||||
// console.log(res);
|
||||
return res
|
||||
}
|
||||
return cse.start + unescape(code) + cse.end;
|
||||
})
|
||||
.replace(c.conditional || skip, function(m, elsecase, code) {
|
||||
return elsecase ?
|
||||
(code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
|
||||
(code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
|
||||
})
|
||||
.replace(c.iterate || skip, function(m, iterate, vname, iname) {
|
||||
if (!iterate) return "';} } out+='";
|
||||
sid+=1; indv=iname || "i"+sid; iterate=unescape(iterate);
|
||||
return "';var arr"+sid+"="+iterate+";if(arr"+sid+"){var "+vname+","+indv+"=-1,l"+sid+"=arr"+sid+".length-1;while("+indv+"<l"+sid+"){"
|
||||
+vname+"=arr"+sid+"["+indv+"+=1];out+='";
|
||||
})
|
||||
.replace(c.evaluate || skip, function(m, code) {
|
||||
return "';" + unescape(code) + "out+='";
|
||||
})
|
||||
+ "';return out;")
|
||||
.replace(/\n/g, "\\n").replace(/\t/g, '\\t').replace(/\r/g, "\\r")
|
||||
.replace(/(\s|;|\}|^|\{)out\+='';/g, '$1').replace(/\+''/g, "");
|
||||
//.replace(/(\s|;|\}|^|\{)out\+=''\+/g,'$1out+=');
|
||||
|
||||
if (needhtmlencode) {
|
||||
// console.log('需要编码');
|
||||
// console.log(c.doNotSkipEncoded);
|
||||
if (!c.selfcontained && _globals && !_globals._encodeHTML) _globals._encodeHTML = drT.encodeHTMLSource(c.doNotSkipEncoded);
|
||||
str = "var encodeHTML = typeof _encodeHTML !== 'undefined' ? _encodeHTML : ("
|
||||
+ drT.encodeHTMLSource.toString() + "(" + (c.doNotSkipEncoded || '') + "));"
|
||||
+ str;
|
||||
// console.log(str);
|
||||
}else{
|
||||
// console.log('不需要编码');
|
||||
}
|
||||
// console.log(c.varname);
|
||||
// console.log(str);
|
||||
try {
|
||||
return new Function(c.varname, str);
|
||||
} catch (e) {
|
||||
/* istanbul ignore else */
|
||||
// console.log(e.message);
|
||||
if (typeof console !== "undefined") console.log("Could not create a template function: " + str);
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
drT.compile = function(tmpl, def) {
|
||||
return drT.template(tmpl, null, def);
|
||||
};
|
||||
drT.renderText = function (tmpl,dict,varname){
|
||||
varname = varname||'';
|
||||
if(varname){
|
||||
drT.templateSettings.varname = varname;
|
||||
}
|
||||
dict = dict||{};
|
||||
return drT.compile(tmpl)(dict);
|
||||
};
|
||||
}());
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,138 @@
|
|||
const peq = new Uint32Array(0x10000);
|
||||
const myers_32 = (a, b) => {
|
||||
const n = a.length;
|
||||
const m = b.length;
|
||||
const lst = 1 << (n - 1);
|
||||
let pv = -1;
|
||||
let mv = 0;
|
||||
let sc = n;
|
||||
let i = n;
|
||||
while (i--) {
|
||||
peq[a.charCodeAt(i)] |= 1 << i;
|
||||
}
|
||||
for (i = 0; i < m; i++) {
|
||||
let eq = peq[b.charCodeAt(i)];
|
||||
const xv = eq | mv;
|
||||
eq |= ((eq & pv) + pv) ^ pv;
|
||||
mv |= ~(eq | pv);
|
||||
pv &= eq;
|
||||
if (mv & lst) {
|
||||
sc++;
|
||||
}
|
||||
if (pv & lst) {
|
||||
sc--;
|
||||
}
|
||||
mv = (mv << 1) | 1;
|
||||
pv = (pv << 1) | ~(xv | mv);
|
||||
mv &= xv;
|
||||
}
|
||||
i = n;
|
||||
while (i--) {
|
||||
peq[a.charCodeAt(i)] = 0;
|
||||
}
|
||||
return sc;
|
||||
};
|
||||
const myers_x = (b, a) => {
|
||||
const n = a.length;
|
||||
const m = b.length;
|
||||
const mhc = [];
|
||||
const phc = [];
|
||||
const hsize = Math.ceil(n / 32);
|
||||
const vsize = Math.ceil(m / 32);
|
||||
for (let i = 0; i < hsize; i++) {
|
||||
phc[i] = -1;
|
||||
mhc[i] = 0;
|
||||
}
|
||||
let j = 0;
|
||||
for (; j < vsize - 1; j++) {
|
||||
let mv = 0;
|
||||
let pv = -1;
|
||||
const start = j * 32;
|
||||
const vlen = Math.min(32, m) + start;
|
||||
for (let k = start; k < vlen; k++) {
|
||||
peq[b.charCodeAt(k)] |= 1 << k;
|
||||
}
|
||||
for (let i = 0; i < n; i++) {
|
||||
const eq = peq[a.charCodeAt(i)];
|
||||
const pb = (phc[(i / 32) | 0] >>> i) & 1;
|
||||
const mb = (mhc[(i / 32) | 0] >>> i) & 1;
|
||||
const xv = eq | mv;
|
||||
const xh = ((((eq | mb) & pv) + pv) ^ pv) | eq | mb;
|
||||
let ph = mv | ~(xh | pv);
|
||||
let mh = pv & xh;
|
||||
if ((ph >>> 31) ^ pb) {
|
||||
phc[(i / 32) | 0] ^= 1 << i;
|
||||
}
|
||||
if ((mh >>> 31) ^ mb) {
|
||||
mhc[(i / 32) | 0] ^= 1 << i;
|
||||
}
|
||||
ph = (ph << 1) | pb;
|
||||
mh = (mh << 1) | mb;
|
||||
pv = mh | ~(xv | ph);
|
||||
mv = ph & xv;
|
||||
}
|
||||
for (let k = start; k < vlen; k++) {
|
||||
peq[b.charCodeAt(k)] = 0;
|
||||
}
|
||||
}
|
||||
let mv = 0;
|
||||
let pv = -1;
|
||||
const start = j * 32;
|
||||
const vlen = Math.min(32, m - start) + start;
|
||||
for (let k = start; k < vlen; k++) {
|
||||
peq[b.charCodeAt(k)] |= 1 << k;
|
||||
}
|
||||
let score = m;
|
||||
for (let i = 0; i < n; i++) {
|
||||
const eq = peq[a.charCodeAt(i)];
|
||||
const pb = (phc[(i / 32) | 0] >>> i) & 1;
|
||||
const mb = (mhc[(i / 32) | 0] >>> i) & 1;
|
||||
const xv = eq | mv;
|
||||
const xh = ((((eq | mb) & pv) + pv) ^ pv) | eq | mb;
|
||||
let ph = mv | ~(xh | pv);
|
||||
let mh = pv & xh;
|
||||
score += (ph >>> (m - 1)) & 1;
|
||||
score -= (mh >>> (m - 1)) & 1;
|
||||
if ((ph >>> 31) ^ pb) {
|
||||
phc[(i / 32) | 0] ^= 1 << i;
|
||||
}
|
||||
if ((mh >>> 31) ^ mb) {
|
||||
mhc[(i / 32) | 0] ^= 1 << i;
|
||||
}
|
||||
ph = (ph << 1) | pb;
|
||||
mh = (mh << 1) | mb;
|
||||
pv = mh | ~(xv | ph);
|
||||
mv = ph & xv;
|
||||
}
|
||||
for (let k = start; k < vlen; k++) {
|
||||
peq[b.charCodeAt(k)] = 0;
|
||||
}
|
||||
return score;
|
||||
};
|
||||
const distance = (a, b) => {
|
||||
if (a.length < b.length) {
|
||||
const tmp = b;
|
||||
b = a;
|
||||
a = tmp;
|
||||
}
|
||||
if (b.length === 0) {
|
||||
return a.length;
|
||||
}
|
||||
if (a.length <= 32) {
|
||||
return myers_32(a, b);
|
||||
}
|
||||
return myers_x(a, b);
|
||||
};
|
||||
const closest = (str, arr) => {
|
||||
let min_distance = Infinity;
|
||||
let min_index = 0;
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
const dist = distance(str, arr[i]);
|
||||
if (dist < min_distance) {
|
||||
min_distance = dist;
|
||||
min_index = i;
|
||||
}
|
||||
}
|
||||
return arr[min_index];
|
||||
};
|
||||
export { closest, distance };
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue