All files / src/lib surface.converter.lib.ts

100% Statements 449/449
100% Branches 47/47
100% Functions 14/14
100% Lines 449/449

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 4491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 53x 53x 53x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 1x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 1x 1x 1x 1x 1x 60x 20x 20x 20x 20x 20x 20x 12x 12x 20x 1x 1x 7x 7x 7x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 60x 60x 60x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 60x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 80x 60x 120x 120x 120x 120x 120x 120x 120x 120x 120x 60x 20x 20x 20x 20x 20x 20x 20x 20x 20x 60x 6x 6x 2x 6x 4x 4x 6x 6x 1x 1x 1x 6x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 6x 60x 126x 126x 131x 131x 131x 126x 130x 130x 126x 6x 6x 6x 6x 6x 6x 6x 5x 6x 6x 6x 6x 6x 6x 2x 6x 4x 4x 6x 6x 1x 1x 1x 6x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 6x 6x 6x 6x 1x 1x 6x 1x 1x 6x 1x 1x 6x 1x 1x 6x 1x 1x 6x 1x 1x 6x 6x 126x 60x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 60x 20x 20x 20x 20x 60x 21x 21x 21x 21x 21x 21x 1x 1x 21x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x 60x
import Big from "big.js";
import DniGorahyan from "../index";
 
import Utils from "./utils.lib";
import { adjustForLeapSeconds } from "./leap.second.lib";
import EarthMonthConstants from "../constants/earth.month.constants";
import DniMonthConstants from "../constants/dni.month.constants";
 
import { TimestampFormatLoaders } from "./timestamp.format.lib";
 
 
/**
 * Default File Export
 * */
export default function convertSurfaceTimestampToDniCavernTimestamp(dniGorahyan: DniGorahyan) {
    const { convertSurfaceTimestampToCavern } = SurfaceConverterLib(dniGorahyan);
    return convertSurfaceTimestampToCavern;
}
 
/**
 * Helper Exports
 * */
export function setNthBell(pahrtahvo: number): string {
    const bellSuperScripts = [
        "", "st", "nd", "rd", "th", "th", "th", "th", "th", "th",
        "th", "th", "th", "th", "th", "th", "th", "th", "th", "th",
        "th", "st", "nd", "rd", "th", "th"
    ];
 
    return `${pahrtahvo}${bellSuperScripts[pahrtahvo]}`;
}
export function setSurfaceTimeArtifactsByString(surfaceDateTime: string, dniGorahyan: DniGorahyan) {
    const
        parsedData = surfaceDateTime.split("T"),
        requestedDate = parsedData[0].split("-"),
        requestedTime = parsedData[1].split(":");
 
    let // Convert strings To ints and store them against the class object.
        year    = parseInt(requestedDate[0]),
        month   = parseInt(requestedDate[1]),
        day     = parseInt(requestedDate[2]),
        hour    = parseInt(requestedTime[0]),
        minute  = parseInt(requestedTime[1]),
        second  = parseInt(requestedTime[2]);
    
    const monthData = EarthMonthConstants.filter(item => item.id === (month - 1));
 
    dniGorahyan.userProvidedSurfaceTS = surfaceDateTime;
    dniGorahyan.timeFragment = {
        type: "year",
        value: year,
        source: "surface"
    };
    dniGorahyan.monthId = monthData[0].id;
    dniGorahyan.monthText = monthData[0].monthNameText;
    dniGorahyan.timeFragment = {
        type: "day",
        value: day,
        source: "surface"
    };
    dniGorahyan.timeFragment = {
        type: "hour",
        value: hour,
        source: "surface"
    };;
    dniGorahyan.timeFragment = {
        type: "minute",
        value: minute,
        source: "surface"
    };;
    dniGorahyan.timeFragment = {
        type: "second",
        value: second,
        source: "surface"
    };;
 
    let dt = new Date(year, month - 1, day);
    dt.setUTCHours(hour);
    dt.setUTCMinutes(minute + (7 * 60));
    dt.setUTCSeconds(second);
    let temp = Date.parse(dt.toISOString());
 
    return adjustForLeapSeconds(Big(temp), dniGorahyan.gorahyan);
}
 
/**
 * Internal File Methods
 * */
export function SurfaceConverterLib(dniGorahyan: DniGorahyan) {
    const _convertSurfaceTimestampToCavern = function(surfaceDateTime?: Date | string | null | undefined) {
        let // Process And Store Surface Time Artifacts
            stringPassed = typeof surfaceDateTime === "string",
            dateObjectPassed = typeof surfaceDateTime === "object",
            nullOrUndefinedPassed = surfaceDateTime === undefined || surfaceDateTime === null;
 
        if(stringPassed) {
            let safeDateTimeString = safeStringOperation(surfaceDateTime);
            dniGorahyan.elapsedTimeForGivenDate = setSurfaceTimeArtifactsByString(safeDateTimeString, dniGorahyan);
        } else if (dateObjectPassed && !nullOrUndefinedPassed) {
            let safeDateTimeObject = safeDateOperation(surfaceDateTime);
            dniGorahyan.elapsedTimeForGivenDate = _setSurfaceTimeArtifactsByDateObject(safeDateTimeObject);
        } else {
            dniGorahyan.elapsedTimeForGivenDate = _setSurfaceTimeArtifactsByDateObject(new Date("2024-09-23T09:34:06"));
        }
 
        let earthDeltaInMS =
            Big(dniGorahyan.elapsedTimeForGivenDate)
                .minus(Big(dniGorahyan.elapsedTimeAtConvergence));
 
        dniGorahyan.calculatedEarthDelta = earthDeltaInMS.toNumber();
 
        /**
         * Calculate and Store D'ni Hahrtee / Hahr Delta
         * */
        let deltaWithHahrRemoved = storeAndShiftDniTimeFragment(
            earthDeltaInMS,
            dniGorahyan.EARTH_MS_PER_HAHR_BIG,
            { type: "hahr", source: "cavern" }
        );
 
        /**
         * Convert Delta to D'ni Prorahntee For Rest of Calculations
         * */
        let
            prorahnAdjustment = dniGorahyan
                .PRORAHNTEE_PER_HAHR_BIG
                .div(dniGorahyan.EARTH_MS_PER_HAHR_BIG),
            deltaInProrahntee = deltaWithHahrRemoved.times(prorahnAdjustment);
 
        dniGorahyan.calculatedProrahnteeDelta = deltaInProrahntee.toNumber();
 
        /**
         * Calculate and Store the Vailee
         * */
        let vaileeId: number = deltaInProrahntee
            .div(dniGorahyan.VAILEE_SHIFT_BIG)
            .round(Big.roundDown)
            .toNumber();
 
        dniGorahyan.vaileetee = vaileeId;
 
        let prorahnteeDeltaWithVaileeRemoved = shiftDelta(vaileeId, dniGorahyan.VAILEE_SHIFT_BIG, deltaInProrahntee, "vailee");
 
        /**
         * Calculate and Store the Yahr
         * */
        let value: Big | number = prorahnteeDeltaWithVaileeRemoved
                .div(dniGorahyan.YAHR_SHIFT_BIG).round(0, Big.roundDown).toNumber();
 
        dniGorahyan.timeFragment = {
            type: "yahr",
            value,
            source: "cavern"
        };
 
        let prorahnteeDeltaWithYahrRemoved =  shiftDelta(value, dniGorahyan.YAHR_SHIFT_BIG, prorahnteeDeltaWithVaileeRemoved, "yahr");
 
        /**
         * Calculate and Store the Gahrtahvo
         * */
        let prorahnteeDeltaWithGartahvoRemoved = storeAndShiftDniTimeFragment(
            prorahnteeDeltaWithYahrRemoved,
            dniGorahyan.GAHRTAHVO_SHIFT_BIG,
            { type: "gahrtahvo", source: "cavern" }
        );
 
        /**
         * Calculate and Store the Pahrtahvo
         * */
        let pahrtahvo: Big | number = prorahnteeDeltaWithYahrRemoved.div(dniGorahyan.PAHRTAHVO_SHIFT_BIG);
        pahrtahvo = Math.floor(pahrtahvo.toNumber());
        dniGorahyan.timeFragment = {
            type: "pahrtahvo",
            value: pahrtahvo,
            source: "cavern"
        };
 
        /**
         * Calculate and Store the Tahvo
         * */
        let tahvo: Big | number = prorahnteeDeltaWithGartahvoRemoved
            .div(dniGorahyan.TAHVO_SHIFT_BIG).round(0, Big.roundDown).toNumber();
 
        let prorahnteeDeltaWithTahvoRemoved = storeAndShiftDniTimeFragment(
            prorahnteeDeltaWithGartahvoRemoved,
            dniGorahyan.TAHVO_SHIFT_BIG,
            { type: "tahvo", source: "cavern" }
        );
 
        /**
         * Calculate and Store the Gorahn
         * */
        let prorahnteeDeltaWithGorahnRemoved = storeAndShiftDniTimeFragment(
            prorahnteeDeltaWithTahvoRemoved,
            dniGorahyan.GORAHN_SHIFT_BIG,
            { type: "gorahn", source: "cavern" }
        );
 
        /**
         * Calculate and Store the Prorahn
         * */
        dniGorahyan.timeFragment = {
            type: "prorahn",
            value: prorahnteeDeltaWithGorahnRemoved.round(0, Big.roundDown).toNumber(),
            source: "cavern"
        };
 
        /**
         * Adjust All Calculated Times As Needed And Return Final TS Value
         * */
        adjustCalculatedTimes();
        setDniConvertedTimestamp();
        return dniGorahyan;
    }
 
    const { safeDateOperation, safeStringOperation } = Utils();
    const _setSurfaceTimeArtifactsByDateObject = function(surfaceDateTime: Date) {
        const
            year = surfaceDateTime.getUTCFullYear(),
            month = surfaceDateTime.getMonth(),
            day = surfaceDateTime.getDate(),
            hours = surfaceDateTime.getHours(),
            minutes = surfaceDateTime.getMinutes(),
            seconds = surfaceDateTime.getSeconds();
 
        const monthData = EarthMonthConstants.filter(item => item.id === month);
 
        dniGorahyan.userProvidedSurfaceTS = surfaceDateTime;
        dniGorahyan.timeFragment = {
            type: "year",
            value: year,
            source: "surface"
        };
        dniGorahyan.monthId = monthData[0].id;
        dniGorahyan.monthText = monthData[0].monthNameText;
        dniGorahyan.timeFragment = {
            type: "day",
            value: day,
            source: "surface"
        };
        dniGorahyan.timeFragment = {
            type: "hour",
            value: hours,
            source: "surface"
        };
        dniGorahyan.timeFragment = {
            type: "minute",
            value: minutes,
            source: "surface"
        };
        dniGorahyan.timeFragment = {
            type: "second",
            value: seconds,
            source: "surface"
        };
 
        let dt = new Date(year, month, day);
        dt.setUTCHours(hours);
        dt.setUTCMinutes(minutes + (7 * 60));
        dt.setUTCSeconds(seconds);
        let temp = Date.parse(dt.toISOString());
 
        return adjustForLeapSeconds(Big(temp), dniGorahyan.gorahyan);
    }
    const storeAndShiftDniTimeFragment = function(delta: Big, shift: Big, passedFragment: { type: string, source: string }) {
        let
            { type, source } = passedFragment,
            value = delta
                .div(shift)
                .round(0, Big.roundDown)
                .toNumber(),
            shiftFor = type.charAt(0).toUpperCase() + type.slice(1);
 
        // Store The Fragment Value
        dniGorahyan.timeFragment = { type, value, source };
 
        // Return the Shifted Delta After Fragment Processing
        return shiftDelta(value, shift, delta, `${shiftFor}Delta`);
    }
    const shiftDelta = function(dniUnit: number, dniUnitShift: Big, currentDelta: Big, shiftFor: string): Big {
        let
            deltaShiftValueToRemove = Big(dniUnit).times(dniUnitShift),
            remainingDelta: Big | number = currentDelta.minus(deltaShiftValueToRemove);
 
        // @ts-ignore
        dniGorahyan[`calculated${shiftFor}`] = remainingDelta.toNumber();
 
        return remainingDelta;
    }
    const adjustCalculatedTimes = function() {
        adjustTimeValue('prorahn');
        adjustTimeValue('gorahn');
        adjustTimeValue('tahvo');
        adjustTimeValue('gartahvo');
        adjustTimeValue('yahr');
        adjustTimeValue('vailee');
        setFullVaileeData();
        finalCalendarAdjustments();
    }
    const _subAdjustment = function(adjustsFor: string, currentValue: number, min: number, max: number) {
        let adjustment = 0;
        if(currentValue > max) {
            adjustment = 1;
        } else if (currentValue < min) {
            adjustment = -1;
        }
 
        if(adjustsFor === "vailee") {
            dniGorahyan.vaileetee = Big(dniGorahyan.vaileetee)
                .plus(adjustment)
                .toNumber();
        } else {
            // @ts-ignore
            let newValue = Big(dniGorahyan[`${adjustsFor}tee`])
                .plus(adjustment)
                .toNumber();
 
            dniGorahyan.timeFragment = {
                type: adjustsFor,
                value: newValue,
                source: "cavern"
            };
        }
    }
    const adjustTimeValue = function(unitOfTime: string) {
        while ((
                (unitOfTime === "vailee" && dniGorahyan.vaileetee > dniGorahyan.VAILEE_MAX) ||
                (unitOfTime === "vailee" && dniGorahyan.vaileetee < dniGorahyan.VAILEE_MIN)
            ) || (
                // @ts-ignore
                unitOfTime !== "vailee" && dniGorahyan[`${unitOfTime}tee`] > dniGorahyan[`${unitOfTime.toUpperCase()}_MAX`]  ||
                // @ts-ignore
                unitOfTime !== "vailee" && dniGorahyan[`${unitOfTime}tee`] < dniGorahyan[`${unitOfTime.toUpperCase()}_MIN`]
            )) {
 
            let // @ts-ignore
                minValue = dniGorahyan[`${unitOfTime.toUpperCase()}_MIN`],
                // @ts-ignore
                maxValue = dniGorahyan[`${unitOfTime.toUpperCase()}_MAX`],
                currentValue: number = (unitOfTime === "vailee") ?
                    dniGorahyan.vaileetee :
                    // @ts-ignore
                    dniGorahyan[`${unitOfTime}tee`];
 
            let adjustedValue = 0;
 
            // @ts-ignore
            if (currentValue > maxValue) {
                adjustedValue = -maxValue;
            } else if (currentValue < minValue) {
                adjustedValue = maxValue;
            }
 
            if(unitOfTime === "vailee") {
                dniGorahyan.vaileetee = Big(dniGorahyan.vaileetee)
                    .plus(adjustedValue)
                    .toNumber();
            } else {
                // @ts-ignore
                let numberValue = dniGorahyan[`${unitOfTime}tee`],
                    newValue = Big(numberValue)
                        .plus(adjustedValue)
                        .toNumber();
 
                dniGorahyan.timeFragment = {
                    type: unitOfTime,
                    value: newValue,
                    source: "cavern"
                };
            }
 
            // Sub-processing when a main component is adjusted
            switch(unitOfTime) {
                case "prorahn":
                    _subAdjustment("gorahn", currentValue, minValue, maxValue);
                    break;
                case "gorahn":
                    _subAdjustment("tahvo", currentValue, minValue, maxValue);
                    break;
                case "tahvo":
                    _subAdjustment("gahrtahvo", currentValue, minValue, maxValue);
                    break;
                case "gahrtahvo":
                    _subAdjustment("yahr", currentValue, minValue, maxValue);
                    break;
                case "yahr":
                    _subAdjustment("vailee", currentValue, minValue, maxValue);
                    break;
                case "vailee":
                    _subAdjustment("hahr", currentValue, minValue, maxValue);
                    break;
            }
        }
    }
    const finalCalendarAdjustments = function() {
        // Final Calendar Adjustments
        // Always Increment Hahr By 9647 After Date Conversion
        dniGorahyan.timeFragment = {
            type: "hahr",
            value: Big(dniGorahyan.hahrtee)
                .plus(dniGorahyan.DNI_HAHR_REFERENCE_BIG)
                .toNumber(),
            source: "cavern"
        };
 
        // Always Increment Pahrtahvo By 1 After Date Conversion
        dniGorahyan.timeFragment = {
            type: "pahrtahvo",
            value: Big(dniGorahyan.pahrtahvotee)
                .plus(1)
                .toNumber(),
            source: "cavern"
        };
 
        // Always Increment Yahr By 1 After Date Conversion
        dniGorahyan.timeFragment = {
            type: "yahr",
            value: Big(dniGorahyan.yahrtee)
                .plus(1)
                .toNumber(),
            source: "cavern"
        };
    }
    const setDniConvertedTimestamp = function() {
        let format = TimestampFormatLoaders(dniGorahyan);
        dniGorahyan.systemProvidedSurfaceTS = format.dniDateTimeStringFormatter();
        return dniGorahyan;
    }
    const setFullVaileeData = function() {
        try {
            let data = DniMonthConstants[dniGorahyan.vaileetee];
 
            dniGorahyan.vaileeTextName = data.vaileeNameText;
            dniGorahyan.vaileeDniFontMappingName = data.dniFontVaileeNameText;
        } catch(err) {
            throw new Error("Vailee Parse Failed. Unable To Continue Date Conversion. Check the error console for more details.");
        }
    }
 
    return {
        convertSurfaceTimestampToCavern: _convertSurfaceTimestampToCavern,
        tests: {
            setFullVaileeData,
            setDniConvertedTimestamp,
            adjustTimeValue,
            subAdjustment: _subAdjustment
        }
    }
}