@guardian/libs

中文文档
33.0.0发布于 2026/06/10TypeScriptESM + CJS构建来源已验证近 30 天下载 4.6万

A collection of JavaScript libraries and TypeScript types for Guardian projects

原始使用文档
文档目录24 节
  1. ArticleElementRole
  2. Consent
  3. Cookies
  4. Countries
  5. Format
  6. Locale
  7. Switches
  8. isBoolean
  9. isNonNullable
  10. isObject
  11. isOneOf
  12. isString
  13. isUndefined
  14. joinUrl
  15. loadScript
  16. log , debug
  17. Ophan
  18. Performance
  19. storage
  20. timeAgo
  21. getErrorMessage
  22. Installation
  23. Bundling
  24. Local Testing Using Linking

@guardian/libs

npm (scoped) ES version npm type definitions

A collection of JavaScript libraries and TypeScript types for Guardian projects

ArticleElementRole

Type that describes the role of an element in an article.

The Guardian's consent management platform.

Cookies

API over document.cookies.

Countries

Country data and methods to access it.

Format

Codified editorial design and information architecture.

Locale

Get the user’s current location.

Switches

Get the active switches on theguardian.com.

isBoolean

Check whether a value is a boolean.

isNonNullable

Check whether a value is a NonNullable.

isObject

Checks whether a value is a plain object (i.e. {}-like).

isOneOf

Check whether a value is one of a set of literal string or numbers.

isString

Check whether a value is a string.

isUndefined

Check whether a value is undefined.

joinUrl

Combines separate url parts into one valid url string.

loadScript

Inject an external JavaScript file.

log, debug

Selectively log team-specific messages to the console.

Ophan

Types relating to Ophan.

Performance

API over window.performance with hooks into log the data warehouse.

storage

Robust API over localStorage and sessionStorage.

timeAgo

Format absolute dates as time-ago strings.

getErrorMessage

Utility for extracting a useful error message from Errors (or other values) that can be thrown in JavaScript.

Installation

Generic badge

yarn add @guardian/libs

or

npm install @guardian/libs

then

import { loadScript, storage, ...etc } from '@guardian/libs';

Bundling

This package uses ES2020.

If your target environment does not support that, make sure you transpile this package when bundling your application.

Local Testing Using Linking

  1. In the command line in your terminal, navigate to the csnx root directory. Run:
make @guardian/libs:build

This build command has to be re-run for changes to be picked up.

  1. Navigate to the repo that will test your code changes. In the appropriate folder of that repo, run
npm/yarn/pnpm link ../csnx/libs/@guardian/libs

where '../csnx' is the path to your local changes in csnx where you cloned the csnx code to.

After re-building the '@guardian/libs' in step 1. this command does not have to be repeated and changes will be automatically picked up.

  1. Once you’ve developed your changes and tested, you can unlink from the repo where you are testing the changes
npm/yarn/pnpm unlink ../csnx/libs/@guardian/libs

版本与发布记录

@guardian/libs 共发布 539 个版本,首次发布于 2020/10/06;过去 12 个月发布了 49 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
33.0.0latest2026/06/105 个月前
0.0.0-canary-20260518131757canary2026/05/185 个月前
32.0.02026/05/146 个月前
31.0.12026/05/066 个月前
0.0.0-canary-202604301412582026/04/306 个月前
0.0.0-canary-202604301408592026/04/306 个月前

关于 @guardian/libs 的常见问题

@guardian/libs 是什么?

A collection of JavaScript libraries and TypeScript types for Guardian projects

如何安装 @guardian/libs?

在项目目录运行 npm install @guardian/libs,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@guardian/ophan-tracker-js(^2.2.10)、tslib(^2.8.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@guardian/libs 支持 TypeScript 吗?

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

@guardian/libs 支持 ESM 还是 CommonJS?

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

@guardian/libs 可以商用吗?

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

@guardian/libs 的下载量有多少?

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

@guardian/libs 还在维护吗?

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