UNPKG

typia

Version:

Superfast runtime validators with only one line

1 lines 3.76 kB
{"version":3,"file":"_randomFormatIpv6.mjs","names":[],"sources":["../../src/internal/_randomFormatIpv6.ts"],"sourcesContent":["import { _randomFormatIpv4 } from \"./_randomFormatIpv4\";\nimport { _randomInteger } from \"./_randomInteger\";\nimport {\n _ILengthProps,\n _randomLengthPick,\n _randomLengthWindow,\n} from \"./_randomStringLength\";\nimport { __randomComposition } from \"./private/__randomComposition\";\n\nexport const _randomFormatIpv6 = (props?: _ILengthProps): string => {\n if (props?.minLength === undefined && props?.maxLength === undefined)\n return new Array(GROUPS).fill(0).map(random).join(\":\");\n const length: number = _randomLengthPick(\n _randomLengthWindow(props, {\n minimum: SHORTEST,\n maximum: LONGEST,\n spread: 12,\n }),\n );\n // `::` alone is the all-zero address, and the three forms below cover every\n // length between it and the padded IPv4-suffixed address: a compressed prefix\n // for the short ones, eight groups of one to four hexadecimal digits for the\n // middle, and six groups plus a dotted-quad tail for the long ones.\n if (length === SHORTEST) return \"::\";\n if (length < GROUPS * DIGITS_MIN + COLONS) {\n // `::` plus `count` groups spends one colon between each pair.\n const count: number = Math.ceil((length - 1) / (DIGITS_MAX + 1));\n return `::${compose(length - 1 - count, count)}`;\n }\n if (length <= GROUPS * DIGITS_MAX + COLONS)\n return compose(length - COLONS, GROUPS);\n // The dotted-quad tail is drawn at its longest so the six leading groups keep\n // a composable number of digits.\n const tail: string = _randomFormatIpv4({\n minLength: IPV4_MAX,\n maxLength: IPV4_MAX,\n });\n return `${compose(length - IPV4_MAX - EMBEDDED, EMBEDDED)}:${tail}`;\n};\n\nconst GROUPS = 8;\nconst COLONS = GROUPS - 1;\nconst DIGITS_MIN = 1;\nconst DIGITS_MAX = 4;\nconst EMBEDDED = 6; // groups preceding a dotted-quad tail\nconst IPV4_MAX = \"255.255.255.255\".length;\nconst SHORTEST = \"::\".length;\nconst LONGEST = EMBEDDED * DIGITS_MAX + EMBEDDED + IPV4_MAX;\n\nconst compose = (digits: number, count: number): string =>\n __randomComposition({\n total: digits,\n count,\n minimum: DIGITS_MIN,\n maximum: DIGITS_MAX,\n })\n .map(group)\n .join(\":\");\n\nconst random = () =>\n _randomInteger({\n type: \"integer\",\n minimum: 0,\n maximum: 65_535,\n }).toString(16);\n\nconst group = (digits: number): string => {\n let text: string = \"\";\n for (let i: number = 0; i < digits; ++i)\n text += _randomInteger({\n type: \"integer\",\n minimum: 0,\n maximum: 15,\n }).toString(16);\n return text;\n};\n"],"mappings":";;;;;AASA,MAAa,qBAAqB,UAAkC;CAClE,IAAI,OAAO,cAAc,KAAA,KAAa,OAAO,cAAc,KAAA,GACzD,OAAO,IAAI,MAAM,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,KAAK,GAAG;CACvD,MAAM,SAAiB,kBACrB,oBAAoB,OAAO;EACzB,SAAS;EACT,SAAS;EACT,QAAQ;CACV,CAAC,CACH;CAKA,IAAI,WAAW,UAAU,OAAO;CAChC,IAAI,SAAS,SAAS,aAAa,QAAQ;EAEzC,MAAM,QAAgB,KAAK,MAAM,SAAS,MAAM,aAAa,EAAE;EAC/D,OAAO,KAAK,QAAQ,SAAS,IAAI,OAAO,KAAK;CAC/C;CACA,IAAI,UAAU,SAAS,aAAa,QAClC,OAAO,QAAQ,SAAS,QAAQ,MAAM;CAGxC,MAAM,OAAe,kBAAkB;EACrC,WAAW;EACX,WAAW;CACb,CAAC;CACD,OAAO,GAAG,QAAQ,SAAS,WAAW,UAAU,QAAQ,EAAE,GAAG;AAC/D;AAEA,MAAM,SAAS;AACf,MAAM,SAAS,SAAS;AACxB,MAAM,aAAa;AACnB,MAAM,aAAa;AACnB,MAAM,WAAW;AACjB,MAAM,WAAW;AACjB,MAAM,WAAW;AACjB,MAAM,UAAU;AAEhB,MAAM,WAAW,QAAgB,UAC/B,oBAAoB;CAClB,OAAO;CACP;CACA,SAAS;CACT,SAAS;AACX,CAAC,CAAC,CACC,IAAI,KAAK,CAAC,CACV,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