Skip to content

Commit

Permalink
Merge branch 'release/v0.24.14'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Sep 5, 2024
2 parents 00b23e7 + 1d4c948 commit 03a415d
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 27 deletions.
5 changes: 4 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default antfu(
},
{},
{
rules: eslintRulesDefaults(),
rules: {
...eslintRulesDefaults(),
'unicorn/consistent-function-scoping': 'off',
},
},
)
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.24.13",
"version": "0.24.14",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down Expand Up @@ -69,18 +69,18 @@
"watch": "nr build -- --watch src"
},
"devDependencies": {
"@antfu/eslint-config": "^2.27",
"@antfu/ni": "^0.22.4",
"@types/node": "^22.5.0",
"@vitejs/plugin-vue": "^5.1.2",
"@antfu/eslint-config": "^3.2",
"@antfu/ni": "^0.23.0",
"@types/node": "^22.5.4",
"@vitejs/plugin-vue": "^5.1.3",
"@vitest/browser": "^2.0.5",
"@vitest/coverage-v8": "^2.0.5",
"esbuild": "^0.23.1",
"eslint": "^9",
"playwright": "^1.46.1",
"tsup": "^8.2.4",
"typescript": "^5.5.4",
"vite": "^5.4.2",
"vite": "^5.4.3",
"vitest": "^2.0.5"
},
"pnpm": {
Expand Down
12 changes: 4 additions & 8 deletions src/browser/localstorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,13 @@ export class LocalStorage<T = Json> implements ObjectStorage<T> {
}

clear(): void {
Object.keys(localStorage)
.filter(key => key.startsWith(this.prefix))
.forEach((key) => {
localStorage.removeItem(key)
})
Object.keys(localStorage).filter(key => key.startsWith(this.prefix)).forEach((key) => {
localStorage.removeItem(key)
})
}

allKeys(): string[] {
const prefixLength = this.prefix.length
return Object.keys(localStorage)
.filter(key => key.startsWith(this.prefix))
.map(key => key.substr(prefixLength))
return Object.keys(localStorage).filter(key => key.startsWith(this.prefix)).map(key => key.substr(prefixLength))
}
}
2 changes: 1 addition & 1 deletion src/common/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const log = DefaultLogger('crypto.spec')
describe('crypto', () => {
it('should not have collisions', () => {
expect(equalBinary(new Uint8Array(2), new Uint8Array([0, 0]))).toBe(true)
const list: Uint8Array[] = Array.apply(null, Array(100)).map(() =>
const list: Uint8Array[] = Array.apply(null, Array.from({ length: 100 })).map(() =>
randomUint8Array(8),
)
let id: Uint8Array | undefined
Expand Down
4 changes: 4 additions & 0 deletions src/common/data/camelcase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ kebab-case
PascalCase // I won't be using this here, but it's only one small step further if you want to use it
*/

/// Convert a string to camelCase from snake_case, kebab-case or pascalCase
export function toCamelCase(s: string) {
// if only capital letters, convert to lower case
if (s.length > 0) {
Expand All @@ -25,10 +26,12 @@ export function toCamelCase(s: string) {
return s
}

/// Capitalize the first letter of a string.
export function toCapitalize(s: string) {
return s.charAt(0).toUpperCase() + s.toLowerCase().slice(1)
}

/// Capitalize the first letter of each word in a string.
export function toCapitalizeWords(s: string) {
return s.replace(/\w\S*/g, toCapitalize)
}
Expand All @@ -41,6 +44,7 @@ export function toCapitalizeWords(s: string) {
// .replace(/\s+/g, "")
// }

/// Convert a string from camelCase to snake_case or kebab-case
export function fromCamelCase(str: string, separator = '-') {
separator = typeof separator === 'undefined' ? '_' : separator
return str
Expand Down
7 changes: 1 addition & 6 deletions src/common/dispose-defer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ function callDisposer(disposable: Disposer): Promise<void> | void {
result = disposable.dispose()
else if (isPromise(disposable.dispose))
result = disposable.dispose
// else if (typeof disposable.cleanup === 'function')
// result = disposable.cleanup()
// else if (isPromise(disposable.cleanup))
// result = disposable.cleanup

if (isPromise(result))
return result
Expand All @@ -44,8 +40,7 @@ export interface UseDisposeConfig {
log?: LoggerInterface
}

export function useDispose(config?: string | UseDisposeConfig | LoggerInterface) {
let opt = config as any
export function useDispose(opt?: string | UseDisposeConfig | LoggerInterface) {
if (opt != null) {
if (isString(opt))
opt = { name: opt }
Expand Down
2 changes: 1 addition & 1 deletion src/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function getGlobal(): any {
: typeof global !== 'undefined'
? global
// eslint-disable-next-line no-new-func
: Function('return this;')()
: new Function('return this;')()
}

/** @deprecated */
Expand Down
2 changes: 1 addition & 1 deletion src/common/schema/rpc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { func, object, string } from './schema'
import type { Infer, Type } from './schema'
import type { Infer } from './schema'

// see https://github.com/colinhacks/zod?tab=readme-ov-file#functions

Expand Down
51 changes: 50 additions & 1 deletion src/common/time.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sleep } from './exec'
import { datetimeToLocal, datetimeToUTC, duration, parseDate } from './time'
import { dateFromSeconds, datetimeToLocal, datetimeToUTC, duration, formatMilliseconds, getPerformanceTimestamp, getTimestamp, getTimestampInSeconds, parseDate } from './time'

describe('time.spec', () => {
it('should measure', async () => {
Expand Down Expand Up @@ -36,4 +36,53 @@ describe('time.spec', () => {
// expect(datetimeToLocal(dateLocal)).toMatchInlineSnapshot(`2022-01-01T10:00:00.000Z`)
expect(datetimeToLocal(datetimeToUTC(dateLocal))).toEqual(dateLocal)
})

it('should return the current timestamp in milliseconds', () => {
const timestamp = getTimestamp()
expect(typeof timestamp).toBe('number')
expect(timestamp).toBeCloseTo(Date.now(), -2)
})

it('should return the current timestamp in seconds', () => {
const timestampInSeconds = getTimestampInSeconds()
expect(typeof timestampInSeconds).toBe('number')
expect(timestampInSeconds).toBeCloseTo(Math.floor(Date.now() / 1000), -2)
})

it('should convert seconds to a Date object', () => {
const seconds = 1640995200 // January 1, 2022 00:00:00 UTC
const date = dateFromSeconds(seconds)
expect(date).toEqual(new Date('2022-01-01T00:00:00.000Z'))
})

it('should format milliseconds', () => {
const milliseconds = 1234
const formatted = formatMilliseconds(milliseconds)
expect(formatted).toBe('1.2 s')
})

it('should return the current timestamp using performance.now if available', () => {
const timestamp = getPerformanceTimestamp()
expect(typeof timestamp).toBe('number')
expect(timestamp).toBeCloseTo(typeof performance !== 'undefined' ? performance.now() : Date.now(), -2)
})

it('should measure the duration of a function', async () => {
const getDuration = duration()
await sleep(50)
const elapsed = getDuration()
expect(/\d+.\d\d ms/.test(elapsed)).toBe(true)
})

// it('should convert a date to local time', () => {
// const date = new Date('2022-01-01T12:00:00')
// const localDate = datetimeToLocal(date)
// expect(localDate).toEqual(new Date('2022-01-01T12:00:00'))
// })

it('should convert a date to UTC time', () => {
const date = new Date('2022-01-01T12:00:00')
const utcDate = datetimeToUTC(date)
expect(utcDate).toEqual(new Date('2022-01-01T12:00:00Z'))
})
})
15 changes: 15 additions & 0 deletions src/common/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
export function getTimestamp(): number {
return Date.now()
}

/**
* @returns Timestamp in seconds
*/
export function getTimestampInSeconds(): number {
return Math.floor(Date.now() / 1000)
}

/**
* @returns Timestamp in seconds
*/
export function dateFromSeconds(ts: number): Date {
return new Date(ts * 1000)
}

// typeof performance !== "undefined" ? performance.now() : new Date().getTime()

export function formatMilliseconds(ms: number): string {
Expand Down
4 changes: 2 additions & 2 deletions src/common/uuid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('uuid', () => {
})

it('should not have collisions', () => {
const list = Array.apply(null, Array(100)).map(uuid)
const list = Array.apply(null, Array.from({ length: 100 })).map(uuid)
while (list.length) {
const id = list.pop()
expect(id?.length).toBe(22)
Expand All @@ -29,7 +29,7 @@ describe('uuid', () => {
})

it('should not have collisions v4', () => {
const list = Array.apply(null, Array(100)).map(uuidv4)
const list = Array.apply(null, Array.from({ length: 100 })).map(uuidv4)
while (list.length) {
const id = list.pop()
expect(id?.length).toBe(36)
Expand Down
38 changes: 38 additions & 0 deletions src/node/files.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from 'node:path'
import { files, walkSync } from './files'

describe('files.spec', () => {
Expand Down Expand Up @@ -49,4 +50,41 @@ describe('files.spec', () => {
]
`)
})

it('should fetch some subfolders', async () => {
const basePath = resolve(__dirname, '..')
expect(basePath).toMatchInlineSnapshot(`"/Users/dirk/work/github-zeed/src"`)

Check failure on line 56 in src/node/files.spec.ts

View workflow job for this annotation

GitHub Actions / build

src/node/files.spec.ts > files.spec > should fetch some subfolders

Error: Snapshot `files.spec > should fetch some subfolders 1` mismatched Expected: ""/Users/dirk/work/github-zeed/src"" Received: ""/home/runner/work/zeed/zeed/src"" ❯ src/node/files.spec.ts:56:22

const result = files({
basePath,
pattern: '*/log-*.ts',
})

expect(result).toMatchInlineSnapshot(`
Array [
"browser/log/log-browser-factory.ts",
"browser/log/log-browser.ts",
"browser/log/log-colors.ts",
"browser/log/log-context-browser.ts",
"common/log/log-base.ts",
"common/log/log-colors.ts",
"common/log/log-config.ts",
"common/log/log-console-capture.ts",
"common/log/log-console-original.ts",
"common/log/log-console.ts",
"common/log/log-context.ts",
"common/log/log-filter.spec.ts",
"common/log/log-filter.ts",
"common/log/log-memory.spec.ts",
"common/log/log-memory.ts",
"node/log/log-context-node.ts",
"node/log/log-file.spec.ts",
"node/log/log-file.ts",
"node/log/log-node.spec.ts",
"node/log/log-node.ts",
"node/log/log-util.spec.ts",
"node/log/log-util.ts",
]
`)
})
})
5 changes: 5 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ if (isBrowser || isPreview) {
'./src/browser/**/*.{client,test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
'./src/common/**/*.{client,test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
],
exclude: [
'**/_archive/**',
'**/demos/**',
'**/docs/**',
],
browser: {
enabled: true,
name: browserName,
Expand Down

0 comments on commit 03a415d

Please sign in to comment.