From cd0bfd9e076514f3f4a87759e2e1cc4195c4bd95 Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Sun, 7 Jan 2024 22:49:37 +0100 Subject: [PATCH 1/3] chore: ideas for speed up with Math.sign documented --- src/common/bin/lib0/decoding.ts | 2 +- src/common/data/orderby.ts | 2 +- src/common/data/sortable.ts | 2 +- src/common/data/sorted.ts | 2 +- src/common/platform.ts | 4 +--- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/common/bin/lib0/decoding.ts b/src/common/bin/lib0/decoding.ts index 9d86a7ed..fe9183f7 100644 --- a/src/common/bin/lib0/decoding.ts +++ b/src/common/bin/lib0/decoding.ts @@ -208,7 +208,7 @@ export function readVarInt(decoder: Decoder): number { let r = decoder.arr[decoder.pos++] let num = r & BITS6 let mult = 64 - const sign = (r & BIT7) > 0 ? -1 : 1 + const sign = (r & BIT7) > 0 ? -1 : 1 // use Math.sign(?) for performance? if ((r & BIT8) === 0) { // don't continue reading return sign * num diff --git a/src/common/data/orderby.ts b/src/common/data/orderby.ts index efff80eb..499aaab2 100644 --- a/src/common/data/orderby.ts +++ b/src/common/data/orderby.ts @@ -24,7 +24,7 @@ export function composeOrderby(field: string, asc = true): string { export function cmp(a: any, b: any, asc = true): -1 | 0 | 1 { const aa = a || 0 const bb = b || 0 - return aa > bb ? (asc ? 1 : -1) : aa < bb ? (asc ? -1 : 1) : 0 + return aa > bb ? (asc ? 1 : -1) : aa < bb ? (asc ? -1 : 1) : 0 // use Math.sign(?) for performance? } // todo: support localeCompare() diff --git a/src/common/data/sortable.ts b/src/common/data/sortable.ts index af8742dc..bf226f86 100644 --- a/src/common/data/sortable.ts +++ b/src/common/data/sortable.ts @@ -44,7 +44,7 @@ export function moveSortWeight( // Make sure they are sorted items = sortedItems([...items]) - const step = moveLower ? -1 : 0 + const step = moveLower ? -1 : 0 // use Math.sign(?) for performance? const lower = items[newIndex + step].sort_weight || 0 const upper = items[newIndex + step + 1].sort_weight || 0 const distance = upper - lower diff --git a/src/common/data/sorted.ts b/src/common/data/sorted.ts index fb7bdccb..4a68d48a 100644 --- a/src/common/data/sorted.ts +++ b/src/common/data/sorted.ts @@ -65,7 +65,7 @@ export function useSorted>( // Make sure they are sorted sortableItems = items([...sortableItems]) - const step = moveLower ? -1 : 0 + const step = moveLower ? -1 : 0 // use Math.sign(?) for performance? const lower = getter(sortableItems[newIndex + step]) || 0 const upper = getter(sortableItems[newIndex + step + 1]) || 0 const distance = upper - lower diff --git a/src/common/platform.ts b/src/common/platform.ts index b556064f..d973e9fd 100644 --- a/src/common/platform.ts +++ b/src/common/platform.ts @@ -53,9 +53,7 @@ export function detect( info.windows = !!_navigator?.platform?.startsWith('Win') info.beaker = _window?.beaker != null // https://github.com/beakerbrowser/beaker - info.electron - = (_navigator?.userAgent?.toLowerCase()?.indexOf(' electron/') || -1) > -1 - && !info.beaker + info.electron = (_navigator?.userAgent?.toLowerCase()?.indexOf(' electron/') || -1) > -1 && !info.beaker info.wkwebview = _window?.webkit?.messageHandlers != null // Apple embedded info.pwa = _navigator?.serviceWorker != null From 3546c0c6500cc1e6a4d72bdd972d5f5d337a487e Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Mon, 8 Jan 2024 21:35:51 +0100 Subject: [PATCH 2/3] feat: imporved file utils --- src/node/fs.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/node/fs.ts b/src/node/fs.ts index ede436b8..7d0edd5a 100644 --- a/src/node/fs.ts +++ b/src/node/fs.ts @@ -1,7 +1,9 @@ import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises' -import { join as joinPath, normalize } from 'node:path' +import { dirname, join as joinPath, normalize } from 'node:path' import process from 'node:process' +import { isUint8Array } from '../common' +/** Try to use `~` for HOME folder if possible */ export function toHumanReadableFilePath(path: string) { const p = normalize(path) const h = process.env.HOME @@ -25,11 +27,19 @@ export function isHiddenPath(path: string): boolean { return path.startsWith('.') || path.includes('/.') } +/** Create missing folders e.g. `/a/b/c` will create folders `/a/b/c` */ export async function ensureFolder(...parts: string[]): Promise { const path = joinPath(...parts) if (!(await exists(path))) await mkdir(path, { recursive: true }) + return path +} +/** Create missing folder to file location e.g. `/a/b/c/s.txt` will create folders `/a/b/c` */ +export async function ensureFolderForFile(...parts: string[]): Promise { + const path = dirname(joinPath(...parts)) + if (!(await exists(path))) + await mkdir(path, { recursive: true }) return path } @@ -41,16 +51,23 @@ export async function removeFolder(...parts: string[]): Promise { return path } -export async function readText( - ...parts: string[] -): Promise { +export async function readText(...parts: string[]): Promise { const path = joinPath(...parts) if (await exists(path)) return await readFile(path, 'utf-8') } -export async function writeText(path: string, content: string): Promise { +export async function writeText(path: string, content: string, createFolders = false): Promise { + if (createFolders) + await ensureFolderForFile(path) await writeFile(path, content, 'utf-8') } +export async function writeData(path: string, content: object | Uint8Array, createFolders = false): Promise { + if (createFolders) + await ensureFolderForFile(path) + const data = isUint8Array(content) ? content : JSON.stringify(content) + await writeFile(path, data) +} + // todo: writeBinary, readBinary From 4583f51e671a86a80f14cee533030fc29bfd4f1b Mon Sep 17 00:00:00 2001 From: Dirk Holtwick Date: Mon, 8 Jan 2024 21:35:54 +0100 Subject: [PATCH 3/3] 0.16.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 453c49cd..7868524e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.16.3", + "version": "0.16.4", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick",