cscg24-guacamole

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

week-year.js (3274B)


      1import { addFormatToken } from '../format/format';
      2import {
      3    addRegexToken,
      4    match1to2,
      5    match1to4,
      6    match1to6,
      7    match2,
      8    match4,
      9    match6,
     10    matchSigned,
     11} from '../parse/regex';
     12import { addWeekParseToken } from '../parse/token';
     13import {
     14    weekOfYear,
     15    weeksInYear,
     16    dayOfYearFromWeeks,
     17} from './week-calendar-utils';
     18import toInt from '../utils/to-int';
     19import { hooks } from '../utils/hooks';
     20import { createUTCDate } from '../create/date-from-array';
     21
     22// FORMATTING
     23
     24addFormatToken(0, ['gg', 2], 0, function () {
     25    return this.weekYear() % 100;
     26});
     27
     28addFormatToken(0, ['GG', 2], 0, function () {
     29    return this.isoWeekYear() % 100;
     30});
     31
     32function addWeekYearFormatToken(token, getter) {
     33    addFormatToken(0, [token, token.length], 0, getter);
     34}
     35
     36addWeekYearFormatToken('gggg', 'weekYear');
     37addWeekYearFormatToken('ggggg', 'weekYear');
     38addWeekYearFormatToken('GGGG', 'isoWeekYear');
     39addWeekYearFormatToken('GGGGG', 'isoWeekYear');
     40
     41// ALIASES
     42
     43// PARSING
     44
     45addRegexToken('G', matchSigned);
     46addRegexToken('g', matchSigned);
     47addRegexToken('GG', match1to2, match2);
     48addRegexToken('gg', match1to2, match2);
     49addRegexToken('GGGG', match1to4, match4);
     50addRegexToken('gggg', match1to4, match4);
     51addRegexToken('GGGGG', match1to6, match6);
     52addRegexToken('ggggg', match1to6, match6);
     53
     54addWeekParseToken(
     55    ['gggg', 'ggggg', 'GGGG', 'GGGGG'],
     56    function (input, week, config, token) {
     57        week[token.substr(0, 2)] = toInt(input);
     58    }
     59);
     60
     61addWeekParseToken(['gg', 'GG'], function (input, week, config, token) {
     62    week[token] = hooks.parseTwoDigitYear(input);
     63});
     64
     65// MOMENTS
     66
     67export function getSetWeekYear(input) {
     68    return getSetWeekYearHelper.call(
     69        this,
     70        input,
     71        this.week(),
     72        this.weekday() + this.localeData()._week.dow,
     73        this.localeData()._week.dow,
     74        this.localeData()._week.doy
     75    );
     76}
     77
     78export function getSetISOWeekYear(input) {
     79    return getSetWeekYearHelper.call(
     80        this,
     81        input,
     82        this.isoWeek(),
     83        this.isoWeekday(),
     84        1,
     85        4
     86    );
     87}
     88
     89export function getISOWeeksInYear() {
     90    return weeksInYear(this.year(), 1, 4);
     91}
     92
     93export function getISOWeeksInISOWeekYear() {
     94    return weeksInYear(this.isoWeekYear(), 1, 4);
     95}
     96
     97export function getWeeksInYear() {
     98    var weekInfo = this.localeData()._week;
     99    return weeksInYear(this.year(), weekInfo.dow, weekInfo.doy);
    100}
    101
    102export function getWeeksInWeekYear() {
    103    var weekInfo = this.localeData()._week;
    104    return weeksInYear(this.weekYear(), weekInfo.dow, weekInfo.doy);
    105}
    106
    107function getSetWeekYearHelper(input, week, weekday, dow, doy) {
    108    var weeksTarget;
    109    if (input == null) {
    110        return weekOfYear(this, dow, doy).year;
    111    } else {
    112        weeksTarget = weeksInYear(input, dow, doy);
    113        if (week > weeksTarget) {
    114            week = weeksTarget;
    115        }
    116        return setWeekAll.call(this, input, week, weekday, dow, doy);
    117    }
    118}
    119
    120function setWeekAll(weekYear, week, weekday, dow, doy) {
    121    var dayOfYearData = dayOfYearFromWeeks(weekYear, week, weekday, dow, doy),
    122        date = createUTCDate(dayOfYearData.year, 0, dayOfYearData.dayOfYear);
    123
    124    this.year(date.getUTCFullYear());
    125    this.month(date.getUTCMonth());
    126    this.date(date.getUTCDate());
    127    return this;
    128}