<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmed Elharouny</title>
    <description>The latest articles on DEV Community by Ahmed Elharouny (@harouny).</description>
    <link>https://dev.to/harouny</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F176656%2F2910ea7f-1bc0-4624-a88f-496c02871d69.jpeg</url>
      <title>DEV Community: Ahmed Elharouny</title>
      <link>https://dev.to/harouny</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harouny"/>
    <language>en</language>
    <item>
      <title>Introducing xcomponent support for angular 2+</title>
      <dc:creator>Ahmed Elharouny</dc:creator>
      <pubDate>Wed, 12 Jul 2017 22:39:29 +0000</pubDate>
      <link>https://dev.to/harouny/introducing-xcomponent-support-for-angular-2-4bmh</link>
      <guid>https://dev.to/harouny/introducing-xcomponent-support-for-angular-2-4bmh</guid>
      <description>&lt;p&gt;For more information about &lt;a href="https://github.com/krakenjs/xcomponent"&gt;xcomponent&lt;/a&gt; framework itself or how to create components please check &lt;a href="https://medium.com/@bluepnume/introducing-xcomponent-seamless-cross-domain-web-components-from-paypal-c0144f3e82bf"&gt;this post&lt;/a&gt; by &lt;a href="https://medium.com/u/97f5eca783db"&gt;Daniel Brain&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Angular2 driver
&lt;/h4&gt;

&lt;p&gt;With version 3.0.0 of xcomponent, its now easier than ever to embed xcomponents into your angular apps by utilizing the new angular2 driver. Using the driver will enable xcomponent to integrate with angular specific concepts such as input bindings and change detection cycle which results in a better and more predictable experience.&lt;/p&gt;

&lt;h4&gt;
  
  
  Usage example
&lt;/h4&gt;

&lt;p&gt;Given that inside an angular app we want to use a login component that is declared as:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(login.js)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.MyLoginXComponent = xcomponent.create({
 tag: 'my-login-component',
 url: 'http://www.component.com/login.htm',
 props: {
 prefilledEmail: {
 type: 'string',
 required: false
 },
 onLogin: {
 type: 'function',
 required: true
 }
 }
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first step is to make sure that component script is loaded into current page:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(index.html)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="http://www.component.com/login.js"\&amp;gt;&amp;lt;/script\&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then we need to register angular2 driver and get a reference to the angular module that holds the component:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(my-login-xcomponent-module.ts)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as ngCore from '@angular/core';
declare const MyLoginXComponent:any;
export const MyLoginXComponentModule = MyLoginXComponent.driver('angular2', ngCore);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then import that module into any module where you want to use the component:&lt;/p&gt;

&lt;p&gt;(&lt;em&gt;app-module.ts&lt;/em&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app-component';
import { MyLoginXComponentModule } from './my-login-xcomponent-module';

@NgModule({

imports: [BrowserModule, MyLoginXComponentModule],
 declarations: [AppComponent],
 bootstrap: [AppComponent]
})

export class AppModule {
 constructor () {
 }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we can use the component the same way we would use any other component in the app:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(app-component.html)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;my-login-component [props]="{ onLogin: onLogin, prefilledEmail: prefilledEmail }"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(app-component.ts)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';

@Component({
 selector: 'my-app',
 templateUrl:'./app-component.html',
})
export class AppComponent {
 prefilledEmail:string;
 email:string;
 constructor() {
 this.prefilledEmail = 'foo@bar.com';
 this.onLogin = this.onLogin.bind(this);
 }
 public onLogin (email){
 this.email = email;
 }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A complete example code can be fond at the following URLs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/krakenjs/xcomponent/tree/master/demo/frameworks/angular2_TypeScript"&gt;TypeScript version&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/krakenjs/xcomponent/tree/master/demo/frameworks/angular2"&gt;Javascript/UMD version&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Things to notice:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;In order to register the angular2 driver we need to pass all functions exported by “&lt;em&gt;@angular/core”&lt;/em&gt; module. That is to make sure that you don’t have to update your code if we later on decided to use another angular core function. You can get access to an object that has all exported functions using “&lt;em&gt;import * as”&lt;/em&gt; technique.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as ngCore from '@angular/core';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Alternatively you can still only pass functions that is used by angular2 driver at the moment
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, NgModule, ElementRef, NgZone } from '@angular/core';

declare const MyLoginXComponent:any;
export const MyLoginXComponentModule = MyLoginXComponent.driver('angular2', { Component, NgModule, ElementRef, NgZone } );
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The contract for the wrapping angular component is identical to the contract for creating the xcomponent or rendering it manually; one “&lt;em&gt;props”&lt;/em&gt; input binding where we can add properties and functions expected by the underlying xcomponent.&lt;/li&gt;
&lt;li&gt;Because functions will be called from xcomponent on another context and potentially another domain we need to make sure we “&lt;em&gt;bind”&lt;/em&gt; the function back to “&lt;em&gt;this”&lt;/em&gt; (the class) as by default it will be bound to the xcomponent instance itself. This will make sure we can safely use “&lt;em&gt;this”&lt;/em&gt; inside “&lt;em&gt;onLogin”&lt;/em&gt; function to refer to the component class as we expect.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.onLogin = this.onLogin.bind(this);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We are very excited about this feature as it allows along with other drivers like react, angularJs and glimmer for a seamless integration with components written in different technologies and hosted on different domains.&lt;/p&gt;

&lt;p&gt;Feel free to reach out if you have any questions. And please report any issues to &lt;a href="https://github.com/krakenjs/xcomponent/issues"&gt;xcomponent repository on github&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>ui</category>
      <category>xcomponent</category>
      <category>angular</category>
    </item>
    <item>
      <title>Currying in Javascript — arrow function sequence</title>
      <dc:creator>Ahmed Elharouny</dc:creator>
      <pubDate>Thu, 18 May 2017 07:19:37 +0000</pubDate>
      <link>https://dev.to/harouny/currying-in-javascript-arrow-function-sequence-142c</link>
      <guid>https://dev.to/harouny/currying-in-javascript-arrow-function-sequence-142c</guid>
      <description>&lt;h3&gt;
  
  
  Currying
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument.&lt;br&gt;&lt;br&gt;
(source &lt;a href="https://en.wikipedia.org/wiki/Currying" rel="noopener noreferrer"&gt;wikipedia&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Prologue
&lt;/h3&gt;

&lt;p&gt;Next time you read code that contains a sequence of arrow functions as in the following snippet, don’t freak out. You are not alone!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AGmrfdnPto-sfkpUQWL_BRQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2AGmrfdnPto-sfkpUQWL_BRQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yes, having a sequence or a chain of arrow functions is hard to read at first but there is a reason why its becoming more popular (checkout &lt;a href="http://redux.js.org/docs/api/applyMiddleware.html" rel="noopener noreferrer"&gt;Redux applyMiddleware&lt;/a&gt; as an example) as it allows functional style goodness like passing curried functions to map/reduce functions. Let’s see this means and how we can read it.&lt;/p&gt;

&lt;h3&gt;
  
  
  What currying means
&lt;/h3&gt;

&lt;p&gt;Take the following simple &lt;em&gt;sum&lt;/em&gt; function as an example. It takes three numeric arguments and returns the sum.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We can achieve the same result using this syntax (which is not using arrow functions):&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now we have three nested functions each one is taking one argument and returning the following function that takes the next argument, except that last function which is doing the calculation.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to call
&lt;/h4&gt;

&lt;p&gt;Since &lt;em&gt;sum&lt;/em&gt; now only takes one argument: &lt;em&gt;x&lt;/em&gt;, we can call it like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;sum(1);&lt;br&gt;&lt;br&gt;
// [Function]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However we also know that it returns a function that takes &lt;em&gt;y&lt;/em&gt;, so we can call it like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;sum(1)(5);&lt;br&gt;&lt;br&gt;
// [Function]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And we know that that also return a function that takes &lt;em&gt;z&lt;/em&gt;, so we can call it like this to return the sum of the three numbers:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;sum(1)(5)(6);&lt;br&gt;&lt;br&gt;
// 12&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Why to use
&lt;/h4&gt;

&lt;p&gt;We can now pass curried functions to other functions&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[1, 2, 3, 4].map(sum(1)(5));&lt;br&gt;&lt;br&gt;
// [7, 8, 9, 10 ]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Rewrite using arrow functions
&lt;/h4&gt;

&lt;p&gt;Now we can also make use of arrow functions to make our functions more concise, and we can still achieve the same result.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://babeljs.io/repl/#?babili=false&amp;amp;evaluate=true&amp;amp;lineWrap=false&amp;amp;presets=es2015%2Ces2017%2Creact%2Cstage-2&amp;amp;targets=&amp;amp;browsers=&amp;amp;builtIns=false&amp;amp;debug=false&amp;amp;code=const%20sum%20%3D%20x%20%3D%3E%20y%20%3D%3E%20z%20%3D%3E%20x%20%2B%20y%20%2B%20z%3B%0Aconsole.log(sum(1)(5)(6))%3B" rel="noopener noreferrer"&gt;Play with this snippet in babel repl&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to read
&lt;/h3&gt;

&lt;p&gt;We can split the line into three segments:&lt;/p&gt;

&lt;h4&gt;
  
  
  1- sum is a function that takes “x” as an argument….
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F753%2F1%2AO0t8YbipbUWOp5ezPw_VlQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F753%2F1%2AO0t8YbipbUWOp5ezPw_VlQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  2- and return a function that takes “y” as an argument…
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F751%2F1%2AGvlFbD2jiO7_68PXQZd2sQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F751%2F1%2AGvlFbD2jiO7_68PXQZd2sQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  3- and return a function that takes “z” as and argument and returns the sum of “x”, “y” and “z”.
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F746%2F1%2AYr9gTcvgNAExzofP0E-orQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F746%2F1%2AYr9gTcvgNAExzofP0E-orQ.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Alternatives
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://lodash.com" rel="noopener noreferrer"&gt;lodash&lt;/a&gt; has a nice &lt;a href="https://lodash.com/docs/4.17.4#curry" rel="noopener noreferrer"&gt;curry&lt;/a&gt; function that does exactly the same.&lt;/p&gt;

</description>
      <category>functionalprogrammig</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
