Skip to content

Commit

Permalink
Merge pull request #175 from blackflux/dev
Browse files Browse the repository at this point in the history
[Gally]: master <- dev
  • Loading branch information
simlu authored Jun 11, 2024
2 parents 13bd30b + 55d9378 commit ff8775f
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ contains({ a: [1, 2], b: 'c' }, { a: [1] });
// => false
```

### jsonSmartParse(str: String)

Deals with parsing invalid json outputs that are commonly generated by llms

### Merge(logic: Object = {})(...obj: Object[])

Allows merging of objects. The logic defines paths that map to a field, or a function, to merge by.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"js-gardener": "5.0.4",
"lodash.clonedeep": "4.5.0",
"lodash.samplesize": "4.2.0",
"node-tdd": "5.2.4"
"node-tdd": "5.2.4",
"smart-fs": "4.0.1"
},
"licenses": [
{
Expand Down
29 changes: 29 additions & 0 deletions src/core/json-smart-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default (str) => {
let result = str;
if (result.includes('```')) {
result = result.split(/```(?:json)?/)[1];
}

const unescaped = [];
let escaped = false;
let quoted = false;
for (let i = 0; i < result.length; i += 1) {
const char = result[i];
if (['\n', '\r', '\t'].includes(char) && quoted) {
unescaped.push(i);
}
if (!escaped && char === '"') {
quoted = !quoted;
}
escaped = char === '\\' ? !escaped : false;
}

result = result.replace(/\n/g, (m, idx) => {
if (unescaped.includes(idx)) {
return JSON.stringify(m).slice(1, -1);
}
return m;
});

return JSON.parse(result);
};
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as align } from './core/align.js';
export { default as clone } from './core/clone.js';
export { default as contains } from './core/contains.js';
export { default as Merge } from './core/merge.js';
export { default as jsonSmartParse } from './core/json-smart-parse.js';
18 changes: 18 additions & 0 deletions test/core/json-smart-parse.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'smart-fs';
import { expect } from 'chai';
import { describe } from 'node-tdd';
import jsonSmartParse from '../../src/core/json-smart-parse.js';

const filename = fs.filename(import.meta.url);
const fixtureDir = `${filename}__fixtures`;

describe('Testing jsonSmartParse', () => {
// eslint-disable-next-line mocha/no-setup-in-describe
fs.walkDir(fixtureDir).forEach((file) => {
it(`Testing ${file}`, async ({ fixture }) => {
const { input, expected } = fixture(file);
const result = jsonSmartParse(input);
expect(result).to.deep.equal(expected);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "{ \"x\": \"\nhello\\\"\nyou\n\" }",
"expected": {
"x": "\nhello\"\nyou\n"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "{ \"x\\\"y\": \"\nhello\\\"\nyou\n\" }",
"expected": {
"x\"y": "\nhello\"\nyou\n"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "\n{\n \n\"x\":\n\"\nhello\n\nyou\n\"\n \n}\n",
"expected": {
"x": "\nhello\n\nyou\n"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "{ \"x\": \"hello\\\nyou\" }",
"expected": {
"x": "hello\\nyou"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "{ \"x\": \"hello\nyou\" }",
"expected": {
"x": "hello\nyou"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "```json{ \"x\": \"hello\nyou\" }```",
"expected": {
"x": "hello\nyou"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"input": "```{ \"x\": \"hello\nyou\" }```",
"expected": {
"x": "hello\nyou"
}
}
3 changes: 2 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe('Testing index.js', () => {
'Merge',
'align',
'clone',
'contains'
'contains',
'jsonSmartParse'
]);
});
});

0 comments on commit ff8775f

Please sign in to comment.