Skip to content

Commit

Permalink
updated function to be able to take booleans as values
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilt committed Oct 15, 2018
1 parent 3286dbd commit fd51124
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export type QueryString = string;

export type ValidValue = string | number | boolean;

export interface ParamObject {
[key: string]: string | number | (string | number)[];
[key: string]: ValidValue | ValidValue[];
}

export enum ParamError {
Expand All @@ -24,6 +26,7 @@ function checkIfObjectisValid(paramObject: ParamObject): void {
if (
typeof value !== "string" &&
typeof value !== "number" &&
typeof value !== "boolean" &&
!Array.isArray(value) &&
value !== undefined &&
value !== null
Expand All @@ -47,8 +50,12 @@ function checkIfObjectisValid(paramObject: ParamObject): void {
});
}

export function isValidValue(val: number | string) {
return typeof val === "number" || (typeof val === "string" && !!val.length);
export function isValidValue(val: ValidValue) {
return (
typeof val === "number" ||
(typeof val === "string" && !!val.length) ||
typeof val === "boolean"
);
}

/** This function removes any undefined/null properties from an object as well as any undefined/null values in any arrays on the object */
Expand All @@ -69,7 +76,7 @@ export function removeEmpty(
delete current[key];
}
} else if (isValidValue(obj[key])) {
current[key] = obj[key];
current[key] = obj[key].toString();
}

return current;
Expand Down
18 changes: 15 additions & 3 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ test(`It returns a query string when an integer is passed in`, t => {
t.end();
});

test(`It returns a query string when a boolean is passed in`, t => {
const object = {
a: false
};

const result = objToQueryString(object);

t.equal(result, "a=false");
t.end();
});

test(`It can handle an integer as a key`, t => {
const object = {
3: 1
Expand Down Expand Up @@ -132,19 +143,20 @@ test(`It will handle an array of strings by creating the format 'arrayName[]=val
t.end();
});

test(`It will handle integers, strings, and multiple array of strings of strings/numbers by creating the format 'arrayName[]=val1&arrayName[]=val2`, t => {
test(`It will handle integers, strings, booleans, and multiple array of strings of strings/numbers by creating the format 'arrayName[]=val1&arrayName[]=val2`, t => {
const object = {
a: ["a string", "the second string", "the third string", 2],
b: 3,
c: "a string by itself",
4: [1, "more", "array of stuff"]
4: [1, "more", "array of stuff"],
d: false
};

const result = objToQueryString(object);

t.equal(
result,
"4[]=1&4[]=more&4[]=array%20of%20stuff&a[]=a%20string&a[]=the%20second%20string&a[]=the%20third%20string&a[]=2&b=3&c=a%20string%20by%20itself"
"4[]=1&4[]=more&4[]=array%20of%20stuff&a[]=a%20string&a[]=the%20second%20string&a[]=the%20third%20string&a[]=2&b=3&c=a%20string%20by%20itself&d=false"
);
t.end();
});
Expand Down

0 comments on commit fd51124

Please sign in to comment.