Skip to content

Instantly share code, notes, and snippets.

View patarapolw's full-sized avatar

Pacharapol Withayasakpunt patarapolw

View GitHub Profile
@patarapolw
patarapolw / mongo-serialize.ts
Created February 17, 2020 13:49
MongoDB serialize
const cond = {
a: new Date(),
b: /regexp/gi
}
const r = JSON.stringify(cond, function (k, v) {
const v0 = this[k]
if (v0) {
if (v0 instanceof Date) {
return { $date: v0.toISOString() }
@patarapolw
patarapolw / qa-offline-first.md
Created November 7, 2019 12:33
What is the best offline-first database and asset storage for desktop apps?

What is the best offline-first database and asset storage for desktop apps?

The closest that I know, and open source too, is either Git or https://github.com/dsnopek/anki-sync-server

According to advertising, I am tempted to use Firestore, but

  • Serverless web app cannot store too many images in IndexedDB. (Nor it is efficient)

I also tried PouchDB / CouchDB, which can also store images as attachments, but I noticed that user account control is missing.

@patarapolw
patarapolw / mongo-api.md
Last active October 30, 2019 18:45
MongoDB API specification
@patarapolw
patarapolw / type-check.js
Last active August 23, 2019 14:58
JavaScript instance type checking.
function isSameType(a, b) {
const tA = typeof a;
const tB = typeof b;
if (tA !== "object") {
return tA === typeof b;
} else if (tB === "object") {
return a.constructor === b.constructor;
}
return tA === tB; // always false???
}
************* Module src.test.test_search
src/test/test_search.py:1:0: C0111: Missing module docstring (missing-docstring)
src/test/test_search.py:4:0: C0111: Missing function docstring (missing-docstring)
src/test/test_search.py:8:0: C0111: Missing function docstring (missing-docstring)
************* Module src.python.server
src/python/server.py:1:0: C0111: Missing module docstring (missing-docstring)
src/python/server.py:17:0: C0103: Constant name "app" doesn't conform to UPPER_CASE naming style (invalid-name)
src/python/server.py:18:0: C0103: Constant name "socketio" doesn't conform to UPPER_CASE naming style (invalid-name)
src/python/server.py:28:0: C0111: Missing function docstring (missing-docstring)
src/python/server.py:33:0: C0111: Missing function docstring (missing-docstring)
@patarapolw
patarapolw / mongoFilter.ts
Created April 8, 2019 14:47
convert mongo query to filter function
export function mongoFilter(x: any, cond: any): boolean {
return Object.keys(cond).every((k) => {
if (k === "$or") {
return (cond[k] as any[]).some((c0) => mongoFilter(x, c0));
} else if (k === "$and") {
return (cond[k] as any[]).every((c0) => mongoFilter(x, c0));
} else {
let dotIndex = k.indexOf(".");
while (dotIndex !== -1) {
x = x[k.slice(0, dotIndex)];
@patarapolw
patarapolw / myJSsort.ts
Created April 3, 2019 10:09
Javascript sort
a.sort((a, b) => {
function convert(x: any) {
return x[sortBy];
}
function compare() {
const m = convert(a) || -Infinity;
const n = convert(b) || -Infinity;
if (typeof m === "string" && typeof n === "string") {
return m.localeCompare(n);
@patarapolw
patarapolw / Google Sheets API for TypeScript.ts
Last active March 14, 2024 15:38
Google Sheets API for TypeScript
// yarn add googleapis@27 @types/node
import fs from "fs";
import readline from "readline";
import {google} from "googleapis";
import { OAuth2Client } from "google-auth-library";
const CRED_PATH = "secret/credentials.json";
const TOKEN_PATH = "secret/token.json";
const SCOPES = ["https://www.googleapis.com/auth/spreadsheets.readonly"];
import lark
parser = lark.Lark(r'''
?atom : atom or atom
| atom and atom
| "(" atom ")"
| compare
| value
compare : value ":" value
import ast
import re
class SearchBox:
ast_table = {
ast.Eq: ':',
ast.Is: '=',
ast.Or: 'or',
ast.And: 'and',