Skip to content

Instantly share code, notes, and snippets.

View alextanhongpin's full-sized avatar
😹
Focusing

Alex Tan Hong Pin alextanhongpin

😹
Focusing
View GitHub Profile
@alextanhongpin
alextanhongpin / main.go
Last active July 10, 2017 15:24
Bulk indexing for elasticsearch (throttled). Limit to index 1000 documents per iterations.
package main
import (
"context"
"fmt"
"strconv"
elastic "gopkg.in/olivere/elastic.v5"
)
@alextanhongpin
alextanhongpin / main.py
Created July 9, 2017 08:35
Sample reading from css and writing to json file python
import re
import json
country_codes = []
with open('./flags.css', 'r') as f:
lines = [x for x in f.readlines()]
for _, line in enumerate(lines):
out = re.match(r'\.flag\.flag-(.*)\s\{', line)
if out:
@alextanhongpin
alextanhongpin / time.go
Last active January 13, 2025 02:16
JavaScript timestamp to golang time.Time
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
)
@alextanhongpin
alextanhongpin / graceful-shutdown.go
Created June 27, 2017 07:21
Shutting down server gracefully in golang (TODO: Cleanup)
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
)
@alextanhongpin
alextanhongpin / concatenate.js
Last active July 1, 2017 06:47
Things to avoid in js
// Concatenating strings directly
const firstName = 'john'
const lastName = 'doe'
// Bad, fullName(null, null) will return "null null"
function fullName (firstName, lastName) {
return firstName + ' ' + lastName
}

Arrow function is faster than class or function:

class Foo {
  doSomething() {}
}

// Fastest
const Foo = () => {
 // do something
@alextanhongpin
alextanhongpin / index.js
Created June 6, 2017 16:47
Github API Queries
// sample queries from github.api for specific requests
const searchUsersInMalaysia = 'https://api.github.com/search/users?q=location:malaysia'
@alextanhongpin
alextanhongpin / factory.js
Created June 4, 2017 06:56
javascript patterns
const DBFactory = (name) => {
switch (name) {
case 'mongodb': return MongoDB()
case 'rethinkdb': return RethinkDB()
case 'redis': return Redis()
// For testing/mocking
case 'inmemory': return InMemory()
}
}
@alextanhongpin
alextanhongpin / conversion.go
Created June 4, 2017 03:39
Type conversion in Go v1.8
package main
import (
"encoding/json"
"fmt"
)
// This program demonstrates a simple conversion from one type to another
// Available from go1.8 onwards
@alextanhongpin
alextanhongpin / concurrency.go
Last active May 30, 2017 02:40
Compare concurrency between nodejs and golang
package main
import (
// "fmt"
"log"
"time"
)
func delay100() bool {
time.Sleep(1 * time.Second)