Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
fix: cleanParameters now works with array of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed May 26, 2020
1 parent faf049a commit 27324d7
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export interface MWSOptions {
}

type HttpMethod = 'GET' | 'POST'
type Parameters = Record<string, string | number | (number | string)[] | undefined>
type ParameterTypes = string | number | (number | string)[] | object[] | boolean | undefined
type Parameters = Record<string, ParameterTypes>
type CleanParameters = Record<string, string>

export enum Resource {
Expand Down Expand Up @@ -134,14 +135,35 @@ const canonicalizeParameters = (parameters: CleanParameters): string => {
return sp.toString().replace(/\+/g, '%20')
}

const cleanParameters = (parameters: Parameters): CleanParameters =>
export const toDotNotation = (object: object, prefix: string) => {
const result: { [key: string]: string | number | boolean } = {}
function dotify(plainObject: object, currentKey?: string | number) {
Object.entries(plainObject).forEach(([key, value]) => {
const newKey = currentKey ? `${currentKey}.${key}` : key // joined key with dot
if (value && typeof value === 'object') {
dotify(value, newKey) // it's a nested object, so do it again
} else {
Object.assign(result, { [`${prefix}.${newKey}`]: value }) // it's not an object, so set the property
}
})
}

dotify(object)
return result
}

export const cleanParameters = (parameters: Parameters): CleanParameters =>
Object.entries(parameters)
.filter(([, v]) => v !== undefined)
.reduce((result, [k, v]) => {
if (Array.isArray(v)) {
for (let index = 0; index < v.length; index += 1) {
Object.assign(result, { [`${k}.${index + 1}`]: String(v) })
}
v.forEach((element: string | number | object, index: number) => {
if (typeof element === 'string' || !Number.isNaN(Number(element))) {
Object.assign(result, { [`${k}.${index + 1}`]: String(element) })
} else {
Object.assign(result, toDotNotation(element as object, `${k}.${index + 1}`))
}
})
} else {
Object.assign(result, { [k]: String(v) })
}
Expand Down

0 comments on commit 27324d7

Please sign in to comment.