-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(timestamp): add new plugin to fetch timestamp
- Loading branch information
1 parent
98c3a21
commit 5f85158
Showing
6 changed files
with
75 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../.npmignore |
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,30 @@ | ||
{ | ||
"name": "@slangroom/timestamp", | ||
"version": "1.19.0", | ||
"dependencies": { | ||
"@slangroom/core": "workspace:*", | ||
"@slangroom/shared": "workspace:*" | ||
}, | ||
"repository": "https://github.com/dyne/slangroom", | ||
"license": "AGPL-3.0-only", | ||
"type": "module", | ||
"main": "./build/esm/src/index.js", | ||
"types": "./build/esm/src/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": { | ||
"types": "./build/esm/src/index.d.ts", | ||
"default": "./build/esm/src/index.js" | ||
} | ||
}, | ||
"./*": { | ||
"import": { | ||
"types": "./build/esm/src/*.d.ts", | ||
"default": "./build/esm/src/*.js" | ||
} | ||
} | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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 @@ | ||
export * from '@slangroom/timestamp/plugin'; |
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,15 @@ | ||
import { Plugin } from '@slangroom/core'; | ||
|
||
const p = new Plugin(); | ||
|
||
export const milliseconds = p.new('fetch the local timestamp in milliseconds', async (ctx) => { | ||
const t = new Date().getTime(); | ||
return ctx.pass(t.toString()); | ||
}); | ||
|
||
export const seconds = p.new('fetch the local timestamp in seconds', async (ctx) => { | ||
const t = new Date().getTime() / 1000; | ||
return ctx.pass(t.toString()); | ||
}); | ||
|
||
export const timestamp = p; |
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,27 @@ | ||
import test from 'ava'; | ||
import { Slangroom } from '@slangroom/core'; | ||
import { timestamp } from '@slangroom/timestamp'; | ||
|
||
test('fetch timestamp in seconds', async (t) => { | ||
const contract = `Rule unknown ignore | ||
Given I fetch the local timestamp in seconds and output into 'output' | ||
Given I have a 'string' named 'output' | ||
Then print data | ||
` | ||
const sl = new Slangroom(timestamp); | ||
const res = await sl.execute(contract); | ||
const ts = new Date().getTime() / 1000; | ||
t.true(Math.abs(Number(res) - ts) < 100); | ||
}); | ||
|
||
test('fetch timestamp in milliseconds', async (t) => { | ||
const contract = `Rule unknown ignore | ||
Given I fetch the local timestamp in seconds and output into 'output' | ||
Given I have a 'string' named 'output' | ||
Then print data | ||
` | ||
const sl = new Slangroom(timestamp); | ||
const res = await sl.execute(contract); | ||
const ts = new Date().getTime(); | ||
t.true(Math.abs(Number(res) - ts) < 100); | ||
}); |
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 @@ | ||
../../tsconfig.json |