<?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: thomasaudo</title>
    <description>The latest articles on DEV Community by thomasaudo (@thomasaudo).</description>
    <link>https://dev.to/thomasaudo</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%2F122793%2F6190ff75-2ac6-46d7-b523-b4e34eaa05b3.jpg</url>
      <title>DEV Community: thomasaudo</title>
      <link>https://dev.to/thomasaudo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thomasaudo"/>
    <language>en</language>
    <item>
      <title>Array manipulation in JavaScript 🧐</title>
      <dc:creator>thomasaudo</dc:creator>
      <pubDate>Tue, 12 Feb 2019 23:59:41 +0000</pubDate>
      <link>https://dev.to/thomasaudo/advanced-array-manipulation-in-javascript--fhi</link>
      <guid>https://dev.to/thomasaudo/advanced-array-manipulation-in-javascript--fhi</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;When we start programming, we tend do create &lt;strong&gt;our own function&lt;/strong&gt; with our own loop to &lt;strong&gt;manipulate arrays&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
In reality, almost all &lt;strong&gt;OOP languages&lt;/strong&gt;, including JavaScript, provide us &lt;strong&gt;methods to achieve it&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;If you begin with JavaScript, this article should be really &lt;strong&gt;useful&lt;/strong&gt;, for others, it will be a &lt;strong&gt;great reminder&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Every
&lt;/h2&gt;

&lt;p&gt;The every method allows us to test if all the elements of an array &lt;strong&gt;verify a condition.&lt;/strong&gt;&lt;br&gt;
This method returns &lt;strong&gt;true&lt;/strong&gt; if all elements verify the test, otherwise, &lt;strong&gt;false&lt;/strong&gt;.&lt;br&gt;
The condition is a function.  &lt;/p&gt;

&lt;p&gt;For example, the following example will test if every 'human' in our array is an adult:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The condition has to return a boolean
function condition (human) {
    return human.age &amp;gt;= 18
}

var arr = [{
    name: 'Thomas',
    age: 19},{
    name: 'Noé',
    age: 17},{
    name: 'Luc',
    age: 20}]

console.log(arr.every(condition))
// Should output false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, the some() method returns &lt;strong&gt;true&lt;/strong&gt; if at least &lt;strong&gt;one element&lt;/strong&gt; passes the test&lt;/p&gt;

&lt;h2&gt;
  
  
  Filter
&lt;/h2&gt;

&lt;p&gt;The filter method creates and returns a &lt;strong&gt;new array&lt;/strong&gt; with all elements that &lt;strong&gt;verify a condition&lt;/strong&gt;.&lt;br&gt;
The condition is a function.&lt;/p&gt;

&lt;p&gt;The following example  returns an array uniquely composed of adult:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function condition (human) {
    return human.age &amp;gt;= 18
}
var arr = [{
    name: 'Thomas',
    age: 19},{
    name: 'Noé',
    age: 17},{
    name: 'Luc',
    age: 20}]

console.log(arr.filter(condition))
// Returns Thomas and Luc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Find
&lt;/h2&gt;

&lt;p&gt;The method find() returns the &lt;strong&gt;first element&lt;/strong&gt; in the array that &lt;strong&gt;verify the condition&lt;/strong&gt;.&lt;br&gt;
Otherwise, if no element verifies it, find() returns &lt;strong&gt;'undefined'&lt;/strong&gt;.&lt;br&gt;
As usual, the condition is a function.&lt;/p&gt;

&lt;p&gt;This example returns the first adult:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function condition (human) {
    return human.age &amp;gt;= 18
}

var arr = [{
    name: 'Thomas',
    age: 19},{
    name: 'Noé',
    age: 17},{
    name: 'Luc',
    age: 20}]

console.log(arr.find(condition))
// Should returns Thomas
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;p&gt;The map method creates a &lt;strong&gt;new array&lt;/strong&gt; with the &lt;strong&gt;return value&lt;/strong&gt; of a function executed for &lt;strong&gt;each element&lt;/strong&gt; of the calling array&lt;/p&gt;

&lt;p&gt;This example increments the age of each human:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [{
    name: 'Thomas',
    age: 19},{
    name: 'Noé',
    age: 17},{
    name: 'Luc',
    age: 20}]

console.log(arr.map(function(element){
    element.age += 1    
    return element
}))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Reduce
&lt;/h2&gt;

&lt;p&gt;Last but not least, the reduce() method is for me the most &lt;strong&gt;tricky one&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The reduce method reduces an array to a &lt;strong&gt;single value&lt;/strong&gt; by executing a provided function for &lt;strong&gt;each value&lt;/strong&gt; of the array.&lt;br&gt;
For each element, the return value is stored in an &lt;strong&gt;'accumulator'&lt;/strong&gt; that can be used in all the &lt;strong&gt;iterations&lt;/strong&gt;.&lt;br&gt;
The &lt;strong&gt;final return&lt;/strong&gt; value of the reduce() method is this &lt;strong&gt;accumulator&lt;/strong&gt;.&lt;br&gt;
The &lt;strong&gt;accumulator&lt;/strong&gt; should be &lt;strong&gt;initialized&lt;/strong&gt; in the method call&lt;/p&gt;

&lt;p&gt;Furthermore, the reducer function can take two other parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The current index &lt;/li&gt;
&lt;li&gt;The source array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This example returns the sum of ages:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reducer (accumulator,element) {
    return accumulator + element.age
}

var arr = [{
    name: 'Thomas',
    age: 19},{
    name: 'Noé',
    age: 17},{
    name: 'Luc',
    age: 20}]

console.log(arr.reduce(reducer,0))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The previous example is very simple, but the reduce() method is really &lt;strong&gt;powerful&lt;/strong&gt;, you can achieve a lot of things with it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Thank's for reading me, if you have any questions, &lt;strong&gt;ask them&lt;/strong&gt;! 🧐&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Better if statement w/ ternary operator ? : 💻</title>
      <dc:creator>thomasaudo</dc:creator>
      <pubDate>Tue, 05 Feb 2019 20:45:48 +0000</pubDate>
      <link>https://dev.to/thomasaudo/better-if-statement-w-ternary-operator----24pc</link>
      <guid>https://dev.to/thomasaudo/better-if-statement-w-ternary-operator----24pc</guid>
      <description>&lt;p&gt;The &lt;strong&gt;if operator&lt;/strong&gt; is a mainstay of computer programming, we use it everywhere.&lt;/p&gt;

&lt;p&gt;Sometimes we need to test a value to then, execute some simple code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
if (a === b) {
    console.log('true');
} else {
    console.log('false');
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This syntax is really &lt;strong&gt;clear&lt;/strong&gt; and &lt;strong&gt;simple&lt;/strong&gt; to use, but it takes a lot of place.&lt;/p&gt;

&lt;p&gt;In a large-scale program, it can reduce the &lt;strong&gt;visibility&lt;/strong&gt; and &lt;strong&gt;comprehension&lt;/strong&gt; of the whole.&lt;/p&gt;

&lt;p&gt;For a simple case like this, it's often more simple to use the &lt;strong&gt;ternary operator&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(condition) ? (operation A) : (operation B)

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This operator is very useful but often &lt;strong&gt;unknown&lt;/strong&gt; or &lt;strong&gt;misunderstood&lt;/strong&gt; by programmers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Details&lt;/strong&gt;:&lt;br&gt;
    - condition, the condition&lt;br&gt;
    - operation A, the action to execute if the condition returns true&lt;br&gt;
    - operation B, the action to execute if the condition is false, concretely an else&lt;/p&gt;
&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Here is a simple example in javascript :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// If the age &amp;gt;= 18, then the adult variable takes the value true, otherwise, it's taking the value 
//false

age &amp;gt;= 18 ? adult = true : adult = false

// With a basic if statement

if (age &amp;gt;= 18 ) {
    adult = true
} else {
    adult = false
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Way simpler no?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Another example: a function which returns the &lt;strong&gt;GCD&lt;/strong&gt; of a and b.&lt;br&gt;
However it's not the best example at all. &lt;/p&gt;

&lt;p&gt;Indeed, it's not easy to read, a &lt;strong&gt;normal if statement&lt;/strong&gt; will be &lt;strong&gt;better&lt;/strong&gt; in this case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function gcd(a, b) {

    return ( (a%b) === 0 ?  b : gcd(b, a%b) )

}


&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To conclude, this notation is really &lt;strong&gt;helpful&lt;/strong&gt; and help us &lt;strong&gt;increase&lt;/strong&gt; our &lt;strong&gt;productivity&lt;/strong&gt;.&lt;br&gt;
However, it has to be used in really &lt;strong&gt;simple cases&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For complex statements or operations, the if / else notation is still the &lt;strong&gt;best&lt;/strong&gt; way!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading me 😇&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Get started with GitHub GraphQL API 👨‍🔬</title>
      <dc:creator>thomasaudo</dc:creator>
      <pubDate>Wed, 23 Jan 2019 18:04:10 +0000</pubDate>
      <link>https://dev.to/thomasaudo/get-started-with-github-grapql-api--1g8b</link>
      <guid>https://dev.to/thomasaudo/get-started-with-github-grapql-api--1g8b</guid>
      <description>&lt;p&gt;&lt;a href="https://fr.wikipedia.org/wiki/GraphQL"&gt;GraphQL&lt;/a&gt; APIs are since a few years more and more popular. The purpose of this tutorial is not to teach you the GraphQL concepts and techniques but to teach you how to query the GitHub GraphQL API.&lt;br&gt;
I will use &lt;a href="https://fr.wikipedia.org/wiki/Node.js"&gt;Node.js&lt;/a&gt; for the example, but you can achieve it with any programming language which allows you to perform &lt;a href="https://fr.wikipedia.org/wiki/Hypertext_Transfer_Protocol"&gt;HTTP&lt;/a&gt; request.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up
&lt;/h2&gt;

&lt;p&gt;First of all, you need to create a &lt;a href="https://github.com/settings/tokens/new"&gt;personal access token&lt;/a&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;github &amp;gt; settings &amp;gt; developper settings &amp;gt; personnal access token &amp;gt; generate new token
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Github will ask you a token description and the scope for your application, select what you need.&lt;/p&gt;

&lt;p&gt;Now that we have our token, it's query time 🎯&lt;/p&gt;
&lt;h2&gt;
  
  
  Create Query
&lt;/h2&gt;

&lt;p&gt;To test and create your queries, GitHub proposes a very powerful tool : &lt;a href="https://developer.github.com/v4/explorer/"&gt;GraphQL API Explorer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For this example, I've created a very simple query to get my GitHub pinned repositories, which is not possible with the GitHub REST API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query{
  repositoryOwner(login: "thomasaudo") {
    ... on User {
      pinnedRepositories(first: 6) {
        edges {
          node {
            name,
            description,
            url
          }
        }
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://developer.github.com/v4/"&gt;documentation&lt;/a&gt; is really complete 📒&lt;/p&gt;

&lt;h2&gt;
  
  
  Query
&lt;/h2&gt;

&lt;p&gt;To query the GitHub server, we will need to send a post request to the &lt;a href="https://api.github.com/graphql"&gt;unique endpoint&lt;/a&gt;&lt;br&gt;
Of course, we have to custom a little bit our request :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A parameter: the query&lt;/li&gt;
&lt;li&gt;Bearer Authentication also known as token authentication: your personal access token ( in the header )&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A CURL example from the GitHub official documentation :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -H "Authorization: bearer token" -X POST -d " \
 { \
   \"query\": \"query { viewer { login }}\" \
 } \
" https://api.github.com/graphql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I have created a really simple nodeJS example. To perform the query, I use the package 'axios'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Require axios to perform easy promise-based POST request
const axios = require('axios');
// Define constant
// Endpoint URL
const githubUrl = 'https://api.github.com/graphql'
// Your personal access token
const token = '7bacddc5d40dabfe6edca28b986a0247cfe3803b'
// The Authorization in the header of the request
const oauth = {Authorization: 'bearer ' + token}
// The GraphQL query, a string
const query = '{' +
                'repositoryOwner(login: "thomasaudo") { ' +
                  '... on User {' +
                    'pinnedRepositories(first: 6) {' +
                      'edges {' +
                        'node {' +
                          'name,' +
                          'description,' +
                          'url' +
                          '}' +
                        '}' +
                      '}' +
                    '}' +
                  '}' +
                '}'

// Post request, axios.post() return a Promise
axios.post(githubUrl, {query: query}, {headers: oauth})
  .then(function (response) {
    // On success, print the response
    console.log(response.data);
  })
  .catch(function (error) {
    On error, print the error
    console.log(error);
  });

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Here we are!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope you have liked this post, my first one 🚀&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>node</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
