All files / src/helpers datetime.ts

100% Statements 37/37
100% Branches 2/2
100% Functions 2/2
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 952x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 29x                                                                                               29x 13x 13x   12x   12x 3x 2x 2x 2x     11x 3x 2x 2x 2x 2x     10x 10x 10x 10x 10x 10x   10x      
import { HandlebarsHelper, HelperConstructorBlock } from "./helper";
import { AttributeValueType, AttributeDefinition, AttributeParser } from "./utils/attributes";
import * as moment from "moment";
 
const FORMAT = "format";
const SET_DATE = "set_date";
const SET_TIME = "set_time";
const DELTA_YEARS = "delta_years";
const DELTA_MONTHS = "delta_months";
const DELTA_DAYS = "delta_days";
const DELTA_HOURS = "delta_hours";
const DELTA_MINUTES = "delta_minutes";
const DELTA_SECONDS = "delta_seconds";
 
export const datetimeHelper: HelperConstructorBlock = (ctx) => {
    const schema: AttributeDefinition[] = [
        {
            name: FORMAT,
            valueType: AttributeValueType.String,
            defaultValue: ctx.dateAndTimeUtils.getDateTimeFormat()
        },
        {
            name: SET_DATE,
            valueType: AttributeValueType.String,
            defaultValue: ""
        },
        {
            name: SET_TIME,
            valueType: AttributeValueType.String,
            defaultValue: ""
        },
        {
            name: DELTA_YEARS,
            valueType: AttributeValueType.Number,
            defaultValue: 0
        },
        {
            name: DELTA_MONTHS,
            valueType: AttributeValueType.Number,
            defaultValue: 0
        },
        {
            name: DELTA_DAYS,
            valueType: AttributeValueType.Number,
            defaultValue: 0
        },
        {
            name: DELTA_HOURS,
            valueType: AttributeValueType.Number,
            defaultValue: 0
        },
        {
            name: DELTA_MINUTES,
            valueType: AttributeValueType.Number,
            defaultValue: 0
        },
        {
            name: DELTA_SECONDS,
            valueType: AttributeValueType.Number,
            defaultValue: 0
        }
    ];
 
    return new HandlebarsHelper("datetime", function (options) {
        const parser = new AttributeParser(schema);
        const attrs = parser.parse(options.hash);
 
        const now = moment(new Date().getTime());
 
        if (attrs[SET_DATE]) {
            const parsedDate = ctx.dateAndTimeUtils.parseDate(attrs[SET_DATE] as string, ctx.dateAndTimeUtils.getDateFormat());
            now.set("date", parsedDate.date);
            now.set("month", parsedDate.month);
            now.set("year", parsedDate.year);
        }
 
        if (attrs[SET_TIME]) {
            const parsedTime = ctx.dateAndTimeUtils.parseTime(attrs[SET_TIME] as string, ctx.dateAndTimeUtils.getTimeFormat());
            now.set("hours", parsedTime.hours);
            now.set("minutes", parsedTime.minutes);
            now.set("seconds", parsedTime.seconds);
            now.set("milliseconds", 0);
        }
 
        now.add(attrs[DELTA_YEARS] as number, "years");
        now.add(attrs[DELTA_MONTHS] as number, "months");
        now.add(attrs[DELTA_DAYS] as number, "days");
        now.add(attrs[DELTA_HOURS] as number, "hours");
        now.add(attrs[DELTA_MINUTES] as number, "minutes");
        now.add(attrs[DELTA_SECONDS] as number, "seconds");
 
        return ctx.dateAndTimeUtils.formatMsToLocal(now.toDate().getTime(), attrs[FORMAT] as string);
    });
};