Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from stoplightio/feat/add-srn-utilities
feat: add srn helper functions
- Loading branch information
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('/'); | ||
} |