From 9e096eb4e7aa0a63c4a6877508cf9294869ca1ed Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Sun, 29 Dec 2024 19:25:43 +0100 Subject: [PATCH] Added some classes --- index.html | 9 +++ main.js | 16 +++++ src/System/Math.js | 9 +++ src/System/TestNamespace/TestClass.js | 5 ++ src/System/TimeSpan.js | 93 +++++++++++++++++++++++++++ src/System/export.js | 11 ++++ 6 files changed, 143 insertions(+) create mode 100644 index.html create mode 100644 main.js create mode 100644 src/System/Math.js create mode 100644 src/System/TestNamespace/TestClass.js create mode 100644 src/System/TimeSpan.js create mode 100644 src/System/export.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..f3c47bc --- /dev/null +++ b/index.html @@ -0,0 +1,9 @@ + + + + System Namespace + + + + + diff --git a/main.js b/main.js new file mode 100644 index 0000000..dca1de8 --- /dev/null +++ b/main.js @@ -0,0 +1,16 @@ + +import { System } from './src/System/export.js'; + +const span1 = new System.TimeSpan(1, 30, 15, 500); +console.log(span1.toString()); + +const span2 = System.TimeSpan.fromMinutes(90); +console.log(span2.toString()); + +//// + +const span = new System.TimeSpan(1, 30, 0); +console.log(span.toString()); + +const sum = System.Math.add(10, 5); +console.log(sum); diff --git a/src/System/Math.js b/src/System/Math.js new file mode 100644 index 0000000..127116a --- /dev/null +++ b/src/System/Math.js @@ -0,0 +1,9 @@ +export class Math { + static add(a, b) { + return a + b; + } + + static subtract(a, b) { + return a - b; + } +} diff --git a/src/System/TestNamespace/TestClass.js b/src/System/TestNamespace/TestClass.js new file mode 100644 index 0000000..f346f6b --- /dev/null +++ b/src/System/TestNamespace/TestClass.js @@ -0,0 +1,5 @@ +export class TestClass { + constructor() { + + } +} diff --git a/src/System/TimeSpan.js b/src/System/TimeSpan.js new file mode 100644 index 0000000..5a886ed --- /dev/null +++ b/src/System/TimeSpan.js @@ -0,0 +1,93 @@ +export class TimeSpan { + constructor(hours = 0, minutes = 0, seconds = 0, milliseconds = 0) { + this.milliseconds = milliseconds + seconds * 1000 + minutes * 60000 + hours * 3600000; + } + + static fromMilliseconds(milliseconds) { + return new TimeSpan(0, 0, 0, milliseconds); + } + + static fromSeconds(seconds) { + return new TimeSpan(0, 0, seconds); + } + + static fromMinutes(minutes) { + return new TimeSpan(0, minutes); + } + + static fromHours(hours) { + return new TimeSpan(hours); + } + + static fromDays(days) { + return new TimeSpan(days * 24); + } + + get totalMilliseconds() { + return this.milliseconds; + } + + get totalSeconds() { + return this.milliseconds / 1000; + } + + get totalMinutes() { + return this.milliseconds / 60000; + } + + get totalHours() { + return this.milliseconds / 3600000; + } + + get totalDays() { + return this.milliseconds / 86400000; + } + + get millisecondsPart() { + return this.milliseconds % 1000; + } + + get secondsPart() { + return Math.floor(this.milliseconds / 1000) % 60; + } + + get minutesPart() { + return Math.floor(this.milliseconds / 60000) % 60; + } + + get hoursPart() { + return Math.floor(this.milliseconds / 3600000) % 24; + } + + get daysPart() { + return Math.floor(this.milliseconds / 86400000); + } + + get ticks() { + return this.milliseconds * 10000; + } + + add(timeSpan) { + return TimeSpan.fromMilliseconds(this.milliseconds + timeSpan.totalMilliseconds); + } + + subtract(timeSpan) { + return TimeSpan.fromMilliseconds(this.milliseconds - timeSpan.totalMilliseconds); + } + + toString() { + const pad = (n) => n.toString().padStart(2, '0'); + return `${this.daysPart > 0 ? this.daysPart + '.' : ''}${pad(this.hoursPart)}:${pad(this.minutesPart)}:${pad(this.secondsPart)}.${this.millisecondsPart}`; + } +} + +/* +const span1 = new TimeSpan(1, 30, 15, 500); +console.log(span1.toString()); + +const span2 = TimeSpan.fromMinutes(90); +console.log(span2.toString()); + +const sum = span1.add(span2); +console.log(sum.toString()); +*/ diff --git a/src/System/export.js b/src/System/export.js new file mode 100644 index 0000000..09fdccc --- /dev/null +++ b/src/System/export.js @@ -0,0 +1,11 @@ +import { TimeSpan } from './TimeSpan.js'; +import { Math } from './Math.js'; +import { TestClass } from './TestNamespace/TestClass.js'; + +export const System = { + TimeSpan, + Math, + TestNamespace: { + TestClass + } +};