Skip to content

Instantly share code, notes, and snippets.

View arisris's full-sized avatar
👨‍🦱
Open to work

Aris Riswanto arisris

👨‍🦱
Open to work
View GitHub Profile
@arisris
arisris / post.md
Last active March 10, 2022 04:30
Simple State Management Using React Hooks, Inpired By Hyperapp

useState.js

import { createContext, useContext, useState, useMemo } from 'react';

export const createStore = (state, actions) => {
  const set = (obj) =>
    obj && typeof obj === 'object'
      ? ((state = { ...state, ...obj }), state)
      : state;
  const newActions = Object.keys(actions)
@arisris
arisris / post.md
Last active March 10, 2022 04:29
Detect detect file type in codemirror mode
const loadMode = (editor, filename: string) => {
    if (!editor) return;
    const CodeMirror = window.CodeMirror;
    // @ts-ignore
    window.CodeMirror.modeURL = scriptCfg.baseUrl + "mode/%N/%N.min.js";
    var val = filename,
      m: any[],
      mode: string,
      spec: string | CodeMirror.ModeSpec<CodeMirror.ModeSpecOptions>;
@arisris
arisris / post.md
Last active March 10, 2022 04:27
Experiment Using Deno deploy and Oak
import { Application, Router } from "https://deno.land/x/oak@v10.4.0/mod.ts";

const app = new Application();
const router = new Router();

// routes
router.get("/", ({ response }: { response: Response }) => {
    const data = `<h3>Hello Deno</h3>`;
 response.headers.set("content-type", "text/html");
@arisris
arisris / post.md
Created March 10, 2022 04:37
ucfirst(); lcfirst(); ucwords(); in typescript
const ucFirst = (str: string) =>
  str
    .split("")
    .map((v, i) => (i === 0 ? v.toUpperCase() : v))
    .join("");
const lcFirst = (str: string) =>
  str
    .split("")
 .map((v, i) =&gt; (i === 0 ? v.toLowerCase() : v))
@arisris
arisris / Component.kt
Last active August 26, 2022 23:32
Datastore Preferences Composable
@Composable
fun TestPrefs() {
val (count, isLoading, setCount) = rememberIntPreferences("counter", 0)
Column {
Text("Current Count: ${if (isLoading) "loading" else count}")
Button(onClick = { setCount(count+1) }) {
Text("Increment by 1")
}
Button(onClick = { setCount(count-1) }) {
@arisris
arisris / react_test.md
Created October 31, 2022 06:19
React SSR Suspense fallback & streaming suport : Example Code
// file: react_test.ts
// it just for my note
import React from "https://esm.sh/react@18.2.0";
import ReactDomServer from "https://esm.sh/react-dom@18.2.0/server";
import { Handler, serve } from "https://deno.land/std@0.161.0/http/server.ts";

const Module = {
  default: () =>
    React.createElement("div", {
@arisris
arisris / router.ts
Last active November 1, 2022 16:24
Simple Http Router Using JS Proxy Hack
export type TRouter<Value, Method extends string> =
& {
find: (method: Method, pathname: string) => TMatchedRoute<Value>;
}
& {
[k in Lowercase<Method>]: (
pathname: string,
...values: Value[]
) => TRouter<Value, Method>;
};
@arisris
arisris / worker-router.ts
Created November 25, 2022 03:13
Simple Worker Router
interface ICtx {
readonly req: Request;
readonly params: Record<string, any>;
readonly env: Record<string, any>;
readonly executionContext: Record<string, any>;
}
type TExecResult = {
groups: Record<string, string>;
};
type THandler = (
@arisris
arisris / _seeder.ts
Last active January 2, 2023 15:11
Kysely & Deno postgres
import { hash } from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
import { Kysely } from "https://esm.sh/kysely@0.22.0";
export async function seed(db: Kysely<unknown>) {
const userAdmin = await db.selectFrom("users" as never).where(
"email",
"=",
"admin@example.net" as never,
).executeTakeFirst();
if (!userAdmin) {
@arisris
arisris / pgproxy.ts
Created January 5, 2023 15:12
Deno deploy Postgres proxy
import "https://deno.land/std@0.170.0/dotenv/load.ts";
import { parse } from "https://deno.land/std@0.170.0/flags/mod.ts";
import { serve, Status } from "https://deno.land/std@0.170.0/http/mod.ts";
import { Pool, PoolClient } from "https://deno.land/x/postgres@v0.17.0/mod.ts";
const args = parse(Deno.args, {
string: ["dsn", "port", "key"],
alias: { "d": "dsn", "p": "port", "k": "key" },
default: {
dsn: Deno.env.get("PG_DSN"),