Skip to content

Commit 02c86ef

Browse files
authored
Merge pull request #5 from SecJS/feat/len-json-api
feat/fix: Add class Json and adjust removeQueryParams method
2 parents f67e162 + 478080f commit 02c86ef

File tree

4 files changed

+111
-7
lines changed

4 files changed

+111
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/utils",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"description": "",
55
"scripts": {
66
"build": "tsc",

src/Classes/Json.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export class Json {
2+
/**
3+
* Verify if array is and array of objects
4+
*
5+
* @param array The array to be validated
6+
* @return true or false
7+
*/
8+
isArrayOfObjects(array: any[]): boolean {
9+
if (!array.length) return false
10+
11+
const results = array.map(object => typeof object === 'object')
12+
13+
return !results.includes(false)
14+
}
15+
16+
/**
17+
* Find all JSON's inside string and return it.
18+
* @param text A valid string with one or more JSON's inside.
19+
* @returns An array of JSON's found in the string.
20+
*/
21+
getJson(text: string): string[] {
22+
let match: RegExpExecArray
23+
const matchs = []
24+
25+
while ((match = /{(?:[^{}])*}/.exec(text)) !== null) {
26+
text = text.replace(match[0], '')
27+
28+
matchs.push(match[0])
29+
}
30+
31+
return matchs
32+
}
33+
34+
/**
35+
* Converts a JavaScript Object Notation (JSON) string into an object without exception.
36+
* @param text A valid JSON string.
37+
* @param reviver A function that transforms the results. This function is called for each member of the object.
38+
* If a member contains nested objects, the nested objects are transformed before the parent object is.
39+
*/
40+
parse(
41+
text: string,
42+
reviver?: (this: any, key: string, value: any) => any,
43+
): any {
44+
try {
45+
return JSON.parse(text, reviver)
46+
} catch (error) {
47+
return null
48+
}
49+
}
50+
}

src/Classes/Route.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class Route {
1010
getQueryString(route: string): string {
1111
const queryIndex = route.search(/\?(.*)/)
1212

13-
if (queryIndex === -1) return route
13+
if (queryIndex === -1) return null
1414

1515
return route.substring(queryIndex)
1616
}
@@ -22,9 +22,11 @@ export class Route {
2222
* @return The route without the query params
2323
*/
2424
removeQueryParams(route: string): string {
25-
if (this.getQueryString(route) === route) return route
25+
const queryString = this.getQueryString(route)
2626

27-
return route.replace(this.getQueryString(route), '')
27+
if (!queryString) return route
28+
29+
return route.replace(queryString, '')
2830
}
2931

3032
/**
@@ -34,7 +36,11 @@ export class Route {
3436
* @return The object of queryParams found inside route
3537
*/
3638
getQueryParamsValue(route: string): any {
37-
return new Parser().formDataToJson(this.getQueryString(route))
39+
const queryString = this.getQueryString(route)
40+
41+
if (!queryString) return {}
42+
43+
return new Parser().formDataToJson(queryString)
3844
}
3945

4046
/**
@@ -47,6 +53,8 @@ export class Route {
4753
const queryNames = []
4854
let queryString = this.getQueryString(route)
4955

56+
if (!queryString) return []
57+
5058
if (queryString.startsWith('?')) queryString = queryString.replace('?', '')
5159

5260
queryString
@@ -61,7 +69,8 @@ export class Route {
6169
/**
6270
* Get object with :params values from route
6371
*
64-
* @param route The route to get the params
72+
* @param routeWithParams The route with the :params
73+
* @param routeWithValues The route with the :params values
6574
* @return The object of params found inside route
6675
*/
6776
getParamsValue(routeWithParams: string, routeWithValues: string): any {
@@ -125,7 +134,7 @@ export class Route {
125134
routeArray.forEach((r, i) => {
126135
if (r === '') return
127136
if (r.startsWith(':')) {
128-
// Match with any value RegExp
137+
// Match with any word and - value RegExp
129138
routeArray[i] = `(?:\\/[\\w-]+)`
130139

131140
return

tests/Classes/json.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Json } from '../../src/Classes/Json'
2+
3+
describe('\n Json Class', () => {
4+
let json: Json
5+
6+
beforeAll(() => {
7+
json = new Json()
8+
})
9+
10+
it('should verify if the array is an array of objects', async () => {
11+
const data = [
12+
{
13+
hello: 'hello',
14+
world: 'world',
15+
},
16+
]
17+
18+
expect(json.isArrayOfObjects(data)).toBe(true)
19+
expect(json.isArrayOfObjects([])).toBe(false)
20+
expect(json.isArrayOfObjects([1, 2, 3])).toBe(false)
21+
})
22+
23+
it('should return all json found inside of the string', () => {
24+
const text =
25+
'this is a string with a json inside of it {"text":"hello"} and one more json {"hello":"world"}'
26+
27+
console.log(json.getJson(text))
28+
expect(json.getJson(text)).toStrictEqual([
29+
'{"text":"hello"}',
30+
'{"hello":"world"}',
31+
])
32+
})
33+
34+
it('should return null if JSON parse goes wrong', () => {
35+
const text = 'a string that is not a valid JSON'
36+
37+
expect(json.parse(text)).toBe(null)
38+
})
39+
40+
it('should return the object when string is a valid JSON', () => {
41+
const text = '{"text":"hello"}'
42+
43+
expect(json.parse(text)).toStrictEqual({ text: 'hello' })
44+
})
45+
})

0 commit comments

Comments
 (0)