From 0d3266303a5fa1643c089d4f5f8bf96e924654f1 Mon Sep 17 00:00:00 2001 From: Tom Wayson Date: Tue, 1 Sep 2020 11:28:42 -0700 Subject: [PATCH] feat(dataset slugs): add addContextToSlug() to prefix slug with context (orgKey) AFFECTS PACKAGES: @esri/hub-content --- packages/content/src/slugs.ts | 17 +++++++++++++++++ packages/content/test/slugs.test.ts | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/content/src/slugs.ts b/packages/content/src/slugs.ts index 5c9568a2a49..f2341632bbb 100644 --- a/packages/content/src/slugs.ts +++ b/packages/content/src/slugs.ts @@ -17,3 +17,20 @@ export function isSlug(identifier: string): boolean { // otherwise assume it's a slug return true; } + +/** + * Add a context (prefix) to slug if it doesn't already have one + * + * @param slug Hub API slug (with or without context) + * @param context usually a portal's orgKey + * @returns slug with context ({context}::{slug}) + */ +export function addContextToSlug(slug: string, context: string): string { + // the slug has an org key already e.g. dc::crime-incidents + if (/.+::.+/.test(slug)) { + return slug; + // the slug belongs to the org that owns the site e.g. crime-incidents + } else { + return `${context}::${slug}`; + } +} diff --git a/packages/content/test/slugs.test.ts b/packages/content/test/slugs.test.ts index 32d5a88dfb4..34fd504cc0e 100644 --- a/packages/content/test/slugs.test.ts +++ b/packages/content/test/slugs.test.ts @@ -1,4 +1,4 @@ -import { isSlug } from "../src/slugs"; +import { isSlug, addContextToSlug } from "../src/slugs"; describe("slugs", () => { describe("isSlug", function() { @@ -19,4 +19,21 @@ describe("slugs", () => { expect(result).toBe(true); }); }); + describe("addContextToSlug", () => { + const title = "foo-bar"; + const orgKey = "org-key"; + const slugWithContext = `${orgKey}::${title}`; + it("appends the context to slug without context", () => { + const slug = addContextToSlug(title, orgKey); + expect(slug).toBe(slugWithContext); + }); + it("returns the slug as is when it already has context", () => { + const slug = addContextToSlug(slugWithContext, orgKey); + expect(slug).toBe(slugWithContext); + }); + it("returns the slug as is when it has a different context", () => { + const slug = addContextToSlug(slugWithContext, "another-org"); + expect(slug).toBe(slugWithContext); + }); + }); });