Skip to content

Commit

Permalink
feat: add compile, build and execute functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Nov 25, 2018
1 parent 409e4d7 commit bf3f905
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import compile from './compile'
import Thing from './Thing'

/**
* Build a `Thing`.
*
* The `build` function, like the `compile` function is used to prepare a thing
* for execution. However, it usually involves the creation of build artifacts
* (which may take some time to build) that are exernal to the thing
* e.g. a binary executable or Docker image.
* Like `compile`, it may add or modify properties of the thing
* such as providing a URL to the built artifacts.
*
* @param thing The thing to build
* @param format The format of the thing as a MIME type (only applicable when `thing` is a string)
*/
export default function build (thing: string | object | Thing, format: string = 'application/ld+json'): Thing {
thing = compile(thing, format)
return thing as Thing
}
13 changes: 13 additions & 0 deletions src/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import import_ from './import'
import Thing from './Thing'

/**
* Compile a thing
*
* @param thing The thing to compile
* @param format The format of the thing as a MIME type (only applicable when `thing` is a string)
*/
export default function compile (thing: string | object | Thing, format: string = 'application/ld+json'): Thing {
thing = import_(thing, format)
return thing as Thing
}
13 changes: 13 additions & 0 deletions src/execute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import build from './build'
import Thing from './Thing'

/**
* Execute a thing
*
* @param thing The thing to execute
* @param format The format of the thing as a MIME type (only applicable when `thing` is a string)
*/
export default function execute (thing: string | object | Thing, format: string= 'application/ld+json'): Thing {
thing = build(thing, format)
return thing as Thing
}
9 changes: 9 additions & 0 deletions tests/build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import build from '../src/build'
import Thing from '../src/Thing'

test('build:Thing', () => {
const thing = new Thing()
expect(build('{"type": "Thing"}')).toEqual(thing)
expect(build({type: 'Thing'})).toEqual(thing)
expect(build(thing)).toEqual(thing)
})
9 changes: 9 additions & 0 deletions tests/compile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import compile from '../src/compile'
import Thing from '../src/Thing'

test('compile:Thing', () => {
const thing = new Thing()
expect(compile('{"type": "Thing"}')).toEqual(thing)
expect(compile({type: 'Thing'})).toEqual(thing)
expect(compile(thing)).toEqual(thing)
})
9 changes: 9 additions & 0 deletions tests/execute.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import execute from '../src/execute'
import Thing from '../src/Thing'

test('execute:Thing', () => {
const thing = new Thing()
expect(execute('{"type": "Thing"}')).toEqual(thing)
expect(execute({type: 'Thing'})).toEqual(thing)
expect(execute(thing)).toEqual(thing)
})

0 comments on commit bf3f905

Please sign in to comment.