MCPcopy Create free account
hub / github.com/codemix/graph / fromString

Method fromString

packages/graph/src/TemporalTypes.ts:1074–1151  ·  view source on GitHub ↗

* Create a DurationValue from an ISO 8601 duration string. * Format: P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S

(iso: string)

Source from the content-addressed store, hash-verified

1072 * Format: P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S
1073 */
1074 public static fromString(iso: string): DurationValue | null {
1075 // Handle negative durations
1076 let isNegative = false;
1077 let str = iso;
1078 if (str.startsWith("-")) {
1079 isNegative = true;
1080 str = str.substring(1);
1081 }
1082
1083 if (!str.startsWith("P")) return null;
1084 str = str.substring(1);
1085
1086 let months = 0;
1087 let days = 0;
1088 let seconds = 0;
1089 let nanoseconds = 0;
1090
1091 // Split by T to separate date and time parts
1092 const parts = str.split("T");
1093 const datePart = parts[0] || "";
1094 const timePart = parts[1] || "";
1095
1096 // Parse date part (years, months, weeks, days)
1097 // Match patterns like "1Y", "2M", "1W", "3D"
1098 const datePattern = /(-?\d+(?:\.\d+)?)(Y|M|W|D)/g;
1099 let match: RegExpExecArray | null;
1100 while ((match = datePattern.exec(datePart)) !== null) {
1101 const value = parseFloat(match[1] ?? "0");
1102 const unit = match[2];
1103 switch (unit) {
1104 case "Y":
1105 months += value * 12;
1106 break;
1107 case "M":
1108 months += value;
1109 break;
1110 case "W":
1111 days += value * 7;
1112 break;
1113 case "D":
1114 days += value;
1115 break;
1116 }
1117 }
1118
1119 // Parse time part (hours, minutes, seconds)
1120 // Match patterns like "1H", "30M", "15.5S"
1121 const timePattern = /(-?\d+(?:\.\d+)?)(H|M|S)/g;
1122 while ((match = timePattern.exec(timePart)) !== null) {
1123 const value = parseFloat(match[1] ?? "0");
1124 const unit = match[2];
1125 switch (unit) {
1126 case "H":
1127 seconds += value * 3600;
1128 break;
1129 case "M":
1130 seconds += value * 60;
1131 break;

Callers

nothing calls this directly

Calls 1

startsWithMethod · 0.45

Tested by

no test coverage detected