Skip to content

Instantly share code, notes, and snippets.

View ahamed's full-sized avatar
🎯
Focusing on open source.

Sajeeb Ahamed ahamed

🎯
Focusing on open source.
View GitHub Profile
@ahamed
ahamed / mergingArrays.js
Created January 7, 2021 12:22
Merging two or more arrays by making unique by a list of keys.
/**
* Merge two or more same structured arrays of objects
* and make them unique by user provided keys.
*
* @author Sajeeb Ahamed
*/
const mergeArrays = (...arrays) => {
// Consider the last argument as the keys
const keys = arrays.pop();
@ahamed
ahamed / strToNumber.js
Last active January 8, 2021 11:18
Converting string to number using JavaScript.
/**
* Ways to convert a numeric string to a number.
*
* @author Sajeeb Ahamed
*/
const str = '343';
// Using parseInt method.
number = parseInt(str);
@ahamed
ahamed / oddEven.js
Created January 10, 2021 12:52
Detect odd even number.
/**
* Check a number is odd or even.
*
* @author Sajeeb Ahamed
*/
let num = 234;
/** Mod the number with 2 and if it returns 1 then odd, even otherwise */
if (num % 2) console.log('Odd');
else console.log('Even');
@ahamed
ahamed / arrayChunk.js
Created January 12, 2021 17:47
Make a chunk of fixed size of a linear array.
/**
* Create chunk using a fixed size.
*
* @author Sajeeb Ahamed
* @see https://stackoverflow.com/a/61413202/4610740
*/
// Add the chunk function into the array prototype.
Array.prototype.chunk = function(size) {
@ahamed
ahamed / forcePushGit.sh
Created January 28, 2021 10:53
Force push a branch to another branch without keeping the history or matches.
# Checkout to the reseting branch
#e.g. `git checkout master`
git checkout old_branch
# reset hard by the new branch's contents
# e.g. `git reset --hard origin/v2.0.0`
git reset --hard new_branch
# force push the contents to the origin
@ahamed
ahamed / integerSort.js
Created February 2, 2021 16:03
Sort integer array using javascript native method.
/**
* The behavior of JavaScript sort method on integer array.
*
* @author Sajeeb Ahamed <sajeeb07ahamed@gmail.com>
*/
const arr = [3, 4, 2, 100, 98, 34];
arr.sort();
/**
* Output: [100, 2, 3, 34, 4, 98]
@ahamed
ahamed / runAsync.js
Created February 2, 2021 16:38
Run Async function and get result and error without try catch.
/**
* Run Async function and get the result and the error without using try catch.
*
* @author Sajeeb Ahamed <sajeeb07ahamed@gmail.com>
*/
const runAsync = (func) => func.then((data) => [data, undefined]).catch((error) => Promise.resolve([undefined, error]));
// A function which will return a promise and throw error
const getData = () => {
return new Promise((_, reject) => {
@ahamed
ahamed / getEdgeValue.js
Created February 2, 2021 17:06
Get the edge value of an array i.e. the max or min value w.r.t the condition.
/**
* Get edge value of an array using user defined function. The edge value means either max or min value.
*
* @author Sajeeb Ahamed <sajeeb07ahamed@gmail.com>
*/
Array.prototype.findEdge = function(callback) {
// If no value exists to the array then return undefined
if (this.length === 0) return undefined;
let edge = this[0];
@ahamed
ahamed / array_unique_jsphp.php
Created May 16, 2021 21:52
Make an array unique using filter and indexOf.
<?php
/**
* @package Ahamed\JsPhp
* @author Sajeeb Ahamed <sajeeb07ahamed@gmail.com>
* @copyright Copyright (c) 2021 Sajeeb Ahamed
* @see https://github.com/ahamed/jsphp
*/
use Ahamed\JsPhp\JsArray;
@ahamed
ahamed / phptable.php
Created June 4, 2021 15:28
PHP table from array.
<?php
/**
* @package Ahamed\JsPhp
* @author Sajeeb Ahamed <sajeeb07ahamed@gmail.com>
* @copyright (c) Sajeeb Ahamed 2021
* @see https://github.com/ahamed/jsphp
*/
$book = [
'id' => [1, 2, 3, 4, 5],
'title' => ['Master PHP', 'Python In Action', 'Advance engllish for learners', 'Maths for kids', 'Color theory'],