@craftercms/utils

中文文档
4.3.0发布于 2025/02/21GNU LGPL 3.0TypeScriptESM + CJS零依赖近 30 天下载 4697

Crafter CMS utility and helper functions

原始使用文档
文档目录5 节
  1. Usage
  2. Via npm
  3. Via html script imports
  4. Package Index
  5. createLookupTable

npm (scoped)

@craftercms/utils

Contains various utilities useful when developing with Crafter CMS

Usage

All of Crafter CMS packages can be used either via npm or in plain html/javascript via regular script imports.

Via npm

  • yarn add @craftercms/utils or npm install @craftercms/utils
  • import or require the functions you wish.

Via html script imports

  • Download the bundle and import them in your page.
  • The bundles declare a global variable named craftercms. You can access all craftercms' packages and functions under this root.
  • The utils package depends on rxjs, make sure to import rxjs too before the utils script.

Tip: Once you've imported the scripts, type craftercms on your browser's dev tools console to inspect the package(s)

Vanilla html/js example

<div id="myFeature"></div>
<script src="https://unpkg.com/rxjs"></script>
<script src="https://unpkg.com/@craftercms/utils"></script>
<script>
 (function ({ utils }) {

   const people = [
     { id: 1, name: 'Mary' }
   ]

   const myLookup = utils.createLookupTable(people);
   console.log(myLookup); // => { 1: { id: 1, name: 'Mary' } }

 })(craftercms);
</script>

Package Index

The examples below assume usage in the style of using via npm. If you're using the bundles, directly importing as a script in the browser, these functions will be under the global variable named craftercms.utils (i.e. window.craftercms.utils).

createLookupTable

Creates a lookup table based on an array of items (of a type) and the items id identifier.

Example

  • Create a lookup table of items:
import { Item, LookupTable } from "@craftercms/models";
import { createLookupTable } from "@craftercms/utils";

const items: Item[] = [/* ... */];
const itemsLookupTable: LookupTable<Item> = createLookupTable<Item>(items, 'url');

The lookupTable will look like this:

  {
    "/site/website/index.xml": {
      "name": "index.xml",
      "url": "/site/website/index.xml",
      "descriptorUrl": "/site/website/index.xml",
      "descriptorDom": { /* ... */ },
      "folder": false
    }
  }

版本与发布记录

@craftercms/utils 共发布 35 个版本,首次发布于 2018/07/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.3.0latest2025/02/211.7 年前
4.2.22025/01/211.8 年前
4.2.12024/12/161.9 年前
4.1.9support-4.1.x2024/11/262 年前
4.2.02024/10/242 年前
4.1.82024/09/132 年前

关于 @craftercms/utils 的常见问题

@craftercms/utils 是什么?

Crafter CMS utility and helper functions

如何安装 @craftercms/utils?

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

@craftercms/utils 支持 TypeScript 吗?

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

@craftercms/utils 支持 ESM 还是 CommonJS?

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

@craftercms/utils 可以商用吗?

@craftercms/utils 使用 GNU LGPL 3.0 许可。声明的许可为 GNU LGPL 3.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@craftercms/utils 的下载量有多少?

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

@craftercms/utils 还在维护吗?

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