purposeless refactorings

This commit is contained in:
Daylin Morgan 2024-08-17 09:34:56 -05:00
parent 97134d4ea8
commit a3d31ad8b1
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 29 additions and 23 deletions

View file

@ -1,4 +1,4 @@
import std/[strutils, sequtils, uri] import std/[strutils, uri]
import karax/[kbase, karax, karaxdsl, vdom, jstrutils] import karax/[kbase, karax, karaxdsl, vdom, jstrutils]
@ -23,35 +23,36 @@ proc parseQuery*(s: kstring): Query =
k = subparts[0] k = subparts[0]
v = subparts[1] v = subparts[1]
case k: case k:
of "name": of "name":
result.name = v result.name = v
of "tag": of "tag":
result.tag = v.replace("-") result.tag = v.replace("-")
of "license": of "license":
result.license = v result.license = v
else: discard else: discard
else: else:
result.all &= part result.all &= part
proc toLowerAscii(ks: kstring): kstring {.inline.} = proc toLowerAscii(ks: kstring): kstring {.inline.} = ($ks).toLowerAscii().kstring
($ks).toLowerAscii().kstring
proc genericSearchString(p: NimPackage): kstring = proc genericSearchString(p: NimPackage): kstring =
(@[p.url, p.name, p.description, p.tags.join(" ").kstring].join(" ").kstring).toLowerAscii() (@[p.url, p.name, p.description, p.tags.join(" ").kstring].join(" ").kstring).toLowerAscii()
proc searchPackages*(q: Query): seq[NimPackage] = proc `~=`(q: Query, pkg: NimPackage): bool =
if q == Query(): let searchStr = pkg.genericSearchString()
result = ctx.nimpkgs.packages.values.toSeq() if (q.name notin pkg.name) or (q.license notin pkg.license) or
(q.tag != "".kstring and (q.tag notin pkg.tags)):
return return
for name, pkg in ctx.nimpkgs.packages: if q.all.toLowerAscii() in searchStr:
let searchStr = pkg.genericSearchString() return true
if (q.name notin pkg.name) or (q.license notin pkg.license) or
(q.tag != "".kstring and (q.tag notin pkg.tags)):
continue
if q.all in searchStr: proc searchPackages*(q: Query): seq[NimPackage] =
result.add pkg if q == Query(): return nimpkgsList()
collect:
for _, pkg in ctx.nimpkgs.packages:
if q ~= pkg: pkg
proc getSearchFromUri*(): kstring = proc getSearchFromUri*(): kstring =
var url = currentUri() var url = currentUri()

View file

@ -1,5 +1,5 @@
import std/[ import std/[
asyncjs, jsconsole, jsfetch, sugar, tables asyncjs, jsconsole, jsfetch, sequtils, sugar, tables
] ]
import karax/[kbase, karax] import karax/[kbase, karax]
@ -14,6 +14,8 @@ type
nimpkgs*: NimPkgs nimpkgs*: NimPkgs
loaded*: bool loaded*: bool
var ctx* = Context()
let nimpkgsUrl = let nimpkgsUrl =
when defined(debug): "http://localhost:8080/nimpkgs.json" when defined(debug): "http://localhost:8080/nimpkgs.json"
else: "https://raw.githubusercontent.com/nimpkgs/nimpkgs/main/nimpkgs.json" else: "https://raw.githubusercontent.com/nimpkgs/nimpkgs/main/nimpkgs.json"
@ -29,5 +31,8 @@ proc fetchPackages*(ctx: var Context){.async.} =
) )
.catch((err: Error) => console.log err .catch((err: Error) => console.log err
) )
var ctx* = Context()
discard ctx.fetchPackages discard ctx.fetchPackages
proc nimpkgsList*(): seq[NimPackage] {.inline.} =
ctx.nimpkgs.packages.values.toSeq()

View file

@ -90,7 +90,7 @@ proc filteredPackagesDom(): VNode =
pkg.card pkg.card
proc update(pgCtx: var PageContext) = proc update(pgCtx: var PageContext) =
pgCtx.filteredPackages = ctx.nimpkgs.packages.values().toSeq() pgCtx.filteredPackages = nimpkgsList()
pgCtx.search = getSearchFromUri() pgCtx.search = getSearchFromUri()
pgCtx.filteredPackages = searchPackages(parseQuery(pgCtx.search)) pgCtx.filteredPackages = searchPackages(parseQuery(pgCtx.search))