Skip to content

Instantly share code, notes, and snippets.

@ldarren
ldarren / api.js
Created April 28, 2020 03:52
MWM Branching Model
const mwm = require('pico-mw-mgr')
mwm(
'forAdmin',
[user.updateForAdmin, 'user', 'input'],
[merchant.updateForAdmin, 'user', 'input', 'output'],
[output, 'ctx', 'output', null]
)
mwm(
@ldarren
ldarren / iframe_preview.html
Last active November 21, 2019 22:26
local realtime web preview with iframe
<textarea id=html><p>Hello</p></textarea>
<textarea id=css>p{color:red}</textarea>
<textarea id=js>document.addEventListener('click', console.log)</textarea>
<iframe src="./preview.html" sandbox="allow-forms allow-modals allow-pointer-lock allow-popups allow-same-origin allow-scripts" allowfullscreen="true" allowtransparency="true" allowpaymentrequest="true"></iframe>
<button id=refresh>refresh</button>
<script src=./main.js></script>
@ldarren
ldarren / construct_rest.cpp
Last active November 10, 2019 07:13
construct rest API path
/*
* construct an url path from a template and a dictionary
* example 1:
* Input: "https://api.com/users/:groupId/:userId" and {"userId": 12, "groupId: 1}
* Output: "https://api.com/users/1/12"
*
* example 2:
* Input: "https://api.com/parcel/:merchantId/:productId" and {"merchantId": "BLU-123", "productId": "LG-NOTE-637"}
* Output: "https://api.com/parcel/BLU-123/LG-NOTE-637"
*/
@ldarren
ldarren / workinghour.js
Last active July 11, 2022 17:03
Three Working Hours
/**
#Objective:
write a function to calculate the expiry datetime. The expiry datetime is 3 WORKING hours from the "now" input parameter.
the working hours is defined in the "schedule" input parameter.
You should write the function in any programming language.
@param {Date} now - current datetime. e.g: '2019-10-11T08:13:07+0800'
@param {Array} schedule - an array of objects. which specified the day open or close and also the start and end of working hours
[
{"open": false, "open_at": "", close_at: ""}, // sunday
@ldarren
ldarren / question.js
Last active October 16, 2019 10:17
nodejs performance test question
const _ = require('lodash');
const model = require('./model');
const {InvErrors} = require('./errors');
const constants = require('./const');
/* table schemas
properties {
id: bigint,
metadata: json
}
const Koa = require('koa');
const app = module.exports = new Koa();
// look ma, error propagation!
app.use(async function(ctx, next) {
try {
await next();
} catch (err) {
// some errors will have .status
@ldarren
ldarren / lazada_request.js
Created September 8, 2019 16:46
minimalist lazada request
const crypto = require('crypto')
const util = require('picos-util')
const {app_key, app_secret, access_token} = require('./app.json')
const endpoints = {
vn: 'https://api.lazada.vn/rest',
sg: 'https://api.lazada.sg/rest',
th: 'https://api.lazada.co.th/rest',
id: 'https://api.lazada.co.id/rest',
ph: 'https://api.lazada.com.ph/rest',
my: 'https://api.lazada.com.my/rest',
<script src="dist/amazon-cognito-identity.min.js"></script>
<script src="dist/aws-sdk.min.js"></script>
<script>
var authenticationData = {
Username : 'test',
Password : 'password',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId : 'ap-southeast-1_xxxxxxx',
@ldarren
ldarren / pojs_demo.html
Last active November 21, 2019 22:28
pojs demo
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8>
<meta name=description content="POJS Unit Test">
<meta name=viewport content=width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1>
<meta name=app-version content=0.0.1>
<meta name=app-support-native content=1>
<link rel="shortcut icon" href=data:;base64,iVBORw0KGgo=>
<title>POJS: Demo</title>
@ldarren
ldarren / empty-then.js
Created November 17, 2017 10:26
DB Connection
const Rand = Math.random
const Ceil = Math.ceil
const knex = require('knex-pg-rr')({
client: 'pg',
connection:{
host: 'PG_BOUNCER_HOST',
port: xxxx,
user:"USER_NAME",
password:"PASSWORD",
database: "DB_NAME"