@consumidor-positivo/ts-utils

中文文档
1.35.1发布于 2026/08/05TypeScriptESM + CJS零依赖近 30 天下载 2518
TypeScript Utils Logo
原始使用文档
文档目录16 节
  1. Installation
  2. Usage
  3. Utility Functions
  4. React Hooks (Optional)
  5. Development Guidelines
  6. Contributing
  7. Documentation
  8. Generate documentation
  9. View documentation locally
  10. Build library and documentation
  11. Testing
  12. Run tests
  13. Run tests in watch mode
  14. Run tests with coverage
  15. Run tests with UI
  16. License
TypeScript Utils Logo

@consumidor-positivo/ts-utils

TypeScript utilities library for Consumidor Positivo projects.

Installation

npm install @consumidor-positivo/ts-utils

Usage

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 docs

The 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:serve

This 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:all

Testing

This library uses Vitest for testing. Here are the available test commands:

Run tests

npm test

Run tests in watch mode

npm run test:watch

Run tests with coverage

npm run test:coverage

Run tests with UI

npm run test:ui

License

Proprietary - Consumidor Positivo

版本与发布记录

@consumidor-positivo/ts-utils 共发布 41 个版本,首次发布于 2025/05/15;过去 12 个月发布了 20 个版本。

版本发布日期距今
1.35.1latest2026/08/053 个月前
1.35.02026/07/024 个月前
1.34.02026/06/234 个月前
1.33.02026/06/224 个月前
1.32.12026/06/025 个月前
1.32.02026/06/015 个月前

关于 @consumidor-positivo/ts-utils 的常见问题

如何安装 @consumidor-positivo/ts-utils?

在项目目录运行 npm install @consumidor-positivo/ts-utils,或使用 pnpm add / yarn add / bun add。

@consumidor-positivo/ts-utils 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@consumidor-positivo/ts-utils 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@consumidor-positivo/ts-utils 可以商用吗?

@consumidor-positivo/ts-utils 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@consumidor-positivo/ts-utils 的下载量有多少?

根据 npm 官方下载统计,@consumidor-positivo/ts-utils 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,518 次,日均约 84 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@consumidor-positivo/ts-utils 还在维护吗?

最新版本 1.35.1 发布于 2026/08/05(3 个月前);至今共发布 41 个版本,其中过去 12 个月发布了 20 个;首次发布于 2025/05/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。