Skip to content

Instantly share code, notes, and snippets.

View haohcraft's full-sized avatar

Hao haohcraft

View GitHub Profile
object MyDateTypeAdapter : JsonDeserializer<Date>, JsonSerializer<Date> {
private val DATE_FORMATS = arrayOf("yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd")
private val dateFormatters: List<SimpleDateFormat> = DATE_FORMATS.map { SimpleDateFormat(it, Locale.US) }
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): Date {
for (formatter in dateFormatters) {
try {
return formatter.parse(json?.asString)
} catch (ignore: ParseException) {
}
@DisplayName("Given a value with two data format, When use setDateFormat on GsonBuilder and a simpleDateFormat, Then return as expected")
@Test
fun testSetDateFormatWithSimpleDateFormat() {
val gson = GsonBuilder().apply {
setDateFormat("yyyy-MM-dd HH:mm:ss")
}.create()
val dateFormatter = SimpleDateFormat(targetFormat, Locale.US)
{
"expire_date": "2020-01-23 21:52:43",
"start_date": "2020-03-23"
}
@haohcraft
haohcraft / rAF.js
Created December 21, 2017 20:37 — forked from paulirish/rAF.js
requestAnimationFrame polyfill
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];

The Benefits of Firing Small Actions

Background

In a Redux app, when updating State we would dispatch an action and then the related Reducer would compute a new version of State based on the actionType passed in.

We would come to a point where we need to update multiple part of State by a function, like within an onClickHandler. In this situation, there are at least two ways to accomplish that.

  • Create a single action to dispatch in the onClickHanlder, and the Reducer would be responsible for implement to logic of updating those different parts of the State
@haohcraft
haohcraft / animation_site.md
Created September 27, 2016 15:01
Cool website which has tons of animation
/*
Partial Application
*/
/*
Combine functions

To kill port

lsof -n -i4TCP:7000 | grep LISTEN | tr -s ' ' | cut -f 2 -d ' ' | xargs kill -9

@haohcraft
haohcraft / railway_oriented_programming.js
Created June 6, 2016 20:24 — forked from volodymyrprokopyuk/railway_oriented_programming.js
Railway Oriented Programming (JavaScript)
var _ = require('lodash');
var Success = function(success) { this.success = success; };
var Failure = function(failure) { this.failure = failure; };
var bindAll = function(fs) {
var bind = function(res, f) {
return res instanceof Success ? f(res.success) : res;
};
var bindF = function(f) { return _.partial(bind, _, f); };
@haohcraft
haohcraft / pr.md
Created April 28, 2016 15:06 — forked from piscisaureus/pr.md
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this: