Advertisement
avr39ripe

jsTimeObjectClojureBased

Mar 11th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Study</title>
  6. </head>
  7. <body>
  8.     <div id="output"></div>
  9.     <script>
  10.         'use strict'
  11.  
  12.         function makeTime(h=0, m=0, s=0) {
  13.             let tmObj = { hour: h, minute: m, second: s };
  14.             let tmFunctions = {
  15.                 format: function () {
  16.                     let tmStr = {};
  17.                     for (let key in tmObj) {
  18.                         tmStr[key] = (tmObj[key] < 9 ? '0' : '') + tmObj[key];
  19.                     }
  20.                     return `${tmStr.hour}:${tmStr.minute}:${tmStr.second}`;
  21.                 },
  22.                 print: function (printer = (str) => console.log(str)) {
  23.                     printer(tmFunctions.format(tmObj));
  24.                     return tmFunctions;
  25.                 },
  26.                 correct: function () {
  27.                     const secInMin = 60;
  28.                     const minInHour = 60;
  29.                     const hourInDay = 24;
  30.  
  31.                     if (tmObj.second >= secInMin) {
  32.                         tmObj.minute += Math.floor(tmObj.second / secInMin);
  33.                         tmObj.second %= secInMin;
  34.                     }
  35.  
  36.                     if (tmObj.minute >= minInHour) {
  37.                         tmObj.hour += Math.floor(tmObj.minute / minInHour);
  38.                         tmObj.minute %= minInHour;
  39.                     }
  40.  
  41.                     if (tmObj.hour >= hourInDay) {
  42.                         tmObj.hour %= hourInDay;
  43.                     }
  44.                     return tmFunctions;
  45.                 },
  46.                 add: function (addTmObj) {
  47.                     for (let key in tmObj) {
  48.                         tmObj[key] += addTmObj[key];
  49.                     }
  50.                     return tmFunctions.correct();
  51.                 },
  52.                 addSeconds: function (seconds) {
  53.                     return tmFunctions.add({ hour: 0, minute: 0, second: seconds });
  54.                 },
  55.                 addMinutes: function (minutes) {
  56.                     return tmFunctions.add({ hour: 0, minute: minutes, second: 0 });
  57.                 },
  58.                 addHours: function (hours) {
  59.                     return tmFunctions.add({ hour: hours, minute: 0, second: 0 });
  60.                 }
  61.             };
  62.  
  63.             return tmFunctions;
  64.  
  65.         }
  66.  
  67.  
  68.         //function makePoint(pX = 0, pY = 0) {
  69.         //    let x = pX;
  70.         //    let y = pY;
  71.  
  72.         //    let funObj = {
  73.         //        setX(pX) { x = pX; },
  74.         //        setY(pY) { y = pY; },
  75.         //        getX() { return x; },
  76.         //        getY() { return y; },
  77.         //        print() { console.log(`(${x},${y})`); }
  78.         //    };
  79.  
  80.         //    return funObj;
  81.         //}
  82.  
  83.         function makePoint(pX = 0, pY = 0) {
  84.             let pointObj = { x: pX, y: pY };
  85.  
  86.             let funObj = {
  87.                 setX(pX) {
  88.                     if (pX >= 0) { pointObj.x = pX; return true }; return false; },
  89.                 setY(pY) {
  90.                     if (pY >= 0) { pointObj.y = pY; return true; }; return false; },
  91.                 getX() { return pointObj.x; },
  92.                 getY() { return pointObj.y; },
  93.                 print() { console.log(`(${pointObj.x},${pointObj.y})`); }
  94.             };
  95.  
  96.             return funObj;
  97.         }
  98.  
  99.         {
  100.             //let p1 = makePoint();
  101.             //p1.print();
  102.  
  103.             //p1.setX(-10);
  104.             //p1.setY(20);
  105.             //p1.print();
  106.  
  107.  
  108.             let time1 = makeTime(9, 48, 26);
  109.             let time2 = makeTime(11, 26, 7);
  110.  
  111.             time1.print();
  112.             time1.add({ hour: 12, minute: 67, second: 23 }).print();
  113.             time1.addMinutes(67).print();
  114.             time2.print().addHours(20).print();
  115.  
  116.             //// printer implementation for HTML output
  117.             //let printerHTML = function (str) {
  118.             //    output.innerHTML += `<li>${str}</li>`
  119.             //}
  120.  
  121.             //// printer implementation for alert output
  122.             //let printerAlert = function (str) {
  123.             //    alert(str);
  124.             //}
  125.  
  126.             //let printer; // Leave printer uninitializet,so undefined for default console.log output
  127.             //// or uncomment on of next line
  128.             //printer = printerHTML;
  129.             ////printer = printerAlert;
  130.  
  131.             //let time1 = { hour: 22, minute: 33, second: 44 };
  132.             //let time2 = { hour: 1, minute: 2, second: 3 };
  133.             //let time3 = { hour: 12, minute: 26, second: 7 };
  134.  
  135.             //printTime(time1, printer);
  136.             //printTime(time2, printer);
  137.             //printTime(time3, printer);
  138.  
  139.             //printTime(correctTime({ hour: 12, minute: 67, second: 23 }), printer);
  140.             //printTime(correctTime({ hour: 23, minute: 59, second: 61 }), printer);
  141.  
  142.             //printTime(
  143.             //    addHours(printTime(correctTime({ hour: 23, minute: 59, second: 61 }), printer), 2)
  144.             //    , printer
  145.             //);
  146.  
  147.             //printTime(addMinutes({ hour: 23, minute: 59, second: 61 }, 26), printer);
  148.  
  149.             //printTime(addHours({ hour: 23, minute: 59, second: 61 }, 7), printer);
  150.         }
  151.  
  152.  
  153.        
  154.     </script>
  155. </body>
  156. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement