cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

get-set.js (3544B)


      1import { normalizeUnits, normalizeObjectUnits } from '../units/aliases';
      2import { getPrioritizedUnits } from '../units/priorities';
      3import { hooks } from '../utils/hooks';
      4import isFunction from '../utils/is-function';
      5import { isLeapYear } from '../units/year';
      6
      7export function makeGetSet(unit, keepTime) {
      8    return function (value) {
      9        if (value != null) {
     10            set(this, unit, value);
     11            hooks.updateOffset(this, keepTime);
     12            return this;
     13        } else {
     14            return get(this, unit);
     15        }
     16    };
     17}
     18
     19export function get(mom, unit) {
     20    if (!mom.isValid()) {
     21        return NaN;
     22    }
     23
     24    var d = mom._d,
     25        isUTC = mom._isUTC;
     26
     27    switch (unit) {
     28        case 'Milliseconds':
     29            return isUTC ? d.getUTCMilliseconds() : d.getMilliseconds();
     30        case 'Seconds':
     31            return isUTC ? d.getUTCSeconds() : d.getSeconds();
     32        case 'Minutes':
     33            return isUTC ? d.getUTCMinutes() : d.getMinutes();
     34        case 'Hours':
     35            return isUTC ? d.getUTCHours() : d.getHours();
     36        case 'Date':
     37            return isUTC ? d.getUTCDate() : d.getDate();
     38        case 'Day':
     39            return isUTC ? d.getUTCDay() : d.getDay();
     40        case 'Month':
     41            return isUTC ? d.getUTCMonth() : d.getMonth();
     42        case 'FullYear':
     43            return isUTC ? d.getUTCFullYear() : d.getFullYear();
     44        default:
     45            return NaN; // Just in case
     46    }
     47}
     48
     49export function set(mom, unit, value) {
     50    var d, isUTC, year, month, date;
     51
     52    if (!mom.isValid() || isNaN(value)) {
     53        return;
     54    }
     55
     56    d = mom._d;
     57    isUTC = mom._isUTC;
     58
     59    switch (unit) {
     60        case 'Milliseconds':
     61            return void (isUTC
     62                ? d.setUTCMilliseconds(value)
     63                : d.setMilliseconds(value));
     64        case 'Seconds':
     65            return void (isUTC ? d.setUTCSeconds(value) : d.setSeconds(value));
     66        case 'Minutes':
     67            return void (isUTC ? d.setUTCMinutes(value) : d.setMinutes(value));
     68        case 'Hours':
     69            return void (isUTC ? d.setUTCHours(value) : d.setHours(value));
     70        case 'Date':
     71            return void (isUTC ? d.setUTCDate(value) : d.setDate(value));
     72        // case 'Day': // Not real
     73        //    return void (isUTC ? d.setUTCDay(value) : d.setDay(value));
     74        // case 'Month': // Not used because we need to pass two variables
     75        //     return void (isUTC ? d.setUTCMonth(value) : d.setMonth(value));
     76        case 'FullYear':
     77            break; // See below ...
     78        default:
     79            return; // Just in case
     80    }
     81
     82    year = value;
     83    month = mom.month();
     84    date = mom.date();
     85    date = date === 29 && month === 1 && !isLeapYear(year) ? 28 : date;
     86    void (isUTC
     87        ? d.setUTCFullYear(year, month, date)
     88        : d.setFullYear(year, month, date));
     89}
     90
     91// MOMENTS
     92
     93export function stringGet(units) {
     94    units = normalizeUnits(units);
     95    if (isFunction(this[units])) {
     96        return this[units]();
     97    }
     98    return this;
     99}
    100
    101export function stringSet(units, value) {
    102    if (typeof units === 'object') {
    103        units = normalizeObjectUnits(units);
    104        var prioritized = getPrioritizedUnits(units),
    105            i,
    106            prioritizedLen = prioritized.length;
    107        for (i = 0; i < prioritizedLen; i++) {
    108            this[prioritized[i].unit](units[prioritized[i].unit]);
    109        }
    110    } else {
    111        units = normalizeUnits(units);
    112        if (isFunction(this[units])) {
    113            return this[units](value);
    114        }
    115    }
    116    return this;
    117}