Skip to content

Commit

Permalink
minor clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sansth1010 committed Nov 21, 2024
1 parent 072a4ae commit 25b430c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/dcat-us/constants/contexts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Context header for DCAT US 1.1
export const HEADER_V_1_1 = {
export const HEADER_V_1X = {
'@context':
'https://project-open-data.cio.gov/v1.1/schema/catalog.jsonld',
'@type': 'dcat:Catalog',
Expand All @@ -9,7 +9,7 @@ export const HEADER_V_1_1 = {

// Context header for DCAT US 3.0
// source: https://raw.githubusercontent.com/DOI-DO/dcat-us/refs/heads/main/context/dcat-us-3.0.jsonld
export const HEADER_V_3 = {
export const HEADER_V_3X = {
'@context': {
'@version': 1.1,
'@protected': true,
Expand Down
7 changes: 3 additions & 4 deletions src/dcat-us/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readableFromArray, streamToString } from '../test-helpers/stream-utils';
import { getDataStreamDcatUs } from './';
import * as datasetFromApi from '../test-helpers/mock-dataset.json';
import { HEADER_V_3 } from './constants/contexts';
import { HEADER_V_3X } from './constants/contexts';

async function generateDcatFeed(dataset, template, templateTransforms, version) {
const { stream: dcatStream } = getDataStreamDcatUs(template, templateTransforms, version);
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('generating DCAT-US 3.0 feed', () => {
it('formats catalog correctly', async function () {
const { feed } = await generateDcatFeed([], {}, {}, version);

expect(feed['@context']).toStrictEqual(HEADER_V_3['@context']);
expect(feed['@context']).toStrictEqual(HEADER_V_3X['@context']);
expect(feed['conformsTo']).toBe('https://resource.data.gov/profile/dcat-us#');
expect(feed['@type']).toBe('dcat:Catalog');
expect(Array.isArray(feed['dcat:dataset'])).toBeTruthy();
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('generating DCAT-US 3.0 feed', () => {
},
version);

expect(feed['@context']).toStrictEqual(HEADER_V_3['@context']);
expect(feed['@context']).toStrictEqual(HEADER_V_3X['@context']);
expect(feed['@type']).toBe('dcat:Catalog');
expect(feed['@id']).toBe('hub.arcgis.com');
expect(feed['conformsTo']).toBe('https://resource.data.gov/profile/dcat-us#');
Expand All @@ -115,5 +115,4 @@ describe('generating DCAT-US 3.0 feed', () => {
expect(feedResponse.publisher).toStrictEqual({ name: 'QA Premium Alpha Hub' });
expect(feedResponse.keyword).toStrictEqual(['Data collection', 'just modified']);
});

});
26 changes: 17 additions & 9 deletions src/dcat-us/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import { compileDcatFeedEntry } from './compile-dcat-feed';
import { FeedFormatterStream } from './feed-formatter-stream';
import { TransformsList } from 'adlib';
import { HEADER_V_3, HEADER_V_1_1 } from './constants/contexts';
import { HEADER_V_3X, HEADER_V_1X } from './constants/contexts';

export function getDataStreamDcatUs(feedTemplate: any, feedTemplateTransforms: TransformsList, version: string) {
const footer = '\n\t]\n}';
let header: string;
let template: Record<string, any>;

if (version === '3.0') {
const { header: templateHeader, ...restFeedTemplate } = feedTemplate;
template = restFeedTemplate;
const catalogStr = JSON.stringify({ ...HEADER_V_3, ...templateHeader }, null, '\t');
header = `${catalogStr.substring(0, catalogStr.length - 2)},\n\t"dcat:dataset": [\n`;
header = generateDcatUs3XHeader(templateHeader);
}

if (version === '1.1') {
const catalogStr = JSON.stringify(HEADER_V_1_1, null, '\t');
header = `${catalogStr.substring(
0,
catalogStr.length - 2,
)},\n\t"dataset": [\n`;
template = feedTemplate;
header = generateDcatUs1XHeader();
}

const formatFn = (chunk) => {
Expand All @@ -32,3 +27,16 @@ export function getDataStreamDcatUs(feedTemplate: any, feedTemplateTransforms: T
stream: new FeedFormatterStream(header, footer, ',\n', formatFn)
};
}

function generateDcatUs1XHeader() {
const catalogStr = JSON.stringify(HEADER_V_1X, null, '\t');
return `${catalogStr.substring(
0,
catalogStr.length - 2,
)},\n\t"dataset": [\n`;
}

function generateDcatUs3XHeader(header: Record<string, any>) {
const catalogStr = JSON.stringify({ ...HEADER_V_3X, ...header }, null, '\t');
return `${catalogStr.substring(0, catalogStr.length - 2)},\n\t"dcat:dataset": [\n`;
}
4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createMockKoopApp } from './test-helpers/create-mock-koop-app';
import { readableFromArray } from './test-helpers/stream-utils';
import { DcatUsError } from './dcat-us/dcat-us-error';
import { PassThrough } from 'stream';
import { HEADER_V_3 } from './dcat-us/constants/contexts';
import { HEADER_V_3X } from './dcat-us/constants/contexts';

function buildPluginAndApp(feedTemplate, feedTemplateTransforms) {
let Output;
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('Output Plugin', () => {
expect(res.body).toBeDefined();
const dcatStream = res.body;
expect(dcatStream['@context']).toBeDefined();
expect(dcatStream['@context']).toStrictEqual(HEADER_V_3['@context']);
expect(dcatStream['@context']).toStrictEqual(HEADER_V_3X['@context']);
expect(dcatStream['@type']).toBe('dcat:Catalog');
expect(dcatStream['conformsTo']).toBe('https://resource.data.gov/profile/dcat-us#');
expect(dcatStream['@type']).toBe('dcat:Catalog');
Expand Down

0 comments on commit 25b430c

Please sign in to comment.