Skip to content

Instantly share code, notes, and snippets.

View mostafa-hz's full-sized avatar

Mustafa mostafa-hz

View GitHub Profile
@mostafa-hz
mostafa-hz / run-for-at-least.ts
Last active April 15, 2022 12:28
This function execute the another function and return the result or throw the error in the least given time, one of the use case is to protect agains timing-attack
export async function runForAtLeast<T>(fn: (...args: any[]) => Promise<T>, ms?: number, ...args: any[]): Promise<T> {
const [res] = await Promise.allSettled([
fn(...args),
new Promise(resolve => setTimeout(resolve, ms)),
]);
switch (res.status) {
case 'fulfilled':
return res.value;
case 'rejected':
throw res.reason;
@mostafa-hz
mostafa-hz / generate-pagination-query.js
Last active May 27, 2023 18:57
A function to generate keyset paginated queries for mongodb
function generatePaginationQuery(query, sort, nextKey) {
const sortField = sort == null ? null : sort[0];
function nextKeyFn(items) {
if (items.length === 0) {
return null;
}
const item = items[items.length - 1];
@mostafa-hz
mostafa-hz / generate-pagination-query.py
Last active October 8, 2020 22:14
A function to generate keyset paginated queries for mongodb
def generate_pagination_query(query, sort=None, next_key=None):
sort_field = None if sort is None else sort[0]
def next_key_fn(items):
if len(items) == 0:
return None
item = items[-1]
if sort_field is None:
return {'_id': item['_id']}
else:
@mostafa-hz
mostafa-hz / ShrinkExtendedFloatingActionButtonOnScrollBehavior.kt
Created January 4, 2020 12:08
A Simple Coordinator Layout Behavior To Handle Shrinking And Extending ExtendedFloatingActionButton
package com.mosius
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
class ShrinkExtendedFloatingActionButtonOnScrollBehavior : CoordinatorLayout.Behavior<ExtendedFloatingActionButton> {
class FragmentPagerAdapterSample(manager: FragmentManager) : FragmentPagerAdapter(manager) {
private val data = ArrayList<Any>()
fun setData(data: List<Any>) {
this.data.clear()
this.data.addAll(data)
notifyDataSetChanged()
}
@mostafa-hz
mostafa-hz / VolleyMultipartRequest.kt
Last active June 29, 2022 05:08
Android Kotlin Volley MultiPart Request
package com.example
import com.android.volley.*
import com.android.volley.toolbox.HttpHeaderParser
import org.json.JSONException
import org.json.JSONObject
import java.io.*
import java.nio.charset.Charset
open class VolleyMultipartRequest(