TypeScript utilities library for Consumidor Positivo projects.
npm install @consumidor-positivo/ts-utils
import { range } from '@consumidor-positivo/ts-utils';
// Generate an array of sequential numbers
const numbers = range(5);
console.log(numbers); // [0, 1, 2, 3, 4]
The library also provides React hooks that can be imported separately. These hooks are optional and require React as a peer dependency.
import { usePersistedState } from '@consumidor-positivo/ts-utils/hooks';
function UserPreferences() {
// Persist state in localStorage (default)
const [preferences, setPreferences, clearPreferences] = usePersistedState('user-preferences', {
theme: 'light',
fontSize: 14
});
// Persist state in sessionStorage
const [sessionData, setSessionData] = usePersistedState('session-data',
{ isLoggedIn: true },
{ storage: 'session' }
);
const toggleTheme = () => {
setPreferences({
...preferences,
theme: preferences?.theme === 'light' ? 'dark' : 'light'
});
};
return (
<div>
<p>Current theme: {preferences?.theme}</p>
<button onClick={toggleTheme}>Toggle Theme</button>
<button onClick={clearPreferences}>Reset Preferences</button>
</div>
);
}
See the documentation for more detailed examples and API reference.
This library is designed to contain only generic utility functions that can be safely used in client-side applications. When contributing, please adhere to these important principles:
The purpose of this library is to provide reusable, generic utility functions that help with common programming tasks across different Consumidor Positivo projects.
Please read our Contributing Guide for details on our development process, Git workflows, and how to submit pull requests.
To generate the library documentation, run:
npm run docs
The documentation will be generated in the docs/
directory.
To generate and view the documentation on a local server, run:
npm run docs:serve
This will start a local server and the documentation will be available at http://localhost:3000.
To build both the library and documentation, run:
npm run build:all
This library uses Vitest for testing. Here are the available test commands:
npm test
npm run test:watch
npm run test:coverage
npm run test:ui
Proprietary - Consumidor Positivo