-
-
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.
Merge pull request #5 from SecJS/feat/len-json-api
feat/fix: Add class Json and adjust removeQueryParams method
- Loading branch information
Showing
4 changed files
with
111 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
export class Json { | ||
/** | ||
* Verify if array is and array of objects | ||
* | ||
* @param array The array to be validated | ||
* @return true or false | ||
*/ | ||
isArrayOfObjects(array: any[]): boolean { | ||
if (!array.length) return false | ||
|
||
const results = array.map(object => typeof object === 'object') | ||
|
||
return !results.includes(false) | ||
} | ||
|
||
/** | ||
* Find all JSON's inside string and return it. | ||
* @param text A valid string with one or more JSON's inside. | ||
* @returns An array of JSON's found in the string. | ||
*/ | ||
getJson(text: string): string[] { | ||
let match: RegExpExecArray | ||
const matchs = [] | ||
|
||
while ((match = /{(?:[^{}])*}/.exec(text)) !== null) { | ||
text = text.replace(match[0], '') | ||
|
||
matchs.push(match[0]) | ||
} | ||
|
||
return matchs | ||
} | ||
|
||
/** | ||
* Converts a JavaScript Object Notation (JSON) string into an object without exception. | ||
* @param text A valid JSON string. | ||
* @param reviver A function that transforms the results. This function is called for each member of the object. | ||
* If a member contains nested objects, the nested objects are transformed before the parent object is. | ||
*/ | ||
parse( | ||
text: string, | ||
reviver?: (this: any, key: string, value: any) => any, | ||
): any { | ||
try { | ||
return JSON.parse(text, reviver) | ||
} catch (error) { | ||
return null | ||
} | ||
} | ||
} |
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,45 @@ | ||
import { Json } from '../../src/Classes/Json' | ||
|
||
describe('\n Json Class', () => { | ||
let json: Json | ||
|
||
beforeAll(() => { | ||
json = new Json() | ||
}) | ||
|
||
it('should verify if the array is an array of objects', async () => { | ||
const data = [ | ||
{ | ||
hello: 'hello', | ||
world: 'world', | ||
}, | ||
] | ||
|
||
expect(json.isArrayOfObjects(data)).toBe(true) | ||
expect(json.isArrayOfObjects([])).toBe(false) | ||
expect(json.isArrayOfObjects([1, 2, 3])).toBe(false) | ||
}) | ||
|
||
it('should return all json found inside of the string', () => { | ||
const text = | ||
'this is a string with a json inside of it {"text":"hello"} and one more json {"hello":"world"}' | ||
|
||
console.log(json.getJson(text)) | ||
expect(json.getJson(text)).toStrictEqual([ | ||
'{"text":"hello"}', | ||
'{"hello":"world"}', | ||
]) | ||
}) | ||
|
||
it('should return null if JSON parse goes wrong', () => { | ||
const text = 'a string that is not a valid JSON' | ||
|
||
expect(json.parse(text)).toBe(null) | ||
}) | ||
|
||
it('should return the object when string is a valid JSON', () => { | ||
const text = '{"text":"hello"}' | ||
|
||
expect(json.parse(text)).toStrictEqual({ text: 'hello' }) | ||
}) | ||
}) |