@arkv/shared

中文文档
0.8.0发布于 2026/07/30MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2680

Shared utilities for @arkv packages

原始使用文档
文档目录17 节
  1. Install
  2. Utilities
  3. Array
  4. Object
  5. Number
  6. Async
  7. String
  8. URL
  9. Types
  10. API Reference
  11. Array utilities
  12. Object utilities
  13. Number utilities
  14. Async utilities
  15. String utilities
  16. URL utilities
  17. License

@arkv/shared

Zero-dependency shared utilities and types for @arkv packages.

Install

bun add @arkv/shared
# or
npm install @arkv/shared

Utilities

Array

import { groupBy, unique, chunk } from '@arkv/shared';

groupBy(
  [{ type: 'fruit', name: 'apple' }, { type: 'veg', name: 'carrot' }],
  i => i.type,
);
// { fruit: [{ type: 'fruit', name: 'apple' }], veg: [{ type: 'veg', name: 'carrot' }] }

unique([1, 2, 2, 3]); // [1, 2, 3]
unique(items, i => i.id); // deduplicate by key

chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]

Object

import { pick, omit, deepClone, isPlainObject, safeEntries } from '@arkv/shared';

pick({ a: 1, b: 2, c: 3 }, ['a', 'c']); // { a: 1, c: 3 }
omit({ a: 1, b: 2, c: 3 }, ['b']);       // { a: 1, c: 3 }

const cloned = deepClone({ nested: { value: 1 } });

// Prototype-based: only object literals, `JSON.parse` output and
// `Object.create(null)` qualify.
isPlainObject({});                 // true
isPlainObject(Object.create(null)); // true
isPlainObject([]);                 // false
isPlainObject(new Error());        // false
isPlainObject(new Map());          // false
isPlainObject(new Date());         // false
isPlainObject(new MyClass());      // false

// `Object.entries` that cannot be taken down by a throwing getter.
safeEntries({ ok: 1, get boom() { throw new Error('x'); } });
// [['ok', 1], ['boom', '[Getter: threw]']]

Number

import { clamp, randomInt } from '@arkv/shared';

clamp(15, 0, 10);  // 10
clamp(-5, 0, 10);  // 0

randomInt(1, 100);  // random integer between 1 and 100 (inclusive)

Async

import { retry, debounce, sleep } from '@arkv/shared';

// Retry up to 3 times with 100ms delay between attempts
const data = await retry(() => fetchData(), 3, 100);

// Debounce a function by 300ms
const debouncedSave = debounce(save, 300);

await sleep(1000); // wait 1 second

String

import { safeStringify } from '@arkv/shared';

// Handles circular references without throwing
const obj: any = { a: 1 };
obj.self = obj;
safeStringify(obj); // '{"a":1,"self":"[Circular]"}'

URL

import { UrlHelper } from '@arkv/shared';

const url = new UrlHelper();

url.buildUrl({
  base: 'https://api.example.com/{version}',
  path: '/users/{id}',
  pathParams: { version: 'v1', id: 123 },
  queryParams: { fields: 'name', active: true },
});
// https://api.example.com/v1/users/123?fields=name&active=true

url.interpolate('/api/{version}/users/{id}', { version: 'v1', id: 42 });
// '/api/v1/users/42'

Types

import type {
  AsyncFn,
  Prettify,
  StringTransformFn,
  ParamsType,
} from '@arkv/shared';
TypeDescription
AsyncFn<T>Generic async function type
Prettify<T>Expands object types for better IDE hover previews
StringTransformFnFunction that transforms a string (e.g., applies color/style)
ParamsTypeRecord<string, string | number | boolean | undefined>

API Reference

Array utilities

FunctionSignatureDescription
groupBy(items: T[], keyFn: (item: T) => string) => Record<string, T[]>Groups array items by key
unique(items: T[], keyFn?: (item: T) => unknown) => T[]Removes duplicates, with optional key function
chunk(items: T[], size: number) => T[][]Splits array into chunks of given size

Object utilities

FunctionSignatureDescription
pick(obj: T, keys: K[]) => Pick<T, K>Picks specified keys from object
omit(obj: T, keys: K[]) => Omit<T, K>Omits specified keys from object
deepClone(value: T) => TDeep clones via structuredClone
isPlainObject(obj: unknown) => obj is Record<string, unknown>True only for an object whose own prototype is Object.prototype or null
safeEntries(obj: Record<string, unknown>) => [string, unknown][]Object.entries where a throwing getter yields '[Getter: threw]'

Number utilities

FunctionSignatureDescription
clamp(value: number, min: number, max: number) => numberClamps number between min and max
randomInt(min: number, max: number) => numberRandom integer between min and max (inclusive)

Async utilities

FunctionSignatureDescription
retry(fn: () => Promise<T>, retries?: number, delayMs?: number) => Promise<T>Retries async function with delay
debounce(fn: T, delayMs: number) => (...args) => voidCreates debounced function
sleep(ms: number) => Promise<void>Promise-based delay

String utilities

FunctionSignatureDescription
safeStringify(obj: unknown) => stringJSON.stringify with circular reference handling

URL utilities

ClassMethodDescription
UrlHelperbuildUrl(config)Builds a URL with path/query/path params
UrlHelperinterpolate(template, params?)Replaces {key} placeholders in a string

License

MIT

版本与发布记录

@arkv/shared 共发布 10 个版本,首次发布于 2026/02/18;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.8.0latest2026/07/303 个月前
0.7.42026/07/263 个月前
0.7.32026/07/263 个月前
0.7.22026/05/076 个月前
0.7.12026/05/056 个月前
0.7.02026/04/136 个月前

关于 @arkv/shared 的常见问题

@arkv/shared 是什么?

Shared utilities for @arkv packages

如何安装 @arkv/shared?

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

@arkv/shared 支持 TypeScript 吗?

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

@arkv/shared 支持 ESM 还是 CommonJS?

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

@arkv/shared 可以商用吗?

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

@arkv/shared 的下载量有多少?

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

@arkv/shared 还在维护吗?

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