Skip to content

Instantly share code, notes, and snippets.

View sufian07's full-sized avatar
🎉
Focusing

Mohammad Abu Sufian sufian07

🎉
Focusing
View GitHub Profile
@sufian07
sufian07 / await_reduce.js
Created July 31, 2024 02:28
promise safe way to iterate array in savascript
async function printFiles () {
const files = await getFilePaths();
await files.reduce(async (promise, file) => {
// This line will wait for the last async function to finish.
// The first iteration uses an already resolved Promise
// so, it will immediately continue.
await promise;
const contents = await fs.readFile(file, 'utf8');
console.log(contents);
@sufian07
sufian07 / pdf.js
Created October 15, 2021 04:06
combine two pdf file
const { PDFDocument } = require('pdf-lib');
const fs = require('fs');
run().catch(err => console.log(err));
async function run() {
// Load cover and content pdfs
const content = await PDFDocument.load(fs.readFileSync('./p.pdf'));
const sign = await PDFDocument.load(fs.readFileSync('./q.pdf'));
@sufian07
sufian07 / stream.js
Created October 10, 2021 09:37
Creating some example for creating stream in node js
const fs = require('fs');
const path = require('path');
const express = require('express');
const app = express()
const port = 5000
const Stream = require('stream')
app.get('/', (req, res) => {
@sufian07
sufian07 / memoize.js
Created August 25, 2018 12:13 — forked from jherax/memoize.generic.ts
High-order function that memoizes a function
/**
* @summary
* High-order function that memoizes a function, by creating a scope
* to store the result of each function call, returning the cached
* result when the same inputs is given.
*
* @description
* Memoization is an optimization technique used primarily to speed up
* functions by storing the results of expensive function calls, and returning
* the cached result when the same inputs occur again.
<?php
$selects[] = rtrim( join( array_map(
function ($custom_field)
{
$alias = 'custom' . $custom_field;
return "(SELECT wcf.value FROM workorder_custom_fields as wcf"
." WHERE wcf.workorder_id=w.workorder_id"
." AND wcf.custom_label_id='". dbsafe($custom_field). "') as `$alias`, ";
},
$customFields
<?php
sprintf(
'SELECT * FROM (%s)',
implode(
' INNER JOIN ',
array_map(
function($s,$i){
return sprintf(
"(%s) as temp%d %s",
$s,
@sufian07
sufian07 / FlattenedPromiseChain.md
Created July 20, 2018 10:32 — forked from geowulf/FlattenedPromiseChain.md
Promises: Flattened Promise Chain

Credit: Thomas Burleson

Since promise handlers can return Promises, let’s use that technique to refactor an implementation:

var Dashboard = function( $scope, user, ServiceOne, ServiceTwo )
    {
        Service-1
            .getAddressInfo( user )                                              // Request #1
@sufian07
sufian07 / fewer_properties_subset_of_object_es6.js
Last active July 18, 2018 11:02
To get subset of an object in es6 syntax
const a = {
name: "Sufian",
age:34,
prof:"SE"
}
console.log(...a)
//Option 1
let {name,age} = a
const b = {name,age}
@sufian07
sufian07 / 1-add-middleware.php
Created February 16, 2018 13:41 — forked from adamwathan/1-add-macros.php
Multiformat Endpoints in Laravel
<?php
namespace App\Http\Middleware;
class CaptureRequestExtension
{
public function handle($request, $next)
{
if ($request->route()->parameter('_extension') !== null) {
$request->attributes->set('_extension', substr($request->route()->parameter('_extension'), 1));
@sufian07
sufian07 / curl.md
Created August 24, 2017 02:12 — forked from subfuzion/curl.md
curl POST examples

Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.

-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).

-c, --cookie-jar <file name> File to save response cookies to.