Skip to content

Commit

Permalink
Merge pull request #51 from zanllp/dev-pattern-matching
Browse files Browse the repository at this point in the history
使用模式匹配重写部分代码
  • Loading branch information
zanllp committed Jun 6, 2023
2 parents e468c93 + a501035 commit 7086771
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
30 changes: 16 additions & 14 deletions script/http_server.as
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
include('lib/server/http_server')
include('lib/message_queue/client')

const port = 8811
const port = to_num((env().process_arg.port) || 8080)
const url = f('http://127.0.0.1:{}', port)

const global_store = {
Expand Down Expand Up @@ -37,19 +36,22 @@ const get_files_html = (dir = '') => {

const route = (params, resp) => {
const curr_dir = env().curr_dir()
const path = params.path
(!path) ? @{
const res = get_files_html()
resp.set_status(200).set_data(f('<pre>{}</pre>', res.join(cr))).end()
} : (is_dir(path)) ? @{
const res = get_files_html(path)
resp.set_status(200).set_data(f('<pre>{}</pre>', res.join(cr))).end()
} : (fs.exist(path)) ? @{
// path = path_calc(curr_dir, path)
resp.set_status(200).set_data(f('<pre>{}</pre>', fs.read(path))).end()
} : @{
resp.set_status(404).set_data('<h1>404</h1>').end()
const res = match (const path = params.path) {
null: {
data: f('<pre>{}</pre>', get_files_html().join(cr)),
code: 200
},
is_dir(path): {
data: f('<pre>{}</pre>', get_files_html(path).join(cr)),
code: 200
},
fs.exist(path): {
data: f('<pre>{}</pre>', fs.read(path)),
code: 200
},
_: { code: 404, data: '<h1>404</h1>' }
}
resp.set_status(res.code).set_data(res.data).end()
}
// /*
make_http_server(port, {
Expand Down
2 changes: 2 additions & 0 deletions script/lib/index.as
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const for = (count, cb) => {
null
}

const to_num = (str) => json.parse(str)

const cowsay = msg => {
log(f(`
____________
Expand Down
46 changes: 11 additions & 35 deletions script/playground.as
Original file line number Diff line number Diff line change
@@ -1,48 +1,24 @@
/*const dd = () => {
const obj = { id: start_timer(() => log('call'), 30) }
make_promise(cb => {
fetch('http://127.0.0.1:8890', { method: 'get' }).then(v => {
log(v.err_msg)
remove_timer(obj.id)
cb(v)
})
})
}
dd().then(v => log(f('end {}', v.keys().join(','))))

const delayExec = (fn, timeout = 1000) => start_timer(fn,timeout, true)
*/

const json_str = fs.read("/Users/wuqinchuan/Desktop/project/agumi/canada.json")

const j = json.parse(json_str)
@{
const fn = (node) => {
const t = typeof(node)
(t == "object") ? @{
node.values().select(fn)
} : (t == "array") ? @{
node.select(fn)
} : log(node)

}
// fn(j)
const arr = []
const fn = (node) => match (typeof(node)) {
"object" : node.values().select(fn),
"array": node.select(fn),
_: arr.push(node)
}
fn(j)
fs.write("./res.json", json.stringify(arr))
}
/*
const t = time()
for(5, () => {
// json.parse(json_str)
json.parse(json_str)
})
log((time() - t)/5)

const match = (params) => {
assert(typeof(params), "object")
full_log(params.values())
}

match({
"hello": 1,
world: 2
})
*/

0 comments on commit 7086771

Please sign in to comment.