@consumidor-positivo/ts-utils
v1.17.0
Published
<div align="center"> <img src="http://assets.acordocerto.com.br/f/114280/500x147/81e872f257/typescript-utils.png" alt="TypeScript Utils Logo" /> </div>
Downloads
2,380
Readme
@consumidor-positivo/ts-utils
TypeScript utilities library for Consumidor Positivo projects.
Installation
npm install @consumidor-positivo/ts-utilsUsage
Utility Functions
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]React Hooks (Optional)
The library also provides React hooks that can be imported separately. These hooks are optional and require React as a peer dependency.
usePersistedState
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.
Development Guidelines
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:
- No business logic: This library should not contain any business-specific logic or rules.
- No internal requests: Do not include API calls or requests to internal services.
- No sensitive code: Avoid including any sensitive information or algorithms.
- Client-side safe: All utilities should be appropriate for use in browser environments.
The purpose of this library is to provide reusable, generic utility functions that help with common programming tasks across different Consumidor Positivo projects.
Contributing
Please read our Contributing Guide for details on our development process, Git workflows, and how to submit pull requests.
Documentation
Generate documentation
To generate the library documentation, run:
npm run docsThe documentation will be generated in the docs/ directory.
View documentation locally
To generate and view the documentation on a local server, run:
npm run docs:serveThis will start a local server and the documentation will be available at http://localhost:3000.
Build library and documentation
To build both the library and documentation, run:
npm run build:allTesting
This library uses Vitest for testing. Here are the available test commands:
Run tests
npm testRun tests in watch mode
npm run test:watchRun tests with coverage
npm run test:coverageRun tests with UI
npm run test:uiLicense
Proprietary - Consumidor Positivo
