Skip to content

Commit

Permalink
feat(dataset slugs): add addContextToSlug() to prefix slug with conte…
Browse files Browse the repository at this point in the history
…xt (orgKey)

AFFECTS PACKAGES:
@esri/hub-content
  • Loading branch information
tomwayson committed Sep 1, 2020
1 parent e646267 commit 0d32663
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/content/src/slugs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
}
19 changes: 18 additions & 1 deletion packages/content/test/slugs.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isSlug } from "../src/slugs";
import { isSlug, addContextToSlug } from "../src/slugs";

describe("slugs", () => {
describe("isSlug", function() {
Expand All @@ -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);
});
});
});

0 comments on commit 0d32663

Please sign in to comment.