How to get type of some data in JavaScript

We known, sometimes it’s difficult to get the type of some data, by native JavaScript. Especially scripting cross frames.

So, we need some tools to get it accurately. Let’s do it.

Limitations of typeof

JavaScript provides typeof to get the type of some data natively.
But the operator can not detect all types of data in JavaScript.

1
2
3
4
5
6
7
8
9
10
11
typeof undefined              // 'undefined'
typeof 123 // 'number'
typeof Infinity // 'number'
typeof NaN // 'number'
typeof true // 'boolean'
typeof 'abc' // 'string'
typeof function(){} // 'function'
typeof {name: 'uolcano'} // 'object'
typeof null // 'object'
typeof [1,2,3] // 'object'
typeof new (function F(){}) // 'object'

The codes above tell us three things:

  • typeof can detect function type
  • it can detect the primitive values, but not exactly for number type
  • it regards all objects as the base object type, even though null is not an object semantically.

The latter two situation need to be solved.

Refining the number detecting

typeof can not exactly detect Infinity and NaN, so we must detect them separately.

There are some hints: the object window has two methods – window.isNaN and window.isFinite, detect whether a number is ‘Not a Number’, and whether a number is finite, respectively.
So, we just need to deal with them solely, the other number is really a number.

1
2
3
4
5
6
var type = typeof arg;
if(type === 'number') {
if(isNaN(arg)) return 'NaN';
if(!isFinite(arg)) return 'Infinity';
return type;
}

Detecting object type

There two ways to get the correct type of an instance of certain type:

  • Object.prototype.toString can get the type of the built-in object
  • An object’s constructor property references the constructor on the closest prototype chain

Firstly, we retrieve the type of the built-in object, thus the other objects is some non-native object – e.g. BOM object, DOM object and developer-customized object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if(typeof arg === 'object') {
var type = Object.prototype.toString.call(arg).slice(8, -1).toLowerCase();
switch(type){
case 'number':
case 'string':
case 'boolean':
return 'object ' + type;
case 'null':
case 'date':
case 'regexp':
return type;
default:
return arg.constructor.toString().match(/function\s*([^\(\s]*)/)[1].toLowerCase();
}
}

The complete codes:

在Windows下,如何修改Vagrant和VirtualBox的路径

由于Vagrant和VirtualBox实在太占空间了,用WinDirStat检测,单文件最占空间的是虚拟机文件,所以决定改下路径。有两个路径要修改,Vagrant的VAGRANT_HOME和VirtualBox的文件默认生成路径。我是直接全部重装,把程序也都换了路径了。

修改Vagrant BOX路径

设置单个用户的环境变量

1
setx VAGRANT_HOME "X:/your/path"

设置系统的环境变量

1
setx VAGRANT_HOME "X:/your/path" /M

或者可以从系统视图中找到:控制面板->所有控制面板项目->系统->高级系统设置->环境变量,上面是用户环境变量,下面是系统环境变量。

注意:还需要把%userprofile%/.vagrant.d/路径下的所有文件移动到前面环境变量设置的路径X:/your/path中。

修改虚拟机文件的默认生成路径

VirtualBox,管理->全局设定(Ctrl+G),修改默认虚拟电脑位置

PuTTY免密码

这里顺便把之前没完成的PuTTY的自动登录来完成了。当然先开Vagrant是免不了的。
注意:每次刚打开PAGEANT时,都需要PAGEANT->Add Key(如果设置了passphrase,还需要输入passphrase)。如果觉得麻烦还是skip吧。

  1. 首先打开PuTTYGEN,点击Load,在
    Vagrant安装目录的子目录.vagrant\machines\default\virtualbox找到private_key(注意要手动选择文件类型,All Files(*.*)),打开。
  2. 可以修改Key comment来作为这个Key的描述,键入Key passphraseComfirm passphrase作为这个Key文件的加密密钥(如果设置了passphrase,后面用PAGEANT时会需要用到,文件夹下的.chm文档是说建议用随机字符串作为这个密钥,但是一定要自己另外记下来),然后点击Save private key,生成扩展名为.ppk的私钥,自己取名。公钥存不存都无所谓(暂时用不到),因为这个生成的私钥里已经包含了,如有需要,直接在PuTTYGEN再load->Save public key就好了。
  3. 打开PAGEANT,在任务栏图标上找到一个戴帽子的电脑的图标,右键->View Keys->Add Key,选择刚才生成的.ppk私钥文件
  4. 以后要打开虚拟机,直接找到PAGEANT,右键->Saved Sessions,选择一个虚拟机。需要关闭PAGEANT,直接右键->Exit

这样一来,之后只要PAGEANT没Exit,登录虚拟机时就只需要输入用户名不需要输入密码了。

Vagrant自动生成的登录密钥文件通过命令行vagrant ssh-config输出的IdentityFile字段可以看到。

参考资料

How to fallback HTTPS to HTTP on GitHub Pages

After GitHub supports HTTPS in June, thousands of bloggers cheer. But what can I do, when I still need HTTP sometimes, specially when I cannot change the server API. Yes, I am.

So, how to fallback HTTPS to HTTP on GitHub Pages?

Obviously, I’d tried.

Try

  1. find a old repo, create a new branch as a placeholder

    1
    2
    cd hello_world
    git checkout -b 'todelete'
  2. delete all local files, add/commit then push data onto the remote repo

    1
    2
    3
    git add .
    git commit -m 'to delete repo'
    git push origin todelete
  3. switch to the ‘todelete’ branch on GitHub, and delete ‘master’ branch on my GitHub repo, change the repo name from ‘hello_world’ to ‘new_repo’

  4. copy the repo URL, and add it to the remote origin of the new local repo

    1
    2
    3
    4
    5
    cd ../new_repo
    git remote remove origin
    git remote add origin https://github.com/uolcano/new_repo.git
    git checkout -b 'gh-pages'
    git push origin gh-pages
  5. switch to gh-pages branch on GitHub, and delete the ‘todelete’ branch on GitHub

  6. done, but when I access to the repo site, it still is a site with HTTPs.

Maybe the new created ‘gh-pages’ branch still works with HTTPS.

Other way

Perhaps, I can use the created ‘gh-pages’ branch in the past.

So, that is simple. Find a old repo on GitHub and local, add the new repo files into the local directory of the old local repo.

1
2
3
4
cd ../old_repo
git add .
git commit -m 'add a new repo site'
git push origin gh-pages

[√] That’s done.

Conclusion

You can not fallback HTTPS to HTTP after HTTPS is enforced. But you can deploy the new site under a old GitHub Pages deployed before June 16 2016.

Install Android SDK On Ubuntu14.04 By CLI

Installing Steps

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# install jdk
sudo apt-get install openjdk-7-jdk
# or install jdk by default
sudo apt-get install default-jdk
# download android sdk
wget http://dl.google.com/android/android-sdk_r24.2.1-linux.tgz
# or download from https://developer.android.com/studio/index.html
# then unzip
tar -xvf android-sdk_r24.2.1-linux.tgz
# chang directory to sdk/tools
cd android-sdk-linux/tools
# install all sdk packages
./android update sdk --no-ui
# set path. skip to next step if not work.
vi ~/.zshrc << EOT
export PATH=${PATH}:$HOME/sdk/android-sdk-linux/platform-tools:$HOME/sdk/android-sdk-linux/tools:$HOME/sdk/android-sdk-linux/build-tools/22.0.1/
EOT
# install 32-bit libraries on 64-bit Ubuntu
sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0 lib32stdc++6

Ref

Install Java On Ubuntu By CommandLine

Installing JRE/JDK

1
2
3
4
sudo apt-get update  # update the package index
java -version # check if java installed
sudo apt-get install default-jre # install JRE
sudo apt-get install default-jdk # install JDK, if necessary

Setting the “JAVA_HOME” environment variable

  1. find out the path of JAVA installation

    1
    sudo update-alternatives --config java

    It perhaps returns:

    1
    There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

    Then, the path is “/usr/lib/jvm/java-7-openjdk-amd64”, hold it.

  2. use nano to edit the file /etc/environment

    1
    sudo nano /etc/environment

    In this file, append the path, holden just now, into the quotes, seperate by colon, like this:

    1
    JAVA_HOME="your/other/paths:/usr/lib/jvm/java-7-openjdk-amd64"
  3. reload the file /etc/environment

    1
    source /etc/environment
  4. check if java installed

    1
    java -version

Finally, it maybe not have to set the environment variable. But I don’t test that.

Reference

在Windows下,利用chocolatey快速安装jekyll(无bundler)

安装步骤

  1. 安装chocolatey。管理员权限的cmd下,执行如下命令:

    1
    @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
  2. 安装ruby。以管理员权限重启cmd,执行choco install ruby -y,自动安装ruby2.2.4。

  3. 安装jekyll。管理员权限重启cmd,执行gem install jekyll

至此,可以使用jekyll命令行:

1
2
3
jekyll new myblog
cd myblog
jekyll serve

最后,据GitHub Help的指南,通过以下命令可以在不安装bundler的情况下直接将本地页面部署到github上(尚未测试可行性

1
gem update github-pages

参考资料

在Windows下,利用Hexo+GitHub Pages快速搭建个人站点

安装NodeJs

nodejs.org下载以.msi为扩展名的安装包,如果不需要进行NodeJs相关开发的话选择LTS版本就够了,双击安装

安装GitHub Desktop

直接下载GitHub Desketop,安装并登录即可

安装hexo

1
npm install -g hexo-cli

至此,即可使用hexo搭建站点了

搭建默认页面

如果不是要将你的username.github.io整个页面搭建为博客站点建议不要以username.github.io这个repo作为站点部署的目标。如果只是要搭建博客站,可以直接在GitHub上新建一个叫blog的repo,后面的_config.yml配置的branch对应gh-pages,以后部署站点直接通过“hexo deploy”就会自动将本地内容push到GitHub上去。

  1. 打开Git Shell,依次输入以下命令:

    1
    2
    3
    hexo init blog # blog为博客目录,可以自定义;这条命令是用来使hexo自动添加站点需要的文件
    cd blog # 切换到刚才创建并初始化的存放博客文件的站点目录
    npm install # 安装包依赖
  2. 至此,已经可以通过以下命令来搭建本地服务器,在浏览器中输入 http://localhost:4000/ 查看测试页面了

    1
    hexo server

    PS:添加-p加端口号作为参数,可以自定义端口,更多命令可查看hexo文档的commands部分

  3. 在上一步创建的blog目录下,找到_config.yml文件,用文本编辑器打开(建议用notedpad++等编辑器,而不是系统自带的notedpad),做如下修改:

    1
    2
    3
    4
    deploy:
    type: git
    repo: https://github.com/uolcano/blog.git
    branch: gh-pages

    PS:这里的repo关键字对应站点部署在GitHub上的目标repo的git地址;branch关键字对应的分支分两种情况:

    1. 就是上面示例的样子,我是要将另一个叫blog的仓库作为我的博客站,所以只需要当作项目demo站点。而内容push全部交给hexo,hexo根据这个_config.yml文件的repobranch字段就会知道如何push到GitHub上去了。
    2. 如果你是要将站点直接部署在username.github.io这个repo上,则修改为如下形式:
      1
      2
      repo: https://github.com/uolcano/uolcano.github.io.git
      branch: master

    我刚开始是对uolcano.github.io直接部署的,结果导致整个repo都被覆盖了,然后就只能delete repo再创建并且把本地的repo数据push到新建的uolcano.github.io,才保证了原有的内容。

  4. 一般在部署之前,都需要安装hexo-deployer-git,而后才能部署成功

    1
    2
    npm install hexo-deployer-git --save
    hexo deploy

    提示如下信息即表示部署成功,可以通过 http://uolcano.github.io/blog/ 来访问我搭建的个人博客:

    1
    INFO Deploty done: git
  5. 以后每次发文只需要进行如下操作:

    1
    2
    3
    hexo new "文章标题名"
    hexo generate
    hexo deploy # 后面这两条命令可以简写为 hexo g -d 或者 hexo d -g

    PS:因为在_config.yml中的default_layout字段默认是post的,即表示你默认是以post的方式发文的,所以新建的文章文件,存放在blog/source/_posts目录下。而hexo有多种发布方式:“_posts”对应post这种发布方式,其他的还有draftpage

常用命令

1
2
3
4
5
6
7
8
hexo init <folder> # 创建并初始化一个站点文件夹
hexo clean # 用于主题切换等涉及到站点整体布局效果改变时清除hexo原有缓存
hexo new 'article title' # 新增一篇文章
hexo generate # 可以简写为 hexo g ,为文章自动生成静态页面的文件
hexo server # 可简写为 hexo s ,开启本地服务器,测试静态页面的效果
hexo deploy # 可简写为 hexo d ,将本地页面部署到GitHub Pages上
hexo generate -d # 生成静态页面的文件并直接部署
hexo deploy -g # 跟上一条命令相同作用

更多的信息建议去查hexo文档和关注hexo的GitHub issues

bonus

如果是需要给你的站点安装NexT主题可以在这里得到帮助。

参考资料

在Windows下,利用GitHub Pages搭建个人站点

搭建项目的demo页面

  1. 将项目的展示页面文件放在repo的根目录下,并且命名为index.html,必须的.html为扩展名。

  2. 然后打开git shell,执行以下命令:

    1
    git checkout -b gh-pages

PS:这里创建了一个名叫gh-pages的分支,因为github pages默认是为这个分支的创建页面的

[√]done!自此即可通过 http://userName.github.io/repoName 访问你的项目demo页面了。

搭建个人博客

你可以选择GitHub网页版傻瓜式地搭建,但是文字编辑和本地测试肯定没有本地的方便,所以一般都是通过jekyll(github官方推荐的,适合Mac和Linux,Windows下会很麻烦)或者hexo(比较容易搭建)来自动生成静态页面,而博客作者只需要编辑MarkDown文本,以及少数的几条命令行即可将自己的文章快速部署到GitHub Pages上去。

利用jekyll搭建

方法一:Windows下通过rubygems安装jekyll

建议在对自己的网络状况有信心的情况下采用。我是顺利安装完rubygems后,bundle install出问题,后面的内容有参考其他指引性文章,只能转而用vagrant虚拟机装了ubuntu系统后配置的jekyll。觉得这个方法不靠谱的可以skip到方法三或者改用hexo

  1. http://rubyinstaller.org/downloads/ 下载rubyinstaller.exe和DevKit,两个要对应版本,且对应操作系统

  2. 执行rubyinstaller.exe,安装ruby

  3. 将DevKit执行,并解到一个指定命名文件夹,如:RubyDevKit;管理员权限打开cmd执行ruby dk.rb init;执行dk.rb install

  4. https://rubygems.org/pages/download 下载RubyGems,解压缩出来的文件夹,如:rubygems;管理员权限重新打开cmd,执行cd rubygems,执行ruby setup.rb

  5. 修改rubygems的源为淘宝源:
    gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
    淘宝的源不行的话就用ruby-china的源 https://gems.ruby-china.org/

  6. 如果出现证书失败,就去下载一个curl,放在一个目录下,cmd执行curl http://curl.haxx.se/ca/cacert.pem -o cacert.pem
    将下载的cacert.pem移到ruby的安装目录下的bin文件夹里,并且在环境变量中(应该是系统变量)新建SSL_CERT_FILE,值为移动后的cacert.pem的绝对路径

  7. 安装bundler

    1
    gem install bundler
  8. 在创建好的作为博客的repo下,新建一个文件,命名为Gemfile,里面的内容是:

    1
    2
    source 'https://ruby.taobao.org'
    gem 'github-pages', group: :jekyll_plugins

    国内使用淘宝的源 https://ruby.taobao.org 比较快,故未使用 https://rubygems.org 。当然国内的用 https://gems.ruby-china.org 也是一个选择。
    要注意,这里要保证在本地已经安装并登录了GitHub,可以直接下载GitHub Desketop安装登陆即可。

  9. 安装jekyll和相关依赖

    1
    bundle install

    安装完成后,会提示Thank you for installing github-pages!

  10. 将本地页面部署到github上去,执行bundle update或者bundle update github-pages

  11. 在Ruby\lib\ruby\gems类似这样的目录下找到对应版本下的gems目录,找到jekyll目录的lib\site_template,这里有个_config.yml文件,用文本编辑器(建议用notepad++等,而不是系统自带的notedpad)打开,在# Build settings字段下添加:

    1
    2
    markdown: kramdown
    highlighter: rouge

    高亮需要安装rouge

    1
    gem install rouge

    另外,如果要以pygments作为高亮风格,还需要安装python,以及pip,转到get-pip.py的下载目录执行:

    1
    python get-pip.py

    安装pygments

    1
    python -m pip install Pygments

    然后在_config.yml文件中,设置为highlighter: pygments

    jekyll的高亮使用语法如下:

    1
    2
    3
    {% highlight bash %}
    $ gem install bundler
    {% endhighlight %}

方法二:Windows下通过chocolatey快速安装jekyll

方法三:Windows下通过vagrant虚拟Linux系统安装jekyll

利用hexo搭建

参考资料

Build Site on GitHub Pages by jekyll under Linux

前序安装

  1. 安装ruby
  2. 安装rubygems
  3. 安装bundler
  4. 安装和配置git
  5. 安装nodejs

可以参考我的这篇文章Install rvm, ruby, rails, nvm, nodejs and git under Ubuntu 14.04

创建本地repo和分支

切换到用于存git repo的目录下,初始化一个用于jekyll搭建静态页面的repo

1
2
3
4
cd ./git
git init blog-by-jekyll # 本地创建并初始化一个repo
cd blog-by-jekyll # 切换到这个repo
git checkout -b gh-pages # 创建并切换到gh-pages分支,用于默认在github自动搭建页面

配置Gemfile

在这个repo下,使用vim Gemfile命令,创建一个叫做Gemfile的文件,注意无扩展名,输入一下内容

1
2
source 'https://ruby.taobao.org'
gem 'github-pages', group: :jekyll_plugins

国内使用淘宝的源https://ruby.taobao.org比较快,故未使用https://rubygems.org。当然国内的用https://gems.ruby-china.org也是一个选择。

安装jekyll和其他依赖

1
bundle install

安装完成后,会提示Thank you for installing github-pages!blablablabla~

生成jekyll静态页面的模版

1
bundle exec jekyll new . --force

开启静态站的本地http服务

用于在本地查看站点效果

1
bundle exec jekyll serve

在GitHub创建repo并上传内容

在GitHub的页面New repository,只需要给repo取名为本地git下的repo名同名,如:blog-by-jekyll,其他都不选,直接Create repository。回到终端,以此输入一下命令:

1
2
3
4
git remote add origin git@github.com:uolcano/blog-by-jekyll.git
git add .
git commit -m 'first blog by jekyll'
git push origin gh-pages

由于在Linux下的git是通过生成ssh密钥来与GitHub账号关联的,所以需要用SSH来push。什么,你说SSH协议的git地址在哪拷贝?就在repo主页的HTTPS选项的旁边或者通过下拉菜单选择

将本地页面部署到GitHub

1
bundle update github-pages

可以简写为bundle update

最后,通过 http://uolcano.github.io/blog-by-jekyll/ 就可以访问jekyll自动生成的页面了。

参考资料

一步步在GitHub上创建博客主页-最新版
Setting up your GitHub Pages site locally with Jekyll
Configuring Jekyll

Install rvm, ruby, rails, nvm, nodejs and git under Ubuntu 14.04

  1. 下载证书

    1
    gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
  2. 安装稳定版rvm以及最新版的ruby

    1
    curl -L https://get.rvm.io | bash -s stable --autolibs=enabled --ruby
  3. 安装依赖,初始化并立即执行

    1
    2
    sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
    source ~/.rvm/scripts/rvm
  4. 修改rubygems的源为淘宝ruby源,并查看确认

    1
    2
    gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
    gem sources -l

    PS:淘宝源如果有问题fetch error的话,可以改用 https://gems.ruby-china.org/ 替换。

  5. 以sudo安装rubygems-update

    1
    2
    3
    gem install rubygems-update
    update_rubygems
    gem update --system
  6. 安装bundler

    1
    gem install bundler
  7. 安装rails

    1
    gem install rails
  8. 安装nvm

    1
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

    安装最新版nodejs

    1
    nvm install 5.0
  9. 安装及配置git

    1
    sudo apt-get install git

    配置git基本信息,并生成ssh公钥

    1
    2
    3
    4
    5
    git config --global color.ui true
    git config --global user.name "YOUR NAME"
    git config --global user.email "YOUR@EMAIL.com"
    git config -l #查看配置信息
    ssh-keygen -t rsa -C "YOUR@EMAIL.com" # 一直敲回车

    查看生成的公钥,并复制之

    1
    cat ~/.ssh/id_rsa.pub # id_rsa 应该是ubuntu系统分配的,只能用这个文件名

    到github settings->SSH and GPG keys->SSH keys->new SSH key,给SSH key写个名字作为Title,在key下粘贴从id_rsa.pub里复制公钥。然后Add SSH key即可。

    将本地git与github账号建立连接

    1
    ssh -T git@github.com

    显示一下信息,即表示连接成功
    Hi YOUR NAME! You've successfully authenticated, but GitHub does not provide shell access.

参考链接