Sha256: e81c9a3b9b9aeae1c937091d4c473336d2f597e1099f918cd53061aa1c975cef

Contents?: true

Size: 1.18 KB

Versions: 255

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) {
  time %= maxValue;
  time += maxValue;
  return time;
}

function at(hours, minutes) {
  minutes = minutes || 0;
  hours = makePositive(hours, HOURS_IN_A_DAY);
  minutes = makePositive(minutes, MINUTES_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 (minutes) {
    value += minutes * MILLIS_IN_A_MINUTE;
    return clock;
  };

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

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

  return Object.create(clock);
};

Version data entries

255 entries across 255 versions & 1 rubygems

Version Path
trackler-2.2.1.37 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.36 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.35 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.34 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.33 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.32 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.31 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.30 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.29 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.28 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.27 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.26 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.25 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.24 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.23 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.22 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.21 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.20 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.19 tracks/javascript/exercises/clock/example.js
trackler-2.2.1.18 tracks/javascript/exercises/clock/example.js