@aglyn/shared-util-tools

中文文档
1.18.0发布于 2026/11/07Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 2.6万

General-purpose TypeScript helpers: type guards, array and object utilities, deep get/set, cloning, JSON and base64 serialization, plus a few larger standalone modules (a linear-time regex engine, an AES-GCM secret box, WCAG contrast math, HTML escaping,

原始使用文档
文档目录6 节
  1. Install
  2. Read this first: the root import patches Array.prototype
  3. What's in it
  4. Usage
  5. How it fits
  6. License

@aglyn/shared-util-tools

General-purpose TypeScript helpers: type guards, array and object utilities, deep get/set, cloning, JSON and base64 serialization, plus a few larger standalone modules (a linear-time regex engine, an AES-GCM secret box, WCAG contrast math, HTML escaping, a PDF writer). It is a dependency of most Aglyn packages and is usable on its own.

Published to npm from the Aglyn monorepo. Versions follow semantic versioning.

Install

npm install @aglyn/shared-util-tools

No peer dependencies.

Read this first: the root import patches Array.prototype

package.json lists ./src/lib/array/array-overrides.* under sideEffects, and the package root re-exports that module. So import ... from '@aglyn/shared-util-tools' runs it, and bundlers are told not to drop it.

What it does, exactly: using Object.defineProperty it defines eight methods on Array.prototype, each non-enumerable, writable and configurable, and it declares them on the global Array<T> TypeScript interface. It defines them unconditionally, so a same-named property already present would be overwritten.

MethodBehavior
$_cloneShallow()shallow copy (lodash clone)
$_cloneDeep()deep copy (lodash cloneDeep)
$_moveAtIndex(index, newIndex)moves an item, mutating the array; appends when newIndex is NaN
$_pushAtIndex(index, ...items)inserts at index, mutating; appends when index is NaN
$_removeItem(item)removes the first strictly-equal item, mutating
$_removeAtIndex(index)removes one item, mutating
$_replaceAtIndex(index, item)replaces one item, mutating
$_truthy()returns a new array without falsy items

The mutating methods return the same array. Because the properties are non-enumerable they do not show up in for...in, Object.keys or JSON.stringify. No built-in method is replaced. (The global type declaration gives $_pushAtIndex the signature (index, newIndex); the implementation is (index, ...items).)

To avoid the patch, import by subpath instead of from the root. No other module in this package imports array-overrides, and every helper the patch wraps is available as a plain function, for example @aglyn/shared-util-tools/array/array-move-at-index. Note that @aglyn/shared-util-dom and @aglyn/shared-util-errors import the root, so they apply the patch too.

What's in it

From the package root:

  • Guards, all _-prefixed: _isArr, _isObj, _isNum, _isBool, _isNull, _isUndOrNull, _isStrEmpty, _isPromiseLike, _hasOwnProperty and the rest of src/lib/guards/lib.
  • Arrays: arraySafe, arrayFrom, arrayFromLength, arrayCopyShallow, arrayCopyDeep, arrayMoveAtIndex, arrayPushAtIndex, arrayRemoveItem, arrayRemoveAtIndex, arrayUpdate, arrayUpdateAtIndex, arraySortBy, arraySortByDeepProperty, arrayOfEntriesToObject.
  • Objects: objectGetDeepProperty, objectSetDeepProperty, objectClone, objectCloneDeep, objectDeleteProperty, objectRemap, objectSafe, objectUpdate, objectGetKeysAndSymbolProperties, getProperty, getStaticField.
  • Values: truthy, truth, falsy, noop, str, trim, length, toNum, numberToHexadecimal, numberFromHexadecimal, numeronym, splitDisplayName, and the compare operator helper with its operator types.
  • Bit flags: bitwiseHasAttribute, bitwiseHasAllAttributes, bitwiseHasOnlyAttributes.
  • Serialization: jsonSerialize, jsonDeserialize, base64IsomorphicEncode, base64IsomorphicDecode.
  • Classes and functions: applyMixins, createChainedFunction, getDisplayName, interopDefault, noSideEffects, cloneDeep, copyShallow, Crud, Normalized, CSS.

By subpath only (kept off the root so they are not pulled into every consumer's bundle):

  • @aglyn/shared-util-tools/linear-regex - compileLinearPattern, compileLinearTest and their explain* counterparts. A regex engine that parses the pattern itself and runs an NFA simulation, so matching time is bounded by input length times program length. Intended for patterns written by someone you do not trust. Backreferences and lookaround are not supported.
  • @aglyn/shared-util-tools/secret-box - createSecretBoxKey, parseSecretBoxKeyring, sealSecret, openSecret, needsReseal, sealedSecretKeyId, SecretBoxError. AES-256-GCM sealing of short secrets with key ids, key rotation and an optional authenticated context. Imports node:crypto; server-only.
  • @aglyn/shared-util-tools/escape-html - escapeHtml(value), escaping & < > " '.
  • @aglyn/shared-util-tools/contrast - relativeLuminanceOfRgb, rgbChannelsOfHex, prefersDarkInk and related sRGB helpers.
  • @aglyn/shared-util-tools/serialize/php-serial-* - wrappers over php-serialize.
  • @aglyn/shared-util-tools/pdf/pdf-writer - PdfWriter, a PDF page layout with no PDF library under it: text in the standard Helvetica faces (left, right or centered, in any RGB color), paragraphs that wrap and run onto new pages, filled and stroked rectangles and rules, JPEG images fitted to a box, clickable links, tables whose cells wrap and whose header row repeats on every page, and a footer with page numbers. pdfRgbFromHex turns #rrggbb into its color.
  • @aglyn/shared-util-tools/pdf/pdf-text - the text half: toWinAnsiText (any string as characters the standard fonts can draw: look-alikes swapped in, accents dropped, emoji removed, ? for other scripts), pdfString, measureText and wrapLines by Adobe's Helvetica widths, and the quicker textWidth/wrapText estimate.
  • @aglyn/shared-util-tools/pdf/pdf-file - the file half: buildPdfFile (pages of drawing operators, JPEG images as DCTDecode streams and URI links into a PDF with a correct cross-reference table, the same bytes for the same input), readJpeg (a JPEG's size and channels from its header) and pdfNumber/pdfUri.

Usage

// Subpath imports: no prototype patch.
import { objectGetDeepProperty } from '@aglyn/shared-util-tools/object/object-get-deep-property'
import { arraySafe } from '@aglyn/shared-util-tools/array/array-safe'
import { escapeHtml } from '@aglyn/shared-util-tools/escape-html'

const city = objectGetDeepProperty<string>(user, 'address.city')
const tags = arraySafe(input.tags) // always an array
const html = `<p>${escapeHtml(comment)}</p>`
// Root import: same helpers, and Array.prototype gains the $_ methods.
import { _isArr, truthy } from '@aglyn/shared-util-tools'

How it fits

A shared package: generic, with no knowledge of Aglyn's model. Shared packages may only import other shared packages; this one depends on @aglyn/shared-data-types, lodash-es and php-serialize. Most of the monorepo depends on it, including @aglyn/aglyn, @aglyn/besigner, the shared UI packages and several plugins.

License

Apache-2.0. Source: https://github.com/aglyn/aglyn/tree/main/libs/shared/util/tools

版本与发布记录

@aglyn/shared-util-tools 共发布 152 个版本,首次发布于 2026/09/21;过去 12 个月发布了 152 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.18.0latest2026/11/07今天
1.17.02026/11/07今天
1.16.02026/11/07今天
1.15.22026/11/061 天前
1.15.12026/11/061 天前
1.15.02026/11/061 天前

其他发布通道:beta → 1.0.0-beta.296。

关于 @aglyn/shared-util-tools 的常见问题

@aglyn/shared-util-tools 是什么?

General-purpose TypeScript helpers: type guards, array and object utilities, deep get/set, cloning, JSON and base64 serialization, plus a few larger standalone modules (a linear-time regex engine, an AES-GCM secret box, WCAG contrast math, HTML escaping,

如何安装 @aglyn/shared-util-tools?

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

@aglyn/shared-util-tools 支持 TypeScript 吗?

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

@aglyn/shared-util-tools 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aglyn/shared-util-tools 可以商用吗?

@aglyn/shared-util-tools 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aglyn/shared-util-tools 的下载量有多少?

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

@aglyn/shared-util-tools 还在维护吗?

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