This commit is contained in:
yumoqing 2024-04-07 22:01:20 +08:00
parent c292767a0d
commit 9e687ee3c1
2 changed files with 59 additions and 0 deletions

View File

@ -50,6 +50,51 @@ dispatch_event:
params: params:
*/ */
bricks.str2data = function(s, d){
/* fmt like
'my name is ${name}, ${age:type}'
type can be:
int, str, json
*/
funcs = {
'json':JSON.stringify
}
var regex = /\${(\w+)(?::(int|str|json))?}/;
var match = s.match(regex)
if (match){
var key = match[1];
var typ = match[2];
var ss = '${' + key;
if (typ != ''){
ss += ':' + typ;
}
ss += '}';
if (s == ss){
if (!d.hasOwnProperty(key)){
return null;
}
if (typ == ''){
return d[key];
}
var f = funcs[typ];
if (f){
return f(d[key]);
}
return d[key];
}
return s.replace(regex, (k, key, typ) => {
if (d.hasOwnProperty(key){
var f = funcs[typ];
if (f){
return f(d[key]);
}
return d[key];
}
return '';
});
}
return s;
}
bricks.apply_data = function(desc, data){ bricks.apply_data = function(desc, data){
var tmpl = JSON.stringify(desc); var tmpl = JSON.stringify(desc);
var s = bricks.obj_fmtstr(data, tmpl); var s = bricks.obj_fmtstr(data, tmpl);

View File

@ -2,6 +2,20 @@ var bricks = window.bricks || {};
function url_params(data) { function url_params(data) {
return Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&'); return Object.keys(data).map(key => `${key}=${encodeURIComponent(data[key])}`).join('&');
} }
/*
async function doit() {
const response = await fetch("http://localhost:3000")
const reader = response.body.getReader()
const decoder = new TextDecoder()
let result = await reader.read()
while (!result.done) {
const text = decoder.decode(result.value)
console.log("chunk is", text)
result = await reader.read()
}
}
// chunked response handle
*/
bricks.HttpText = class { bricks.HttpText = class {
constructor(headers){ constructor(headers){
/* /*