npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

client-side-sanitizer

v1.2.2

Published

A lightweight, real-time client-side input sanitizer for React and Express.

Readme

🛡️Client Side Sanitizer

A lightweight, high-performance utility for real-time input sanitization, primarily designed for controlled React components. This package instantly sanitizes user input using regular expressions, preventing XSS injection and ensuring a clean UI state. Its key strength is providing the data necessary to fix the common cursor jump problem in React forms after state updates.


✨ Features

Real-time XSS Prevention: Instantly removes blacklisted characters (<, >, ", ', &, /) or strictly whitelists characters based on the configuration.

Configurable Presets: Default rules for text, number, email, and url can be globally overridden or extended by the user using setPresets().

Caret Position Fix: Returns metadata required for precise cursor position management in controlled inputs, preventing the cursor from jumping to the end.

Zero Dependencies: Extremely lightweight and fast.


💾 Installation

Install the package using npm:

npm install client-side-sanitizer

🛠 Usage & Integration

The package exports two functions: sanitizeInput (the core logic) and setPresets (for global configuration).

1. Global Configuration (setPresets)

Before using the sanitizer, you can globally override the default rules for presets like number or text. This should be done once in your application's entry file (e.g., src/main.jsx or src/App.jsx).

File: src/main.jsx (or src/App.jsx)

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';

// 1. Import the configuration tool
import { setPresets } from 'client-side-sanitizer'; 

// === GLOBAL SANITIZER CONFIGURATION ===
// Call setPresets ONCE to globally override or extend the default rules.
setPresets({
    // Override the default 'number' rule to allow decimals (period) and commas.
    'number': /[^0-9.,]/g, 
    
    // Override the default 'text' rule to be very strict: only allow letters and spaces.
    'text': /[^a-zA-Z\s]/g,
    
    // Add a brand new, custom preset for zip codes.
    'zipcode': /[^0-9-]/g 
});

ReactDOM.createRoot(document.getElementById('root')).render(
    <React.StrictMode>
        <App />
    </React.StrictMode>
);

2. Using the Sanitizer in a React Component

Use sanitizeInput within your component's change handler to get the clean value and the removedCount needed for cursor fixation.

File: SafeInputComponent.jsx

import React, { useState, useCallback, useRef, useEffect } from 'react';
// Import 'sanitizeInput' from the package 'client-side-sanitizer'
// import { sanitizeInput } from 'client-side-sanitizer';

const SafeInputComponent = () => {
    const [formState, setFormState] = useState({
        username: '',
        age: '',
        productCode: '',
        testText: '',
        testNumber: '',
        testEmail: '',
        testUrl: ''
    });

    const [cursorState, setCursorState] = useState({ name: null, position: 0 });
    const inputRefs = useRef({}); 
    
    const setInputRef = useCallback((el) => {
        if (el && el.name) inputRefs.current[el.name] = el;
    }, []);

    const handleInputChange = useCallback((e, config) => {
        const inputElement = e.target;
        const rawValue = inputElement.value;
        const fieldName = inputElement.name;

        const currentCaret = inputElement.selectionStart;
        const { safeValue, removedCount } = sanitizeInput(rawValue, config);

        setFormState(prev => ({ ...prev, [fieldName]: safeValue }));
        setCursorState({ name: fieldName, position: currentCaret - removedCount });
    }, []);

    useEffect(() => {
        if (cursorState.name) {
            const input = inputRefs.current[cursorState.name];
            if (input) {
                input.focus(); 
                input.selectionStart = cursorState.position;
                input.selectionEnd = cursorState.position;
            }
        }
    }, [cursorState]); 

    return (
        <div style={{ padding: '20px', fontFamily: 'Arial, sans-serif' }}>
            <form>
                {/* --- Testing Global Presets --- */}
                <h2>A. Global Preset Behavior</h2>

                {/* 1. Username (Preset: 'text') */}
                <div style={{ marginBottom: '15px' }}>
                    <label style={{ display: 'block', fontWeight: 'bold' }}>1. Username (Preset: 'text'):</label>
                    <input ref={setInputRef} name="username" type="text" value={formState.username}
                        onChange={(e) => handleInputChange(e, 'text')}
                        placeholder="Type: <script>Test 123 !@#"
                        style={{ padding: '8px', width: '300px' }}
                    />
                </div>

                {/* 2. Age (Preset: 'number') */}
                <div style={{ marginBottom: '15px' }}>
                    <label style={{ display: 'block', fontWeight: 'bold' }}>2. Age (Preset: 'number'):</label>
                    <input ref={setInputRef} name="age" type="text" value={formState.age}
                        onChange={(e) => handleInputChange(e, 'number')}
                        placeholder="Try: 12,345.67abc"
                        style={{ padding: '8px', width: '300px' }}
                    />
                </div>
                
                {/* 3. Product Code (Preset: 'productCode') */}
                <div style={{ marginBottom: '15px' }}>
                    <label style={{ display: 'block', fontWeight: 'bold' }}>3. Product Code (Preset: 'productCode'):</label>
                    <input ref={setInputRef} name="productCode" type="text" value={formState.productCode}
                        onChange={(e) => handleInputChange(e, 'productCode')}
                        placeholder="Try: ab-123 (Uses new rule from App.jsx)"
                        style={{ padding: '8px', width: '300px' }}
                    />
                </div>
                
                <hr style={{margin: '30px 0'}}/>

                {/* --- Testing Custom Allow Prop --- */}
                <h2>B. Custom Whitelisting (Overrides Presets)</h2>

                {/* 4. Custom Allow Field */}
                <div style={{ marginBottom: '15px' }}>
                    <label style={{ display: 'block', fontWeight: 'bold' }}>4. Custom (Config: allow: '0-9./'):</label>
                    <input ref={setInputRef} name="testNumber" type="text" value={formState.testNumber}
                        onChange={(e) => handleInputChange(e, { allow: '0-9./' })}
                        placeholder="Only allows digits, period, and slash."
                        style={{ padding: '8px', width: '300px' }}
                    />
                </div>
                
            </form>
        </div>
    );
};

export default SafeInputComponent;