@dasch-ng/web-utils

中文文档
1.0.0-rc.2发布于 2026/11/07MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2136

A collection of utility functions for common web development tasks including SVG conversion, file downloads, and file handling.

原始使用文档
文档目录17 节
  1. Installation
  2. Functions
  3. convertSvgToImage(svgString, format?)
  4. download(data, filename, type?)
  5. createFileArray(files)
  6. parseContentDisposition(header)
  7. fileTypeOf(file)
  8. toSlug(value, { maxLength?, fallback?, expandUmlauts? })
  9. redactUrl(url, { params?, keepHash? })
  10. Usage Patterns
  11. SVG to Image Download
  12. Processing Multiple Files
  13. Export Data as File
  14. Development
  15. Building
  16. Running unit tests
  17. License

@dasch-ng/web-utils

A collection of utility functions for common web development tasks including SVG conversion, file downloads, and file handling.

npm version MIT License

Installation

npm install @dasch-ng/web-utils

Functions

convertSvgToImage(svgString, format?)

Converts an SVG string to a raster image (PNG or JPEG) using HTML5 Canvas.

Parameters:

  • svgString: The SVG markup as a string
  • format (optional): The output image format, either 'png' or 'jpeg'. Defaults to 'png'

Returns: A Promise that resolves to a Blob containing the raster image data, or null if conversion fails

Example:

import { convertSvgToImage } from '@dasch-ng/web-utils';

const svgString = '<svg width="100" height="100"><circle cx="50" cy="50" r="40" fill="red" /></svg>';
const blob = await convertSvgToImage(svgString, 'png');
if (blob) {
  const url = URL.createObjectURL(blob);
  // Use the URL for download or display
}

download(data, filename, type?)

Triggers a browser download for the given data with automatic cleanup of object URLs.

Parameters:

  • data: The data to download, either as a string or Blob
  • filename: The name of the file to be downloaded
  • type (optional): MIME type for the Blob (e.g., 'text/plain', 'application/json')

Returns: A Promise that resolves when the download has been triggered

Example:

import { download } from '@dasch-ng/web-utils';

// Download a text file
await download('Hello, World!', 'greeting.txt', 'text/plain');

// Download JSON data
const data = { name: 'John', age: 30 };
await download(JSON.stringify(data, null, 2), 'data.json', 'application/json');

// Download a Blob (e.g., an image)
const blob = await convertSvgToImage(svgString);
if (blob) {
  await download(blob, 'image.png', 'image/png');
}

createFileArray(files)

Converts a single File or FileList to an array of File objects for easier processing.

Parameters:

  • files: A single File object or a FileList from an input element

Returns: An array containing the File object(s)

Example:

import { createFileArray } from '@dasch-ng/web-utils';

// Handle file input change event
const input = document.querySelector('input[type="file"]') as HTMLInputElement;
input.addEventListener('change', (event) => {
  const files = createFileArray(input.files!);
  files.forEach((file) => {
    console.log(`File: ${file.name}, Size: ${file.size} bytes`);
  });
});

// Works with both single and multiple file inputs
const singleFile = new File(['content'], 'test.txt');
const fileArray = createFileArray(singleFile); // [File]

parseContentDisposition(header)

Parses a Content-Disposition header (RFC 6266), including quoted names and filename*=UTF-8''….

const { filename } = parseContentDisposition(response.headers.get('Content-Disposition')) ?? {};

fileTypeOf(file)

Classifies a file name or File by extension: 'video' | 'image' | 'audio' | 'document' | 'spreadsheet' | 'archive' | null.

toSlug(value, { maxLength?, fallback?, expandUmlauts? })

URL slug of [a-z0-9-], accents removed, umlauts written out, cut at a word boundary: toSlug('Grüne Wälder') → 'gruene-waelder'.

redactUrl(url, { params?, keepHash? })

Removes secret query parameters (SECRET_URL_PARAMS) and the fragment from an http(s) URL before logging it.

Usage Patterns

SVG to Image Download

import { convertSvgToImage, download } from '@dasch-ng/web-utils';

const svgElement = document.querySelector('svg');
const svgString = new XMLSerializer().serializeToString(svgElement);

const blob = await convertSvgToImage(svgString, 'png');
if (blob) {
  await download(blob, 'chart.png', 'image/png');
}

Processing Multiple Files

import { createFileArray, download } from '@dasch-ng/web-utils';

const input = document.querySelector('input[type="file"]') as HTMLInputElement;
input.addEventListener('change', async () => {
  const files = createFileArray(input.files!);

  for (const file of files) {
    const text = await file.text();
    const processed = text.toUpperCase();
    await download(processed, `processed-${file.name}`, 'text/plain');
  }
});

Export Data as File

import { download } from '@dasch-ng/web-utils';

// Export application state as JSON
function exportState(state: object) {
  const json = JSON.stringify(state, null, 2);
  return download(json, 'app-state.json', 'application/json');
}

// Export CSV data
function exportCsv(data: string[][]) {
  const csv = data.map((row) => row.join(',')).join('\n');
  return download(csv, 'data.csv', 'text/csv');
}

Development

Building

Run nx build web-utils to build the library.

Running unit tests

Run nx test web-utils to execute the unit tests via Vitest.

License

MIT

版本与发布记录

@dasch-ng/web-utils 共发布 3 个版本,首次发布于 2025/12/26;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0-rc.2latest2026/11/076 天前
1.0.0-rc.12025/12/3010 个月前
1.0.0-rc.02025/12/2610 个月前

关于 @dasch-ng/web-utils 的常见问题

@dasch-ng/web-utils 是什么?

A collection of utility functions for common web development tasks including SVG conversion, file downloads, and file handling.

如何安装 @dasch-ng/web-utils?

在项目目录运行 npm install @dasch-ng/web-utils,或使用 pnpm add / yarn add / bun add。

@dasch-ng/web-utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dasch-ng/web-utils 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dasch-ng/web-utils 可以商用吗?

@dasch-ng/web-utils 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dasch-ng/web-utils 的下载量有多少?

根据 npm 官方下载统计,@dasch-ng/web-utils 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,136 次,日均约 71 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dasch-ng/web-utils 还在维护吗?

最新版本 1.0.0-rc.2 发布于 2026/11/07(6 天前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/12/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。