Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(timestamp): add new plugin to fetch timestamp #81

Merged
merged 7 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
matteo-cristino marked this conversation as resolved.
Show resolved Hide resolved
return ctx.pass(t.toString());
});

export const seconds = p.new('fetch the local timestamp in seconds', async (ctx) => {
const t = Math.floor(new Date().getTime() / 1000);
return ctx.pass(t.toString());
});

export const timestamp = p;
29 changes: 29 additions & 0 deletions pkg/timestamp/test/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 'time' named 'output'
Then print data
`
const sl = new Slangroom(timestamp);
const res = await sl.execute(contract);
const ts = new Date().getTime() / 1000;
t.truthy(res.result['output']);
t.true(Math.abs(Number(res.result['output']) - ts) < 100);
});

test('fetch timestamp in milliseconds', async (t) => {
const contract = `Rule unknown ignore
Given I fetch the local timestamp in milliseconds 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.truthy(res.result['output']);
t.true(Math.abs(Number(res.result['output']) - ts) < 100000);
});
1 change: 1 addition & 0 deletions pkg/timestamp/tsconfig.json
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading