Sha256: 13dc81f6435adfcd9ae7151b754c7c612c7c6c245e4960d312b2fd3feebe5dae

Contents?: true

Size: 1.18 KB

Versions: 94

Compression:

Stored size: 1.18 KB

Contents

exports.at = at;

var HOURS_IN_A_DAY = 24;
var MINUTES_IN_AN_HOUR = 60;
var MINUTES_IN_A_DAY = HOURS_IN_A_DAY * MINUTES_IN_AN_HOUR;
var MILLIS_IN_A_MINUTE = 60 * 1000;
var MILLIS_IN_AN_HOUR = MINUTES_IN_AN_HOUR * MILLIS_IN_A_MINUTE;
var MILLIS_IN_A_DAY = HOURS_IN_A_DAY * MILLIS_IN_AN_HOUR;

function makePositive(time, maxValue) {
  return time % maxValue + maxValue;
}

function at(inputHours, inputMinutes) {
  var minutes = makePositive(inputMinutes || 0, MINUTES_IN_A_DAY);
  var hours = makePositive(inputHours, HOURS_IN_A_DAY);

  var clock = {};
  var value = (hours * MILLIS_IN_AN_HOUR) + (minutes * MILLIS_IN_A_MINUTE);
  value = makePositive(value, MILLIS_IN_A_DAY);

  clock.valueOf = function () {
    return value;
  };

  clock.toString = function () {
    var time = new Date(value).toISOString().split('T')[1].split(':');
    return time[0] + ':' + time[1];
  };

  clock.plus = function (addMinutes) {
    value += addMinutes * MILLIS_IN_A_MINUTE;
    return clock;
  };

  clock.minus = function (subMinutes) {
    value -= subMinutes * MILLIS_IN_A_MINUTE;
    return clock;
  };

  clock.equals = function (other) {
    return +clock === +other;
  };

  return Object.create(clock);
}

Version data entries

94 entries across 94 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.118 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.117 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.116 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.115 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.114 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.113 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.111 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.110 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.109 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.108 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.107 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.106 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.105 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.104 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.103 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.102 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.101 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.100 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.99 tracks/javascript/exercises/clock/example.js