@develohpanda/fluent-builder

中文文档
2.1.2发布于 2020/10/15MITTypeScriptCommonJS零依赖近 30 天下载 1.3万

A typed, fluent builder for creating objects in Typescript

原始使用文档
文档目录7 节
  1. Generate a fluent, typed object builder for any interface o…
  2. Why?
  3. Installation
  4. Gotcha!
  5. Usage
  6. Contributing
  7. License

🥨 fluent-builder

npm

Generate a fluent, typed object builder for any interface or type.

fluent-builder consumes a seeding schema, and generates a builder with a signature identical to the type being built, but with mutate functions, to make iterative modifications to your object. The builder contains two additional properties, reset() and build().

createBuilder<Product>(schema).name('Shirt').price(42).build();

Why?

fluent-builder aims to simplify the use of the builder pattern for Typescript, using generics. This pattern allows for iterative construction of complex, often nested objects. Typically, a unique builder class needs to be implemented for each unique interface or type, to ensure correct typing is available.

Installation

The usual

yarn add -D @develohpanda/fluent-builder

npm i --save-dev @develohpanda/fluent-builder

Gotcha!

In order for types to be detected correctly in VS Code (eg. IntelliSense on hover), any file using fluent-builder should be included in the default project tsconfig to be compiled. If you know how to fix this, please submit a PR! :)

Usage

Define your interface / type

interface Product {
  name: string;
  price: number;
  color?: string;
  buy: () => void;
}

Define a schema

import {Schema} from '@develohpanda/fluent-builder';

const schema: Schema<Product> = {
  name: () => 'Shirt',
  price: () => 2,
  color: () => undefined,
  buy: () => jest.fn(),
}

Create a builder

import {createBuilder} from '@develohpanda/fluent-builder';

const builder = createBuilder(schema);

Consume

describe('suite', () => {
  beforeEach(() => builder.reset());

  it('test', () => {
    const mock = jest.fn();
    const instance = builder.price(4).buy(mock).build();

    // use instance and mock
  });
});

The overhead of constructing a new builder can be avoided by using the builder.reset() method. This resets the mutated schema back to its original, and can be chained.

builder.reset().price(5).build();

Contributing

Please raise issues or feature requests via the Github issue tracker.

Feel free to submit a pull request with your change!

yarn install
yarn test

License

MIT

版本与发布记录

@develohpanda/fluent-builder 共发布 15 个版本,首次发布于 2019/10/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.2latest2020/10/156 年前
2.1.12020/10/156 年前
2.1.02020/10/106 年前
2.0.32020/10/106 年前
2.0.22019/10/267 年前
2.0.12019/10/267 年前

关于 @develohpanda/fluent-builder 的常见问题

@develohpanda/fluent-builder 是什么?

A typed, fluent builder for creating objects in Typescript

如何安装 @develohpanda/fluent-builder?

在项目目录运行 npm install @develohpanda/fluent-builder,或使用 pnpm add / yarn add / bun add。

@develohpanda/fluent-builder 支持 TypeScript 吗?

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

@develohpanda/fluent-builder 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@develohpanda/fluent-builder 可以商用吗?

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

@develohpanda/fluent-builder 的下载量有多少?

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

@develohpanda/fluent-builder 还在维护吗?

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