Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"scripts": {
"test": "jest",
"build": "rm -rf ./build && npx tsc --outDir build",
"postpublish": "PACKAGE_VERSION=$(cat package.json | grep \\\"version\\\" | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]') && git tag v$PACKAGE_VERSION && git push --tags"
"postpublish": "PACKAGE_VERSION=$(cat package.json | grep \\\"version\\\" | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]') && git tag v$PACKAGE_VERSION && git push --tags",
"watch-test": "jest --watch"
},
"repository": {
"type": "git",
Expand Down
24 changes: 24 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,30 @@ export class Duration implements ArchetypeType {
}
}


export class TezDate implements ArchetypeType {
private _content: Date
constructor(v: Date = new Date()) {
this._content = new Date(v.getTime() - v.getMilliseconds())
}
equals(x: TezDate): boolean {
return this.toSecond() === x.toSecond()
}
toSecond(): number {
return this._content.getTime() / 1000
}
addDuration(dur: Duration): TezDate {
return new TezDate(
new Date(this.toSecond()*1000 + dur.toSecond()*1000)
)}
addDurationLiteral(x : string) : TezDate {
return this.addDuration(new Duration(x))
}
toDate(): Date {
return new Date(this._content)
}
}

export class Entrypoint implements ArchetypeType {
addr: string
name: string
Expand Down
35 changes: 33 additions & 2 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {cmp_date } from './../src/main';
import BigNumber from 'bignumber.js';
import { Address, Chain_id, Duration, Key, Micheline, mich_to_ticket, Mstring, Nat, Rational, Signature, Ticket, Key_hash } from '../src/main'

import { Address, Chain_id, TezDate, Duration, Key, Micheline, mich_to_ticket, Mstring, Nat, Rational, Signature, Ticket, Key_hash } from '../src/main'
describe('ArchetypeType', () => {

describe('Address', () => {
Expand Down Expand Up @@ -150,6 +150,37 @@ describe('Chain_id', () => {

})

describe('tezDate', () => {

test('Convert Date to TezDate and back again succesfully ', () => {
const native_date_now = new Date()
const tez_date_now = new TezDate(native_date_now)
const native_date_now_two = tez_date_now.toDate()
expect(cmp_date(native_date_now, native_date_now_two)).toEqual(true)
});

test('Sucessfully add durations to a TezDate', () => {
const MINUTES = 2
const native_date_now = new Date()
const native_date_soon = new Date(native_date_now.getTime() + MINUTES*60000)
const tez_date_now = new TezDate(native_date_now)
const tez_date_soon = tez_date_now.addDuration(new Duration(`${MINUTES}m`))
expect(cmp_date(tez_date_soon.toDate(), native_date_soon)).toEqual(true)

});

test('Sucessfully add duration string to a TezDate', () => {
const MINUTES = 2
const native_date_now = new Date()
const native_date_soon = new Date(native_date_now.getTime() + MINUTES*60000)
const tez_date_now = new TezDate(native_date_now)
const tez_date_soon = tez_date_now.addDurationLiteral(`${MINUTES}m`)
expect(cmp_date(tez_date_soon.toDate(), native_date_soon)).toEqual(true)
});
})



describe('Key', () => {
test('Fails with empty string', () => {
const input = ""
Expand Down