Skip to content

Commit

Permalink
formatItemCollection added
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkulkarni committed Apr 11, 2019
1 parent ef9f8ef commit 7907338
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions packages/search/src/ago/ago-response-to-jsonapi-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ISearchRequest } from "@esri/arcgis-rest-items";

/*
* This util function take in a JSON object response from /sharing/rest/search
* and return JSONAPI-format page parameters
*
* example AGO API response
* {
* nextStart: 31,
* num: 10,
* query: '(group:aa9b3013b77141cca147ac540139b353 OR group:3b8014ff38a9422ea99303979fa4d539)',
* results: [ array of items ],
* start: 21,
* total: 147
* }
*
* should return =>
* {
* number: 3,
* size: 10
* }
*/

export function convertAgoPages(
response: any
): { number: number; size: number } {
return {
number: Math.floor(response.start / response.num) + 1,
size: response.num
};
}
35 changes: 35 additions & 0 deletions packages/search/src/ago/format-item-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ISearchResult } from "@esri/arcgis-rest-items";
import { ISearchParams } from "../common/params";
import { formatItem } from "./format-item";
import { convertAgoPages } from "./ago-response-to-jsonapi-page";

// This function is responsible for formatting results from the AGO API into the JSONAPI collection format
export function formatItemCollection(
searchResults: ISearchResult,
facets: any = {},
params: ISearchParams
) {
// console.log('In hub.js => formatItemCollection');
const queryParams = queryParameters(searchResults, params);
return {
data: searchResults.results.map(result => {
return formatItem(result, queryParams.q);
}),
meta: {
query: searchResults.query,
queryParameters: queryParams,
stats: {
aggs: facets,
count: searchResults.results.length,
totalCount: searchResults.total
}
}
};
}

function queryParameters(searchResults: ISearchResult, params: ISearchParams) {
const queryParams: any = {
page: convertAgoPages(searchResults)
};
return Object.assign({}, params, queryParams);
}
64 changes: 64 additions & 0 deletions packages/search/src/ago/format-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// helper functions to make items look more like datasets
//
// item queries will look like this ->
// {
// data: [{
// id: item-id,
// type: 'item',
// attributes: {
// // will have attribute 'parity' with datasets. Many of these values will be null.
// },
// meta: { highlights: {} }
// links: {
// // computed links for the item result. These can be used throughout the app when linking to
// // AGO, APIs, etc
// }
// }],
// meta: {
// aggs: {
// // built by another query against AGO API and then formatted in the same format as the V3 API
// },
// stats: {
// count:,
// totalCount:,
// }
// }
// }
import { hubTypeLookup } from "../common/hub-type-map";
import { calcHighlights } from "../common/highlights";
import { IItem } from "@esri/arcgis-rest-common-types";

export function formatItem(item: IItem, query: string) {
const formattedItem: any = {
id: item.id,
type: "item",
attributes: formatItemAttributes(item)
};
if (query) {
if (!item.meta) {
// create highlights since AGO deos not return them
formattedItem.meta = {};
formattedItem.meta.highlights = highlights(item, query);
}
}
return formattedItem;
}

function formatItemAttributes(item: IItem) {
const hubTypes = hubTypeLookup(item.type);
return Object.assign({}, item, {
/* computed or null attributes so items & datasets look the same */
name: item.title,
searchDescription: item.description,
hubType: hubTypes[0] || "Other"
});
}

function highlights(item: IItem, query: string) {
// calculate highlights based on AGO restricted item, hence use description field but return as `searchDescription`
// because the search-result/component expects searchDescription
return {
name: calcHighlights(item.title, query, "name"),
searchDescription: calcHighlights(item.description, query, "description")
};
}
1 change: 1 addition & 0 deletions packages/search/src/ago/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
* Apache-2.0 */

export * from "./search";
export * from "./format-item-collection";
16 changes: 16 additions & 0 deletions packages/search/src/common/highlights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function calcHighlights(input: string, query: string, property: string) {
// 1. identify all the matches case insensitively
// 2. Replace the original match(es) with mark tags
// We want to match case insensitively but highlight case sensitively the original term
// E.g. input string: `Capital bike share... blah blah capital.... CAPITAL`
// We would like to highlight: `Capital`, `capital`, `CAPITAL`
if (!input) return undefined;
const matches = input.match(new RegExp(query, "ig")); // search globally and case insensitively
if (!matches) return undefined;
return matches.reduce((highlights, match) => {
// match is what appears as is in the input string
const replacement = `<mark class="hub-search-highlight ${property}-highlight">${match}</mark>`;
// replace the case sensitive match with mark tags
return highlights.replace(new RegExp(match, "g"), replacement);
}, input);
}
5 changes: 4 additions & 1 deletion packages/search/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

export { search as ago } from "./ago";
export {
search as agoSearch,
formatItemCollection as agoFormatItemCollection
} from "./ago";
2 changes: 1 addition & 1 deletion test.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
q: 'land'
}
console.log(' -- ', arcgisHub)
arcgisHub.ago(params, authentication).then(response => {
arcgisHub.agoSearch(params, authentication).then(response => {
console.log(response)
})
</script>
Expand Down

0 comments on commit 7907338

Please sign in to comment.