Skip to content

Commit

Permalink
Support array of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeTschudi committed Nov 16, 2020
1 parent 7cc8b02 commit fd3b1a9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
14 changes: 13 additions & 1 deletion packages/arcgis-rest-request/src/utils/encode-query-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@

import { processParams } from "./process-params";

export function encodeParam(key: string, value: any) {
/**
* Encodes keys and parameters for use in a URL's query string.
*
* @param key Parameter's key
* @param value Parameter's value
* @returns Query string with key and value pairs separated by "&"
*/
export function encodeParam(key: string, value: any): string {
// For array of arrays, repeat key=value for each element of containing array
if (Array.isArray(value) && value[0] && Array.isArray(value[0])) {
return value.map((arrayElem: string) => encodeParam(key, arrayElem)).join("&");
}

return encodeURIComponent(key) + "=" + encodeURIComponent(value);
}

Expand Down
15 changes: 7 additions & 8 deletions packages/arcgis-rest-request/src/utils/process-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ export function processParams(params: any): any {
// null, undefined, function are excluded. If you want to send an empty key you need to send an empty string "".
switch (type) {
case "Array":
// Based on the first element of the array, classify array as an array of objects to be stringified
// or an array of non-objects to be comma-separated
// Based on the first element of the array, classify array as an array of arrays, an array of objects
// to be stringified, or an array of non-objects to be comma-separated
const firstElementType = param[0]?.constructor?.name;
value =
param[0] &&
param[0].constructor &&
param[0].constructor.name === "Object"
? JSON.stringify(param)
: param.join(",");
firstElementType === "Array" ? param : // pass thru array of arrays
firstElementType === "Object" ? JSON.stringify(param) : // stringify array of objects
param.join(","); // join other types of array elements
break;
case "Object":
value = JSON.stringify(param);
Expand All @@ -100,7 +99,7 @@ export function processParams(params: any): any {
value = param;
break;
}
if (value || value === 0 || typeof value === "string") {
if (value || value === 0 || typeof value === "string" || Array.isArray(value)) {
newParams[key] = value;
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { encodeParam } from "../../src/utils/encode-query-string";

describe("encodeQueryString", () => {
it("should encode simple value", () => {
expect(encodeParam("key", "value")).toEqual("key=value");
});

it("should encode array", () => {
expect(encodeParam("key", ["value1", "value2"])).toEqual("key=value1%2Cvalue2");
});

it("should encode array of arrays", () => {
expect(encodeParam("key", [["value1a", "value1b"], ["value2"]])).toEqual("key=value1a%2Cvalue1b&key=value2");
});
});
14 changes: 13 additions & 1 deletion packages/arcgis-rest-request/test/utils/process-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("processParams", () => {
expect(processParams(params)).toEqual(expected);
});

it("should comma seperate arrays of non objects", () => {
it("should comma separate arrays of non objects", () => {
const params = {
foo: ["bar", "baz"]
};
Expand All @@ -80,6 +80,18 @@ describe("processParams", () => {
expect(processParams(params)).toEqual(expected);
});

it("should pass array of arrays through", () => {
const params = {
foo: [["bar1"], ["baz1", "baz2"]]
};

const expected = {
foo: [["bar1"], ["baz1", "baz2"]]
};

expect(processParams(params)).toEqual(expected);
});

it("should stringify booleans", () => {
const params = {
foo: true,
Expand Down

0 comments on commit fd3b1a9

Please sign in to comment.