Skip to content

Instantly share code, notes, and snippets.

View ktutnik's full-sized avatar

Ketut Sandiarsa ktutnik

  • VetVision
  • Indonesia
View GitHub Profile
***Requirements:***
install TSD
editor atom
express-generator
> express <project name>
> touch app.ts
https://goo.gl/vYexMR
export class ProductController{
async getByPage(offset:number, pageWidth = 50){
return await factory.getProducts(offset, pageWidth);
}
async get(id:number){
return await factory.getProductById(id);
}
interface IManusia {
IPakaian Bawahan {get; set}
IBagianTubuh Kaki {get; set;}
IKemaluan Titit {get; set;}
}
interface ILakiLaki : IManusia{
default void Ngenceh(){
Bawahan.Find("Resleting").Buka()
Kaki.Ngangkang()
import { ApiController, Core, val, HttpStatusError, interceptor } from "kamboja"
import { ItemIdCheckInterceptor } from "../interceptor/item-idcheck-interceptor"
import { MongooseHelper } from "kamboja-mongoose"
import { ItemModel } from "../model/item-model"
import { ItemOdm } from "../model/odm"
export class ItemsController extends ApiController {
get( @val.required() id: string) {
@ktutnik
ktutnik / simple-route.js
Created May 10, 2017 09:24
Route generator sederhana
class StrategyBase {
next() {
return { type: "next" };
}
exit(result) {
return { type: "exit", result: result };
}
}
class DefaultStrategy extends StrategyBase {
transform(meta) {
versi C#
class Animal {
speak(){}
}
class Bird : Animal {
fly(){}
}
@ktutnik
ktutnik / cache.ts
Created March 27, 2019 06:30
TypeScript HOF cache
type Fun<A extends any[], B> = (...args:A) => B
type Cache<T> = {[key:string]: T}
function cache<P extends any[], R>(cache: Cache<R>, fn: Fun<P, R>, getKey: Fun<P, string>) {
return (...args: P) => {
const key = getKey(...args)
const saved = cache[key]
if (!!saved) return saved
else return cache[key] = fn(...args)
}
@ktutnik
ktutnik / hof-cache.ts
Last active April 3, 2019 02:29
Higher order cache
function useCache<K, P extends any[], R>(cache: Map<K, R>, fn: (...args: P) => R, getKey: (...args: P) => K) {
return (...args: P) => {
const key = getKey(...args)
const result = cache.get(key)
if(!!result) return result
else {
const newResult = fn(...args)
cache.set(key, newResult)
return newResult
}
@ktutnik
ktutnik / express-types.ts
Last active April 29, 2019 05:06
Express typescript example
app.use(async (req, res) => {
const user:UserInfo = res.body
//other process use the user data
})
@ktutnik
ktutnik / animal.ts
Last active May 1, 2019 00:42
TypeScript decorator example
//decorator that does noting
function noop(){
return (...args:any[]) => {}
}
//target
class AnimalClass {
@noop()
list(index:number, limit:number): any[] {
return []