UNPKG

typia

Version:

Superfast runtime validators with only one line

1 lines 2.33 kB
{"version":3,"file":"_randomFormatIpv4.mjs","names":[],"sources":["../../src/internal/_randomFormatIpv4.ts"],"sourcesContent":["import { _randomInteger } from \"./_randomInteger\";\nimport {\n _ILengthProps,\n _randomLengthPick,\n _randomLengthWindow,\n} from \"./_randomStringLength\";\nimport { __randomComposition } from \"./private/__randomComposition\";\n\nexport const _randomFormatIpv4 = (props?: _ILengthProps): string => {\n if (props?.minLength === undefined && props?.maxLength === undefined)\n return new Array(4).fill(0).map(random).join(\".\");\n // Three dots plus four octets of one to three digits: `0.0.0.0` is the\n // shortest address and `255.255.255.255` the longest, and every length\n // between them is reached by choosing how many digits each octet spends.\n const length: number = _randomLengthPick(\n _randomLengthWindow(props, {\n minimum: 4 * DIGITS_MIN + DOTS,\n maximum: 4 * DIGITS_MAX + DOTS,\n spread: 8,\n }),\n );\n return __randomComposition({\n total: length - DOTS,\n count: 4,\n minimum: DIGITS_MIN,\n maximum: DIGITS_MAX,\n })\n .map(octet)\n .join(\".\");\n};\n\nconst DOTS = 3;\nconst DIGITS_MIN = 1;\nconst DIGITS_MAX = 3;\n\nconst random = () =>\n _randomInteger({\n type: \"integer\",\n minimum: 0,\n maximum: 255,\n });\n\n// An octet is bounded by 255, so a three-digit one starts at 100 and stops\n// there rather than at 999.\nconst octet = (digits: number): string =>\n String(\n _randomInteger({\n type: \"integer\",\n minimum: digits === 1 ? 0 : Math.pow(10, digits - 1),\n maximum: digits === 3 ? 255 : Math.pow(10, digits) - 1,\n }),\n );\n"],"mappings":";;;;AAQA,MAAa,qBAAqB,UAAkC;CAClE,IAAI,OAAO,cAAc,KAAA,KAAa,OAAO,cAAc,KAAA,GACzD,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,KAAK,GAAG;CAWlD,OAAO,oBAAoB;EACzB,OARqB,kBACrB,oBAAoB,OAAO;GACzB,SAAS,IAAI,aAAa;GAC1B,SAAS,IAAI,aAAa;GAC1B,QAAQ;EACV,CAAC,CAGW,IAAI;EAChB,OAAO;EACP,SAAS;EACT,SAAS;CACX,CAAC,CAAC,CACC,IAAI,KAAK,CAAC,CACV,KAAK,GAAG;AACb;AAEA,MAAM,OAAO;AACb,MAAM,aAAa;AACnB,MAAM,aAAa;AAEnB,MAAM,eACJ,eAAe;CACb,MAAM;CACN,SAAS;CACT,SAAS;AACX,CAAC;AAIH,MAAM,SAAS,WACb,OACE,eAAe;CACb,MAAM;CACN,SAAS,WAAW,IAAI,IAAI,KAAK,IAAI,IAAI,SAAS,CAAC;CACnD,SAAS,WAAW,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,IAAI;AACvD,CAAC,CACH"}