@bloomberg/record-tuple-polyfill

中文文档
0.0.4发布于 2021/10/27Apache-2.0ESM + CJS零依赖近 30 天下载 27.8万

A polyfill for the Record and Tuple Stage 2 ECMAScript proposal.

原始使用文档

Record & Tuple Polyfill

The Record and Tuple ECMAScript proposal introduces new deeply immutable value types to JavaScript that have similar access idioms to objects and arrays.

This is an experiemental and explicitly not production ready polyfill for the Record and Tuple proposal, and a babel transform to support using the literal syntax.

The polyfill and transform are constant works in progress and are not the source of truth for the proposal.

Requirements

In order to use the syntax transform, babel must be installed with at least version 7.9.0.

In order to use the polyfill, the environment must support WeakMap, WeakRef, and FinalizationRegistry. If implementations of these features are not provided, an error will be thrown.

Installation

Note: the Babel transform described below is not currently published on NPM, as it is awaiting review. If you really want to try the Babel transform plugin in it's packaged state, publishing locally via verdaccio is an excellent option.

To install the transform and polyfill:

# install the babel transform as a dev dependency, only needed at compile time
npm install -D @bloomberg/babel-plugin-proposal-record-tuple

# install the polyfill as a regular dependency, needed at runtime
npm install --save @bloomberg/record-tuple-polyfill

Next, add the plugin to your babel configuration. Example:

{
    "plugins": [["@bloomberg/babel-plugin-proposal-record-tuple", { "syntaxType": "hash" }]]
}

Note, the syntaxType option is required, and must be set to either hash or bar.

Usage

If the babel transform and the polyfill are setup, you can start using the Record and Tuple literal syntax.

console.log(#{ a: 1 } === #{ a: 1 });
console.log(#[1, 2, 3] === #[1, 2, 3]);

If you want to use the Record or Tuple global objects, you can import them from the polyfill directly.

import { Record, Tuple } from "@bloomberg/record-tuple-polyfill";

const record = Record({ a: 1 });
const tuple = Tuple(1, 2, 3);
const array = [1,2,3];

console.log(Record.isRecord(record));
console.log(Record.keys(record));
console.log(Tuple.from(array));

Unsupported Features

typeof will return an incorrect value when provided a Record or Tuple. This is because the polyfill implements the proposal via interning frozen objects.

Playground

The Record and Tuple polyfill has been deployed in an easy to use REPL here.

Supported Environments/Browsers

The Record and Tuple polyfill requires several JavaScript features that are only available experimentally in browsers, specifically WeakRef and FinalizationRegistry.

The following environments support these experimental features out-of-the-box.

environmentsupported
Chrome Canary:heavy_check_mark:

In order to use these experimental features other browsers, you must run the browser/environment with specific flags:

environmentflags
node--harmony-weak-refs
Chrome--js-flags="--harmony-weak-refs"
Firefox Nightlyjavascript.options.experimental.weakrefs

版本与发布记录

@bloomberg/record-tuple-polyfill 共发布 4 个版本,首次发布于 2020/10/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.4latest2021/10/275 年前
0.0.32020/11/036 年前
0.0.22020/10/076 年前
0.0.12020/10/076 年前

关于 @bloomberg/record-tuple-polyfill 的常见问题

@bloomberg/record-tuple-polyfill 是什么?

A polyfill for the Record and Tuple Stage 2 ECMAScript proposal.

如何安装 @bloomberg/record-tuple-polyfill?

在项目目录运行 npm install @bloomberg/record-tuple-polyfill,或使用 pnpm add / yarn add / bun add。

@bloomberg/record-tuple-polyfill 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bloomberg__record-tuple-polyfill 是否存在。

@bloomberg/record-tuple-polyfill 支持 ESM 还是 CommonJS?

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

@bloomberg/record-tuple-polyfill 可以商用吗?

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

@bloomberg/record-tuple-polyfill 的下载量有多少?

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

@bloomberg/record-tuple-polyfill 还在维护吗?

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