Skip to content

Commit

Permalink
feat(hub-common): add capitalize() and getServiceTypeFromUrl()
Browse files Browse the repository at this point in the history
affects: @esri/hub-common
  • Loading branch information
tomwayson committed Aug 30, 2021
1 parent 9d04b8c commit f66e08a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/common/src/urls/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { capitalize } from "../util";
export * from "./build-url";
export * from "./get-hub-locale-asset-url";
export * from "./get-portal-api-url";
Expand All @@ -12,6 +13,25 @@ export * from "./get-item-home-url";
export * from "./get-item-api-url";
export * from "./get-item-data-url";

const MAP_OR_FEATURE_SERVER_URL_REGEX = /\/(map|feature)server/i;

/**
*
* @param url
* @returns true if the url is of a map or feature service
*/
export const isMapOrFeatureServerUrl = (url: string) => {
return /\/(map|feature)server/i.test(url);
return MAP_OR_FEATURE_SERVER_URL_REGEX.test(url);
};

/**
* parses map or feature service type from URL
* @param url map or feature service URL
* @returns item type, either "Map Service" or "Feature Service"
* or undefined for other types of URLs
*/
export const getServiceTypeFromUrl = (url: string) => {
const match = url.match(MAP_OR_FEATURE_SERVER_URL_REGEX);
const mapOrFeature = match && match[1];
return mapOrFeature && `${capitalize(mapOrFeature)} Service`;
};
12 changes: 12 additions & 0 deletions packages/common/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,15 @@ export function chunkArray(arr: any[], size: number) {
* @returns
*/
export const isNil = (value: unknown) => value == null;

/**
* Upper case first letter (only) of a string
* @param word
* @returns Word
*/
export const capitalize = (word: string) => {
// upper case first letter and return as element in array for backwards compatibility
const chars = Array.from(word);
chars[0] = chars[0].toUpperCase();
return chars.join("");
};

0 comments on commit f66e08a

Please sign in to comment.