Skip to content

Instantly share code, notes, and snippets.

View iOliverNguyen's full-sized avatar
a new journey

Oliver N iOliverNguyen

a new journey
View GitHub Profile
@iOliverNguyen
iOliverNguyen / get.sh
Created November 29, 2018 09:36
get.sh is a tool for retrieving environment information for scripting with go modules
#!/usr/bin/env bash
this=$0
prog=$(basename $0)
current=$prog
quickusage="Run '$prog help' for usage."
quickusage() {
echo ""
echo "$current: unknown command"
echo "$quickusage"
@iOliverNguyen
iOliverNguyen / µjson.go
Last active February 27, 2019 08:11
Minimal JSON parser which works with correct input only
// Minimal JSON parser which works with correct input only.
// Usecase:
// 1. Walk through unstructured json
// 2. Transform unstructured json
// without fully unmarshalling it into a map[string]interface{}
//
// Caution: Behaviour is undefined on invalid json. Use on trusted input only.
package µjson
@iOliverNguyen
iOliverNguyen / 0_simplest.go
Last active January 9, 2021 04:25
ujson: examples
package main
import "fmt"
import "github.com/olvrng/ujson"
func main() {
input0 := []byte(`true`)
ujson.Walk(input0, func(level int, key, value []byte) bool {
fmt.Printf("level=%v key=%s value=%s\n", level, key, value)
return true
@iOliverNguyen
iOliverNguyen / proxy.go
Created January 20, 2021 10:15
A simple http proxy in Go
package main
import (
"fmt"
"io"
"net/http"
"time"
)
var client = http.Client{Timeout: 5 * time.Second}
// execute function(s) with a limit timeout, throws error if the function(s)
// do not complete within the given time
export function execWithTimeout(timeout, ...promises) {
let timeoutId;
const countDown = new Promise((_, reject) => {
timeoutId = setTimeout(reject, timeout, new Error('timed out'));
});
return Promise.race([countDown, ...promises]).then((result) => {
clearTimeout(timeoutId);
<!doctype html>
<html lang="en">
<head>
<title>My little app</title>
</head>
<body>
<script type="module" src="myscript.js"></script>
</body>
</html>
<script type="module" src="myscript.js"></script>
@iOliverNguyen
iOliverNguyen / regexp.go
Last active March 4, 2021 03:16
a function for batch processing regular expressions
func processRegex(
s string, rs []*regexp.Regexp,
fn func(string), funcs ...func(string, ...string),
) {
if len(rs) != len(funcs) {
panic("func and regex do not match")
}
for s != "" {
mi, minA, minB, mIdx := 0, len(s), len(s), []int(nil)
for i, re := range rs {
@iOliverNguyen
iOliverNguyen / go.json
Created March 20, 2021 10:02
List of golang/go files from GitHub API
[
".gitattributes",
".github",
".github/CODE_OF_CONDUCT.md",
".github/ISSUE_TEMPLATE",
".github/PULL_REQUEST_TEMPLATE",
".github/SUPPORT.md",
".gitignore",
"AUTHORS",
"CONTRIBUTING.md",
@iOliverNguyen
iOliverNguyen / findIntersection.js
Last active March 24, 2021 07:46
find intersection of N strictly sorted lists
// find intersection of 2 strictly sorted lists
function findIntersection(a: number[], b: number[]): number[] {
let ai = 0, bi = 0, result = [];
while (ai < a.length && bi < b.length) {
if (a[ai] < b[bi]) ai++;
else if (a[ai] > b[bi]) bi++;
else { // they are equal
result.push(a[ai]);
ai++;
bi++;