-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { deepStrictEqual, throws } from "assert"; | ||
import { jsonparse } from "./jsonparse"; // Ensure this path is correct | ||
|
||
describe('jsonparse', function () { | ||
const TEST_CASES = [ | ||
{ | ||
input: "{\"kirs\":\"OKE\"}", | ||
expected: "Failed to parse JSON", | ||
isThrow: true | ||
}, | ||
{ | ||
input: `${"{\"kirs\":\"OKE\",\"name\":\"kris\"}"}`, | ||
expected: "Failed to parse JSON", | ||
isThrow: true | ||
} | ||
]; | ||
|
||
TEST_CASES.forEach(({ input, expected, isThrow}) => { | ||
it(`Should convert ${input} to ${JSON.stringify(expected)}`, function () { | ||
if (isThrow) { | ||
throws(() => jsonparse(input), new Error(expected)); | ||
} else { | ||
let result = jsonparse(input); | ||
console.log(`${typeof(input)} to ${typeof(expected)} KRIS`); | ||
|
||
deepStrictEqual(result, expected); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Transfor String to JSON. | ||
*/ | ||
export const jsonparse = (str: string) => { | ||
try { | ||
const text = JSON.parse(str).toString(); | ||
if (text === '[object Object]') { | ||
throw new Error("Failed to parse JSON"); | ||
} | ||
return text; | ||
} catch (error) { | ||
throw new Error("Failed to parse JSON"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { strictEqual } from "assert"; | ||
import { jsonstringify } from "./jsonstringify"; | ||
|
||
describe('jsonstringify', function () { | ||
|
||
const TEST_CASES = [ | ||
{ | ||
input: {"kirs": "OKE"}, | ||
expected: "{\"kirs\":\"OKE\"}" | ||
}, | ||
{ | ||
input: { | ||
"kirs": "OKE", | ||
"name": "kris" | ||
}, | ||
expected: "{\"kirs\":\"OKE\",\"name\":\"kris\"}" | ||
} | ||
]; | ||
|
||
TEST_CASES.forEach(({ input, expected }: any) => { | ||
it(`Sould convert ${input} to ${expected}`, function () { | ||
let result = jsonstringify(input); | ||
strictEqual(result, expected.toString()); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Transfor JSON to string. | ||
*/ | ||
export const jsonstringify = (str: string) => JSON.stringify(str).toString(); |