-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CMR-10165: Extend sorting capabilities for CMR-STAC collection search (…
…#355) * CMR-10165: Adds support sorting collection result * CMR-10165: Added test for sortby * CMR-10165: Updates documentation for collection search * CMR-10165: Adds sort extension to conformance * CMR-10165: Addresses PR feedback * CMR-10165: Addressing PR feedback and updates the free text documentation * CMR-10165: Fixes lint error
- Loading branch information
Showing
9 changed files
with
240 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { expect } from "chai"; | ||
import { parseSortFields } from "../sort"; | ||
import { SortObject } from "../../models/StacModels"; | ||
|
||
describe("parseSortFields", () => { | ||
it("should return an empty array for undefined input", () => { | ||
const parsedField = parseSortFields(); | ||
|
||
expect(parsedField).to.deep.equal([]); | ||
}); | ||
|
||
it("should handle a single field in string based sorting (GET)", () => { | ||
const parsedField = parseSortFields("field1"); | ||
expect(parsedField).to.deep.equal(["field1"]); | ||
}); | ||
|
||
it("should handle multi field string based sorting (GET)", () => { | ||
const parsedField = parseSortFields("field1, -field2, field3"); | ||
|
||
expect(parsedField).to.deep.equal(["field1", "-field2", "field3"]); | ||
}); | ||
|
||
it("should handle a single object in object based sorting (POST)", () => { | ||
const input: SortObject[] = [{ field: "field1", direction: "desc" }]; | ||
expect(parseSortFields(input)).to.deep.equal(["-field1"]); | ||
}); | ||
|
||
it("should handle multi field object based sorting (POST)", () => { | ||
const input: SortObject[] = [ | ||
{ field: "field1", direction: "asc" }, | ||
{ field: "field2", direction: "desc" }, | ||
{ field: "field3", direction: "asc" }, | ||
]; | ||
expect(parseSortFields(input)).to.deep.equal(["field1", "-field2", "field3"]); | ||
}); | ||
|
||
it("should return an empty array for an empty array", () => { | ||
const parsedField = parseSortFields([]); | ||
|
||
expect(parsedField).to.deep.equal([]); | ||
}); | ||
|
||
it("should handle mixed array (treating non-strings as empty strings)", () => { | ||
const input: any[] = ["field1", { field: "field2", direction: "desc" }, "-field3"]; | ||
|
||
expect(parseSortFields(input)).to.deep.equal(["field1", "", "-field3"]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { SortObject } from "../models/StacModels"; | ||
|
||
/** | ||
* Parses sortby value into a single array | ||
* This function handles three possible input formats: | ||
* 1. A string of comma-separated sort fields (used in GET requests) | ||
* - /collections?sortby=endDate | ||
* 2. An array of SortObject (used in POST requests) | ||
* { | ||
"sortby": [ | ||
{ | ||
"field": "properties.endDate", | ||
"direction": "desc" | ||
} | ||
] | ||
} | ||
* 3. Undefined or null (returns an empty array) | ||
* | ||
* @param sortBys - The sortby value | ||
* @returns An array of strings, each representing a sort field. | ||
* Fields for descending sort are prefixed with '-'. | ||
*/ | ||
export const parseSortFields = (sortBys?: string | string[] | SortObject[]): string[] => { | ||
if (Array.isArray(sortBys)) { | ||
if (sortBys.length === 0) return []; | ||
|
||
if (typeof sortBys[0] === "object") { | ||
// Handle object-based sorting (POST) | ||
return (sortBys as SortObject[]).map( | ||
(sort) => `${sort.direction === "desc" ? "-" : ""}${sort.field}` | ||
); | ||
} else { | ||
// Handle array of strings | ||
return sortBys.map((item) => (typeof item === "string" ? item.trim() : "")); | ||
} | ||
} else if (typeof sortBys === "string") { | ||
// Handle string-based sorting (GET) | ||
return sortBys.split(",").map((key) => key.trim()); | ||
} | ||
|
||
return []; | ||
}; |