Skip to content

Instantly share code, notes, and snippets.

View arifvn's full-sized avatar
🥦
Focusing

arifvn arifvn

🥦
Focusing
View GitHub Profile
@arifvn
arifvn / server.js
Last active November 21, 2021 04:05
Sample nodejs server
require("colors");
const express = require("express");
const logger = require("morgan");
const helmet = require("helmet");
const xss = require("xss-clean");
const hpp = require("hpp");
const mongoSanitize = require("express-mongo-sanitize");
const rateLimit = require("express-rate-limit");
const cors = require("cors");
const morgan = require("morgan");
@arifvn
arifvn / axios read and upload file.js
Last active November 21, 2021 03:58
Sample read and upload file using axios
var axios = require('axios');
var fs = require('fs');
var path = require('path')
var util = require('util')
let readfile = util.promisify(fs.readFile)
async function sendData(url,data) {
let params = data
let resp = await axios({
// declaration
inline fun inlineFunction(callback: () -> User) {
println("before callback")
val result = callback()
println("after callback")
}
// Illegal usage of inline-parameter 'callback' in '...'.
@arifvn
arifvn / inline.kt
Last active November 15, 2020 20:10
// declaration
inline fun inlineFunction(callback: () -> Unit) {
println("before callback")
callback()
println("after callback")
}
// calling function
btnPrintMessage.setOnClickListener(){
inlineFunction(){
btnPrintMessage.setOnClickListener {
nonInline { println("Noninline function has been called") }
}
// in java code | simplified version
btnPrintMessage.setOnClickListener(){
@override
void onClick(View v){
noninline(new Function {
println("Noninline function has been called")
nonInline {
println("Noninline function has been called")
}
// in java code | simplified version
nonInline(new Function() {
@Override
public void invoke() {
System.out.println("Noninline function has been called");
}
fun nonInline(callback: () -> Unit) {
println("before callback")
callback()
println("after callback")
}
// in java code
public void nonInline(Function callback) {
System.out.println("before callback");
callback.invoke();
class MainActivity : AppCompatActivity() {
private val tvUsername: TextView by lazy { findViewById(R.id.tv_username) }
private var text: String by Delegates.observable("Initial Value") { property, oldvalue, newValue ->
tvUsername.text = newValue
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
class MainActivity: AppCompatActivity {
val buttonLogin by id(R.id.btn_login)
// other stuff
// ..
// ..
fun AppCompatActivity.id(viewId: Int) = ViewDelegate(viewId)
}
class ViewDelegate(
private val viewId: Int
): ReadOnlyProperty<AppCompatActivity, String> {
private var result: String? = null
override fun getValue(thisRef: AppCompatActivity, property: KProperty<*>): String =
result ?: thisRef.findViewById(viewId).also { result = it }
}