Skip to content

Commit

Permalink
Merge branch 'release/v0.20.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed May 2, 2024
2 parents 1359dad + 44974a6 commit f7cafff
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.20.2",
"version": "0.20.3",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down Expand Up @@ -65,16 +65,16 @@
"watch": "nr build -- --watch src"
},
"devDependencies": {
"@antfu/eslint-config": "^2.13.0",
"@antfu/eslint-config": "^2.16.1",
"@antfu/ni": "^0.21.12",
"@types/node": "^20.12.6",
"@vitest/coverage-v8": "^1.4.0",
"@types/node": "^20.12.8",
"@vitest/coverage-v8": "^1.5.3",
"esbuild": "^0.20.2",
"eslint": "^9.0.0",
"eslint": "^9.1.1",
"tsup": "^8.0.2",
"typedoc": "^0.25.13",
"typescript": "^5.4.4",
"vite": "^5.2.8",
"vitest": "^1.4.0"
"typescript": "^5.4.5",
"vite": "^5.2.10",
"vitest": "^1.5.3"
}
}
2 changes: 1 addition & 1 deletion src/common/data/json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import jsonParse, { jsonStringifySafe, jsonStringifySorted } from './json'
import { jsonParse, jsonStringifySafe, jsonStringifySorted } from './json'

describe('convert', () => {
it('should jsonStringify', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/common/data/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ function jsonParseTransform(key: string, value: any): any {
return value
}

export default function jsonParse(val: string): any {
/** @deprecated a safe parser? */
export function jsonParse(val: string): any {
if (typeof val !== 'string')
return val

Expand Down
43 changes: 41 additions & 2 deletions src/node/fs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises'
import { dirname, join as joinPath, normalize } from 'node:path'
import process from 'node:process'
import { isUint8Array } from '../common'
import { isUint8Array, jsonStringifySorted, toUint8Array } from '../common'

/** Try to use `~` for HOME folder if possible */
export function toHumanReadableFilePath(path: string) {
Expand Down Expand Up @@ -57,17 +57,56 @@ export async function readText(...parts: string[]): Promise<string | undefined>
return await readFile(path, 'utf-8')
}

export async function readJson<T = object>(...parts: string[]): Promise<T | undefined> {
const content = await readText(...parts)
if (content != null) {
try {
return JSON.parse(content)
}
catch (err) {}
}
}

export async function readBin(...parts: string[]): Promise<Uint8Array | undefined> {
const path = joinPath(...parts)
if (await exists(path))
return toUint8Array(await readFile(path))
}

/** @deprecated use readJson or readBin */
export async function readData(...parts: string[]): Promise<Uint8Array | undefined> {
const path = joinPath(...parts)
if (await exists(path))
return await readFile(path)
}

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

/** @deprecated use writeBin or writeJson */
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
export async function writeBin(path: string, content: Uint8Array, info: {
createFolders?: boolean
} = {}): Promise<void> {
const { createFolders = false } = info
if (createFolders)
await ensureFolderForFile(path)
await writeFile(path, content)
}

export async function writeJson<T = object>(path: string, content: T, info: {
createFolders?: boolean
pretty?: boolean
} = {}): Promise<void> {
const { createFolders = false, pretty = false } = info
await writeText(path, jsonStringifySorted(content, pretty ? 2 : undefined), createFolders)
}

0 comments on commit f7cafff

Please sign in to comment.