UNPKG

typia

Version:

Superfast runtime validators with only one line

1 lines 2.05 kB
{"version":3,"file":"_randomFormatDatetime.mjs","names":[],"sources":["../../src/internal/_randomFormatDatetime.ts"],"sourcesContent":["import {\n _ILengthProps,\n _RANDOM_LENGTH_ERROR,\n _randomLengthPick,\n _randomLengthWindow,\n} from \"./_randomStringLength\";\nimport { __randomDigits } from \"./private/__randomComposition\";\nimport { __IEpochProps, __randomEpoch } from \"./private/__randomEpoch\";\n\nexport const _randomFormatDatetime = (\n props?: __IEpochProps & _ILengthProps,\n) => {\n const instant = (): string => new Date(__randomEpoch(props)).toISOString();\n if (props?.minLength === undefined && props?.maxLength === undefined)\n return instant();\n // `YYYY-MM-DDTHH:MM:SS` plus `Z` is the shortest instant, and a fractional\n // second grows it one digit at a time. Because that fraction needs both a dot\n // and at least one digit, 21 is the single length between the two forms that\n // no instant can have.\n const window = _randomLengthWindow(props, {\n minimum: SECONDS + 1,\n spread: 8,\n });\n let length: number = _randomLengthPick(window);\n if (length === SECONDS + 2)\n length = window.high >= SECONDS + 3 ? SECONDS + 3 : SECONDS + 1;\n if (length < window.low || length > window.high)\n throw new Error(_RANDOM_LENGTH_ERROR);\n const base: string = instant().substring(0, SECONDS);\n return length === SECONDS + 1\n ? `${base}Z`\n : `${base}.${__randomDigits(length - SECONDS - 2)}Z`;\n};\n\nconst SECONDS = \"0000-00-00T00:00:00\".length;\n"],"mappings":";;;;AASA,MAAa,yBACX,UACG;CACH,MAAM,gBAAwB,IAAI,KAAK,cAAc,KAAK,CAAC,CAAC,CAAC,YAAY;CACzE,IAAI,OAAO,cAAc,KAAA,KAAa,OAAO,cAAc,KAAA,GACzD,OAAO,QAAQ;CAKjB,MAAM,SAAS,oBAAoB,OAAO;EACxC,SAAS,UAAU;EACnB,QAAQ;CACV,CAAC;CACD,IAAI,SAAiB,kBAAkB,MAAM;CAC7C,IAAI,WAAW,UAAU,GACvB,SAAS,OAAO,QAAQ,UAAU,IAAI,UAAU,IAAI,UAAU;CAChE,IAAI,SAAS,OAAO,OAAO,SAAS,OAAO,MACzC,MAAM,IAAI,MAAM,oBAAoB;CACtC,MAAM,OAAe,QAAQ,CAAC,CAAC,UAAU,GAAG,OAAO;CACnD,OAAO,WAAW,UAAU,IACxB,GAAG,KAAK,KACR,GAAG,KAAK,GAAG,eAAe,SAAS,UAAU,CAAC,EAAE;AACtD;AAEA,MAAM,UAAU"}