cscg24-guacamole

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

valid.js (1184B)


      1import hasOwnProp from '../utils/has-own-prop';
      2import toInt from '../utils/to-int';
      3import indexOf from '../utils/index-of';
      4import { createDuration } from './create';
      5
      6var ordering = [
      7    'year',
      8    'quarter',
      9    'month',
     10    'week',
     11    'day',
     12    'hour',
     13    'minute',
     14    'second',
     15    'millisecond',
     16];
     17
     18export default function isDurationValid(m) {
     19    var key,
     20        unitHasDecimal = false,
     21        i,
     22        orderLen = ordering.length;
     23    for (key in m) {
     24        if (
     25            hasOwnProp(m, key) &&
     26            !(
     27                indexOf.call(ordering, key) !== -1 &&
     28                (m[key] == null || !isNaN(m[key]))
     29            )
     30        ) {
     31            return false;
     32        }
     33    }
     34
     35    for (i = 0; i < orderLen; ++i) {
     36        if (m[ordering[i]]) {
     37            if (unitHasDecimal) {
     38                return false; // only allow non-integers for smallest unit
     39            }
     40            if (parseFloat(m[ordering[i]]) !== toInt(m[ordering[i]])) {
     41                unitHasDecimal = true;
     42            }
     43        }
     44    }
     45
     46    return true;
     47}
     48
     49export function isValid() {
     50    return this._isValid;
     51}
     52
     53export function createInvalid() {
     54    return createDuration(NaN);
     55}