Skip to content

Commit

Permalink
Merge pull request #9 from stoplightio/feat/add-srn-utilities
Browse files Browse the repository at this point in the history
feat: add srn helper functions
  • Loading branch information
pytlesk4 committed Aug 26, 2019
2 parents 4badf21 + 7cb5987 commit b1e9f56
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -20,13 +20,15 @@
"node": ">=8"
},
"scripts": {
"build": "pegjs --optimize size src/grammar.pegjs && sl-scripts build",
"build": "yarn build.grammar && sl-scripts build",
"build.grammar": "pegjs --optimize size src/grammar.pegjs",
"commit": "git-cz",
"lint": "tslint -c tslint.json 'src/**/*.ts?'",
"lint.fix": "yarn lint --fix",
"release": "sl-scripts release",
"release.docs": "sl-scripts release:docs",
"release.dryRun": "sl-scripts release --dry-run --debug",
"pretest": "yarn build.grammar",
"test": "jest",
"test.prod": "yarn lint && yarn test --coverage --maxWorkers=2",
"test.update": "yarn test --updateSnapshot",
Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/srn.spec.ts
@@ -0,0 +1,61 @@
import { deserializeSrn, serializeSrn } from '../srn';

describe('deserializeSrn', () => {
it('should work deserialize org srns', () => {
expect(deserializeSrn('sl/org')).toEqual({
shortcode: 'sl',
orgSlug: 'org',
});
});

it('should work deserialize project srns', () => {
expect(deserializeSrn('sl/org/project')).toEqual({
shortcode: 'sl',
orgSlug: 'org',
projectSlug: 'project',
});
});

it('should work deserialize node srns', () => {
expect(deserializeSrn('sl/org/project/reference/todos/openapi.yml')).toEqual({
shortcode: 'sl',
orgSlug: 'org',
projectSlug: 'project',
uri: '/reference/todos/openapi.yml',
file: 'openapi.yml',
ext: '.yml',
});
});
});

describe('serializeSrn', () => {
it('should serialize org srns', () => {
expect(
serializeSrn({
shortcode: 'sl',
orgSlug: 'org',
}),
).toEqual('sl/org');
});

it('should serialize project srns', () => {
expect(
serializeSrn({
shortcode: 'sl',
orgSlug: 'org',
projectSlug: 'project',
}),
).toEqual('sl/org/project');
});

it('should serialize node srns', () => {
expect(
serializeSrn({
shortcode: 'sl',
orgSlug: 'org',
projectSlug: 'project',
uri: '/reference/todos/openapi.yml',
}),
).toEqual('sl/org/project/reference/todos/openapi.yml');
});
});
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -13,3 +13,4 @@ export * from './sep';
export * from './startsWithWindowsDrive';
export * from './toFSPath';
export * from './types';
export * from './srn';
37 changes: 37 additions & 0 deletions src/srn.ts
@@ -0,0 +1,37 @@
import { basename } from './basename';
import { extname } from './extname';

export interface IDeserializedSrn {
shortcode: string;
orgSlug: string;
projectSlug?: string;
uri?: string;
file?: string;
ext?: string;
}

export function deserializeSrn(srn: string): IDeserializedSrn {
const [shortcode, orgSlug, projectSlug, ...uriParts] = srn.split('/');

const uri = uriParts.length ? `/${uriParts.join('/')}` : undefined;

let file;
let ext;
if (uri) {
ext = extname(uri);
file = basename(uri);
}

return {
shortcode,
orgSlug,
projectSlug,
uri,
file,
ext,
};
}

export function serializeSrn({ shortcode, orgSlug, projectSlug, uri = '' }: IDeserializedSrn): string {
return [shortcode, orgSlug, projectSlug, uri.replace(/^\//, '')].filter(Boolean).join('/');
}

0 comments on commit b1e9f56

Please sign in to comment.