@dpaskhin/unique

中文文档
1.1.0发布于 2026/07/10MITTypeScriptESM + CJS零依赖近 30 天下载 5643

Ensures unique values by rejecting duplicates.

原始使用文档
文档目录14 节
  1. Contents
  2. Installation
  3. Features
  4. Usage
  5. API Reference
  6. uniqueFactory(fn, options)
  7. unique(fn, args, options)
  8. unique(value, options)
  9. UniqueError
  10. GLOBAL_STORE
  11. Error Handling
  12. Best Practices
  13. License
  14. Contributing

@dpaskhin/unique

npm version bundle size license

@dpaskhin/unique is a TypeScript library for generating unique values. It ensures no duplicates by rejecting repeats and retrying until a new result is found. Ideal for ensuring non-repeated random values.

Contents

Installation

To install @dpaskhin/unique, run:

npm install --save-dev @dpaskhin/unique

Or, if you use pnpm:

pnpm add --save-dev @dpaskhin/unique

Features

  • Generate unique values based on the output of any function or plain value.
  • Supports custom stores or global storage for uniqueness tracking.
  • Configurable retry and timeout options.
  • stringifier support for custom handling of complex objects during uniqueness checks.

Usage

Basic Example

Here’s a quick example using @dpaskhin/unique:

import { unique } from '@dpaskhin/unique';

// Outputs a unique random number each time
console.log(unique(Math.random));
console.log(unique(Math.random));
console.log(unique(Math.random));

Example with Faker.js

@dpaskhin/unique works well with libraries like Faker.js for generating unique random data.

import { unique, uniqueFactory } from '@dpaskhin/unique';
import { faker } from '@faker-js/faker';

// Outputs a unique first name each time
console.log(unique(faker.person.firstName));
console.log(unique(faker.person.firstName));
console.log(unique(faker.person.firstName));

// ----------------------------------------

const createUniqueUser = uniqueFactory(() => ({
  firstName: faker.person.firstName(),
  lastName: faker.person.lastName(),
  age: faker.number.int({ min: 18, max: 100 }),
}));

// Each output user is structurally unique
console.log(createUniqueUser());
console.log(createUniqueUser());
console.log(createUniqueUser());

Custom Store

You can provide your own store to isolate uniqueness checks. This ensures that uniqueness is tracked within the scope of your application.

import { unique } from '@dpaskhin/unique';

const scopedStore = new Set();

// Outputs a unique random number
console.log(unique(Math.random, [], { store: scopedStore }));

Global Store

If you don’t provide a custom store, the library will use the GLOBAL_STORE by default. This can be useful if you want to track unique values across different parts of your application.

import { unique, GLOBAL_STORE } from '@dpaskhin/unique';

// Use the global store for uniqueness checks
const uniqueRandomValue1 = unique(Math.random);
const uniqueRandomValue2 = unique(Math.random);

// Outputs two unique random values
console.log(uniqueRandomValue1, uniqueRandomValue2);

// Clear the global store when needed
GLOBAL_STORE.clear();

Excluding Specific Values

You can also provide an exclude list to prevent specific values from being considered during uniqueness checks.

import { uniqueFactory } from '@dpaskhin/unique';

const createUniqueRandom = uniqueFactory(Math.random, {
  // Prevents these values from being returned
  exclude: [0.5, 0.6],
});

// Outputs a unique random number, excluding 0.5 and 0.6
console.log(createUniqueRandom());

Stringifier for Complex Objects or other non-primitive types

The stringifier option allows you to handle complex objects by converting them into strings for uniqueness checks.

By default, a JSON.stringify-based stringifier is used — it additionally distinguishes NaN/Infinity/-Infinity from null (which bare JSON.stringify otherwise collapses to the same "null" key) and always returns a real string, even for values like undefined for which JSON.stringify itself returns undefined.

import { uniqueFactory } from '@dpaskhin/unique';
import { faker } from '@faker-js/faker';
import JsonMarshal from 'json-marshal';

const createUniqueUser = uniqueFactory(
  () => ({
    name: faker.person.firstName(),
    age: faker.number.int({ min: 18, max: 100 }),
  }),
  {
    // Here can be any function that takes the return value of the function provided above and converts it to a string.
    // Or just omit it to use the default stringifier described above.
    stringifier: JsonMarshal.stringify,
  }
);

const user1 = createUniqueUser();
const user2 = createUniqueUser();

// Outputs two unique users
console.log(user1, user2);

API Reference

uniqueFactory(fn, options)

Creates a function that ensures unique results based on the provided function fn.

Parameters

  • fn - The function used to generate values.
  • options - Optional. An object containing configuration options:
    • store (Set): A custom store to track unique values. Defaults to a new Set.
    • maxRetries (number): The maximum number of retries to generate a unique value before throwing an error. Defaults to 50.
    • maxTime (number): The maximum time (in milliseconds) to attempt generating a unique value before throwing an error. Defaults to 50ms.
    • exclude (Array): A list of values to exclude from the result set. These values will not be returned by the unique generator.
    • stringifier (function): A function to stringify a result before storing it. Defaults to a JSON.stringify-based stringifier (see Stringifier for Complex Objects).

Returns

A function that generates unique values based on fn with the same signature.

Example

import { uniqueFactory } from '@dpaskhin/unique';
import { faker } from '@faker-js/faker';

const createUniqueUser = uniqueFactory(() => ({
  name: faker.person.firstName(),
  age: faker.number.int({ min: 18, max: 100 }),
}));

// Outputs a unique user each time
console.log(createUniqueUser());
console.log(createUniqueUser());
console.log(createUniqueUser());

unique(fn, args, options)

Generates a unique value by running fn and ensuring uniqueness via either a provided or global store.

Parameters

  • fn - The function used to generate values.
  • args - Optional. An array of arguments to be passed to fn.
  • options - Optional. An object containing configuration options:
    • store (Set): A custom store to track unique values. By default, the GLOBAL_STORE is used.
    • maxRetries (number): The maximum number of retries to generate a unique value before throwing an error. Defaults to 50.
    • maxTime (number): The maximum time (in milliseconds) to attempt generating a unique value before throwing an error. Defaults to 50ms.
    • exclude (Array): A list of values to exclude from the result set. These values will not be returned by the unique generator.
    • stringifier (function): A function to stringify a result before storing it. Defaults to a JSON.stringify-based stringifier (see Stringifier for Complex Objects).

Returns

A unique value generated by fn.

Example

import { unique } from '@dpaskhin/unique';
import { faker } from '@faker-js/faker';

const uniqueEmail = unique(faker.internet.email, [
  {
    firstName: faker.person.firstName(),
    lastName: faker.person.lastName(),
  },
]);

// Outputs a unique email
console.log(uniqueEmail);

unique(value, options)

Ensures a plain value is unique by checking against either a provided or global store.

Parameters

  • value - The plain value (non-functional) to ensure uniqueness for.
  • options - Optional. An object containing configuration options:
    • store (Set): A custom store to track unique values. By default, the GLOBAL_STORE is used.
    • exclude (Array): A list of values to exclude. These values will not be added to the store or returned.
    • stringifier (function): A function to stringify the value before storing it. Defaults to a JSON.stringify-based stringifier (see Stringifier for Complex Objects).

Returns

The unique value provided.

Example

This overload is for checking a value you already have — not a randomly generated one — against a store, e.g. a slug derived from user input:

import { unique } from '@dpaskhin/unique';

const usedSlugs = new Set();

function toSlug(title: string) {
  return unique(title.toLowerCase().replace(/\s+/g, '-'), { store: usedSlugs });
}

console.log(toSlug('Hello World')); // 'hello-world'
toSlug('Hello World'); // throws UniqueError — slug already taken

UniqueError

The error class thrown by uniqueFactory/unique when a unique value could not be produced. Extends Error.

Fields

  • storeSize (number): The number of values held by the store at the time of failure.
  • duration (number): How long (in milliseconds) generation was attempted before giving up.
  • iterations (number): How many times the generator function was called before giving up.

See Error Handling below for a usage example.

GLOBAL_STORE

The module-level Set used by unique() calls that don't provide a custom store option. Shared across your entire application by default — see Global Store above, and Best Practices for when to avoid it.

Error Handling

uniqueFactory/unique throw a UniqueError when they can't produce a unique value:

  • For function-based generation (uniqueFactory(fn), unique(fn, args)), this happens once maxRetries or maxTime is exceeded.
  • For the plain-value form (unique(value)), this happens immediately if value is already in the store.

UniqueError is exported, so you can check for it specifically instead of parsing the error message:

import { unique, UniqueError } from '@dpaskhin/unique';

try {
  unique(() => Math.floor(Math.random() * 2), [], { maxRetries: 5 });
} catch (error) {
  if (error instanceof UniqueError) {
    console.error(error.message);
    console.error(error.storeSize, error.iterations, error.duration);
  }
}

Best Practices

  • Avoid using the global store when local uniqueness is required, as it may lead to unexpected results due to shared state.
  • Configure Max Retries and Timeouts: Customize maxRetries and maxTime based on the expected uniqueness and complexity of generated values.
  • Avoid using plain values and prefer generating functions for flexibility and retries.

License

@dpaskhin/unique is licensed under the MIT License.

Contributing

Contributions are welcome! If you have ideas or improvements, feel free to submit a pull request.

版本与发布记录

@dpaskhin/unique 共发布 6 个版本,首次发布于 2024/10/03;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.1.0latest2026/07/104 个月前
1.0.02024/11/222 年前
0.3.02024/11/112 年前
0.2.02024/10/092 年前
0.1.02024/10/032 年前
0.0.02024/10/032 年前

关于 @dpaskhin/unique 的常见问题

@dpaskhin/unique 是什么?

Ensures unique values by rejecting duplicates.

如何安装 @dpaskhin/unique?

在项目目录运行 npm install @dpaskhin/unique,或使用 pnpm add / yarn add / bun add。

@dpaskhin/unique 支持 TypeScript 吗?

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

@dpaskhin/unique 支持 ESM 还是 CommonJS?

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

@dpaskhin/unique 可以商用吗?

@dpaskhin/unique 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dpaskhin/unique 的下载量有多少?

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

@dpaskhin/unique 还在维护吗?

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