UNPKG

json-restructure

Version:

A TypeScript library for repairing malformed JSON strings

35 lines 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../src/index"); const types_1 = require("../src/types"); describe('fixJson', () => { it('should return the original string for valid JSON', () => { const validJson = '{"name":"John","age":30}'; const result = (0, index_1.fixJson)(validJson); expect(result).toBe(validJson); }); it('should repair and return a valid JSON string for malformed JSON', () => { const malformedJson = '{"name":"John","age":30,}//Trailing comma'; const expectedJson = '{"name":"John","age":30}'; const result = (0, index_1.fixJson)(malformedJson); expect(result).toBe(expectedJson); }); it('should throw a JsonParseError for invalid JSON that cannot be repaired', () => { const invalidJson = '{name:"John",age:30}'; expect(() => (0, index_1.fixJson)(invalidJson)).toThrowError(types_1.JsonParseError); }); }); describe('repairJson', () => { it('should repair and return a valid JSON string for malformed JSON with trailing commas and comments', () => { const malformedJson = '{"name":"John","age":30,}//Trailing comma'; const expectedJson = '{"name":"John","age":30}'; const result = (0, index_1.repairJson)(malformedJson); expect(result).toBe(expectedJson); }); it('should return the original string for valid JSON', () => { const validJson = '{"name":"John","age":30}'; const result = (0, index_1.repairJson)(validJson); expect(result).toBe(validJson); }); }); //# sourceMappingURL=index.test.js.map