5 JavaScript Object Destructuring Tricks


This post is by Josh Branchaud from Hashrocket Articles


Click here to view on the original site: Original Post




The ES6 destructuring syntax is quite flexible. It allows us to do a lot of
nice things with objects and arrays that in the past would have taken many
distracting lines of code.

Let's look at five handy destructuring tricks for objects.

Trick #1

Remove an item from an object

// const data = { a: 1, b: 2, thingToRemove: "bye" };

const { thingToRemove, ...rest } = data;

console.log(rest); // { a: 1, b: 2 }

This is a concise way of getting a new copy of an object with a specific
key/value pair removed. Note that it is immutable and you are free to
specify as many keys as you'd like separated from the ...rest object.

Trick #2

Rename things in a destructuring

// const data = { coordinates: { x: 5, y: 6 }};

const { x: xCoord, y
Continue reading "5 JavaScript Object Destructuring Tricks"

Managing React Router Pathnames


This post is by Josh Branchaud from Hashrocket Articles


Click here to view on the original site: Original Post




So, you've introduced
react-router into your
single page React app as a way of managing routing. The app has started to
grow. There are many top-level routes for rendering different "pages". Some
of those "pages" now have sub-routing within them for managing wizard
workflows and sections with multiple "sub-pages". This is great. The dream
of routing within your React SPA has been realized.

Fast-forward a few months, and your elegant routing solution has become a brittle, error-prone maintenance nightmare. What happened?

A Simplified Example

Early into the development of a large React SPA, I noticed the above
scenario beginning to emerge. Some of what I noticed can be summarized in
the following series of code snippets. Note: these code snippets are not
full, working examples. Only the instructive parts have been included.

/* App.js */
return (
  <Switch>
    <Route path="/" component={Home
Continue reading "Managing React Router Pathnames"

Where Am I: URL Assertions with Cypress


This post is by Josh Branchaud from Hashrocket Articles


Click here to view on the original site: Original Post




The URL that appears in the browser's URL bar is one of the first and
primary ways that users interact with our web apps. The URL tells the app
and the user of their location within the route hierarchy of the app. It
sometimes even contains state. The URL consequently serves as an excellent
first assertion when writing integration tests.

When writing Cypress integration tests, there are
several ways for us to assert about the current URL.

The cy.url() and
cy.location()
functions are the primary ways for us to gain access to information about
the current state of the URL. From there, we can perform a variety of
assertions.

We can assert about the entire URL:

// URL: http://localhost:8000/pokemon
cy.url().should('eq', 'http://localhost:8000/pokemon');
// ✅ passes

This is certain the most precise assertion. The tradeoff is that it requires
us to know something

Continue reading “Where Am I: URL Assertions with Cypress”

When To Use Redux


This post is by Josh Branchaud from Hashrocket Articles


Click here to view on the original site: Original Post




Have you had a conversation about Redux recently that has left you wondering — despite all of its praise and the assured recommendations — if you made a big mistake when you typed the words, yarn add redux?

Jani is being a bit facetious here. But if you’ve been following along on twitter, there has been a lot of discussion about redux. The crux of the conversation is aimed at whether most apps even call for the use of redux and consequently if we, as developers, are using it when we don’t need it.

Generate Dates in PostgreSQL


This post is by Josh Branchaud from Hashrocket Articles


Click here to view on the original site: Original Post




PostgreSQL has shipped with the generate_series() function for quite some
time. This function is often used to generate series of numeric data. For
instance, in my post, Understanding Common Table Expressions with
FizzBuzz
,
I used it to generate a series of integers from 1 to 100.

As of PostgreSQL 8.4, support for generating series of timestamp data was
added. I don't see this used often, so let's take a look at it.

We could take a peak at the online documentation to see the function
signature, or we could pull it up in psql using \df.

> \df generate_series()
                                                               List of functions
   Schema   |      Name       |         Result data type          |                        Argument data types                         |  Type
------------+-----------------+-----------------------------------+--------------------------------------------------------------------+--------
 pg_catalog | generate_series | SETOF bigint                      | bigint, bigint                                                     | normal
 pg_catalog | generate_series | SETOF bigint                      | bigint, bigint, bigint                                             | normal
 pg_catalog | generate_series | 
Continue reading "Generate Dates in PostgreSQL"

Swap Two Column Values in SQL: Part 2


This post is by Josh Branchaud from Hashrocket Articles


Click here to view on the original site: Original Post




In the first part of this post we looked at a technique for swapping two
column values for existing records in a single statement. It is a relatively
sound solution that takes advantage of subqueries and a case statement.
Unfortunately, it has a couple drawbacks which we will address in this post.

This post builds off of Part 1. If you haven't already, give it a
read.

The first drawback can be demonstrated by building on our existing example,
the pages table which has the following data.

select * from pages order by ordering;
 id |  name  | ordering
----+--------+----------
  3 | Green  |        1
  1 | Red    |        2
  4 | Yellow |        3
  2 | Blue   |        4

In the interest of maintaining data integrity, it is reasonable to expect
that a table like this would have a uniqueness constraint on ordering.
Let's add that to ensure

Continue reading “Swap Two Column Values in SQL: Part 2”

Swap Two Column Values in SQL: Part 1


This post is by Josh Branchaud from Hashrocket Articles


Click here to view on the original site: Original Post




I recently needed to write a PostgreSQL snippet that would massage some
data. More specifically, I needed to swap the ordering of two different
records in the pages table. These records are made up of an id, a
name, and an ordering.

Here is a snapshot of the data in pages.

select * from pages order by ordering;
 id |  name  | ordering
----+--------+----------
  3 | Green  |        1
  1 | Red    |        2
  4 | Yellow |        3
  2 | Blue   |        4

We've been informed that we need to swap the ordering of Red and Blue.
We can see here in this snapshot of the data that Red has an ordering of
2 and Blue has an ordering of 4.

Doing a swap based on the data that we have locally would be most easily
accomplished with a couple update statements.

update pages set ordering 
Continue reading "Swap Two Column Values in SQL: Part 1"

Advanced Queries with ActiveRecord’s From Method


This post is by Josh Branchaud from Hashrocket Articles


Click here to view on the original site: Original Post




For those of us that write database-backed Rails apps,
ActiveRecord is a
familiar beast. We know its various query building methods well: where,
order, limit, and even group. The most important SQL clause is
conspicuously missing from our repertoire: the from clause.

Chances are you can't think of many, if any, times that you've used
ActiveRecord's from method. This is to be expected. The from clause in
SQL specifies the target table of a query. In the context of ActiveRecord, the
target table is explicitly expressed by the model from which you query.

Consider the times you use one-liners such as Developer.first,
Developer.all, and Developer.count. With each of these, the developers
table as the target of the query is expressed by the use of the Developer
model.

So, where does the from method come into the picture?

The
docs
describe the

Continue reading "Advanced Queries with ActiveRecord’s From Method"