Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"docs:dev": "vitepress dev packages/docs",
"docs:build": "vitepress build packages/docs",
"docs:preview": "vitepress preview packages/docs",
"ci-docs": "nr docs:clean && tsno run scripts/ci-docs.ts"
"ci-docs": "nr docs:clean && tsno run scripts/ci-docs.ts",
"ci": "tsno run scripts/ci-docs.ts"
},
"dependencies": {
"dayjs": "^1.11.7",
Expand All @@ -62,12 +63,14 @@
"@antfu/ni": "^0.21.3",
"@antfu/utils": "^0.7.2",
"@rollup/plugin-typescript": "^11.1.0",
"@types/fs-extra": "^11.0.4",
"@types/node": "^18.16.5",
"@use-kit/node": "^0.0.1",
"bumpp": "^9.1.0",
"eslint": "^8.40.0",
"esno": "^0.16.3",
"fast-glob": "^3.2.12",
"fs-extra": "^11.2.0",
"lint-staged": "^13.2.2",
"mkdirp": "^3.0.1",
"pnpm": "^7.32.3",
Expand Down
15 changes: 15 additions & 0 deletions packages/browser/speak/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
category: '@Browser'
---

# speak

speaks a message.

## Usage

```ts
import { speak } from '@use-kit/functions'

speak('Hi!')
```
25 changes: 25 additions & 0 deletions packages/browser/speak/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const speak = (
text: string,
lang = 'en-US',
rate = 1.0,
pitch = 1.0,
volume = 1.0) => {
if (typeof window !== 'undefined' && 'speechSynthesis' in window) {
try {
const utterance = new SpeechSynthesisUtterance(text)
utterance.lang = lang
utterance.rate = rate
utterance.pitch = pitch
utterance.volume = volume
window.speechSynthesis.speak(utterance)
return true
}
catch (error) {
return false
}
}
else {
console.warn('Speech synthesis is not supported in this environment.')
return false
}
}
23 changes: 23 additions & 0 deletions packages/core/cacheField/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
category: '@Core'
---

# cacheField

Cache a field value.

## Usage

<!-- ```ts
import { cacheField } from '@vueuse/core'

const { value, set } = cacheField()
set('foo', 'bar')
value.value // 'bar'
``` -->

```ts
import { cacheField } from '@use-kit/functions'

const ret = await cacheField('a', 'test')
```
18 changes: 18 additions & 0 deletions packages/core/cacheField/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it, vi } from 'vitest'
import { cacheField } from '.'

describe('cacheField', () => {
it('should cache the result of the provider function', async () => {
const provider = vi.fn(() => 'test')
const result = await cacheField('test', provider)
expect(result).toBe('test')
expect(provider).toHaveBeenCalledTimes(1)
})

it('should return the cached result if the field already exists', async () => {
const provider = vi.fn(() => 'test2')
const ret = await cacheField('test', provider)
expect(ret).toBe('test')
expect(provider).toHaveBeenCalledTimes(0)
})
})
32 changes: 32 additions & 0 deletions packages/core/cacheField/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
let cache: { [key: string]: any } = {}

export async function cacheField(
field: string,
provider: () => Promise<any> | any,
): Promise<any> {
if (cache[field])
return cache[field]

if (typeof provider === 'function') {
const result = await provider()
cache[field] = result
return result
}

cache[field] = provider
return provider
}

export function clearCache(field?: string): boolean {
if (!field) {
cache = {}
return true
}

if (!cache[field])
return false

// delete cache[field]
cache[field] = undefined
return true
}
16 changes: 15 additions & 1 deletion packages/core/isValidJSON/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
// TODO:
---
category: '@Core'
---

# isValidJSON

Validates if a string is a valid JSON.

## Usage

```ts
import { isValidJSON } from '@use-kit/functions'

isValidJSON('{ "foo": "bar" }') // true
```
2 changes: 1 addition & 1 deletion packages/core/mergeField/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('merge field', () => {
it('should', () => {
const target = { a: 'a', d: 'd' }
const source = { a: 'b', b: 'b', c: 'c' }
const ret = mergeField(target, source)
mergeField(target, source)
expect(target).toMatchInlineSnapshot(`
{
"a": "b",
Expand Down
8 changes: 4 additions & 4 deletions packages/core/replaceTreeFields/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
interface TreeNode<T> {
value: T
children: TreeNode<T>[]
}
// interface TreeNode<T> {
// value: T
// children: TreeNode<T>[]
// }

export const replaceTreeFields = (tree: Record<string, unknown>[], fields: Record<string, string>) => {
const newTree = tree.map((node) => {
Expand Down
21 changes: 21 additions & 0 deletions packages/core/stringByteSize/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
// TODO:
---
category: '@Core'
---

# stringByteSize

Checks if a string is of a certain length.

## Usage

```ts
import { stringByteSize } from '@use-kit/functions'

stringByteSize('') // 0
stringByteSize('a') // 1
stringByteSize('ab') // 2
stringByteSize('abc') // 3
stringByteSize('abcd') // 4
stringByteSize('abcde') // 5
stringByteSize('😜') // 4
```
15 changes: 15 additions & 0 deletions packages/docs/browser/speak/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
category: '@Browser'
---

# speak

speaks a message.

## Usage

```ts
import { speak } from '@use-kit/functions'

speak('Hi!')
```
23 changes: 23 additions & 0 deletions packages/docs/core/cacheField/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
category: '@Core'
---

# cacheField

Cache a field value.

## Usage

<!-- ```ts
import { cacheField } from '@vueuse/core'

const { value, set } = cacheField()
set('foo', 'bar')
value.value // 'bar'
``` -->

```ts
import { cacheField } from '@use-kit/functions'

const ret = await cacheField('a', 'test')
```
16 changes: 15 additions & 1 deletion packages/docs/core/isValidJSON/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
// TODO:
---
category: '@Core'
---

# isValidJSON

Validates if a string is a valid JSON.

## Usage

```ts
import { isValidJSON } from '@use-kit/functions'

isValidJSON('{ "foo": "bar" }') // true
```
21 changes: 21 additions & 0 deletions packages/docs/core/stringByteSize/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
// TODO:
---
category: '@Core'
---

# stringByteSize

Checks if a string is of a certain length.

## Usage

```ts
import { stringByteSize } from '@use-kit/functions'

stringByteSize('') // 0
stringByteSize('a') // 1
stringByteSize('ab') // 2
stringByteSize('abc') // 3
stringByteSize('abcd') // 4
stringByteSize('abcde') // 5
stringByteSize('😜') // 4
```
Loading
Loading