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]
@ -33,25 +33,26 @@ proc parseQuery*(s: kstring): Query =
else:
result.all &= part
proc toLowerAscii(ks: kstring): kstring {.inline.} =
($ks).toLowerAscii().kstring
proc toLowerAscii(ks: kstring): kstring {.inline.} = ($ks).toLowerAscii().kstring
proc genericSearchString(p: NimPackage): kstring =
(@[p.url, p.name, p.description, p.tags.join(" ").kstring].join(" ").kstring).toLowerAscii()
proc searchPackages*(q: Query): seq[NimPackage] =
if q == Query():
result = ctx.nimpkgs.packages.values.toSeq()
return
for name, pkg in ctx.nimpkgs.packages:
proc `~=`(q: Query, pkg: NimPackage): bool =
let searchStr = pkg.genericSearchString()
if (q.name notin pkg.name) or (q.license notin pkg.license) or
(q.tag != "".kstring and (q.tag notin pkg.tags)):
continue
return
if q.all in searchStr:
result.add pkg
if q.all.toLowerAscii() in searchStr:
return true
proc searchPackages*(q: Query): seq[NimPackage] =
if q == Query(): return nimpkgsList()
collect:
for _, pkg in ctx.nimpkgs.packages:
if q ~= pkg: pkg
proc getSearchFromUri*(): kstring =
var url = currentUri()

View file

@ -1,5 +1,5 @@
import std/[
asyncjs, jsconsole, jsfetch, sugar, tables
asyncjs, jsconsole, jsfetch, sequtils, sugar, tables
]
import karax/[kbase, karax]
@ -14,6 +14,8 @@ type
nimpkgs*: NimPkgs
loaded*: bool
var ctx* = Context()
let nimpkgsUrl =
when defined(debug): "http://localhost:8080/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
)
var ctx* = Context()
discard ctx.fetchPackages
proc nimpkgsList*(): seq[NimPackage] {.inline.} =
ctx.nimpkgs.packages.values.toSeq()

View file

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