{"ScriptPreparationCode":"let values1000 = [...Array(1000).keys()];\r\nvar documentCookie1000 = \u0027\u0027; // mock document.cookie string - 1000 SHORT VALUES\r\nvalues1000.forEach(v =\u003E {\r\n\tdocumentCookie1000 \u002B= \u0060${v}name=${v}some-unique-cookie-value-with=equal-sign; \u0060;\r\n})\r\ndocumentCookie1000 \u002B= \u0027findMe=found-test-value-with=equal-sign; lastCookie=test-value\u0027;\r\nconsole.log(documentCookie1000);\r\n\r\nlet values10 = [...Array(10).keys()];\r\nvar documentCookie10 = \u0027\u0027; // mock document.cookie string - 10 LONG VALUES\r\nvalues10.forEach(v =\u003E {\r\n\tdocumentCookie10 \u002B= \u0060${v}name=${v}511=RmKsfdVddsfsdmrAQi8UK_S8CF7Bsdfds=O9lh5lAa74bsuEu31d3hB-_3wcypj6fdssqWzo-DbjOKnHHL6bkiEt6dsfZHrEXiFuuKTq2kdlOtE9up4EFdsfsdk90XLwjT1Up7maWRYT8EHm3eluUoQ0KHLEjpSfjFjiY75b8XRHWuNMcw9j4vLaQU3CcfIY-GMRz9DhYF1dsfsdec8PeJj9Mg9do_7ziOcdsfdsDrmLXj3P69RWkIVMpXh_iYQ2bpPWW7zAq0Vqkdfdq7fqYZYsARIz57JXJwGnezDrLjbnFEJNnCfFW52jqHYJlfKpmdfdhPp9DZzfdDCoXDuQ1KRDRgAxmfukkZcMIbdOWcooD41lgezwxpaXXuvYrJTpJhJd5iifSd1STFyxMeQq6y9mIBuc-Q1FXVKqVzu1KmMvRozzqz3bt95zUKmEdGGUkdcPdE8n11cDMdJutpNhEm2gMVcKmKAcEtt-5qOCbOjmqdoFd-qdSF7b19v8Zdsfds8oOf5LPQjB8kU1WlwrEJ59KAkMEyEBjGfVZMkbvxdfdsaLxLgOHqEWrHZoZjrnMMEOgoarCviw7qCD4OqabPhdsfdsbh9UVwJelx_cVddfdsf9GetyLbPy1PFdfOgT; \u0060;\r\n})\r\ndocumentCookie10 \u002B= \u0027findMe=found-test-value-with=equal-sign; lastCookie=test-value\u0027;\r\nconsole.log(documentCookie10);\r\n\r\nfunction getCookieFor(name, cookies) {\r\n const nameEQ = name \u002B \u0022=\u0022;\r\n const ca = cookies.split(\u0027; \u0027);\r\n for(let i=0;i \u003C ca.length;i\u002B\u002B) {\r\n const c = ca[i];\r\n if (c.indexOf(nameEQ) === 0) {\r\n const value = c.substring(nameEQ.length);\r\n return decodeURIComponent(value); // returns first found cookie\r\n }\r\n }\r\n return null;\r\n}\r\n\r\nfunction getCookieForOf(name, cookies) {\r\n const nameEQ = name \u002B \u0027=\u0027;\r\n for (const cookie of cookies.split(\u0027; \u0027)) {\r\n if (cookie.indexOf(nameEQ) === 0) {\r\n const value = cookie.substring(nameEQ.length);\r\n return decodeURIComponent(value); // returns first found cookie\r\n }\r\n }\r\n return null;\r\n}\r\n\r\nfunction getCookieIndexOf(name, cookies) {\r\n const nameEQ = name \u002B \u0027=\u0027;\r\n const cookieStart = cookies.indexOf(nameEQ);\r\n if (cookieStart !== -1) {\r\n const cookieValueStart = cookieStart \u002B nameEQ.length;\r\n const cookieEnd = cookies.indexOf(\u0027;\u0027, cookieValueStart);\r\n const value = cookies.substring(\r\n cookieValueStart,\r\n cookieEnd !== -1 ? cookieEnd : undefined\r\n );\r\n return decodeURIComponent(value); // returns first found cookie\r\n }\r\n return null;\r\n}\r\n\r\nfunction getCookieFind(name, cookies) {\r\n const nameEQ = name \u002B \u0027=\u0027;\r\n const foundCookie = cookies\r\n .split(\u0027; \u0027)\r\n .find(c =\u003E c.indexOf(nameEQ) === 0); // returns first found cookie\r\n if (foundCookie) {\r\n return decodeURIComponent(foundCookie.substring(nameEQ.length));\r\n }\r\n return null;\r\n}\r\n\r\nfunction getCookieReduce(name, cookies) {\r\n return cookies.split(\u0027; \u0027).reduce((r, v) =\u003E {\r\n const [n, ...val] = v.split(\u0027=\u0027); // cookie value can contain \u0022=\u0022\r\n if(r) return r; // returns first found cookie\r\n return n === name ? decodeURIComponent(val.join(\u0027=\u0027)) : r; // returns last found cookie (overwrites)\r\n }, \u0027\u0027);\r\n}\r\n\r\nfunction getCookieRegex(name, cookies) {\r\n const matched = \u0060; ${cookies}\u0060.match(\u0060;\\\\s*${name}=([^;]\u002B)\u0060);\r\n if (matched) return matched[1];\r\n return null;\r\n}\r\n","TestCases":[{"Name":"for...of - 1000 short cookies","Code":"return getCookieForOf(\u0027findMe\u0027, documentCookie1000);","IsDeferred":false},{"Name":"IndexOf() - 1000 short cookies","Code":"return getCookieIndexOf(\u0027findMe\u0027, documentCookie1000);","IsDeferred":false},{"Name":"find() - 1000 short cookies","Code":"return getCookieFind(\u0027findMe\u0027, documentCookie1000);","IsDeferred":false},{"Name":"reduce() - 1000 short cookies","Code":"return getCookieReduce(\u0027findMe\u0027, documentCookie1000);","IsDeferred":false},{"Name":"for...of - 10 long cookies","Code":"return getCookieForOf(\u0027findMe\u0027, documentCookie10);","IsDeferred":false},{"Name":"IndexOf() - 10 long cookies","Code":"return getCookieIndexOf(\u0027findMe\u0027, documentCookie10);","IsDeferred":false},{"Name":"find() - 10 long cookies","Code":"return getCookieFind(\u0027findMe\u0027, documentCookie10);","IsDeferred":false},{"Name":"reduce() - 10 long cookies","Code":"return getCookieReduce(\u0027findMe\u0027, documentCookie10);","IsDeferred":false},{"Name":"for - 1000 short cookies","Code":"return getCookieFor(\u0027findMe\u0027, documentCookie1000);","IsDeferred":false},{"Name":"for - 10 long cookies","Code":"return getCookieFor(\u0027findMe\u0027, documentCookie10);","IsDeferred":false},{"Name":"getCookieRegex - 1000 short cookies","Code":"return getCookieRegex(\u0027findMe\u0027, documentCookie1000);","IsDeferred":false},{"Name":"getCookieRegex - 10 long cookies","Code":"return getCookieRegex(\u0027findMe\u0027, documentCookie10);","IsDeferred":false}]}