@bonniernews/json-autocomplete

中文文档
0.0.15发布于 2025/11/24MITTypeScriptESM + CJS零依赖近 30 天下载 3575

Completes incomplete JSON string with caching capabilites

原始使用文档
文档目录13 节
  1. Table of Contents
  2. Features
  3. Installation
  4. Usage
  5. Basic Example
  6. Using jsonAutocomplete Helper
  7. API Reference
  8. createJsonAutocomplete()
  9. jsonAutocomplete
  10. Contributing
  11. Code of Conduct
  12. License
  13. Support

JSON Autocomplete

npm License Coverage

A utility to incrementally build and complete JSON strings from potentially incomplete JSON append operations with caching capabilities. Perfect for modern LLM applications that should stream json output. Perfectly combined with chat completions API from OpenAI.

Table of Contents

Features

  • Incremental Building: Append parts of a JSON string incrementally and get the best possible valid JSON.
  • String Handling: Correctly handles strings, escape characters, and nested structures.
  • Error Checking: Detects unbalanced brackets and braces.
  • Caching Capabilities: Efficiently manages the internal state to optimize performance.

Installation

You can install JSON Autocomplete via npm or Yarn:

# Using npm
npm install @bonniernews/json-autocomplete

# Using Yarn
yarn add @bonniernews/json-autocomplete

Usage

Basic Example

import { createJsonAutocomplete } from "@bonniernews/json-autocomplete";

// Create a new autocompleter instance
const autocompleter = createJsonAutocomplete();

// Append incomplete JSON fragments
const part1 = '{"name": "Alice", "age": 30';
const completed1 = autocompleter.append(part1);
console.log(completed1); // Outputs: {"name": "Alice", "age": 30}

const part2 = ', "address": {"street": "123 Main St';
const completed2 = autocompleter.append(part2);
console.log(completed2); // Outputs: {"name": "Alice", "age": 30, "address": {"street": "123 Main St"}}

const part3 = '", "city": "Wonderland"}}';
const completed3 = autocompleter.append(part3);
console.log(completed3); // Outputs: {"name": "Alice", "age": 30, "address": {"street": "123 Main St", "city": "Wonderland"}}

Using jsonAutocomplete Helper

For simpler use cases, you can use the jsonAutocomplete helper function which manages the autocompleter instance internally.

import { jsonAutocomplete } from "@bonniernews/json-autocomplete";

const incompleteJson1 = '{"isActive": true, "roles": ["admin", "user"';
const completedJson1 = jsonAutocomplete(incompleteJson1);
console.log(completedJson1); // Outputs: {"isActive": true, "roles": ["admin", "user"]}

const incompleteJson2 = ', "permissions": {"read": true, "write": false';
const completedJson2 = jsonAutocomplete(incompleteJson2);
console.log(completedJson2); // Outputs: {"isActive": true, "roles": ["admin", "user"], "permissions": {"read": true, "write": false}}

API Reference

createJsonAutocomplete()

Creates a new JSON autocompleter instance.

Returns

An object with the following method:

  • append(part: string): string: Appends a fragment of JSON and returns the best possible valid JSON string constructed from all appended fragments.

Example

import { createJsonAutocomplete } from "@bonniernews/json-autocomplete";

const autocompleter = createJsonAutocomplete();

const jsonPart1 = '{"product": "Laptop", "price": 1200';
const completedJson1 = autocompleter.append(jsonPart1);
console.log(completedJson1); // {"product": "Laptop", "price": 1200}

const jsonPart2 = ', "specs": {"cpu": "Intel i7", "ram": "16GB"';
const completedJson2 = autocompleter.append(jsonPart2);
console.log(completedJson2); // {"product": "Laptop", "price": 1200, "specs": {"cpu": "Intel i7", "ram": "16GB"}}

const jsonPart3 = "}}";
const completedJson3 = autocompleter.append(jsonPart3);
console.log(completedJson3); // {"product": "Laptop", "price": 1200, "specs": {"cpu": "Intel i7", "ram": "16GB"}}

jsonAutocomplete

A helper function to incrementally autocomplete a JSON string without manually managing the autocompleter instance.

Parameters

  • incompleteJsonString: string: The incomplete JSON string fragment.
  • autocompleter?: ReturnType<typeof createJsonAutocomplete>: (Optional) An existing autocompleter instance. If not provided, a new one is created.

Returns

  • string: The best possible valid JSON string constructed from all appended fragments.

Example

import { jsonAutocomplete } from "@bonniernews/json-autocomplete";

let completedJson = jsonAutocomplete('{"title": "Book"');
console.log(completedJson); // {"title": "Book"}

completedJson = jsonAutocomplete(', "author": "Jane Doe"');
console.log(completedJson); // {"title": "Book", "author": "Jane Doe"}

completedJson = jsonAutocomplete("}");
console.log(completedJson); // {"title": "Book", "author": "Jane Doe"}

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository: Click the Fork button at the top right of the repository page.

  2. Clone your fork:

    git clone https://github.com/your-username/json-autocomplete.git
    cd json-autocomplete
    
  3. Install dependencies:

    npm install
    
  4. Create a new branch:

    git checkout -b feature/YourFeature
    
  5. Make your changes: Implement your feature or bug fix.

  6. Lint and type-check:

    npm run lint
    npm run typecheck
    
  7. Run tests:

    npm test
    
  8. Commit your changes:

    git commit -m "Add your message here"
    
  9. Push to your fork:

    git push origin feature/YourFeature
    
  10. Create a Pull Request: Go to the original repository and submit a pull request.

Code of Conduct

Please adhere to the Bonnier News Code of Conduct in all your interactions with the project.

License

This project is licensed under the MIT License. You are free to use, modify, and distribute it as per the license terms.

Support

If you encounter any issues or have questions, please open an issue on GitHub.

版本与发布记录

@bonniernews/json-autocomplete 共发布 6 个版本,首次发布于 2024/12/12;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.0.15latest2025/11/2411 个月前
0.0.142024/12/171.9 年前
0.0.132024/12/161.9 年前
0.0.122024/12/161.9 年前
0.0.112024/12/161.9 年前
0.0.102024/12/121.9 年前

关于 @bonniernews/json-autocomplete 的常见问题

@bonniernews/json-autocomplete 是什么?

Completes incomplete JSON string with caching capabilites

如何安装 @bonniernews/json-autocomplete?

在项目目录运行 npm install @bonniernews/json-autocomplete,或使用 pnpm add / yarn add / bun add。

@bonniernews/json-autocomplete 支持 TypeScript 吗?

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

@bonniernews/json-autocomplete 支持 ESM 还是 CommonJS?

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

@bonniernews/json-autocomplete 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bonniernews/json-autocomplete 可以商用吗?

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

@bonniernews/json-autocomplete 的下载量有多少?

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

@bonniernews/json-autocomplete 还在维护吗?

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