<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Florence Liang on Medium]]></title>
        <description><![CDATA[Stories by Florence Liang on Medium]]></description>
        <link>https://medium.com/@florenceliang?source=rss-79978d99f77a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*pkTPheJy-vKzZT-KiLAw1w.jpeg</url>
            <title>Stories by Florence Liang on Medium</title>
            <link>https://medium.com/@florenceliang?source=rss-79978d99f77a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 23:48:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@florenceliang/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Working with Restful API]]></title>
            <link>https://medium.com/@florenceliang/working-with-restful-api-25cfd2de662b?source=rss-79978d99f77a------2</link>
            <guid isPermaLink="false">https://medium.com/p/25cfd2de662b</guid>
            <category><![CDATA[rpc]]></category>
            <category><![CDATA[restful-api]]></category>
            <dc:creator><![CDATA[Florence Liang]]></dc:creator>
            <pubDate>Sun, 07 Apr 2024 17:28:18 GMT</pubDate>
            <atom:updated>2024-04-07T17:28:18.895Z</atom:updated>
            <content:encoded><![CDATA[<p>If you have been working with API design, you might be familiar with the term “RESTful”. If you are a user, RESTful API is useful as it simplifies the semantics of URLs and allows you to interact with the website more easily. If you are the developer, you may find using Restful API is easier to build a large, scalable web app.</p><h3>1. What is REST (vs RPC)</h3><p>REST stands for Representational State Transfer. REST is an <strong><em>architectural style</em></strong> of developing web services that <strong>leverage HTTP method to define actions</strong>. Since <strong>every component is a resource, i</strong>t <strong>revolves around resources. Resource </strong>can be accessed by a common interface using HTTP standard methods.</p><p>In REST architecture, a REST Server provides access to resources and REST client accesses and presents those resources. Here each resource is identified by URIs or global IDs. REST uses different ways to represent a resource like text, JSON, and XML. XML and JSON are the most popular representations of resources these days.</p><p>RPC (Remote Procedure Call) is a protocol that allows a program to execute a procedure (or a method) on a remote server. It is focused on actions and tends to be more about calling specific procedures or commands with parameters, expecting a response. On top of JSON and XML, RPC might use binary formats like Protocol Buffers, depending on the specific protocol.</p><h3>2. REST Resouce</h3><p>RESTful services are centered around resources. Essentially, any piece of information(e.g., objects, data) that can be named and represented, which can include documents, images, services, collections of other resources, non-virtual objects like people, and so on. Each resource is identified by a unique identifier (URI), enhancing the web’s ability to find and manipulate resources.</p><p>Web services allow machines to interact over the internet using standardized protocols. It enables computers to perform operations and exchange messages using the same web protocols humans use, thus making the web a platform for machine-to-machine communication as well.</p><p>RPC frameworks (like JSON-RPC, gRPC) abstract the details of the network, allowing developers to call services as if they were local procedures. With RPC, the emphasis is on the specific actions to be performed rather than on the data or resource being manipulated.</p><h3>3. RESTful uses HTTP</h3><p><strong>HTTP Protocol Usage</strong>: RESTful web services use HTTP as the foundation for their communication. This is because HTTP is universally supported and provides a rich set of methods (GET, POST, PUT, DELETE) for interacting with resources. Each method specifies the type of operation you’re trying to perform (retrieve, submit, update, delete) and adheres to the principles of REST.</p><p><strong>Messaging in RESTful Web Services</strong> involves the exchange of HTTP requests and responses. This includes not just the data or “message body”, but also metadata about the message, such as HTTP headers that can describe the content type, authentication, caching policies, etc.</p><p>In my recent project, my team needs to make the <strong>API publicly accessible</strong> by removing the proxy layer between front-end and back-end services. <strong>authentication</strong> becomes even more crucial. This is because direct exposure increases the vulnerability to unauthorized access and potential security threats. We paid extra attention to secure authentication mechanisms, ensuring that only authorized users and applications can access the API, and protecting sensitive data and functionality after the proxy layer is removed.</p><p><strong>HTTP Methods</strong>:</p><ul><li>GET: retrieve resources.</li><li>POST: create or update resources with the expectation that the server will handle the resource ID creation.</li><li>PUT: create or replace resources at a known URL.</li><li>DELETE: remove resources.</li><li>OPTIONS: provide information about the communication options for the resource or server.</li><li>HEAD: retrieve metadata about the resource, similar to GET but without the body.</li></ul><p><strong>GET vs. POST/PUT:</strong> GET should not be used to create resources, it is designed to retrieve information without affecting the resource’s state.</p><p><strong>PUT vs. POST</strong>: PUT and POST are intended for creation and updates, with PUT being idempotent — meaning multiple identical requests should have the same effect as a single one, hence it replaces a resource at a specific URI if it exists or creates it if it doesn’t. In contracts, POST not being idempotent, potentially creates multiple resources if invoked multiple times. POST is used to submit data to be processed to a specified resource, often resulting in the creation of a new resource or the update of an existing one without the client specifying the resource’s URI.</p><p>Source: <a href="https://stackoverflow.com/questions/671118/what-exactly-is-restful-programming">https://stackoverflow.com/questions/671118/what-exactly-is-restful-programming</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25cfd2de662b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing a Good Habit on Making the Original Object Immutable]]></title>
            <link>https://medium.com/launch-school/developing-a-good-habit-on-making-the-original-object-immutable-c1e862db8bbb?source=rss-79978d99f77a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1e862db8bbb</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[wwcode]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Florence Liang]]></dc:creator>
            <pubDate>Fri, 17 Nov 2017 22:15:30 GMT</pubDate>
            <atom:updated>2017-11-20T04:50:43.774Z</atom:updated>
            <content:encoded><![CDATA[<p>When I first learned how to code, I was confused about the concept <em>“Variables as pointers”</em>. Oftentimes, I inadvertently changed the original variable after reassigning another different variable, and that’s when the mistakes happened by creating the “side effect”. To avoid such mistakes, 1) it is important to understand the concepts about mutable/immutable objects, 2) we need to develop a good coding habit if we don’t want to change the original object.</p><ol><li>Mutable vs. Immutable</li></ol><p>When we talk about <strong>mutable</strong> object, it means that we <strong>can change </strong>the object’s values after we create it. An <strong>immutable</strong> object is the opposite: We <strong>can not change the values </strong>after initiating it. In JavaScript, two major data types that are immutable are numbers and strings.</p><p>Let’s consider an example for immutable value:</p><pre><strong>&gt; var a = 1;<br></strong>=&gt; undefined<strong>   <br>&gt; var b = a;       </strong>// assigns b to a<strong><br></strong>=&gt; undefined<strong>   <br>&gt; a += 1;          </strong>// reassigns a&#39;s value<strong><br></strong>=&gt; 2<strong>   <br>a<br></strong>=&gt; 2<strong>   <br>b<br></strong>=&gt; 1</pre><p><strong>Here, the values of numbers </strong>are actually not changed(immutable), that is why b’s value is still “1”. V<strong>ariables</strong> hold a reference to the values. What we are changing is pointing the variable to a different value with the `=` operator.</p><p>Next, let’s look at another example for mutable value with object:</p><pre><strong>&gt; var a = 1;<br></strong>=&gt; undefined<strong>   <br>&gt; var obj = { a: a }<br></strong>=&gt; undefined<strong>   <br>&gt; var newObj = obj;   // assigns newObj to obj<br></strong>=&gt; undefined<strong>   <br>&gt; obj.a += 2;         // updates obj&#39;s property &quot;a&quot;<br></strong>=&gt; 3<strong>   <br>&gt; obj<br></strong>=&gt; { a: 3 }<strong>   <br>&gt; newObj<br></strong>=&gt; { a: 3 }<strong>   <br>&gt; a                  // value of var a will not change<br></strong>=&gt; 1</pre><p>From the above example, we can see that <strong>objects are mutable</strong>: changing a variable’s state also modifies all references to that variable. When we update obj, value of newObj also changes. Both obj and newObj point to the same address in memory.</p><p>In contrast, for the immutable data type number, we can not change the internal state of the data, so the value of var a remains unchanged.</p><p>2. “Copy” the variable, instead of reassigning</p><p>Now that we know simply reassigning an object’s property will change the original object , it is important to have a good discipline. For example, if you want to show changes of an item and want to display both “before” and “after” states to your user, you will still need to keep the original object in order to implement this feature.</p><p>In this case, we can use Object.assign in JavaScript to copy the original object, to avoid the changes reflect on it. The other way to make the object immutable is through Object.freeze. The reason I do not recommend to use Object.freeze function is that, it may prevent us from any changes, and it doesn’t have much flexibility.</p><p>Below is the example on Object.assign:</p><pre><strong>&gt; var obj = { a: &quot;foo&quot;, b: &quot;bar&quot; }; <br></strong>=&gt; undefined<strong>   <br>&gt; var copy = Object.assign({}, obj);<br></strong>=&gt; undefined<strong>   <br>&gt; copy<br></strong>=&gt; { a: &#39;foo&#39;, b: &#39;bar&#39; }<strong>   <br>&gt; copy.a = &quot;hello&quot;;<br></strong>=&gt; &#39;hello&#39;<strong>   <br>&gt; copy;<br></strong>=&gt; { a: &#39;hello&#39;, b: &#39;bar&#39; }<strong>   <br>&gt; obj;<br></strong>=&gt; { a: &#39;foo&#39;, b: &#39;bar&#39; }</pre><p>In the example above, we first use Object.assign({}, obj) to make a copy and assign it to a variable copy. Next, we make the changes on a property in the copy variable. After changing, we successfully changed the copy object without modifying the original obj object.</p><p>If you are dealing with array, an easier function to copy the array is using Array.prototype.slice() .</p><p>Sometimes making mistakes is inevitable, but if you keep practicing and exploring the reasons behind the mistakes, they will help you to be a better developer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1e862db8bbb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/launch-school/developing-a-good-habit-on-making-the-original-object-immutable-c1e862db8bbb">Developing a Good Habit on Making the Original Object Immutable</a> was originally published in <a href="https://medium.com/launch-school">Launch School</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Event Delegation, and event.target vs. event.currentTarget]]></title>
            <link>https://medium.com/@florenceliang/javascript-event-delegation-and-event-target-vs-event-currenttarget-c9680c3a46d1?source=rss-79978d99f77a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9680c3a46d1</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Florence Liang]]></dc:creator>
            <pubDate>Fri, 20 Jan 2017 01:14:31 GMT</pubDate>
            <atom:updated>2017-01-20T01:33:56.733Z</atom:updated>
            <content:encoded><![CDATA[<p>Event delegation is a popular methodology in JavaScript. It allows us to add an event listener to one parent, and avoid to add many event listeners to specific nodes. We can demonstrate this technique with simple example.</p><p>Let’s say we have a list with thousands of items:</p><pre>&lt;body&gt;<br>  &lt;div id=&quot;container&quot;&gt;<br>   &lt;ul id=&quot;list&quot;&gt;<br>     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 1&lt;/a&gt;&lt;/li&gt;<br>     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 2&lt;/a&gt;&lt;/li&gt;<br>     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 3&lt;/a&gt;&lt;/li&gt; <br>     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 4&lt;/a&gt;&lt;/li&gt;<br>     ..................................<br>     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 1000&lt;/a&gt;&lt;/li&gt;<br>   &lt;/ul&gt;<br>  &lt;/div&gt;<br>&lt;/body&gt;</pre><p>With such number of items, it would be a nightmare to loop through every &lt;a&gt; element on the page, adding an event listener one after one. Moreover, it may “freeze” the page when JavaScript is trying to create them all.</p><p>So here comes the event delegation: When the event bubbles up to the body element, we can check the element that triggered the event, using the event object’s target property.</p><pre>document.addEventListener(&quot;click&quot;, function(e) {<br>  if(e.target &amp;&amp; e.target.nodeName == &quot;A&quot;) {<br>  console.log(&quot;List item &quot;, e.target.textContent, &quot; was clicked!&quot;);<br>  }<br>});</pre><pre>// When we click the 2nd item, the page prints out:</pre><pre>&quot;List item Item 2 was clicked!&quot;</pre><h3>target vs. currentTarget</h3><p>Since we already talked about the event.target property, there is another property called event.currentTarget in JavaScript event. It can be very confused by just reading about them on JavaScript documentation.</p><p>As we’ve seen from the last example, when we clicked the a element, click event bubbles up to &lt;body&gt; node of the document like below:</p><pre>&lt;a&gt; → &lt;li&gt; → &lt;ul&gt; → &lt;div&gt; → &lt;body&gt;</pre><p>Let’s add one more line of code and prints out what the e.currentTarget is from the example we used above:</p><pre>document.addEventListener(“click”, function(e) {<br>  if(e.target &amp;&amp; e.target.nodeName == “A”) {<br>    console.log(“List item “, e.target.textContent, “ was clicked!”);  // &quot;List item Item 2 was clicked!&quot;<br>  }<br>  console.log(e.currentTarget);  // #document<br>});</pre><p>It prints out “document” since we attached current event listener to the document while e.target refers to &lt;a&gt; which we clicked.</p><p>We can also look at one more example to see the differences between <em>target </em>and <em>currentTarget</em>. This time, we add the event listener to the &lt;ul&gt;:</p><pre>document.getElementById(“list”).addEventListener(“click”, function(e) {<br>  console.log(e.currentTarget);  //&lt;ul&gt;&lt;li&gt;...&lt;/li&gt;&lt;ul&gt;<br>  console.log(e.target);         //&lt;a href=&quot;#&quot;&gt;Item 2&lt;/a&gt;<br>);</pre><p>Again, the currentTarget refers to the element that the event listener <strong>directly attached to</strong> while the target still refers to the specific &lt;a&gt; we clicked.</p><p>With these two properties target and currentTarget, we can easily manipulate the node when the event gets triggered, as well as the node the event is attached to.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9680c3a46d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Differences Between Call/Apply and Bind (When we should use them?)]]></title>
            <link>https://medium.com/@florenceliang/differences-between-call-apply-and-bind-when-we-should-use-them-baccb2c7c46e?source=rss-79978d99f77a------2</link>
            <guid isPermaLink="false">https://medium.com/p/baccb2c7c46e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[jquery]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Florence Liang]]></dc:creator>
            <pubDate>Fri, 20 Jan 2017 01:14:17 GMT</pubDate>
            <atom:updated>2017-01-20T03:00:39.219Z</atom:updated>
            <content:encoded><![CDATA[<p>Call/Apply and Bind functions are very important in JavaScript. There are a lot of times we will encounter them again and again. When I am going through Launch School Front End Development path, I would like to share my thoughts on what these functions do, and when we need to use them.</p><p>Before jumping into their differences, let’s talk about their<strong> similarities</strong> first.</p><p>If we have below simple code:</p><pre>var a = &quot;foo&quot;;</pre><pre>var o = {<br>  a: &quot;dog&quot;,<br>  b: &quot;cat&quot;,<br>  c: function() {<br>    console.log(this.a + &#39; and &#39; + this.b);<br>  },<br>};</pre><pre>o.c.call(o)   =&gt; &quot;dog and cat&quot;</pre><pre>var foo= o.c.bind(o);<br>foo();          =&gt; &quot;dog and cat&quot;</pre><p>From above, both call and bind allow us to <em>explicitly set</em> the this value when we invoke the method, and print out the desired result with the context in the o object.</p><p>But if we assign a variable to the ‘call’ function:</p><pre>var bar = o.c.call(o);</pre><pre>bar();    =&gt; Uncaught TypeError: bar is <strong>not a function</strong></pre><pre>bar;      =&gt; &quot;dog and cat&quot;</pre><p>Since<strong> </strong><strong>call </strong>allows us to execute the function <strong>immediately</strong> with the ‘o’ context, we can not just call that variable as a function. We can, of course, retrieve the log out content with the bar here.</p><p>Comparing to the ‘bind’ method:</p><pre>o.c.bind(o);  =&gt; <strong>function ()</strong> {<br>                   console.log(this.a + &#39; and &#39; + this.b);<br>                 }</pre><pre>var foo = o.c.bind(o);<br>foo();          =&gt; &quot;dog and cat&quot;</pre><p>With bind, it is perfectly fine to reuse the function without any errors, since bind <strong>will not execute the function right away</strong>. Rather, it returns a new function with the context permanently bound(so we don’t have to worry about ‘context loss’ when calling the method) and we<strong> can execute later</strong>. Therefore, we can store it to a variable and pass it around, re-use the method anywhere in our code. This is why bind is so useful when we deal with event handlers.</p><p>When the user is interacting with a web page, particular events will occur <strong>after </strong>the user performs some actions. For example, the text field will generate a random number when the button is clicked:</p><pre>&lt;button&gt;Get Random Number&lt;/button&gt;​​<br>&lt;input type=”text” /&gt;</pre><pre>document.addEventListener(&quot;DOMContentLoaded&quot;, function() { <br>  var input = document.querySelector(&quot;input&quot;);<br>  var number = {  <br>    randomNum: function() { <br>      return Math.floor(Math.random() * 100) + 1; <br>    },<br>    clickEvent: function (e) { <br>      var num = this.randomNum();<br>      input.value = num;<br>    },<br>  };<br>  <br>  var button = document.querySelector(&quot;button&quot;);<br>});</pre><pre>button.addEventListener(&#39;click&#39;, number.clickEvent); =&gt; error occurred, method is bound to the HTML element, not the number object</pre><pre>Fix with <strong><em>bind</em></strong>:</pre><pre>button.addEventListener(&#39;click&#39;, number.clickEvent.bind(number));</pre><p>With binding the clickEvent method to the number object, we not only set the this value on methods, but also make sure the method is called <strong>after </strong>the button is clicked by the user. It also makes sense to use bind instead of call here since we do not want to immediately execute the method; instead we need to wait for the user to click the button then execute it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=baccb2c7c46e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CSS Pseudo Elements]]></title>
            <link>https://medium.com/@florenceliang/css-pseudo-elements-39e3233727e?source=rss-79978d99f77a------2</link>
            <guid isPermaLink="false">https://medium.com/p/39e3233727e</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Florence Liang]]></dc:creator>
            <pubDate>Wed, 21 Sep 2016 17:59:10 GMT</pubDate>
            <atom:updated>2016-09-21T17:59:10.057Z</atom:updated>
            <content:encoded><![CDATA[<p>Without changing the HTML file, the CSS pseudo elements allow us to apply specific properties on targeted elements. By default, the pseudo elements are inline elements. If we want to have padding and margin effect on them, we need to change the display property.</p><p>The common pseudo elements include :<em>before</em>, <em>:after</em>, <em>:hover</em>, <em>:focus</em> and <em>:nth-child</em>. We can check out some examples for these popular pseudo elements.</p><ol><li><strong>:before and :after</strong></li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fembed%2Fpreview%2FvXyRoY%3Fheight%3D600%26amp%3Bslug-hash%3DvXyRoY%26amp%3Bdefault-tabs%3Dcss%2Cresult%26amp%3Bhost%3Dhttp%253A%252F%252Fcodepen.io%26amp%3Bembed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fpen%2FvXyRoY&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0a0cdc46d701503da2faaf5e7677f5d/href">https://medium.com/media/f0a0cdc46d701503da2faaf5e7677f5d/href</a></iframe><p>In the above example, I add text in the div element in CSS with the pseudo elements. In fact, we can insert image or add special characters just by changing the content value. Another common use of :after is to clear the floats, which will force the parent element to expand after the element’s content, but still inside the parent container.</p><p>2. <strong>:hover</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fembed%2Fpreview%2FpENVZV%3Fheight%3D600%26amp%3Bslug-hash%3DpENVZV%26amp%3Bdefault-tabs%3Dhtml%2Cresult%26amp%3Bhost%3Dhttp%253A%252F%252Fcodepen.io%26amp%3Bembed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fpen%2FpENVZV&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F820433.pENVZV.small.bdd17649-818f-48ba-9cf2-0d5b66cdd01a.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/8265f81819b117148dacbc64fb8903ed/href">https://medium.com/media/8265f81819b117148dacbc64fb8903ed/href</a></iframe><p>:hover is useful when we want to apply different style on an element when the pointer is over it. It doesn’t have to be used with links.</p><p>3. <strong>:first-child, :first-of-type, :last-child, :last-of-type</strong></p><p>The :first-child pseudo-class represents the first child of its parent element; The :last-child pseudo-class represents the last child element in its parent container.</p><p>The :first-of-type pseudo-class represents the first element of its kind in its parent container; The :last-of-type pseudo-class represents the last element of its kind in its parent container.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fembed%2Fpreview%2FzKojdb%3Fheight%3D600%26amp%3Bslug-hash%3DzKojdb%26amp%3Bdefault-tabs%3Dhtml%2Cresult%26amp%3Bhost%3Dhttp%253A%252F%252Fcodepen.io%26amp%3Bembed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fpen%2FzKojdb&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F820433.zKojdb.small.c3e2fc4c-2777-43c9-971a-539c32099d61.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2ff1bc05e01aba32b3529c677213a8d/href">https://medium.com/media/b2ff1bc05e01aba32b3529c677213a8d/href</a></iframe><p>From the example, note that we append the :first-child and :last-child after the elements <strong><em>li, </em></strong>while<strong><em> </em></strong>we append the :first-of-type and :last-of-type after the parent elements <strong><em>ul </em></strong>in order to have the desired effect.</p><p>4.<strong> :nth-child or :nth-of-type</strong></p><p>:nth-child is a versatile pseudo element in CSS. The difference of :nth-of-type is that we target a more specific element contained within the same parent element. The example is like below:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fembed%2Fpreview%2FzKojdb%3Fheight%3D600%26amp%3Bslug-hash%3DzKojdb%26amp%3Bdefault-tabs%3Dhtml%2Cresult%26amp%3Bhost%3Dhttp%253A%252F%252Fcodepen.io%26amp%3Bembed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fpen%2FzKojdb&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F820433.zKojdb.small.c3e2fc4c-2777-43c9-971a-539c32099d61.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2ff1bc05e01aba32b3529c677213a8d/href">https://medium.com/media/b2ff1bc05e01aba32b3529c677213a8d/href</a></iframe><p>Oftentimes we will also see the (an+b) structure, such as (2n+1), meaning that “Select every other child starting with the first”. This is why :nth-child pseudo elements are so helpful, they target one or more elements on their order.</p><p>More information about pseudo-elements: <a href="https://www.smashingmagazine.com/2016/05/an-ultimate-guide-to-css-pseudo-classes-and-pseudo-elements/#first-of-type">https://www.smashingmagazine.com/2016/05/an-ultimate-guide-to-css-pseudo-classes-and-pseudo-elements/#first-of-type</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39e3233727e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Box Model in CSS]]></title>
            <link>https://medium.com/@florenceliang/box-sizing-in-css-ba1677bc8dba?source=rss-79978d99f77a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba1677bc8dba</guid>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Florence Liang]]></dc:creator>
            <pubDate>Wed, 21 Sep 2016 02:13:46 GMT</pubDate>
            <atom:updated>2016-09-21T18:06:20.561Z</atom:updated>
            <content:encoded><![CDATA[<p>The actual width and height of the element can be determined by a number of properties:</p><ol><li>By the contents of the element.</li></ol><p>2. The padding and border affect the width and height of the element. In a CSS box, the width and height can be calculated by the following formulas:</p><p>width = border-right + padding-right + width + padding-left + border-left</p><p>height = border-top + padding-top + height + padding-bottom + border-bottom</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fembed%2Fpreview%2FamBLov%3Fheight%3D600%26amp%3Bslug-hash%3DamBLov%26amp%3Bdefault-tabs%3Dhtml%2Cresult%26amp%3Bhost%3Dhttp%253A%252F%252Fcodepen.io%26amp%3Bembed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fpen%2FamBLov%2F&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fm.cdpn.io%2Fscreenshot-coming-soon-small.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c159125d8580632d29e3b3dc5a7c86b/href">https://medium.com/media/6c159125d8580632d29e3b3dc5a7c86b/href</a></iframe><p>In this example, the <strong><em>a</em></strong> elements accept padding values on all sides. So the width and height of the elements not only include the font size of <strong><em>a,</em></strong> but also include the padding. (It is also worth noting that even the height of the inline elements become longer with padding, the vertical padding will not affect neighboring inline elements.)</p><p>3. Declare width and height properties. If the height you set is shorter than the top and bottom padding, browser will just go ahead to use the padding as the element’s height. For example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fembed%2Fpreview%2FzKopEX%3Fheight%3D600%26amp%3Bslug-hash%3DzKopEX%26amp%3Bdefault-tabs%3Dcss%2Cresult%26amp%3Bhost%3Dhttp%253A%252F%252Fcodepen.io%26amp%3Bembed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fpen%2FzKopEX&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F820433.zKopEX.small.d71aaee3-bd08-4871-9adb-09161014568f.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0d43eecd9eb5d7a251df50da25f56f4/href">https://medium.com/media/f0d43eecd9eb5d7a251df50da25f56f4/href</a></iframe><p>We notice that the yellow background div is longer than the green one. This is because the yellow div takes the content height into account and also add the top and bottom padding as total height. In the green div, the height is set 30px , which is shorter than the combined top and bottom padding(20px + 20px). In this case, the browser will just ignore the declared height value and the content height inside as well — only use the padding as the element’s height.</p><p>4. By the display property. Block-level elements have a default width of 100% to take up the entire horizontal space. For inline elements, width and height can’t be applied and only occupy their own space.</p><iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fpen%2FRGojQz&amp;src=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fembed%2Fpreview%2FRGojQz%3Fheight%3D600%26amp%3Bslug-hash%3DRGojQz%26amp%3Bdefault-tabs%3Dcss%2Cresult%26amp%3Bhost%3Dhttp%253A%252F%252Fcodepen.io%26amp%3Bembed-version%3D2&amp;type=text%2Fhtml&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcf689c1075a60c414ff08b215edb7f7/href">https://medium.com/media/bcf689c1075a60c414ff08b215edb7f7/href</a></iframe><p>In the first example on the left, height and width of the <strong><em>a</em></strong> element in<strong><em> box1 </em></strong>are just determined by the content “Item 1”. Since <strong><em>a</em></strong> elements are inline elements by default, they don’t take width and height. In comparison, I change the display property to inline-block in the second example, the height and width are now determined by the specified width and height in the CSS.</p><p>5. By the box-sizing property. Content-box is the default box-sizing value in CSS, so applying padding on it will actually make the element larger. Its actual width will not be the same with the specified width. However, applying border-box will make the total width as specified. This can be demonstrated by below example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fembed%2Fpreview%2FJRbrWg%3Fheight%3D600%26amp%3Bslug-hash%3DJRbrWg%26amp%3Bdefault-tabs%3Dcss%2Cresult%26amp%3Bhost%3Dhttp%253A%252F%252Fcodepen.io%26amp%3Bembed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Ffloliang%2Fpen%2FJRbrWg&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F820433.JRbrWg.small.e26910d5-b0f9-40d2-88bd-3352bf949e58.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/011c706b1d039c0892f8a276690a33a1/href">https://medium.com/media/011c706b1d039c0892f8a276690a33a1/href</a></iframe><p>Notice that partial words got covered by the border in the border box? In order to achieve total width of 60px, the width of content inside the border-box got reduced to 30px. For content-box, the content width stays 60px and its box’s width becomes 90px(60 + 5*2 + 10*2).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba1677bc8dba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby Hash#sort_by]]></title>
            <link>https://medium.com/@florenceliang/some-notes-about-using-hash-sort-by-in-ruby-f4b3a700fc33?source=rss-79978d99f77a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4b3a700fc33</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Florence Liang]]></dc:creator>
            <pubDate>Fri, 29 Apr 2016 21:12:17 GMT</pubDate>
            <atom:updated>2018-05-03T18:18:01.715Z</atom:updated>
            <content:encoded><![CDATA[<p>Ruby#sort and #sort_by methods are powerful: We can easily arrange the result we want, such as sort the names by alphabetical order or the name’s length. Surprisingly, there are not many examples in Ruby document library for this useful tool. Therefore, I would like to explore some use cases of #sort_by and #sort, specifically for Hash.</p><ul><li><strong>#sort will return a new array</strong></li></ul><p>First thing to note that #sort/#sort_by will return a new array. Suppose we have a hash:</p><pre>hash = {a:1, b:2, c:4, d:3, e:2}</pre><p>When we call sort on this hash, it will convert hash into a nested array:</p><pre><strong>hash.sort</strong></pre><pre>=&gt; [[:a, 1], [:b, 2], [:c, 4], [:d, 3], [:e, 2]]</pre><p>Therefore, if we want to return the sorted hash in hash format, we need to call #to_h after #sort.</p><p><strong>Sort by Hash values in ascending and descending order</strong></p><p>So what if we want to sort by hash values instead of the key’s order? Here comes the #sort_by method:</p><pre><strong>hash.sort_by {|k, v| v}</strong></pre><pre>=&gt; [[:a, 1], [:b, 2], [:e, 2], [:d, 3], [:c, 4]]</pre><p>By default, Ruby returns the value in ascending order. If we want Ruby to return the value in descending order without using #reverse method, we can do this:</p><pre><strong>hash.sort_by {|k, v| -v}</strong></pre><pre>=&gt; [[:c, 4], [:d, 3], [:b, 2], [:e, 2], [:a, 1]]</pre><p><strong>Sort by Hash values and keys at the same time</strong></p><p>Finally, suppose we have a long list of hash like below:</p><pre>hash = {“w”=&gt;2, “k”=&gt;1, “l”=&gt;2, “v”=&gt;5, “d”=&gt;2, “h”=&gt;4, “f”=&gt;1, “u”=&gt;1, “p”=&gt;1, “j”=&gt;1}</pre><p>With this hash, we want to do two things. First, sort the hash by the values in descending order . Then, sort it by letter’s alphabetical order. We can easily achieved this as below:</p><pre><strong>hash.sort_by {|k, v| [-v, k]}</strong></pre><pre>=&gt; [[“v”, 5], [“h”, 4], [“d”, 2], [“l”, 2], [“w”, 2], [“f”, 1], [“j”, 1], [“k”, 1], [“p”, 1], [“u”, 1]]</pre><p>I hope the use cases will help us write cleaner and shorter codes in Ruby.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4b3a700fc33" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>