From 5a8214413ceadba9801ab16aeddbd28bd784b3db Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Wed, 16 Aug 2017 14:29:12 +0200 Subject: [PATCH] first implementation --- dist/index.d.ts | 3 +- dist/index.js | 8 +++- dist/smarttime.classes.hrtmeasurement.d.ts | 22 ++++++++++ dist/smarttime.classes.hrtmeasurement.js | 47 ++++++++++++++++++++++ dist/smarttime.classes.timestamp.d.ts | 22 ++++++++++ dist/smarttime.classes.timestamp.js | 36 +++++++++++++++++ test/test.ts | 31 +++++++++++++- ts/index.ts | 3 +- ts/smarttime.classes.hrtmeasurement.ts | 45 +++++++++++++++++++++ ts/smarttime.classes.timestamp.ts | 44 ++++++++++++++++++++ 10 files changed, 255 insertions(+), 6 deletions(-) create mode 100644 dist/smarttime.classes.hrtmeasurement.d.ts create mode 100644 dist/smarttime.classes.hrtmeasurement.js create mode 100644 dist/smarttime.classes.timestamp.d.ts create mode 100644 dist/smarttime.classes.timestamp.js create mode 100644 ts/smarttime.classes.hrtmeasurement.ts create mode 100644 ts/smarttime.classes.timestamp.ts diff --git a/dist/index.d.ts b/dist/index.d.ts index 4bb0f09..7da57f3 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1 +1,2 @@ -export declare let standardExport: string; +export * from './smarttime.classes.hrtmeasurement'; +export * from './smarttime.classes.timestamp'; diff --git a/dist/index.js b/dist/index.js index 81df592..e48fdb6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,4 +1,8 @@ "use strict"; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} Object.defineProperty(exports, "__esModule", { value: true }); -exports.standardExport = 'Hi there! :) This is a exported string'; -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUVXLFFBQUEsY0FBYyxHQUFHLHdDQUF3QyxDQUFBIn0= \ No newline at end of file +__export(require("./smarttime.classes.hrtmeasurement")); +__export(require("./smarttime.classes.timestamp")); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUVBLHdEQUFrRDtBQUNsRCxtREFBNkMifQ== \ No newline at end of file diff --git a/dist/smarttime.classes.hrtmeasurement.d.ts b/dist/smarttime.classes.hrtmeasurement.d.ts new file mode 100644 index 0000000..02093d9 --- /dev/null +++ b/dist/smarttime.classes.hrtmeasurement.d.ts @@ -0,0 +1,22 @@ +/** + * easy high resolution time measurement + */ +export declare class HrtMeasurement { + nanoSeconds: number; + milliSeconds: number; + private _hrTimeStart; + private _hrTimeStopDiff; + private _started; + /** + * start the measurement + */ + start(): void; + /** + * stop the measurement + */ + stop(): this; + /** + * reset the measurement + */ + reset(): void; +} diff --git a/dist/smarttime.classes.hrtmeasurement.js b/dist/smarttime.classes.hrtmeasurement.js new file mode 100644 index 0000000..a874f2b --- /dev/null +++ b/dist/smarttime.classes.hrtmeasurement.js @@ -0,0 +1,47 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const process = require("process"); +/** + * easy high resolution time measurement + */ +class HrtMeasurement { + constructor() { + this.nanoSeconds = null; + this.milliSeconds = null; + this._hrTimeStart = null; + this._hrTimeStopDiff = null; + this._started = false; + } + /** + * start the measurement + */ + start() { + this._started = true; + this._hrTimeStart = process.hrtime(); + } + /** + * stop the measurement + */ + stop() { + if (this._started === false) { + console.log('Hasn\'t started yet'); + return; + } + this._hrTimeStopDiff = process.hrtime(this._hrTimeStart); + this.nanoSeconds = (this._hrTimeStopDiff[0] * 1e9) + this._hrTimeStopDiff[1]; + this.milliSeconds = this.nanoSeconds / 1000000; + return this; + } + /** + * reset the measurement + */ + reset() { + this.nanoSeconds = null; + this.milliSeconds = null; + this._hrTimeStart = null; + this._hrTimeStopDiff = null; + this._started = false; + } +} +exports.HrtMeasurement = HrtMeasurement; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnR0aW1lLmNsYXNzZXMuaHJ0bWVhc3VyZW1lbnQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHRpbWUuY2xhc3Nlcy5ocnRtZWFzdXJlbWVudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLG1DQUFrQztBQUVsQzs7R0FFRztBQUNIO0lBQUE7UUFDRSxnQkFBVyxHQUFXLElBQUksQ0FBQTtRQUMxQixpQkFBWSxHQUFXLElBQUksQ0FBQTtRQUNuQixpQkFBWSxHQUFHLElBQUksQ0FBQTtRQUNuQixvQkFBZSxHQUFHLElBQUksQ0FBQTtRQUN0QixhQUFRLEdBQVksS0FBSyxDQUFBO0lBa0NuQyxDQUFDO0lBaENDOztPQUVHO0lBQ0gsS0FBSztRQUNILElBQUksQ0FBQyxRQUFRLEdBQUcsSUFBSSxDQUFBO1FBQ3BCLElBQUksQ0FBQyxZQUFZLEdBQUcsT0FBTyxDQUFDLE1BQU0sRUFBRSxDQUFBO0lBQ3RDLENBQUM7SUFFRDs7T0FFRztJQUNILElBQUk7UUFDRixFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsUUFBUSxLQUFLLEtBQUssQ0FBQyxDQUFDLENBQUM7WUFDNUIsT0FBTyxDQUFDLEdBQUcsQ0FBQyxxQkFBcUIsQ0FBQyxDQUFBO1lBQ2xDLE1BQU0sQ0FBQTtRQUNSLENBQUM7UUFDRCxJQUFJLENBQUMsZUFBZSxHQUFHLE9BQU8sQ0FBQyxNQUFNLENBQUMsSUFBSSxDQUFDLFlBQVksQ0FBQyxDQUFBO1FBQ3hELElBQUksQ0FBQyxXQUFXLEdBQUcsQ0FBQyxJQUFJLENBQUMsZUFBZSxDQUFDLENBQUMsQ0FBQyxHQUFHLEdBQUcsQ0FBQyxHQUFHLElBQUksQ0FBQyxlQUFlLENBQUMsQ0FBQyxDQUFDLENBQUE7UUFDNUUsSUFBSSxDQUFDLFlBQVksR0FBRyxJQUFJLENBQUMsV0FBVyxHQUFHLE9BQU8sQ0FBQTtRQUM5QyxNQUFNLENBQUMsSUFBSSxDQUFBO0lBQ2IsQ0FBQztJQUVEOztPQUVHO0lBQ0gsS0FBSztRQUNILElBQUksQ0FBQyxXQUFXLEdBQUcsSUFBSSxDQUFBO1FBQ3ZCLElBQUksQ0FBQyxZQUFZLEdBQUcsSUFBSSxDQUFBO1FBQ3hCLElBQUksQ0FBQyxZQUFZLEdBQUcsSUFBSSxDQUFBO1FBQ3hCLElBQUksQ0FBQyxlQUFlLEdBQUcsSUFBSSxDQUFBO1FBQzNCLElBQUksQ0FBQyxRQUFRLEdBQUcsS0FBSyxDQUFBO0lBQ3ZCLENBQUM7Q0FDRjtBQXZDRCx3Q0F1Q0MifQ== \ No newline at end of file diff --git a/dist/smarttime.classes.timestamp.d.ts b/dist/smarttime.classes.timestamp.d.ts new file mode 100644 index 0000000..c62fd84 --- /dev/null +++ b/dist/smarttime.classes.timestamp.d.ts @@ -0,0 +1,22 @@ +/** + * TimeStamp + * smart timestamp + */ +export declare class TimeStamp { + /** + * The standard JavaScript Date + */ + date: Date; + /** + * The time as linux time + * good for comparison + */ + linuxtime: number; + constructor(creatorArg?: number | TimeStamp); + /** + * Is the current instance older than the argument + * @param TimeStampArg + */ + isOlderThan(TimeStampArg: TimeStamp): boolean; + isYoungerThan(TimeStampArg: TimeStamp): boolean; +} diff --git a/dist/smarttime.classes.timestamp.js b/dist/smarttime.classes.timestamp.js new file mode 100644 index 0000000..4038364 --- /dev/null +++ b/dist/smarttime.classes.timestamp.js @@ -0,0 +1,36 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +/** + * TimeStamp + * smart timestamp + */ +class TimeStamp { + constructor(creatorArg) { + if (!creatorArg) { + this.date = new Date(); + this.linuxtime = this.date.getTime(); + } + } + /** + * Is the current instance older than the argument + * @param TimeStampArg + */ + isOlderThan(TimeStampArg) { + if (this.linuxtime < TimeStampArg.linuxtime) { + return true; + } + else { + return false; + } + } + isYoungerThan(TimeStampArg) { + if (this.linuxtime > TimeStampArg.linuxtime) { + return true; + } + else { + return false; + } + } +} +exports.TimeStamp = TimeStamp; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnR0aW1lLmNsYXNzZXMudGltZXN0YW1wLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnR0aW1lLmNsYXNzZXMudGltZXN0YW1wLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBRUE7OztHQUdHO0FBQ0g7SUFXRSxZQUFhLFVBQStCO1FBQzFDLEVBQUUsQ0FBQyxDQUFDLENBQUMsVUFBVSxDQUFDLENBQUMsQ0FBQztZQUNoQixJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksSUFBSSxFQUFFLENBQUE7WUFDdEIsSUFBSSxDQUFDLFNBQVMsR0FBRyxJQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQ3RDLENBQUM7SUFDSCxDQUFDO0lBRUQ7OztPQUdHO0lBQ0gsV0FBVyxDQUFFLFlBQXVCO1FBQ2xDLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxTQUFTLEdBQUcsWUFBWSxDQUFDLFNBQVMsQ0FBQyxDQUFDLENBQUM7WUFDNUMsTUFBTSxDQUFDLElBQUksQ0FBQTtRQUNiLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNOLE1BQU0sQ0FBQyxLQUFLLENBQUE7UUFDZCxDQUFDO0lBQ0gsQ0FBQztJQUVELGFBQWEsQ0FBRSxZQUF1QjtRQUNwQyxFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsU0FBUyxHQUFHLFlBQVksQ0FBQyxTQUFTLENBQUMsQ0FBQyxDQUFDO1lBQzVDLE1BQU0sQ0FBQyxJQUFJLENBQUE7UUFDYixDQUFDO1FBQUMsSUFBSSxDQUFDLENBQUM7WUFDTixNQUFNLENBQUMsS0FBSyxDQUFBO1FBQ2QsQ0FBQztJQUNILENBQUM7Q0FDRjtBQXJDRCw4QkFxQ0MifQ== \ No newline at end of file diff --git a/test/test.ts b/test/test.ts index da419a9..31d9de1 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1,8 +1,35 @@ import { expect, tap } from 'tapbundle' import * as smarttime from '../ts/index' -tap.test('first test', async () => { - console.log(smarttime.standardExport) + +// Test TimeStamp class +let testTimeStamp: smarttime.TimeStamp +let testTimeStamp2: smarttime.TimeStamp +tap.test('should create valid testTimeStamp instance', async (tools) => { + testTimeStamp = new smarttime.TimeStamp() + await tools.delayFor(2) + testTimeStamp2 = new smarttime.TimeStamp() + expect(testTimeStamp).to.be.instanceof(smarttime.TimeStamp) + expect(testTimeStamp).to.be.instanceof(smarttime.TimeStamp) }) +tap.test('should have valid linuxtime', async () => { + // tslint:disable-next-line:no-unused-expression + expect(testTimeStamp.isOlderThan(testTimeStamp2)).to.be.true + expect(testTimeStamp.isYoungerThan(testTimeStamp2)).to.be.false +}) + +let testHrtMeasurement: smarttime.HrtMeasurement + +// Test HrtMeasurement +tap.test('should create valid HrtMeasurements', async (tools) => { + testHrtMeasurement = new smarttime.HrtMeasurement() + testHrtMeasurement.start() + await tools.delayFor(20) + testHrtMeasurement.stop() + expect(testHrtMeasurement.milliSeconds).to.be.greaterThan(19) + expect(testHrtMeasurement.milliSeconds).to.be.lessThan(25) +}) + + tap.start() diff --git a/ts/index.ts b/ts/index.ts index cdba3f1..2da2d7f 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,3 +1,4 @@ import * as plugins from './smarttime.plugins' -export let standardExport = 'Hi there! :) This is a exported string' +export * from './smarttime.classes.hrtmeasurement' +export * from './smarttime.classes.timestamp' diff --git a/ts/smarttime.classes.hrtmeasurement.ts b/ts/smarttime.classes.hrtmeasurement.ts new file mode 100644 index 0000000..e421710 --- /dev/null +++ b/ts/smarttime.classes.hrtmeasurement.ts @@ -0,0 +1,45 @@ +import * as process from 'process' + +/** + * easy high resolution time measurement + */ +export class HrtMeasurement { + nanoSeconds: number = null + milliSeconds: number = null + private _hrTimeStart = null + private _hrTimeStopDiff = null + private _started: boolean = false + + /** + * start the measurement + */ + start () { + this._started = true + this._hrTimeStart = process.hrtime() + } + + /** + * stop the measurement + */ + stop () { + if (this._started === false) { + console.log('Hasn\'t started yet') + return + } + this._hrTimeStopDiff = process.hrtime(this._hrTimeStart) + this.nanoSeconds = (this._hrTimeStopDiff[0] * 1e9) + this._hrTimeStopDiff[1] + this.milliSeconds = this.nanoSeconds / 1000000 + return this + } + + /** + * reset the measurement + */ + reset () { + this.nanoSeconds = null + this.milliSeconds = null + this._hrTimeStart = null + this._hrTimeStopDiff = null + this._started = false + } +} diff --git a/ts/smarttime.classes.timestamp.ts b/ts/smarttime.classes.timestamp.ts new file mode 100644 index 0000000..fc1e1dd --- /dev/null +++ b/ts/smarttime.classes.timestamp.ts @@ -0,0 +1,44 @@ +import * as plugins from './smarttime.plugins' + +/** + * TimeStamp + * smart timestamp + */ +export class TimeStamp { + /** + * The standard JavaScript Date + */ + date: Date + + /** + * The time as linux time + * good for comparison + */ + linuxtime: number + constructor (creatorArg?: number | TimeStamp) { + if (!creatorArg) { + this.date = new Date() + this.linuxtime = this.date.getTime() + } + } + + /** + * Is the current instance older than the argument + * @param TimeStampArg + */ + isOlderThan (TimeStampArg: TimeStamp) { + if (this.linuxtime < TimeStampArg.linuxtime) { + return true + } else { + return false + } + } + + isYoungerThan (TimeStampArg: TimeStamp) { + if (this.linuxtime > TimeStampArg.linuxtime) { + return true + } else { + return false + } + } +}