diff --git a/package.json b/package.json index 453c49c..7868524 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", diff --git a/src/common/bin/lib0/decoding.ts b/src/common/bin/lib0/decoding.ts index 9d86a7e..fe9183f 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 efff80e..499aaab 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 af8742d..bf226f8 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 fb7bdcc..4a68d48 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 b556064..d973e9f 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 diff --git a/src/node/fs.ts b/src/node/fs.ts index ede436b..7d0edd5 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