react-prize-wheel
Version:
A simple, performant React prize wheel component
119 lines • 4.01 kB
TypeScript
import type { WheelSegment } from '../types';
/**
* Sanitizes text content to prevent XSS attacks and normalize whitespace
*
* @description Removes potentially dangerous characters that could be used for XSS attacks,
* normalizes whitespace by collapsing multiple spaces into single spaces, and limits
* text length to prevent excessive content.
*
* @since 2025-07-25
* @version 1.0.0
*
* @param text - Raw text input to sanitize
* @returns Sanitized and normalized text string (max 100 characters)
*
* @example
* ```typescript
* const clean = sanitizeText(' Hello <script> World ');
* // Returns: "Hello World"
* ```
*/
export declare function sanitizeText(text: string): string;
/**
* Validates if a string represents a valid CSS color value
*
* @description Checks if the provided string is a valid CSS color including:
* - Hex colors (#fff, #ffffff)
* - RGB/RGBA functions (rgb(255,0,0), rgba(255,0,0,0.5))
* - HSL/HSLA functions (hsl(0,100%,50%), hsla(0,100%,50%,0.5))
* - Named colors (red, blue, etc.)
*
* @since 2025-07-25
* @version 1.0.0
*
* @param color - Color string to validate
* @returns True if the color is valid, false otherwise
*
* @example
* ```typescript
* validateColor('#ff0000'); // true
* validateColor('rgb(255,0,0)'); // true
* validateColor('red'); // true
* validateColor('invalid'); // false
* ```
*/
export declare function validateColor(color: string): boolean;
/**
* Validates an array of wheel segments for completeness and correctness
*
* @description Performs comprehensive validation on wheel segments including:
* - Required fields (id, text, color)
* - Color format validation
* - Duplicate ID detection
* - Weight value validation
* - Text content sanitization
*
* @since 2025-07-25
* @version 1.0.0
*
* @param segments - Array of wheel segments to validate
* @returns Object containing validation status and error messages
*
* @example
* ```typescript
* const result = validateSegments([
* { id: '1', text: 'Prize 1', color: '#ff0000' },
* { id: '2', text: 'Prize 2', color: 'invalid-color' }
* ]);
* // Returns: { isValid: false, errors: ['Segment 2: Invalid color format'] }
* ```
*/
export declare function validateSegments(segments: WheelSegment[]): {
isValid: boolean;
errors: string[];
};
/**
* Calculates appropriate contrast color (black or white) for text on a background
*
* @description Uses luminance calculation to determine whether black or white text
* would provide better contrast on the given background color. Assumes hex color input.
*
* @since 2025-07-25
* @version 1.0.0
*
* @param backgroundColor - Hex color string (e.g., '#ff0000')
* @returns '#000000' for light backgrounds, '#ffffff' for dark backgrounds
*
* @example
* ```typescript
* getContrastColor('#ffffff'); // Returns '#000000' (black on white)
* getContrastColor('#000000'); // Returns '#ffffff' (white on black)
* getContrastColor('#ff0000'); // Returns '#ffffff' (white on red)
* ```
*/
export declare function getContrastColor(backgroundColor: string): string;
/**
* Calculates the angle (in degrees) for each segment based on their weights
*
* @description Computes proportional angles for wheel segments based on their weight values.
* Segments with higher weights will occupy larger portions of the wheel. Total angles
* always sum to 360 degrees.
*
* @since 2025-07-25
* @version 1.0.0
*
* @param segments - Array of wheel segments with optional weight properties
* @returns Array of angles in degrees corresponding to each segment
*
* @example
* ```typescript
* const segments = [
* { id: '1', text: 'A', color: '#000', weight: 1 },
* { id: '2', text: 'B', color: '#000', weight: 3 }
* ];
* const angles = calculateSegmentAngles(segments);
* // Returns [90, 270] - segment B is 3x larger than segment A
* ```
*/
export declare function calculateSegmentAngles(segments: WheelSegment[]): number[];
//# sourceMappingURL=validation.d.ts.map