Skip to content

Commit

Permalink
Merge branch 'release/v0.16.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Jan 8, 2024
2 parents 0a9ee53 + 4583f51 commit 6e9be46
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.16.3",
"version": "0.16.4",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
2 changes: 1 addition & 1 deletion src/common/bin/lib0/decoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/common/data/orderby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/common/data/sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/common/data/sorted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function useSorted<S extends Record<string, any>>(
// 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
Expand Down
4 changes: 1 addition & 3 deletions src/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 22 additions & 5 deletions src/node/fs.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<string> {
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<string> {
const path = dirname(joinPath(...parts))
if (!(await exists(path)))
await mkdir(path, { recursive: true })
return path
}

Expand All @@ -41,16 +51,23 @@ export async function removeFolder(...parts: string[]): Promise<string> {
return path
}

export async function readText(
...parts: string[]
): Promise<string | undefined> {
export async function readText(...parts: string[]): Promise<string | undefined> {
const path = joinPath(...parts)
if (await exists(path))
return await readFile(path, 'utf-8')
}

export async function writeText(path: string, content: string): Promise<void> {
export async function writeText(path: string, content: string, createFolders = false): Promise<void> {
if (createFolders)
await ensureFolderForFile(path)
await writeFile(path, content, 'utf-8')
}

export async function writeData(path: string, content: object | Uint8Array, createFolders = false): Promise<void> {
if (createFolders)
await ensureFolderForFile(path)
const data = isUint8Array(content) ? content : JSON.stringify(content)
await writeFile(path, data)
}

// todo: writeBinary, readBinary

0 comments on commit 6e9be46

Please sign in to comment.