Austin Story

Ruby, Rails and Javascript Blog

Powered by Genesis

ES6 Desctructuring with Spread Operator

June 4, 2016 By Austin Story Leave a Comment

Destructuring in ES6 deals with take either an Array or an Object, and get or set variables based on the elements of that Array or Object.

First lets talk about the syntax. To destructure something in ES6 you put … in front of the Array or Object you are wanting to pull apart.
[javascript]
//Array example
var numbers = [1,2,3];

console.log(numbers) //logs [1,2,3]
console.log(…numbers) //logs 1, 2, 3
[/javascript]

In the second example, … has precedence so the [1,2,3] is first split into 3 separate values, then console.log(…numbers) becomes console.log(1,2,3).

That is how we easily get at them, here is an example of the setting pattern.
[javascript]
function getAddress() {
return {city: ‘St. Louis’, state: ‘Missouri’, country: ‘USA’}
}

let {city, state, country} = getAddress();

console.log(city, state, country) //logs ‘St. Louis’, ‘Missouri’, ‘USA’
[/javascript]

This works because we know that getAddress will return a value that is matching what we have in curly braces on the left. So we know getAddress will return a city, so we call it city in the let.

We can also alias variables in our let statement, in the example below, we tell javascript to map the value of getAddress().city to cityAlias locally.
[javascript]
let {city: cityAlias, state, country} = getAddress();

console.log(cityAlias, state, country) //logs ‘St. Louis’, ‘Missouri’, ‘USA’
console.log(city) //Reference error because city is not defined
[/javascript]

You can use this pattern anywhere and it may be more readable. Here is an example in a function call.
[javascript]
function printUserAddress({city, state, country}) {
console.log(city, state, country);
}

printUserAddress(getAddres());
////logs ‘St. Louis’, ‘Missouri’, ‘USA’
[/javascript]

You can also set default values to deal with cases where the object passed in does not have the value you are trying to get.
[javascript]
function printUserAddress({city, state, country, zip=’N/A’}) {
console.log(city, state, country, zip);
}

printUserAddress(getAddres());
//logs ‘St. Louis’, ‘Missouri’, ‘USA’, ‘N/A’
[/javascript]

Under the hood here are the rules.

{} on the left means that the object on the right MUST have the properties, or it will be an error
[javascript]
function printUserAddress({city, state, country, zip}) {
console.log(city, state, country, zip);
}

printUserAddress(getAddres());
//throws b/c zip is not in the object
[/javascript]

You can bypass the throw by adding a ? in front of the attribute which may or may not be defined
[javascript]
//zip is optional
function printUserAddress({city, state, country, ?zip}) {
console.log(city, state, country, zip);
}

printUserAddress(getAddres());
//logs ‘St. Louis’, ‘Missouri’, ‘USA’, ‘undefined’

//entire object is optional
function printUserAddress(?{city, state, country, zip}) {
console.log(city, state, country, zip);
}

printUserAddress(getAddres());
//logs ‘St. Louis’, ‘Missouri’, ‘USA’, ‘undefined’
[/javascript]

Filed Under: ES6, Javascript Tagged With: ES6, Javascript

Run Node Packages Directly

May 31, 2016 By Austin Story Leave a Comment

Did you know that you can call your binaries directly in node_modules? I didn’t!

This will tell jest to watch any file changes in the Fetch directory and then test them.

[ruby]
./node_modules/.bin/jest –watch Fetch
[/ruby]

Filed Under: Javascript, React, Redux

Ubuntu 16.04 setup for Ruby on Rails, Postgres, NodeJs, ElasticSearch and Neo4j

May 14, 2016 By Austin Story Leave a Comment

POSTGRES

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

sudo -u postgres createuser –interactive
#name of role = yourLoggedInUserName
#super uesr? yes

Set permissions for local postgres access in pg_hba.conf
# will probalby be in /etc/postgresql/VERSIONNUMBER/main/pg_hba.conf

set all database administrative in peer column to ‘trust’ this allows you to login locally without security (i.e. don’t do this for production)

with the user you are logged in as, run these commands, for instance if you login as my_user_name
sudo su postgresql
psql
CREATE ROLE my_user_name WITH SUPERUSER;
ALTER ROLE my_user_name WITH CREATEDB;
ALTER ROLE my_user_name WITH LOGIN;

Add git SSH access
mkdir ~/.ssh
chmod 700 ~/.ssh
cd ~/.ssh
ssh-keygen -t rsa -C YOURNAME
cat id_rsa.pub

Copy what is displayed into your github/bitbucket to add new keys

install git
sudo apt-get install git

Install RVM
https://rvm.io/

Set RVM as function
copy this
[[ -s “$HOME/.rvm/scripts/rvm” ]] && source “$HOME/.rvm/scripts/rvm”
From: ~/.bash_profile file
To: ~/.bashrc file

Install Ruby
close and open a new terminal
rvm install 2.3.1

Set default ruby
rvm –default use 2.3.1

Install Java
sudo apt-get update
sudo apt-get install default-jdk

Install Elasticsearch
wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-2.3.2.deb
sudo dpkg -i elasticsearch-2.3.2.deb

Install Neo4j
wget -O – https://debian.neo4j.org/neotechnology.gpg.key | sudo apt-key add –
echo ‘deb http://debian.neo4j.org/repo stable/’ >/tmp/neo4j.list
sudo mv /tmp/neo4j.list /etc/apt/sources.list.d
sudo apt-get update
sudo apt-get install neo4j

Install Node version manager (NVM)
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
nvm install 6.1.0

LINK Rubymine properly
https://makandracards.com/makandra/879-install-rubymine-under-ubuntu

tar -xzf RubyMine-X.Y.Z.tar.gz
Make a directory for RubyMines:

mkdir -p ~/bin/rubymines/
mkdir -p ~/bin/rubymine/
Move the unpacked RubyMine folder to the desired destination:

mv ~/Downloads/RubyMine-X.Y.Z ~/bin/rubymines
Make a symlink:

ln -nfs ~/bin/rubymines/RubyMine-X.Y.Z ~/bin/rubymine

Postgress
sudo apt-get install lib-pqdev

image magick – rmagic
sudo apt-get install libmagickwand-dev

Filed Under: Integrations, Javascript, Ruby

  • « Previous Page
  • 1
  • …
  • 3
  • 4
  • 5
  • 6
  • 7
  • …
  • 15
  • Next Page »

Categories

  • AngularJS
  • Books
  • Devise
  • Elasticsearch
  • ES6
  • Information Security
  • Integrations
  • Javascript
  • Linux
  • Minitest
  • PhoneGap
  • Programming
  • React
  • Redux
  • Ruby
  • Ruby on Rails
  • Stripe
  • Testing
  • Theory
  • TypeScript
  • Uncategorized
  • Vue
  • Webpack