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]
@ -33,25 +33,26 @@ proc parseQuery*(s: kstring): Query =
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():
result = ctx.nimpkgs.packages.values.toSeq()
return
for name, pkg in ctx.nimpkgs.packages:
let searchStr = pkg.genericSearchString() let searchStr = pkg.genericSearchString()
if (q.name notin pkg.name) or (q.license notin pkg.license) or if (q.name notin pkg.name) or (q.license notin pkg.license) or
(q.tag != "".kstring and (q.tag notin pkg.tags)): (q.tag != "".kstring and (q.tag notin pkg.tags)):
continue return
if q.all in searchStr: if q.all.toLowerAscii() in searchStr:
result.add pkg 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 = 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))