{"id":8854,"date":"2024-08-15T12:22:09","date_gmt":"2024-08-15T11:22:09","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=8854"},"modified":"2024-10-21T13:11:01","modified_gmt":"2024-10-21T12:11:01","slug":"javascript-iterator-wrapper","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-iterator-wrapper\/","title":{"rendered":"Javascript IteratorWrapper"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"javascript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">(() => {\r\n\r\n    function from(iterable) {\r\n        return new IteratorWrapper(iterable);\r\n    }\r\n\r\n    function* map(iter, f) {\r\n        for (let x of iter) {\r\n            yield f(x);\r\n        }\r\n    }\r\n\r\n    function* filter(iter, f) {\r\n        for (let x of iter) {\r\n            if (f(x)) yield x;\r\n        }\r\n    }\r\n\r\n    function* take(iter, n) {\r\n        let i = 0;\r\n        for (let x of iter) {\r\n            if (i++ &lt; n) yield x;\r\n            else break;\r\n        }\r\n    }\r\n\r\n    class IteratorWrapper {\r\n        constructor(iterable) {\r\n            this.iter = iterable[Symbol.iterator]();\r\n        }\r\n\r\n        [Symbol.iterator]() {\r\n            return this.iter;\r\n        }\r\n\r\n        map(f) {\r\n            return from(map(this.iter, f));\r\n        }\r\n\r\n        filter(f) {\r\n            return from(filter(this.iter, f));\r\n        }\r\n\r\n        take(n) {\r\n            return from(take(this.iter, n));\r\n        }\r\n\r\n        toArray() {\r\n            return [...this.iter];\r\n        }\r\n    }\r\n\r\n    \/\/ Voorbeeld gebruik:\r\n    const numbers = [1, 2, 3, 4, 5];\r\n    const result = from(numbers)\r\n        .map(x => x * 2)\r\n        .filter(x => x > 5)\r\n        .take(2)\r\n        .toArray();\r\n\r\n    console.log(result); \/\/ [6, 8]\r\n\r\n})();\r\n<\/pre><\/div>\n\n\n\n<p>Example:<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"javascript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">(() => {\r\n    function from(iterable) {\r\n        return new IteratorWrapper(iterable);\r\n    }\r\n\r\n    function* map(iter, f) {\r\n        for (let x of iter) {\r\n            yield f(x);\r\n        }\r\n    }\r\n\r\n    function* filter(iter, f) {\r\n        for (let x of iter) {\r\n            if (f(x)) yield x;\r\n        }\r\n    }\r\n\r\n    function* take(iter, n) {\r\n        let i = 0;\r\n        for (let x of iter) {\r\n            if (i++ &lt; n) yield x;\r\n            else break;\r\n        }\r\n    }\r\n\r\n    class IteratorWrapper {\r\n        constructor(iterable) {\r\n            this.iter = iterable[Symbol.iterator]();\r\n        }\r\n        [Symbol.iterator]() {\r\n            return this.iter;\r\n        }\r\n        map(f) {\r\n            return from(map(this.iter, f));\r\n        }\r\n        filter(f) {\r\n            return from(filter(this.iter, f));\r\n        }\r\n        take(n) {\r\n            return from(take(this.iter, n));\r\n        }\r\n        first() {\r\n            return this.iter?.next()?.value || undefined;\r\n        }\r\n        toArray() {\r\n            return [...this.iter];\r\n        }\r\n    }\r\n\r\n    const dataStr = [\"&lt;div class=\\\"c-clients__contain\\\" id=\\\"jsClientsItemsHolder\\\">\",\r\n        \"&lt;a href=\\\"https:\/\/www.2foqus.nl\/\\\" target=\\\"_BLANK\\\" class=\\\"c-clients__item\\\">\",\r\n        \"&lt;figure class=\\\"c-clients__figure\\\">\",\r\n        \"\\t\\t\\t\\t&lt;img src=\\\"https:\/\/tans.net\/storage\/1194\/conversions\/2foqus-2019-Identity-Logo-CMYK-1.2-(002)-logo.png\\\" alt=\\\"2Foqus\\\" class=\\\"c-clients__img\\\">\",\r\n        \"\\t\\t&lt;\/figure>\",\r\n        \"&lt;\/a>\",\r\n        \"\\t&lt;a href=\\\"https:\/\/www.aartstoiletcabine.nl\/\\\" target=\\\"_BLANK\\\" class=\\\"c-clients__item\\\">\",\r\n        \"&lt;figure class=\\\"c-clients__figure\\\">\",\r\n        \"\\t\\t\\t\\t&lt;img src=\\\"https:\/\/tans.net\/storage\/1163\/conversions\/Aarts-Sanitair-Service-logo.PNG\\\" alt=\\\"Aarts Sanitair Service\\\" class=\\\"c-clients__img\\\">\",\r\n        \"\\t\\t&lt;\/figure>\",\r\n        \"&lt;\/a>\",\r\n        \"\\t&lt;a href=\\\"https:\/\/www.abrex.nl\/\\\" target=\\\"_BLANK\\\" class=\\\"c-clients__item\\\">\",\r\n        \"&lt;figure class=\\\"c-clients__figure\\\">\",\r\n        \"\\t\\t\\t\\t&lt;img src=\\\"https:\/\/tans.net\/storage\/1107\/conversions\/Logo-Abrex-logo.png\\\" alt=\\\"Abrex Logistics B.V.\\\" class=\\\"c-clients__img\\\">\",\r\n        \"\\t\\t&lt;\/figure>\",\r\n        \"&lt;\/a>\",\r\n        \"&lt;\/div>\\t\"].join(\"\\n\");\r\n\r\n    const matches = (source, regex) => from(String(source || \"\").matchAll(regex));\r\n\r\n    const reLinks = \/&lt;a\\s(.*?)>(.*?)&lt;\\\/a>\/gms;\r\n    const reHref = \/href=\"(.*?)\"\/gms;\r\n    const reAlt = \/&lt;img\\s.*?alt=\"(.*?)\".*?>\/gms;\r\n\r\n    const items = matches(dataStr, reLinks)\r\n        .map(x => {\r\n            const [_, attrs, content] = x;\r\n            return { \r\n                alt:  matches(content, reAlt).first()[1],\r\n                href: matches(attrs, reHref).first()[1] \r\n            };\r\n        })\r\n        .take(2)\r\n        .toArray();\r\n\r\n    console.log(items);\r\n})();<\/pre><\/div>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"javascript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">(() => {\r\n    function createIteratorWrapper(iterable) {\r\n        const iter = iterable[Symbol.iterator](); \/\/ Haal de iterator op van het meegegeven iterable object\r\n        return {\r\n            [Symbol.iterator]() {\r\n                return iter;\r\n            },\r\n            map(f) {\r\n                return createIteratorWrapper(function* () { \r\n                    for (let x of iter) {\r\n                        yield f(x);\r\n                    }\r\n                }());\r\n            },\r\n            filter(predicate) {\r\n                return createIteratorWrapper(function* () {\r\n                    for (let x of iter) {\r\n                        if (predicate(x)) yield x;\r\n                    }\r\n                }());\r\n            },\r\n            take(n) {\r\n                return createIteratorWrapper(function* () {\r\n                    let i = 0;\r\n                    for (let x of iter) {\r\n                        if (i++ &lt; n) yield x;\r\n                        else break;\r\n                    }\r\n                }());\r\n            },\r\n            toArray() {\r\n                return [...iter];\r\n            }\r\n        };\r\n    }\r\n    \r\n    \/\/ Voorbeeld gebruik:\r\n    const numbers = [1, 2, 3, 4, 5];\r\n    const iter = createIteratorWrapper(numbers);\r\n\r\n    const result = iter\r\n        .map(x => x * 2)\r\n        .filter(x => x > 5)\r\n        .take(2)\r\n        .toArray();\r\n\r\n    console.log(result); \/\/ [6, 8]\r\n})();\r\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Example:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8854","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=8854"}],"version-history":[{"count":7,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8854\/revisions"}],"predecessor-version":[{"id":8864,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8854\/revisions\/8864"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=8854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=8854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=8854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}