Skip to content

Commit

Permalink
feat: added getBase64 and formatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
codebyaadi committed Sep 3, 2024
1 parent 0ccd48a commit 20c1655
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ on:
push:
branches: [main]
paths:
- "docs/**"
- "package.json" # for versioning
- 'docs/**'
- 'package.json' # for versioning

# leave empty, we just want the button
workflow_dispatch:
Expand Down
10 changes: 5 additions & 5 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { defineConfig } from 'vitepress'
import { name } from '../../package.json';
import { name } from '../../package.json'

const isProd = process.env.NODE_ENV === 'production';
const isProd = process.env.NODE_ENV === 'production'

// https://vitepress.dev/reference/site-config
export default defineConfig({
title: "Utilstash",
description: "A utility package for various common tasks",
title: 'Utilstash',
description: 'A utility package for various common tasks',
themeConfig: {
// https://vitepress.dev/reference/default-theme-config
nav: [
Expand All @@ -28,5 +28,5 @@ export default defineConfig({
{ icon: 'github', link: 'https://github.com/vuejs/vitepress' }
]
},
base: isProd ? `/${name}/` : undefined,
base: isProd ? `/${name}/` : undefined
})
6 changes: 6 additions & 0 deletions docs/api-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ const { theme, page, frontmatter } = useData()
## Results

### Theme Data

<pre>{{ theme }}</pre>

### Page Data

<pre>{{ page }}</pre>

### Page Frontmatter

<pre>{{ frontmatter }}</pre>
```

Expand All @@ -36,12 +39,15 @@ const { site, theme, page, frontmatter } = useData()
## Results

### Theme Data

<pre>{{ theme }}</pre>

### Page Data

<pre>{{ page }}</pre>

### Page Frontmatter

<pre>{{ frontmatter }}</pre>

## More
Expand Down
5 changes: 2 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
layout: home

hero:
name: "Utilstash"
text: "A utility package for various common tasks"
name: 'Utilstash'
text: 'A utility package for various common tasks'
tagline: Streamline your development with a versatile toolkit of utilities.
actions:
- theme: brand
Expand All @@ -22,4 +22,3 @@ features:
- title: Optimized for Performance
details: Benefit from optimized and efficient functions that ensure smooth and reliable operation.
---

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
"peerDependencies": {
"typescript": "^5.0.0"
}
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export { getFileDataURI } from '@/utils/get-file-data-uri'
export { validateFile } from '@/utils/validate-file'
export { getFileExtension } from '@/utils/get-file-extension'
export { formatFileSize } from '@/utils/format-file-size'
export { getBase64 } from '@/utils/get-base64'
44 changes: 44 additions & 0 deletions src/utils/get-base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const isBrowser =
typeof window !== 'undefined' && typeof window.document !== 'undefined'

export const getBase64 = (
file: File | Buffer,
mimeType?: string
): Promise<string> => {
if (isBrowser) {
return new Promise((resolve, reject) => {
if (!(file instanceof File)) {
reject(
new Error(
'Invalid file type for browser environment. Expected a File object.'
)
)
return
}

const reader = new FileReader()
reader.onloadend = () => {
const base64File = reader.result as string
resolve(`data:${file.type};base64,${base64File.split(',')[1]}`)
}
reader.onerror = () => reject(new Error('Error reading file'))

reader.readAsDataURL(file)
})
} else {
return new Promise((resolve, reject) => {
if (!(file instanceof Buffer)) {
reject(
new Error(
'Invalid file type for Node.js environment. Expected a Buffer object.'
)
)
return
}

const base64File = file.toString('base64')
const mime = mimeType || 'application/octet-stream' // Provide a default MIME type if not provided
resolve(`data:${mime};base64,${base64File}`)
})
}
}

0 comments on commit 20c1655

Please sign in to comment.