From d9dd881e6ec3efb521cf33ec925bf3a929f596c7 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Wed, 16 Aug 2017 16:24:56 +0200 Subject: [PATCH] add unit computation --- dist/index.d.ts | 1 + dist/index.js | 3 ++- dist/smarttime.units.d.ts | 17 ++++++++++++++ dist/smarttime.units.js | 48 +++++++++++++++++++++++++++++++++++++++ test/test.ts | 12 ++++++++++ ts/index.ts | 1 + ts/smarttime.units.ts | 45 ++++++++++++++++++++++++++++++++++++ 7 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 dist/smarttime.units.d.ts create mode 100644 dist/smarttime.units.js create mode 100644 ts/smarttime.units.ts diff --git a/dist/index.d.ts b/dist/index.d.ts index 7da57f3..573bacc 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,2 +1,3 @@ export * from './smarttime.classes.hrtmeasurement'; export * from './smarttime.classes.timestamp'; +export * from './smarttime.units'; diff --git a/dist/index.js b/dist/index.js index e48fdb6..63e45c0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5,4 +5,5 @@ function __export(m) { Object.defineProperty(exports, "__esModule", { value: true }); __export(require("./smarttime.classes.hrtmeasurement")); __export(require("./smarttime.classes.timestamp")); -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUVBLHdEQUFrRDtBQUNsRCxtREFBNkMifQ== \ No newline at end of file +__export(require("./smarttime.units")); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUVBLHdEQUFrRDtBQUNsRCxtREFBNkM7QUFDN0MsdUNBQWlDIn0= \ No newline at end of file diff --git a/dist/smarttime.units.d.ts b/dist/smarttime.units.d.ts new file mode 100644 index 0000000..908f194 --- /dev/null +++ b/dist/smarttime.units.d.ts @@ -0,0 +1,17 @@ +export declare let units: { + years: (timesArg?: number) => number; + months: (timesArg?: number) => number; + weeks: (timesArg?: number) => number; + days: (timesArg?: number) => number; + hours: (timesArg?: number) => number; + minutes: (timesArg?: number) => number; +}; +export interface IUnitCombinationArg { + years?: number; + months?: number; + weeks?: number; + days?: number; + hours?: number; + minutes?: number; +} +export declare let getMilliSecondsFromUnits: (combinationArg: IUnitCombinationArg) => number; diff --git a/dist/smarttime.units.js b/dist/smarttime.units.js new file mode 100644 index 0000000..b830941 --- /dev/null +++ b/dist/smarttime.units.js @@ -0,0 +1,48 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.units = { + years: (timesArg = 1) => { + return timesArg * 3154e+10; + }, + months: (timesArg = 1) => { + return timesArg * 2628e+9; + }, + weeks: (timesArg = 1) => { + return timesArg * 6048e+8; + }, + days: (timesArg = 1) => { + return timesArg * 864e+7; + }, + hours: (timesArg = 1) => { + return timesArg * 36e+6; + }, + minutes: (timesArg = 1) => { + return timesArg * 60000; + } +}; +exports.getMilliSecondsFromUnits = (combinationArg) => { + let timeInMilliseconds = 0; + let addMilliSeconds = (milliSecondsArg) => { + timeInMilliseconds = timeInMilliseconds + milliSecondsArg; + }; + if (combinationArg.years) { + addMilliSeconds(exports.units.years(combinationArg.years)); + } + if (combinationArg.months) { + addMilliSeconds(exports.units.months(combinationArg.months)); + } + if (combinationArg.weeks) { + addMilliSeconds(exports.units.weeks(combinationArg.weeks)); + } + if (combinationArg.days) { + addMilliSeconds(exports.units.days(combinationArg.days)); + } + if (combinationArg.hours) { + addMilliSeconds(exports.units.hours(combinationArg.hours)); + } + if (combinationArg.minutes) { + addMilliSeconds(exports.units.minutes(combinationArg.minutes)); + } + return timeInMilliseconds; +}; +//# sourceMappingURL=data:application/json;base64,e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UFDO0lBRXRGLE1BQU0sQ0FBQyxrQkFBa0IsQ0FBQTtBQUMzQixDQUFDLENBQUEifQ== \ No newline at end of file diff --git a/test/test.ts b/test/test.ts index 8b0f1ce..7576017 100644 --- a/test/test.ts +++ b/test/test.ts @@ -32,5 +32,17 @@ tap.test('should create valid HrtMeasurements', async (tools) => { expect(testHrtMeasurement.milliSeconds).to.be.lessThan(25) }) +// check units +tap.test('should combine units', async () => { + let computedTime = smarttime.getMilliSecondsFromUnits({ + years: 2, + months: 2, + weeks: 2, + days: 2, + hours: 2, + minutes: 2 + }) + console.log(computedTime) +}) tap.start() diff --git a/ts/index.ts b/ts/index.ts index 2da2d7f..be8af2e 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -2,3 +2,4 @@ import * as plugins from './smarttime.plugins' export * from './smarttime.classes.hrtmeasurement' export * from './smarttime.classes.timestamp' +export * from './smarttime.units' diff --git a/ts/smarttime.units.ts b/ts/smarttime.units.ts new file mode 100644 index 0000000..3e665cb --- /dev/null +++ b/ts/smarttime.units.ts @@ -0,0 +1,45 @@ +export let units = { + years: (timesArg = 1): number => { + return timesArg * 3154e+10 + }, + months: (timesArg = 1): number => { + return timesArg * 2628e+9 + }, + weeks: (timesArg = 1) => { + return timesArg * 6048e+8 + }, + days: (timesArg = 1) => { + return timesArg * 864e+7 + }, + hours: (timesArg = 1) => { + return timesArg * 36e+6 + }, + minutes: (timesArg = 1) => { + return timesArg * 60000 + } +} + +export interface IUnitCombinationArg { + years?: number + months?: number + weeks?: number + days?: number + hours?: number + minutes?: number +} + +export let getMilliSecondsFromUnits = (combinationArg: IUnitCombinationArg) => { + let timeInMilliseconds = 0 + let addMilliSeconds = (milliSecondsArg) => { + timeInMilliseconds = timeInMilliseconds + milliSecondsArg + } + if (combinationArg.years) { addMilliSeconds(units.years(combinationArg.years)) } + if (combinationArg.months) { addMilliSeconds(units.months(combinationArg.months)) } + if (combinationArg.weeks) { addMilliSeconds(units.weeks(combinationArg.weeks)) } + if (combinationArg.days) { addMilliSeconds(units.days(combinationArg.days)) } + if (combinationArg.hours) { addMilliSeconds(units.hours(combinationArg.hours)) } + if (combinationArg.minutes) { addMilliSeconds(units.minutes(combinationArg.minutes)) } + + return timeInMilliseconds +} +