Skip to content

Commit

Permalink
Merge pull request #5 from SecJS/feat/len-json-api
Browse files Browse the repository at this point in the history
feat/fix: Add class Json and adjust removeQueryParams method
  • Loading branch information
jlenon7 authored Aug 19, 2021
2 parents f67e162 + 478080f commit 02c86ef
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/utils",
"version": "1.3.0",
"version": "1.3.1",
"description": "",
"scripts": {
"build": "tsc",
Expand Down
50 changes: 50 additions & 0 deletions src/Classes/Json.ts
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
}
}
}
21 changes: 15 additions & 6 deletions src/Classes/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Route {
getQueryString(route: string): string {
const queryIndex = route.search(/\?(.*)/)

if (queryIndex === -1) return route
if (queryIndex === -1) return null

return route.substring(queryIndex)
}
Expand All @@ -22,9 +22,11 @@ export class Route {
* @return The route without the query params
*/
removeQueryParams(route: string): string {
if (this.getQueryString(route) === route) return route
const queryString = this.getQueryString(route)

return route.replace(this.getQueryString(route), '')
if (!queryString) return route

return route.replace(queryString, '')
}

/**
Expand All @@ -34,7 +36,11 @@ export class Route {
* @return The object of queryParams found inside route
*/
getQueryParamsValue(route: string): any {
return new Parser().formDataToJson(this.getQueryString(route))
const queryString = this.getQueryString(route)

if (!queryString) return {}

return new Parser().formDataToJson(queryString)
}

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

if (!queryString) return []

if (queryString.startsWith('?')) queryString = queryString.replace('?', '')

queryString
Expand All @@ -61,7 +69,8 @@ export class Route {
/**
* Get object with :params values from route
*
* @param route The route to get the params
* @param routeWithParams The route with the :params
* @param routeWithValues The route with the :params values
* @return The object of params found inside route
*/
getParamsValue(routeWithParams: string, routeWithValues: string): any {
Expand Down Expand Up @@ -125,7 +134,7 @@ export class Route {
routeArray.forEach((r, i) => {
if (r === '') return
if (r.startsWith(':')) {
// Match with any value RegExp
// Match with any word and - value RegExp
routeArray[i] = `(?:\\/[\\w-]+)`

return
Expand Down
45 changes: 45 additions & 0 deletions tests/Classes/json.spec.ts
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' })
})
})

0 comments on commit 02c86ef

Please sign in to comment.