MCPcopy
hub / github.com/arktypeio/arktype / tryParseDatePattern

Function tryParseDatePattern

ark/type/keywords/string.ts:229–267  ·  view source on GitHub ↗
(
	data: string,
	opts?: DateOptions
)

Source from the content-addressed store, hash-verified

227 `a ${format}-formatted date`
228
229export const tryParseDatePattern = (
230 data: string,
231 opts?: DateOptions
232): Date | string => {
233 if (!opts?.format) {
234 const result = new Date(data)
235 return isValidDateInstance(result) ? result : "a valid date"
236 }
237 if (opts.format === "iso") {
238 return iso8601Matcher.test(data) ?
239 new Date(data)
240 : writeFormattedExpected("iso")
241 }
242 const dataParts = data.split(dayDelimiterMatcher)
243 // will be the first delimiter matched, if there is one
244 const delimiter: string | undefined = data[dataParts[0].length]
245 const formatParts = delimiter ? opts.format.split(delimiter) : [opts.format]
246
247 if (dataParts.length !== formatParts.length)
248 return writeFormattedExpected(opts.format)
249
250 const parsedParts: ParsedDayParts = {}
251 for (let i = 0; i < formatParts.length; i++) {
252 if (
253 dataParts[i].length !== formatParts[i].length &&
254 // if format is "m" or "d", data is allowed to be 1 or 2 characters
255 !(formatParts[i].length === 1 && dataParts[i].length === 2)
256 )
257 return writeFormattedExpected(opts.format)
258
259 parsedParts[formatParts[i][0] as PartKey] = dataParts[i]
260 }
261
262 const date = new Date(`${parsedParts.m}/${parsedParts.d}/${parsedParts.y}`)
263
264 if (`${date.getDate()}` === parsedParts.d) return date
265
266 return writeFormattedExpected(opts.format)
267}
268
269const isParsableDate = (s: string) => !Number.isNaN(new Date(s).valueOf())
270

Callers

nothing calls this directly

Calls 3

isValidDateInstanceFunction · 0.85
writeFormattedExpectedFunction · 0.85
testMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…