Airbnb style guide for Naming

Extension naming .jsx is preferred over .js for file name

index.jsx

PascalCase for naming file and Reference naming

import DashboardHeader from '/DashboardHeader';

Camel case naming for Component instance
Directory Name should be component name and file name should be lowercase as index.jsx

DashboardHeader/index.js

 

  const dashboardHeader = </DashboardHeader> ;
Advertisements

Async/Await in jS

async/await is built on top of the promise and supports all promise based apis.

async keyword in function

  1. It declares an asynchronous function eg:  async getUsers() {…}
  2. It enables the use of await.
  3. It returns promise. Promise is created when starting the execution of function.

await in async function

  1. await waits for promise to be settled. if promise is fulfilled the result of await is the promise resolved value. if the promise is rejected, await throws the rejection value.
  2. await used in front of promise call, await forces the rest of the code to wait until the promise finished the return result.

 

For reading more:

https://tutorialzine.com/2017/07/javascript-async-await-explained

http://exploringjs.com/es2016-es2017/ch_async-functions.html

Hash#update

Hash#update
Its an alias for !merge which updates the given hash object and returns the updated hash. Basically used to update the value of hash with same key or !merge.

NOTE:  update routine overrides the value of the caller object which keys matches with the parameter pass hash in the update routine.
Usage 1:

  hash1 = { a: 'I am from hash 1'}
  hash2 = { a: 'I am from hash 2'}
  hash1.update(hash2)
   => {:a=>"I am from hash 2"}
  hash1
   => {:a=>"I am from hash 2"}

usage 2:

  hash1 = { a: 'I am from hash 1'}
  hash2 = { b: 'I am from hash 1'}

  hash1.update(hash2)
  hash1
    => {:a=>"I am from hash 1", :b=>"I am from hash 1"}

for more details:
http://ruby-doc.org/core-1.9.3/Hash.html#method-i-update

Selection sort in ruby

a = [9, 8, 6, 5, 4, 3, 2, 1]

// sorting in ascending order

total_index = a.length - 1
(total_index).times do |i|
  current_top_index = i
  (i + 1).upto(total_index) do |j|
    if a[j] < a[current_top_index]
      temp = a[j]
      a[j] = a[current_top_index]
      a[current_top_index] = temp
    end
  end
end
a = [1, 2, 3, 4, 5, 6, 8, 9]

Time complexity
O(n*n)

Space complexity
O(1)