This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Util): Add version and URL utility functions
- Loading branch information
Showing
7 changed files
with
93 additions
and
24 deletions.
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
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,11 @@ | ||
import pkg from '../../package.json' | ||
import { jsonLdUrl } from './urls' | ||
|
||
test('jsonLdUrl', () => { | ||
const expectedBase = `http://schema.stenci.la/v${ | ||
pkg.version.split('.')[0] | ||
}/jsonld/` | ||
expect(jsonLdUrl()).toMatch(expectedBase) | ||
expect(jsonLdUrl('CodeChunk')).toMatch(expectedBase + 'CodeChunk') | ||
expect(jsonLdUrl('outputs')).toMatch(expectedBase + 'outputs') | ||
}) |
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,18 @@ | ||
import { versionMajor } from './version' | ||
|
||
/** | ||
* Get the URL to the JSON-LD for this schema's `@context` or | ||
* for a specific term e.g. `CodeChunk`, `outputs`. | ||
* | ||
* The `@context`'s URL needs to have a trailing slash because | ||
* it gets prefixed to all keys during JSON-LD expansion. | ||
* e.g. the term `CodeChunk` gets expanded to `http://schema.stenci.la/v0/jsonld/CodeChunk` | ||
* (which in gets redirected to `https://unpkg.com/@stencila/schema@0.32.1/dist/CodeChunk.jsonld`) | ||
* | ||
* @param term The term (type or property) to generate the | ||
* URL for. Defaults to empty string i.e. the context. | ||
*/ | ||
export function jsonLdUrl(term = '') { | ||
const version = versionMajor() | ||
return `http://schema.stenci.la/v${version}/jsonld/${term}` | ||
} |
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,19 @@ | ||
import pkg from '../../package.json' | ||
import { version, versionMajor, versionMinor } from './version' | ||
|
||
test('version', () => { | ||
expect(version()).toEqual(pkg.version) | ||
}) | ||
|
||
test('versionMajor', () => { | ||
expect(versionMajor()).toEqual(pkg.version.split('.')[0]) | ||
}) | ||
|
||
test('versionMinor', () => { | ||
expect(versionMinor()).toEqual( | ||
pkg.version | ||
.split('.') | ||
.slice(0, 2) | ||
.join('.') | ||
) | ||
}) |
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,39 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
/** | ||
* Lazily read, cached, package version | ||
*/ | ||
let VERSION: string | ||
|
||
/** | ||
* Get the version string (e.g "1.2.3") for this package | ||
*/ | ||
export function version(): string { | ||
if (VERSION === undefined) { | ||
const json = fs.readFileSync( | ||
path.join(__dirname, '..', '..', 'package.json'), | ||
'utf8' | ||
) | ||
const pkg = JSON.parse(json) | ||
VERSION = pkg.version | ||
} | ||
return VERSION | ||
} | ||
|
||
/** | ||
* Get the major version string (e.g "1") for this package | ||
*/ | ||
export function versionMajor(): string { | ||
return version().split('.')[0] | ||
} | ||
|
||
/** | ||
* Get the minor version string (e.g "1.2") for this package | ||
*/ | ||
export function versionMinor(): string { | ||
return version() | ||
.split('.') | ||
.slice(0, 2) | ||
.join('.') | ||
} |