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 🙏

© 2025 – Pkg Stats / Ryan Hefner

js-utils-toolkit

v0.1.1

Published

A collection of JavaScript utility functions for state management and general operations

Readme

JS Utils Toolkit

A collection of JavaScript utility functions for state management and general operations.

Installation

Local Installation

npm install js-utils-toolkit

Global Installation

npm install -g js-utils-toolkit

Features

Global State Management

  • Create and manage multiple instances of state
  • Store and update state
  • Event handling (subscribe to state changes)
  • Configuration and validation

General Utilities

  • Object utilities: mapping, filtering, transformation
  • Array utilities: conditional operations, searching
  • String utilities: capitalization, formatting
  • Function utilities: safe function calls, switch-like operations

Usage

In Node.js or bundled applications

import { GlobalState, objIf, safeParse } from 'js-utils-toolkit';

// Create a global state instance
GlobalState.create({
  myApp: {
    user: { name: 'John', role: 'admin' },
    settings: { theme: 'dark' }
  }
});

// Get state instance
const state = GlobalState.get('myApp');

// Subscribe to state changes
state.on('user', (prev, next) => {
  console.log('User changed from', prev, 'to', next);
});

// Use utility functions
const additionalFields = objIf(user.role === 'admin', { adminTools: true });

In Browser (via CDN)

<script src="https://unpkg.com/js-utils-toolkit"></script>
<script>
  // Utilities are available under the global JSUtils object
  const { GlobalState, objIf } = JSUtils;
  
  // Use utilities
  const result = objIf(condition, { key: 'value' });
</script>

Via Command Line

# List all available utilities
js-utils list

# Get information about a specific utility
js-utils util objIf

# Show help
js-utils help

API Documentation

GlobalState

The GlobalState class is a powerful state manager that allows you to create, manage, and subscribe to state changes across your application.

Static Methods

GlobalState.create(instances) Creates instances of GlobalState with the provided states.

GlobalState.create({
  appName: {
    user: { id: 1, name: 'John' },
    theme: 'dark'
  }
});

GlobalState.get(name) Gets an instance of GlobalState by name.

const appState = GlobalState.get('appName');

GlobalState.statesList Returns a list of all available state instance names.

const availableStates = GlobalState.statesList;
// ['appName', 'anotherState', ...]

Instance Properties

stored Gets all stored states in the instance.

const allStates = appState.stored;
// { user: { id: 1, name: 'John' }, theme: 'dark' }

events Gets all registered event listeners.

const registeredEvents = appState.events;

Instance Methods

store(entry, emit = true) Store a new state or update an existing state.

// Store a simple state
appState.store({ counter: 1 });

// Store a state using a function
appState.store((instance) => ({
  lastUpdated: new Date()
}));

define(entry) Define a state with custom getters and setters.

appState.define({
  counter: {
    state: 0,
    get: (instance, key, value) => value * 2,
    set: (key, newValue, oldValue) => ({ 
      state: Math.max(0, newValue), 
      safe: true 
    })
  }
});

read(key) Read a state value by key.

const theme = appState.read('theme');
// 'dark'

// Read all states
const allStates = appState.read();

update(states) Update multiple states at once.

appState.update({
  theme: 'light',
  'user.name': 'Jane'
});

setConfig(config, update) Configure the behavior of the GlobalState instance.

appState.setConfig({
  emitOnStateSet: true,
  validators: {
    theme: (value) => ['dark', 'light'].includes(value)
  }
});

validate(validators) Add validators to the GlobalState instance.

appState.validate({
  'user.age': (age) => age >= 18,
  'settings.language': (lang) => ['en', 'fr', 'es'].includes(lang)
});

Event Methods

on(eventName, listener, options) Register an event listener.

// Listen for changes to a specific state
appState.on('theme', (prevTheme, newTheme) => {
  console.log(`Theme changed from ${prevTheme} to ${newTheme}`);
});

// Overwrite existing listeners
appState.on('user', listener, { overwrite: true });

emit(eventName, ...args) Emit an event.

appState.emit('custom-event', { data: 'value' });

off(eventName, listener) Remove a specific event listener.

appState.off('theme', themeChangeListener);

clear(eventName) Clear all listeners for a specific event.

appState.clear('user');

Advanced Usage Examples

Validation and Configuration

// Configure the instance with validators
const userState = GlobalState.get('user');
userState.setConfig({
  emitOnStateSet: true,
  validators: {
    age: (age) => age >= 18,
    email: (email) => /\S+@\S+\.\S+/.test(email)
  }
});

// Try to update state (will only update if valid)
userState.store({ age: 16 }); // Won't update, age validation fails
userState.store({ email: '[email protected]' }); // Updates successfully

Using with React

function ThemeToggle() {
  const appState = GlobalState.get('app');
  const [theme, setTheme] = useState(appState.read('theme'));
  
  useEffect(() => {
    // Subscribe to theme changes
    const handleThemeChange = (prev, next) => setTheme(next);
    appState.on('theme', handleThemeChange);
    
    return () => {
      // Clean up subscription
      appState.off('theme', handleThemeChange);
    };
  }, []);
  
  const toggleTheme = () => {
    const newTheme = theme === 'dark' ? 'light' : 'dark';
    appState.store({ theme: newTheme });
  };
  
  return (
    <button onClick={toggleTheme}>
      Switch to {theme === 'dark' ? 'light' : 'dark'} theme
    </button>
  );
}
```javascript