早上送走爸妈之后才发现今天没看 GitHub 通知,一打开
大部分都是垃圾信息,但是发现居然有人给我的 partial-json
提了一个 PR
Sourcery 的总结还挺好的,一看就懂
说起来,很戏剧性地,由于我这个项目是刚开始用 JavaScript 的时候写的,当时还不太懂 CI,于是没有配 formatter 的 config,这个人直接不小心把所有文件都重新格式化了,造成 600 行的 diff 😂
说回到这个功能,虽然我自己先前也想搞这么个东西,但是由于觉得想不到完美的设计,所以就没干了。
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:
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:
parse(`[0, {"a": [{`, { allow: ALL, (parsed, text, parents) => { ... } })
The validate function will be called at most 4 times with
validate({}, '{', [{ type: "ARR", index: 0}, { type: "OBJ", key: "a" }, { type: "ARR", index: 1}])
validate([{}], '[{', [{ type: "OBJ", key: "a" }, { type: "ARR", index: 1}])
validate({"a": {}}, '{"a": [{', [{ type: "ARR", index: 1}])
validate([0, {"a": {}}], '[0, {"a": [{', [])
Approach Two — Return PartialInfo
Inject some information into the return value. Like this:
export const partial = Symbol('__partial__');
And the partial information may be like this:
interface PartialInfo {
text: string;
}
Then users can filter the result themselves using this information.
For example, if using this way:
> 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 的持续发布
现在每次 commit 都会可以被安装了🥳