@xiping/utils
TypeScript icon, indicating that this package has built-in type declarations

0.0.36 • Public • Published

@xiping/utils

A collection of utility functions for image manipulation, DOM operations, and data validation.

Installation

npm install @xiping/utils

or

yarn add @xiping/utils

or

pnpm install @xiping/utils

Features

  • Image manipulation (resize, flip, rotate)
  • Base64 conversion utilities
  • DOM utilities
  • File handling utilities

API Reference

Image Utilities

imageUrlToBase64(url: string): Promise<{ success: boolean; data: string }>

Converts an image URL to a base64 encoded string.

const result = await imageUrlToBase64('https://example.com/image.jpg');
if (result.success) {
  console.log(result.data); // base64 string
}

resizeImage({ file: File, width: number, height: number }): Promise<{ success: boolean; file?: File }>

Resizes an image file to specified dimensions.

const result = await resizeImage({
  file: imageFile,
  width: 800,
  height: 600
});
if (result.success) {
  console.log(result.file); // compressed file
}

flipImage(image: string, direction: FlipDirection): Promise<string>

Flips an image horizontally or vertically.

const flippedImage = await flipImage(imageSource, FlipDirection.HORIZONTAL);

rotateImage(image: string, angle: number): Promise<string>

Rotates an image by a specified angle.

const rotatedImage = await rotateImage(imageSource, 90);

DOM Utilities

scrollToBottom(element: HTMLElement): void

Smoothly scrolls an HTML element to its bottom.

const element = document.querySelector('.chat-container');
scrollToBottom(element);

autoScroll(element: HTMLElement, step?: number): () => void

Creates a continuous smooth scrolling effect that automatically scrolls an element up and down. Returns a function to stop the scrolling.

const element = document.querySelector('.scroll-container');
// Start auto-scrolling with 2px step
const stopScroll = autoScroll(element, 2);

// Stop scrolling when needed
stopScroll();

Data Utilities

base64ToBlob(base64: string, mimeType: string): Blob

Converts a base64 encoded string to a Blob object.

const blob = base64ToBlob(base64String, 'image/png');

isBase64(value: any, options?: IsBase64Options): boolean

Validates if a value is a valid base64 encoded string.

const isValid = isBase64(someString, {
  allowEmpty: false,
  allowMime: true,
  mimeRequired: false,
  paddingRequired: true
});

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @xiping/utils

Weekly Downloads

123

Version

0.0.36

License

MIT

Unpacked Size

18.7 kB

Total Files

5

Last publish

Collaborators

  • xiping.wang