# 第一次收到别人的 PR 👻 (September 17, 2024)

早上送走爸妈之后才发现今天没看 GitHub 通知，一打开

<img src="/nlark/yuque/0/2024/png/26070246/1726580566616-63ea49f9-d7cc-4cfb-9734-04309f0db6fe.png" width="1492" title="" crop="0,0,1,1" id="u08d9add5" class="ne-image">

大部分都是垃圾信息，但是发现居然有人给我的 `partial-json` 提了一个 PR

[Add `OUTERMOST_OBJ` and `OUTERMOST_ARR` allowances by alanpog · Pull Request #9 · promplate/partial-json-parser-js](https://github.com/promplate/partial-json-parser-js/pull/9)

<img src="https://opengraph.githubassets.com/3f91c7d1e73cf093862a8999b5f45c774e226833cbae59dcbf1efa0db41f68dd/promplate/partial-json-parser-js/pull/9" width="1200" title="null" crop="0,0,1,1" id="GgUkh" class="ne-image">

Sourcery 的总结还挺好的，一看就懂

<img src="/nlark/yuque/0/2024/png/26070246/1726580634530-5827dbbb-3e17-412e-b335-d2a1c96f9fd1.png" width="843.4285714285714" title="" crop="0,0,1,1" id="u68fee144" class="ne-image">

说起来，很戏剧性地，由于我这个项目是刚开始用 JavaScript 的时候写的，当时还不太懂 CI，于是没有配 formatter 的 config，这个人直接不小心把所有文件都重新格式化了，造成 600 行的 diff 😂

<img src="/nlark/yuque/0/2024/png/26070246/1726580909071-a3556c6c-44ae-4601-8493-f735704456ef.png" width="1180" title="" crop="0,0,1,1" id="u8c8caad1" class="ne-image">

说回到这个功能，虽然我自己先前也想搞这么个东西，但是由于觉得想不到完美的设计，所以就没干了。

> Your proposal is great and this implementation works well. But due to my perfectionist tendencies, I often find myself wondering if there's an even better way to solve **these kinds of problems**.
>

但是直接拒绝别人也不太建设性。于是我就想了很久（一个中午下午都耗在这上面了）

想了两个解决方案：

### Approach One — Input Predicate Function
The `parse` will take an `options` object to extend configuration besides simple `allow`. We may support inputing a `validate` predicate function to choose whether to allow an object / array. The signature of `options` may look like this:

```typescript
interface options {
  allow: number;
  validate(parsed: any, text: string, parents: Parent[]): boolean
}

type Parent = {
  type: "OBJ";
  key: string;
} | {
  type: "ARR";
  index: number;
}
```

For example, if user do this:

```typescript
parse(`[0, {"a": [{`, { allow: ALL, (parsed, text, parents) => { ... } })
```

The validate function will be called at most 4 times with

```javascript
validate({}, '{', [{ type: "ARR", index: 0}, { type: "OBJ", key: "a" }, { type: "ARR", index: 1}])
```

```javascript
validate([{}], '[{', [{ type: "OBJ", key: "a" }, { type: "ARR", index: 1}])
```

```javascript
validate({"a": {}}, '{"a": [{', [{ type: "ARR", index: 1}])
```

```javascript
validate([0, {"a": {}}], '[0, {"a": [{', [])
```

### Approach Two — Return PartialInfo
Inject some information into the return value. Like this:

```typescript
export const partial = Symbol('__partial__');
```

And the partial information may be like this:

```typescript
interface PartialInfo {
  text: string;
}
```

Then users can filter the result themselves using this information.

For example, if using this way:

```typescript
> res = parse(`[0, {"a": [{`, { allow: ALL, inject: true }) // [0, {"a": [{}]}]
> res[0][partial] // undefined
> res[1][partial] // { text: '{"a": [{' }
> res[1].a[partial] // { text: '[{' }
> res[1].a[0][partial] // { text: '{' }
```

They can drop at any depth level as they wish.

---

今天顺便集成了 StackBlitz 的 pkg.pr.new 的**持续发布**

<img src="/nlark/yuque/0/2024/png/26070246/1726581062704-338f07c2-3c64-4c83-b59c-040b176e97aa.png" width="715.4285714285714" title="" crop="0,0,1,1" id="u39d70031" class="ne-image">

现在每次 commit 都会可以被安装了🥳