Skip to content

Commit

Permalink
Merge branch 'release/v0.16.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Jan 23, 2024
2 parents 6e9be46 + e6e1b70 commit 0a7fa1a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.16.4",
"version": "0.16.5",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down Expand Up @@ -65,15 +65,15 @@
"watch": "nr build -- --watch"
},
"devDependencies": {
"@antfu/eslint-config": "^2.6.1",
"@antfu/eslint-config": "^2.6.3",
"@antfu/ni": "^0.21.12",
"@types/node": "^20.10.6",
"@vitest/coverage-v8": "^1.1.3",
"esbuild": "^0.19.11",
"@types/node": "^20.11.5",
"@vitest/coverage-v8": "^1.2.1",
"esbuild": "^0.19.12",
"eslint": "^8.56.0",
"tsup": "^8.0.1",
"typescript": "^5.3.3",
"vite": "^5.0.11",
"vitest": "^1.1.3"
"vite": "^5.0.12",
"vitest": "^1.2.1"
}
}
8 changes: 4 additions & 4 deletions src/browser/log/log-colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ export function browserSupportsColors(): boolean {
// explicitly
if (
typeof window !== 'undefined'
&& window.process
&& window.process
// @ts-expect-error xxx
&& (window.process.type === 'renderer' || window.process.__nwjs)
&& (window.process.type === 'renderer' || window.process.__nwjs)
)
return true

// Internet Explorer and Edge do not support colors.
if (
typeof navigator !== 'undefined'
&& navigator.userAgent
&& navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)
&& navigator.userAgent
&& navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)
)
return false

Expand Down
14 changes: 12 additions & 2 deletions src/common/data/format.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { formatBytesToHuman, formatSecondsToTime } from './format'
import { formatBytesToHuman, formatBytesToHumanBase1000, formatSecondsToTime } from './format'

describe('format.spec', () => {
it('should format bytes', async () => {
it('should format bytes in 1024 steps', async () => {
expect(formatBytesToHuman(123)).toMatchInlineSnapshot('"123 bytes"')
expect(formatBytesToHuman(16 * 1024)).toMatchInlineSnapshot('"16.00 KiB"')
expect(formatBytesToHuman(16 * 1024 * 1024)).toMatchInlineSnapshot('"16.00 MiB"')
Expand All @@ -11,6 +11,16 @@ describe('format.spec', () => {
expect(formatBytesToHuman(0)).toMatchInlineSnapshot('"0 bytes"')
})

it('should format bytes in 1000 steps', async () => {
expect(formatBytesToHumanBase1000(123)).toMatchInlineSnapshot('"123 bytes"')
expect(formatBytesToHumanBase1000(16 * 1000)).toMatchInlineSnapshot(`"16.00 KB"`)
expect(formatBytesToHumanBase1000(16 * 1024 * 1024)).toMatchInlineSnapshot(`"16.78 MB"`)
expect(formatBytesToHumanBase1000(16 * 1024 + 123)).toMatchInlineSnapshot(`"16.51 KB"`)
expect(formatBytesToHumanBase1000(16 * 1024 + 123, 0)).toMatchInlineSnapshot(`"17 KB"`)
expect(formatBytesToHumanBase1000(-123)).toMatchInlineSnapshot('"0 bytes"')
expect(formatBytesToHumanBase1000(0)).toMatchInlineSnapshot('"0 bytes"')
})

it('should format time', async () => {
expect(formatSecondsToTime(0)).toMatchInlineSnapshot('"0:00"')
expect(formatSecondsToTime(-123)).toMatchInlineSnapshot('"-2:03"')
Expand Down
20 changes: 19 additions & 1 deletion src/common/data/format.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** Just a simple yet fast helper. Alternatively you may use Intl formatters http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat */
export function formatBytesToHuman(bytes: number, decimals = 2) {
export function formatBytesToHumanBase1024(bytes: number, decimals = 2) {
// https://en.wikipedia.org/wiki/Orders_of_magnitude_(data)
const units = ['bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'] // etc
const c = 1 / 1023 // change it to 1024 and see the diff
Expand All @@ -12,7 +12,25 @@ export function formatBytesToHuman(bytes: number, decimals = 2) {
if (h >= c)
break
}
return (`${(1 / h).toFixed(i > 0 ? decimals : 0).toLocaleString()} ${units[i]}`)
}

export const formatBytesToHuman = formatBytesToHumanBase1024

/** Just a simple yet fast helper. Alternatively you may use Intl formatters http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat */
export function formatBytesToHumanBase1000(bytes: number, decimals = 2) {
// https://en.wikipedia.org/wiki/Orders_of_magnitude_(data)
const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'] // etc
const c = 1 / 999
let i = 0
let h = 0
if (bytes < 0)
bytes = 0
for (; h < c && i < units.length; i++) {
h = 1000 ** i / bytes
if (h >= c)
break
}
return (`${(1 / h).toFixed(i > 0 ? decimals : 0).toLocaleString()} ${units[i]}`)
}

Expand Down
6 changes: 1 addition & 5 deletions src/common/data/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { isArray, isObject } from './is'
/** Like `.map()` for object. Return new key and value or `undefined` to delete. */
export function objectMap<T = any>(
obj: any,
fn: (
key: string,
value: any
) => [key: string, value: T] | T | undefined | null,
fn: (key: string, value: any) => [key: string, value: T] | T | undefined | null,
): Record<string, T> {
if (!isObject(obj))
return {}
Expand All @@ -16,7 +13,6 @@ export function objectMap<T = any>(
const r = fn(k, v)
if (isArray(r) && r.length === 2)
return r

return [k, r]
})
.filter(v => v != null),
Expand Down
6 changes: 3 additions & 3 deletions src/node/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export function setupWebCrypto() {
try {
if (
typeof globalThis !== 'undefined'
&& typeof globalThis.crypto === 'undefined'
&& nodeCrypto
&& nodeCrypto.webcrypto
&& typeof globalThis.crypto === 'undefined'
&& nodeCrypto
&& nodeCrypto.webcrypto
) {
// @ts-expect-error this is a workaround for node environment
globalThis.crypto = nodeCrypto.webcrypto
Expand Down

0 comments on commit 0a7fa1a

Please sign in to comment.