Skip to content

Commit

Permalink
feat(timestamp): add new plugin to fetch timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-cristino committed Feb 29, 2024
1 parent 98c3a21 commit 5f85158
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/timestamp/.npmignore
30 changes: 30 additions & 0 deletions pkg/timestamp/package.json
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"
}
}
1 change: 1 addition & 0 deletions pkg/timestamp/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@slangroom/timestamp/plugin';
15 changes: 15 additions & 0 deletions pkg/timestamp/src/plugin.ts
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;
27 changes: 27 additions & 0 deletions pkg/timestamp/test/e2e.ts
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);
});
1 change: 1 addition & 0 deletions pkg/timestamp/tsconfig.json

0 comments on commit 5f85158

Please sign in to comment.