UNPKG

@microsoft/kiota-abstractions

Version:

Core abstractions for kiota generated libraries in TypeScript and JavaScript

101 lines 4.19 kB
/** * ------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. * See License in the project root for license information. * ------------------------------------------------------------------------------------------- */ import { parse as parseDuration, serialize as serializeDuration } from "tinyduration"; /** * Represents a duration value. ISO 8601. */ export class Duration { /** * Creates a new Duration value from the given parameters. * @param root0 The years, months, weeks, days, hours, minutes, seconds, and negative flag * @param root0.years The years * @param root0.months The months * @param root0.weeks The weeks * @param root0.days The days * @param root0.hours The hours * @param root0.minutes The minutes * @param root0.seconds The seconds * @param root0.negative The negative flag * @returns The new Duration * @throws An error if years is invalid * @throws An error if months is invalid * @throws An error if weeks is invalid * @throws An error if days is invalid * @throws An error if hours is invalid * @throws An error if minutes is invalid * @throws An error if seconds is invalid * @throws An error if weeks is used in combination with years or months */ constructor({ years = 0, months = 0, weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0, negative = false }) { if (years < 0 || years > 9999) { throw new Error("Year must be between 0 and 9999"); } if (months < 0) { throw new Error("Month must be greater or equal to 0"); } if (weeks < 0) { throw new Error("Week must be greater or equal to 0"); } if (days < 0) { throw new Error("Day must be greater or equal to 0"); } if (hours < 0) { throw new Error("Hour must be greater or equal to 0"); } if (minutes < 0) { throw new Error("Minute must be greater or equal to 0"); } if (seconds < 0) { throw new Error("Second must be greater or equal to 0"); } if (weeks > 0 && (days > 0 || hours > 0 || minutes > 0 || seconds > 0)) { throw new Error("Cannot have weeks and days or hours or minutes or seconds"); } if ((years > 0 || months > 0) && weeks > 0) { throw new Error("Cannot have weeks and months or weeks and years"); } this.years = years; this.months = months; this.weeks = weeks; this.days = days; this.hours = hours; this.minutes = minutes; this.seconds = seconds; this.negative = negative; } /** * Parses a string into a Duration. The string can be of the ISO 8601 duration format. * @param value The value to parse * @returns The parsed Duration. * @throws An error if the value is invalid */ static parse(value) { var _a, _b, _c, _d, _e, _f, _g, _h; if (!value || value.length === 0) { return undefined; } const duration = parseDuration(value); return new Duration({ years: (_a = duration.years) !== null && _a !== void 0 ? _a : 0, months: (_b = duration.months) !== null && _b !== void 0 ? _b : 0, weeks: (_c = duration.weeks) !== null && _c !== void 0 ? _c : 0, days: (_d = duration.days) !== null && _d !== void 0 ? _d : 0, hours: (_e = duration.hours) !== null && _e !== void 0 ? _e : 0, minutes: (_f = duration.minutes) !== null && _f !== void 0 ? _f : 0, seconds: (_g = duration.seconds) !== null && _g !== void 0 ? _g : 0, negative: (_h = duration.negative) !== null && _h !== void 0 ? _h : false, }); } /** * Serializes the duration to a string in the ISO 8601 duration format. * @returns The serialized duration. */ toString() { return serializeDuration(this); } } //# sourceMappingURL=duration.js.map