<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://tech.saigonist.com"  xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>tomo&#039;s blog</title>
 <link>http://tech.saigonist.com/blogs/tomo</link>
 <description></description>
 <language>en</language>
<item>
 <title>Should you use Kotlin?</title>
 <link>http://tech.saigonist.com/b/code/should-you-use-kotlin</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on July 8, 2017 - 12:53pm&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/kotlin&quot;&gt;kotlin&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/java&quot;&gt;java&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/jvm&quot;&gt;jvm&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;I spent some time looking at a relatively unknown programming language called Kotlin today. Kotlin was designed by a company rather than by academics in a computer science department. The company happens to make and sell an IDE so perhaps they are hoping that as the language becomes more popular and programmers need an IDE to use it they will become customers of JetBrains, the company behind Kotlin.&lt;/p&gt;

&lt;p&gt;The basic description of the language goes like this: Kotlin is a statically-typed programming language that runs on the Java Virtual Machine&lt;/p&gt;

&lt;p&gt;Kotlin is yet another language that compiles down to other bytecode &quot;languages&quot;. It can even compile to JavaScript so that it can be run in browsers or V8. Thus it is a lot like Scala (which normally compiles for the JVM but also has a compiler to JavaScript).&lt;/p&gt;

&lt;p&gt;It aims to fix many of the problems of Java, yet be usable by mere mortal coders unlike Scala.&lt;/p&gt;

&lt;p&gt;Whereas Java programs can only be object-oriented, Kotlin can have simple procedures as well.&lt;/p&gt;

&lt;p&gt;Since the main compile target is the JVM, Kotlin makes the most sense as a Java replacement, e.g. for Android development. Google is now supporting this internally.&lt;/p&gt;

&lt;p&gt;Kotlin does not make sense for web development or systems programming.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Sat, 08 Jul 2017 05:53:09 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">97 at http://tech.saigonist.com</guid>
</item>
<item>
 <title>Testing Bitcoin with Testnet wallets and coins</title>
 <link>http://tech.saigonist.com/b/blockchain/testing-bitcoin-testnet-wallets-and-coins</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on June 27, 2017 - 8:21pm&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/crypto&quot;&gt;crypto&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/bitcoin&quot;&gt;bitcoin&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;Bitcoin is, among many things, a software system with many participants running Bitcoin software and who all connect to each other over the internet forming a Bitcoin network. It&#039;s a live system yet the software that runs it is continuously under development. This is what the Bitcoin Core dev team does, and sometimes they allow other people to participate in development like an open source project. There are other developers who also develop other Bitcoin code which fulfills the same purposes. Then all of the various Bitcoin nodes run a version of the software.&lt;/p&gt;

&lt;p&gt;Of course, any online software needs a safe place to run in development and be tested. For Bitcoin, this is called Testnet. Bitcoin&#039;s Testnet is an actual separate Bitcoin blockchain and all the nodes or miners which connect to or mine blocks for this blockchain. It&#039;s like if there was a backup copy of the Internet which had its own shadow websites and browsers which could browse them and the information on the shadow Internet was not the same as the real Internet.&lt;/p&gt;

&lt;p&gt;To use Testnet as a consumer (i.e. to send or receive Bitcoin transactions) you need a wallet like with Bitcoin. But the wallet needs to support testnet. Assuming you don&#039;t want to download the testnet blockchain locally and use a heavy wallet, there are a few light wallets which support testnet.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Copay.io has a multi-platform (including binaries for OS X) wallet which allows you to configure wallets for testnet. Just create a new (personal) wallet and in the advanced options enable the checkbox labeled Testnet. Then use it like you would a normal Bitcoin wallet, but with testnet addresses. &lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Note: Copay doesn&#039;t support signing messages (testnet or otherwise)&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Electrum is another light wallet which connects to servers instead of using a local copy of the blockchain. To use Electrum with Testnet just run it with the &lt;code&gt;--testnet&lt;/code&gt; argument. E.g. &lt;code&gt;/Applications/Electrum.app/Contents/MacOS/Electrum --testnet&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Note: You might find a working testnet Electrum server here: &lt;a href=&quot;https://1209k.com/bitcoin-eye/ele.php?chain=tbtc&quot;&gt;list&lt;/a&gt;
Right now testnet.hsmiths.com:53012 using ssl is working.&lt;/p&gt;

&lt;p&gt;Electrum does support signing messages. Unfortunately, currently all known testnet servers are down so you should try Copay unless you need message signing in which case you can sign messages with another piece of software using an exported copy of your Copay wallet (you could import into Electrum even).&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;There was a website called TestnetWallet.com but it has been offline. It was basic, no support for message signing.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;You could also try something like &lt;a href=&quot;https://github.com/weex/bitcoin-signature-tool&quot;&gt;Bitcoin Signature Tool&lt;/a&gt; just to do the message signing.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Mycelium is an Android Bitcoin wallet which has a Testnet version. This supports message signing. It&#039;s Android only and so, while convenient for transacting while outside, it&#039;s not as convenient as a desktop wallet for testing.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Once you have a testnet wallet, you need some coins. Obviously you don&#039;t want to waste real bitcoins on testing and they won&#039;t work anyways. So you need to either mine some testnet coins (in which case you didn&#039;t bother fussing with a light wallet) or get someone to send you some. You shouldn&#039;t have to actually buy testnet coins.&lt;/p&gt;

&lt;p&gt;You can get free testnet coins from a testnet faucet. They often go down and run out of coins. This one works for now: &lt;a href=&quot;https://testnet.manu.backend.hamburg/faucet&quot;&gt;https://testnet.manu.backend.hamburg/faucet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just get a small amount of testnet coins (as in a fractional amount of a single testnet BTC) from a faucet and test with even smaller amounts and send some back to the faucet if you can.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Tue, 27 Jun 2017 13:21:00 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">96 at http://tech.saigonist.com</guid>
</item>
<item>
 <title>@mixin and other CSS @at-rules in Sass/SCSS</title>
 <link>http://tech.saigonist.com/b/code/mixin-and-other-css-rules-sassscss</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on June 27, 2017 - 3:34pm&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/scss&quot;&gt;scss&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/css&quot;&gt;css&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/sass&quot;&gt;sass&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;You&#039;ve probably seen CSS rules which start with the at-sign such as &lt;code&gt;@font-face&lt;/code&gt; for custom fonts or &lt;code&gt;@media&lt;/code&gt; used for responsive styling based on screen size.  Maybe you&#039;ve even used @keyframes for CSS animations. These so-called at-rules can go around normal CSS rules (normally a rule would simply be one or more selectors followed by lines of rules within curly braces).&lt;/p&gt;

&lt;p&gt;But the @-rules are rarely sprinkled throughout a stylesheet.&lt;/p&gt;

&lt;p&gt;In preprocessed CSS like Sass, many new at-rules are possible. Here we&#039;re using Sass/SCSS.&lt;/p&gt;

&lt;p&gt;What&#039;s SCSS? It&#039;s Sassy CSS, and .scss is the new, preferred Sass file extension. Use .scss instead of .sass and the old grammar.&lt;/p&gt;

&lt;p&gt;Sass brings with it many of its own @-rules of much more variety than normal CSS supports. Think of the Sass at-rules as new keywords (like the reserved words in a programming language). Basically, any new keyword which Sass adds is preceded by @, and other programming language-like features in Sass use prefix characters like &lt;code&gt;$&lt;/code&gt; for variables (also &lt;code&gt;:&lt;/code&gt;, &lt;code&gt;%&lt;/code&gt;, &lt;code&gt;#&lt;/code&gt;, &lt;code&gt;&amp;amp;&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Sass also includes at-rules which already existed in normal CSS but gives them a slightly different meaning. This includes &lt;code&gt;@import&lt;/code&gt; and &lt;code&gt;@media&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Other useful Sass at-rules are @extend, @include, @at-root, as well as &quot;control&quot; words useful in functions for adding logic: @if, @while, @each, etc.&lt;/p&gt;

&lt;p&gt;But a really useful feature of Sass is the @mixin, and related at-rule @function. The two are similar, providing reusable shortcuts to cut down on repeating big chunks of code. But a mixin is a type of macro, perhaps familiar to C-language family users or even assembly language programmers. Macros are preprocessed and churn out code in the same language as the source. A function, on the other hand, is written in the programming language and executed. So they are processed at different levels and a macro is processed before a function or any other code.&lt;/p&gt;

&lt;p&gt;Sass mixins are useful for simplifying many lines of vendor-specific rules (e.g. rules that start with &lt;code&gt;-webkit-&lt;/code&gt; or &lt;code&gt;-moz-&lt;/code&gt;, etc.) for new experimental CSS features into a single line. This line then expands into all the vendor rules because they are generally very similar lines with a single value. This value can be used by the mixin as an argument, much like a function.&lt;/p&gt;

&lt;p&gt;There is about to be native support in CSS for mixins. However, the native mixins will not support using arguments and the syntax is different from Sass mixins. So it will still make sense to use Sass and preprocess into native CSS.&lt;/p&gt;

&lt;p&gt;If, instead of repeating chunks of text, you want to calculate values, then use Sass @function instead of @mixin. A function can use any math expression that can otherwise be used in Sass, as well as having controls (like @if or @each) to take different paths using logic and looping.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Tue, 27 Jun 2017 08:34:41 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">95 at http://tech.saigonist.com</guid>
</item>
<item>
 <title>How to read a SPF record and allow Google Apps Gmail to send for your custom domain</title>
 <link>http://tech.saigonist.com/b/server/how-read-spf-record-and-allow-google-apps-gmail-send-your-custom-domain</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on July 23, 2016 - 12:28pm&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/spam&quot;&gt;spam&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/dns&quot;&gt;dns&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/spf&quot;&gt;spf&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/email&quot;&gt;email&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;An SPF record is a hack. It&#039;s a way of using the DNS protocol to say who is allowed to authoritatively send email as a domain name. It does this by using a DNS &lt;code&gt;TXT&lt;/code&gt; record (as opposed to an &lt;code&gt;A&lt;/code&gt; or &lt;code&gt;MX&lt;/code&gt; record). TXT can contain any text. An SPF record is a specially encoded TXT record like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&quot;v=spf1 ip4:198.51.100.26 -all&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;v=spf1&lt;/code&gt; indicates that this is an SPF record, considered version 1 of the spf standard.&lt;/p&gt;

&lt;p&gt;After the version, there are any number of &#039;directives&#039;. In this example, there&#039;s only 1 directive and that is &lt;code&gt;ip4:198.51.100.26&lt;/code&gt;. The type of directive here is &lt;code&gt;ip4&lt;/code&gt; but it could also be &lt;code&gt;a&lt;/code&gt; or &lt;code&gt;mx&lt;/code&gt; or &lt;code&gt;include&lt;/code&gt; or &lt;code&gt;ipv6&lt;/code&gt;. It can also be &lt;code&gt;ptr&lt;/code&gt; or a few other things. And the final &lt;code&gt;-all&lt;/code&gt; can be thought of as the final directive which is a catch-all. If it wasn&#039;t at the end, then no other directive after it would ever be matched. It is meant to fail any query on this spf record which doesn&#039;t match any earlier directives and the minus sign before &lt;code&gt;all&lt;/code&gt; signifies that it fails. Other symbols instead of &lt;code&gt;-&lt;/code&gt; could be &lt;code&gt;+&lt;/code&gt; (pass), &lt;code&gt;?&lt;/code&gt; (neutral), or &lt;code&gt;~&lt;/code&gt; (soft fail). If there&#039;s no sign then neutral is assumed.&lt;/p&gt;

&lt;p&gt;So to create a SPF record for use with Google Apps for custom domains, first we go into our custom domain&#039;s DNS records settings. We create a new record of type TXT. The value starts with: &lt;code&gt;&quot;v=spf1 -all&quot;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, nothing will be able to pass spf for your domain. So we need to add directives which will pass. The directive we want for Google Apps then we add the directive &lt;code&gt;include:_spf.google.com&lt;/code&gt; so the record becomes: &lt;code&gt;&quot;v=spf1 include:_spf.google.com -all&quot;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, this form of &lt;code&gt;-all&lt;/code&gt; is strict and may cause some mails sent from Google to fail. To loosen the restriction change it to &lt;code&gt;~all&lt;/code&gt; instead:  &lt;code&gt;&quot;v=spf1 include:_spf.google.com ~all&quot;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once you&#039;re finished saving your TXT record (and have kicked bind/named if needed) then you can do a quick validation test of your new SPF record using the tool found &lt;a href=&quot;http://www.kitterman.com/spf/validate.html&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Sat, 23 Jul 2016 05:28:06 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">94 at http://tech.saigonist.com</guid>
</item>
<item>
 <title>Import multi-line cells in CSV format into Google Sheets</title>
 <link>http://tech.saigonist.com/b/code/import-multi-line-cells-csv-format-google-sheets</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on July 20, 2016 - 1:13pm&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/csv&quot;&gt;csv&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/spreadsheet&quot;&gt;spreadsheet&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/google-docs&quot;&gt;google docs&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;In Google Sheets (and Excel or Mac Numbers should be similar for most of this), you normally enter a cell&#039;s value by hitting enter which moves to the next cell. But if you need multiple lines in a single cell, you need to know the trick of holding command/Apple-key when pressing enter.&lt;/p&gt;

&lt;p&gt;If you want to import a CSV file into Google Sheets but get the same effect, then you need to escape the new-line markers.&lt;/p&gt;

&lt;p&gt;First, a cell which contains more than one line (instead of each line being on a new row) needs to be &quot;escaped&quot; with double quotes at the start and end of the cell&#039;s value. Next, your file needs to use the &quot;carriage return&quot; (CR) character to demarcate new rows instead of just a newline (LF - line feed). In most programming languages, a new line ends with the &quot;\n&quot; special character. But for the format we want, we need only use &quot;\n&quot; for separating lines within a cell. Then we need to use the &quot;\r&quot; carriage return character to separate rows.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;apple,&quot;From time to time
The clouds give rest
To the moon-beholders.&quot;\r
potatoe,&quot;A single line&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can verify the format which Google Sheets expects for a CSV import by creating a CSV export from Google Sheets, and having cells with multiple lines in it. Then open the resulting file in a code or text editor which can display DOS file endings (carriage returns).&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Wed, 20 Jul 2016 06:13:04 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">93 at http://tech.saigonist.com</guid>
</item>
<item>
 <title>JavaScript Namespaces and Closures instead of Global Variables</title>
 <link>http://tech.saigonist.com/b/code/javascript-namespaces-and-closures-instead-global-variables</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on July 12, 2016 - 2:07pm&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/javascript&quot;&gt;javascript&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/closure&quot;&gt;closure&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/namespace&quot;&gt;namespace&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;Have you heard? Global variables are bad, mmkay? So what should we do if we want to create a variable in our script and be able to refer to it elsewhere, globally?&lt;/p&gt;

&lt;h2&gt;What&#039;s in a Namespace?&lt;/h2&gt;

&lt;p&gt;A JavaScript namespace is just an idea or practice rather than an explicit feature of JavaScript. That is, you don&#039;t create a &quot;namespace object&quot; or declare a namespace. Instead, you would implement a namespace by creating and using an object which would contain sub-variables within it, so that those variables didn&#039;t pollute the namespace of the global scope (i.e. everything under the &lt;code&gt;window&lt;/code&gt; object in the JavaScript DOM).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var aNamespace = {};
aNamespace.aVar = &#039;A public variable&#039;;
aNamespace.getAVar = function () { return aNamespace.aVar; };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Why do we need this? As JavaScript scripts get bigger, as they include more 3rd party libraries, there is more chance for variable naming conflicts. Creating variables inside a namespace reduces the possibility of conflict to just the name of the namespaces colliding, which can happen with libraries which use the &lt;code&gt;$&lt;/code&gt; variable such as jQuery. If you&#039;re using jQuery and another library at the same time which defines &lt;code&gt;$&lt;/code&gt; for its own use, then your code needs to be able to refer to the correct &lt;code&gt;$&lt;/code&gt; object depending on where it&#039;s called. The solution is to create an enclosing function around your jQuery code, passing jQuery in as a local variable &lt;code&gt;$&lt;/code&gt;, e.g.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(function($){
  // now $ is a local variable, not the global one
  $(&#039;#foo&#039;).bar();
  $(&#039;.bar&#039;).baz();
  $(&#039;#foo .bar&#039;).show();
})(jQuery);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This example doesn&#039;t demonstrate namespaces, besides jQuery being a namespace, but shows how variable scoping inside functions can be used to separate or protect a variable in the global scope.&lt;/p&gt;

&lt;h2&gt;Closures returning namespaces&lt;/h2&gt;

&lt;p&gt;In the first example, we showed a simple namespace which demonstrated variables and methods (which is just a function in a namespace, there&#039;s no other distinction within JavaScript). A namespace&#039;s variables and functions can be defined when the namespace is defined or added to the namespace afterwards. But what if we have a lot of variables, including only internally-used ones, and member functions which are just helpers and don&#039;t need to be used outside the namespace? This is where we want both private and public members. But JavaScript has no &lt;code&gt;private&lt;/code&gt; or &lt;code&gt;public&lt;/code&gt; keywords! The concept, like namespaces, doesn&#039;t exist as a native language feature of JavaScript. But we can still use normal JS objects to implement private properties.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var aNamespace = (function () {
  var aPrivateVar = &#039;This is private and cannot be accessed directly&#039;;
  return {
    aPublicVar: &#039;This can be accessed directly&#039;,
    getPrivate: function () { return aPrivateVar; },
    setPrivate: function (v) { aPrivateVar = v; }
  }
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code (1) creates a namespace, (2) creates a closure around private variables in the namespace (by returning a reference to a function which references a variable defined in the scope where the function was defined thus forcing the variable to remain accessible by that function when that function is called even when execution is outside the parent), (3) creates an anonymous function which returns the namespace object and then immediately calls the anonymous function to return the object to set &lt;code&gt;aNamespace&lt;/code&gt; to it.&lt;/p&gt;

&lt;p&gt;There is also the convention of using underscores before property names to have them be considered private instead of public but doesn&#039;t actually offer any protection unlike the above closure pattern which doesn&#039;t provide any backdoor way to access the private members.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Tue, 12 Jul 2016 07:07:00 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">92 at http://tech.saigonist.com</guid>
</item>
<item>
 <title>Cross-platform node scripts: better-npm-run to the rescue</title>
 <link>http://tech.saigonist.com/b/code/cross-platform-node-scripts-better-npm-run-rescue</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on June 6, 2016 - 11:47am&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/nodejs&quot;&gt;node.js&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/javascript&quot;&gt;javascript&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;We know that Node.js is awesome and JavaScript is the ultimate cross-platform language. Node has support for Windows despite the node command being a command line program. Windows users can use the command line shell for node. But the Windows shell environment isn&#039;t completely compatible with what Unix/Linux/Mac users are used to (i.e. Bash). The npm package Better-npm-run helps deal with running tasks with node across platforms including Windows.&lt;/p&gt;

&lt;p&gt;Get it here: &lt;a href=&quot;https://www.npmjs.com/package/better-npm-run&quot;&gt;https://www.npmjs.com/package/better-npm-run&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This package enhances what your &lt;code&gt;scripts&lt;/code&gt; in your package.json for your node project can call. So instead of writing complex command lines which will only run in a POSIX shell (or not, but definitely not in cmd.exe) you can specify the various command line parameters such as inline setting of environment variables (e.g. &lt;code&gt;PATH=/foo/bar&lt;/code&gt;). Windows shell also doesn&#039;t support the conditional &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; to run another command on one line depending on the return value of the left-hand side command.&lt;/p&gt;

&lt;p&gt;Unfortunately, for more advanced stuff which can&#039;t be supported just by adding configuration to package.json, they recommend creating .sh shell scripts and calling them. Thus you&#039;ll still have problems running the same code and configuration on Windows.&lt;/p&gt;

&lt;p&gt;There are other npm packages which help you right npm scripts as configuration rather than command lines, e.g. &lt;code&gt;cross-env&lt;/code&gt;. Using this approach, it is possible to take over the duties of Grunt/Gulp with just node scripts and packages.&lt;/p&gt;

&lt;p&gt;Note: better-npm-run should be a devDependency and be installed on the platform where it will be run. A local relative install of better-npm-run on one platform like Linux will not then run on Windows.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Mon, 06 Jun 2016 04:47:55 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">91 at http://tech.saigonist.com</guid>
</item>
<item>
 <title>Web viewports for mobile responsive websites</title>
 <link>http://tech.saigonist.com/b/code/web-viewports-mobile-responsive-websites</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on May 16, 2016 - 7:39am&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/mobile&quot;&gt;mobile&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/web&quot;&gt;web&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/responsive&quot;&gt;responsive&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/css&quot;&gt;css&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;What is a viewport? You may have seen the meta-tag named &quot;viewport&quot; on newer mobile-friendly websites. The mobile (iOS) version of Safari introduced the &quot;viewport meta tag&quot; and &quot;scale&quot; on the viewport for mobile (scale optionally being one of the special values &lt;code&gt;minimum-scale&lt;/code&gt;, &lt;code&gt;maximum-scale&lt;/code&gt;, and &lt;code&gt;user-scalable&lt;/code&gt;).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A viewport is like a logical window. You are on one side of the window looking through to the website. Sometimes the window (viewport) is big, sometimes you stand close to it. In some cases, you can see all that there is to see in the window, other times the walls around the window block some part of the view.&lt;/p&gt;

&lt;p&gt;Confusingly, there are two different viewports, the layout viewport which is the whole rendered web page whether you can see it all or not, and there&#039;s the visual viewport which is the window metaphor I used above.&lt;/p&gt;

&lt;p&gt;For normal desktop websites viewed on a mobile device the layout viewport will be much bigger than what&#039;s visible on the screen and the screen will be zoomed into some main part, as if through a window or camera viewscreen (this is the so-called visual viewport) of the content (text) and the text will be big enough to be legible. On desktops, the viewport will be the size of the browser window.&lt;/p&gt;

&lt;p&gt;For this reason, many websites explicitly set the viewport metatag to &quot;width=320, initial-scale=1&quot;. A lot of smartphones (iPhones and older Androids) have a width of 320 CSS pixels in their visual viewport (320px width in portrait) and so the mobile website will fit perfectly and will not be zoomed in at first. Trying to solve more problems, by limiting scaling (e.g. setting &lt;code&gt;minimum-scale&lt;/code&gt; and &lt;code&gt;maximum-scale&lt;/code&gt; both to 1.0 in the meta tag) fixes some problems but can prevent people who need to zoom to view your site to not be able to use your site now.&lt;/p&gt;

&lt;p&gt;But a lot of smartphones will say there are more pixels, and what that means is confusing. When it comes to mobile devices and CSS, a pixel is not a pixel. With denser screens (think Retina), there can be more than one hardware pixel for a single CSS pixel.&lt;/p&gt;

&lt;p&gt;If that&#039;s not confusing enough, consider all the possible &quot;widths&quot; for a web page we now have in CSS: width of the html element, or the body element, the desktop-browser-window-viewport width, etc.&lt;/p&gt;

&lt;p&gt;Besides setting a value for viewport width in the meta value, we can use media queries (@media in CSS).&lt;/p&gt;

&lt;p&gt;Height works similarly to width but can usually be ignored if you take care of width.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Mon, 16 May 2016 00:39:00 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">90 at http://tech.saigonist.com</guid>
</item>
<item>
 <title>Embed a YouTube video player and control it via JavaScript</title>
 <link>http://tech.saigonist.com/b/code/embed-youtube-video-player-and-control-it-javascript</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on May 14, 2016 - 4:32am&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/youtube&quot;&gt;youtube&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/javascript&quot;&gt;javascript&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/mobile&quot;&gt;mobile&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/safari&quot;&gt;safari&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;YouTube has long made it easy to share and embed videos into websites by giving you the HTML code to just copy and paste wherever you like. Over the years, different technologies have been used to play videos on the web, from Flash &lt;code&gt;object&lt;/code&gt; elements to pure HTML5. YouTube&#039;s newest API for adding videos to web pages takes care of the technology, automatically choosing the best delivery method on a user by user basis.&lt;/p&gt;

&lt;p&gt;YT did have something called the &quot;YouTube JavaScript Player API&quot; but this is out of date so don&#039;t use it. Instead use the new &lt;a href=&quot;https://developers.google.com/youtube/js_api_reference&quot;&gt;YouTube IFrame Player API&lt;/a&gt;, so called because it creates an iframe which loads YouTube and lets YT handle loading the right code, e.g.. Flash if it&#039;s needed.&lt;/p&gt;

&lt;p&gt;With this API, you can programmatically load videos (or even lists of videos), step into them in the middle of the video if you wish instead of just the beginning, and play the video triggered by JavaScript. This means you could have a video playing when the page loads, or seconds after the page loads, or triggered by events like scrolling down or pressing keys or clicking the mouse. You can really control the video player however you want and can create your own custom interface for it.&lt;/p&gt;

&lt;ol&gt;&lt;li&gt;Get a &lt;code&gt;player&lt;/code&gt; object with &lt;code&gt;YT.Player&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Load a video into the player: &lt;code&gt;loadVideoById&lt;/code&gt; (pass in &lt;code&gt;videoId&lt;/code&gt;, which you can get from the video&#039;s URL).&lt;/li&gt;
&lt;li&gt;Play it! &lt;code&gt;player.playVideo()&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;There are two problems with YouTube control under JavaScript. A big one is that iOS Safari doesn&#039;t support it! JavaScript on Safari cannot cause a YouTube player to play or be controlled. If you need something that works for iPhone/iPad users, then you need to just show them a normal embedded YouTube player and tell them to play it themselves. You can&#039;t create a custom player interface for them.&lt;/p&gt;

&lt;p&gt;Another problem with YouTube JavaScript on mobile is timing. Mobile browsers (like Chrome on Android) are running JavaScript slower than desktops to begin with. There&#039;s also the issue of buffering videos. I&#039;ve found that smartphones using the same web code and the same Internet connection buffer more often when playing a video. You can detect buffering by subscribing to the player&#039;s &lt;code&gt;onStateChange&lt;/code&gt; events, which will fire when the player starts playing, is paused, and also when buffering.&lt;/p&gt;

&lt;p&gt;Even when accounting for buffering, I&#039;ve found that the player&#039;s events can be delayed significantly, or that player actions happen significantly before the events fire. If you are trying to control the player using timers (setTimeout) then you&#039;ll be off by a second or more on mobile.&lt;/p&gt;

&lt;p&gt;If you want to play a segment of a video (e.g. seeking to time 31.5 seconds and playing for 28.5 seconds) then you can achieve it more accurately using the arguments passed in when you load the video. Use the options &lt;code&gt;startSeconds&lt;/code&gt; and &lt;code&gt;endSeconds&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Fri, 13 May 2016 21:32:09 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">89 at http://tech.saigonist.com</guid>
</item>
<item>
 <title>How to detect a swipe touch event on mobile browsers with JavaScript</title>
 <link>http://tech.saigonist.com/b/code/how-detect-swipe-touch-event-mobile-browsers-javascript</link>
 <description>&lt;span class=&quot;submitted-by&quot;&gt;Submitted by tomo on May 14, 2016 - 12:19am&lt;/span&gt;&lt;div class=&quot;field_tags&quot;&gt;&lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/javascript&quot;&gt;javascript&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/mobile&quot;&gt;mobile&lt;/a&gt;&lt;/span&gt; &lt;span class=&quot;label label-info&quot;&gt;&lt;a href=&quot;/tags/swipe&quot;&gt;swipe&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;p&gt;Browsers have supported various mouse-based events like clicks, down to the detection of when the mouse button is down and when it&#039;s released. You can know the coordinates of the mouse cursor, all in JavaScript. Mobile interfaces have brought some new gestures beyond what desktops provided. One big set of such UI events is from the smartphone&#039;s screen allowing touch and even multi-touch. Multitouch allows more types of UI events.&lt;/p&gt;

&lt;p&gt;Many mobile apps support swiping, not just as a way to move a visible object around thescreen but applied for the whole screen. Tinder uses left swipe and right swipe to make processing potential sexual encounters quicker. Ebook readers use the same motion to flip through pages.&lt;/p&gt;

&lt;p&gt;Some browsers support swiping, and all the main mobile browsers support it. There are some desktop browsers, like Firefox, which don&#039;t support the touch events which other browsers implement.&lt;/p&gt;

&lt;p&gt;To detect swipe motions in JavaScript in a browser you can simply subscribe to the &lt;code&gt;tocuhstart&lt;/code&gt; and &lt;code&gt;touchmove&lt;/code&gt; events. In the callbacks to those events you&#039;ll receive event objects with an array called &lt;code&gt;touches&lt;/code&gt; as a member, which has the X and Y coordinates of where the touch occurred. So you note the coordinates when a touchstart happened, then when a touchmove occurs afterwards, the vector between the two coordinates from touchstart to touchmove is the swipe motion. This is different from a click, but requires the user to put their finger down and then move it across the screen to another position.&lt;/p&gt;

&lt;p&gt;Here&#039;s an example from &lt;a href=&quot;http://stackoverflow.com/questions/2264072/detect-a-finger-swipe-through-javascript-on-the-iphone-and-android&quot;&gt;StackOverflow&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;document.addEventListener(&#039;touchstart&#039;, handleTouchStart, false);        
document.addEventListener(&#039;touchmove&#039;, handleTouchMove, false);

var xDown = null;                                                        
var yDown = null;                                                        

function handleTouchStart(evt) {                                         
    xDown = evt.touches[0].clientX;                                      
    yDown = evt.touches[0].clientY;                                      
};                                                

function handleTouchMove(evt) {
    if ( ! xDown || ! yDown ) {
        return;
    }

    var xUp = evt.touches[0].clientX;                                    
    var yUp = evt.touches[0].clientY;

    var xDiff = xDown - xUp;
    var yDiff = yDown - yUp;

    if ( Math.abs( xDiff ) &amp;gt; Math.abs( yDiff ) ) {/*most significant*/
        if ( xDiff &amp;gt; 0 ) {
            /* left swipe */ 
        } else {
            /* right swipe */
        }                       
    } else {
        if ( yDiff &amp;gt; 0 ) {
            /* up swipe */ 
        } else { 
            /* down swipe */
        }                                                                 
    }
    /* reset values */
    xDown = null;
    yDown = null;                                             
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are also libraries that can do this for you, like Hammer.js. Hammer.js does a lot more than just swipes, it does pinches, rotates, and more, but adds another dependency to your project, even if it is smaller than alternatives. It&#039;s also buggy (while doing way more), whereas the above works on my Android devices.&lt;/p&gt;

&lt;p&gt;There&#039;s also a dead project called Swipe.js.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Fri, 13 May 2016 17:19:00 +0000</pubDate>
 <dc:creator>tomo</dc:creator>
 <guid isPermaLink="false">88 at http://tech.saigonist.com</guid>
</item>
</channel>
</rss>
