Skip to content

Instantly share code, notes, and snippets.

View Sidd27's full-sized avatar
🏠
Working from home

Siddharth Pandey Sidd27

🏠
Working from home
  • Bengaluru
View GitHub Profile
@Sidd27
Sidd27 / curry.js
Last active September 10, 2021 06:46
Curry Function in ES5 (Javascript) It returns the curried function
function curry(fn) {
var arity = fn.length;
return (function resolver() {
var memory = Array.prototype.slice.call(arguments);
return function () {
var local = memory.slice(),
next;
Array.prototype.push.apply(local, arguments);
next = local.length >= arity ? fn : resolver;
return next.apply(null, local);
@Sidd27
Sidd27 / indian-currency-pipe.js
Created April 2, 2019 13:39
Indian Currency Pipe for Angular
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'indianCurrency'
})
export class IndiaCurrencyPipe implements PipeTransform {
transform(amount: any, args?: any): string {
if (amount !== null && !isNaN(amount)) {
const currencySymbol = '₹ ';
db.getCollection('companydetails').aggregate([
{ "$group": {
"_id": { "$toLower": "$state" },
"count": { "$sum": 1 }
} },
{ "$group": {
"_id": null,
"counts": {
"$push": { "k": "$_id", "v": "$count" }
}
@Sidd27
Sidd27 / mash.js
Created September 25, 2019 07:38
We convert each item into an array that contains the key and the value. mash folds these tuples into an object where they become the actual key/value pairs.
Array.prototype.mash = function(callback) {
const addKeyValuePair = (acc, item) => {
const [key, value] = callback ? callback(item) : item
return {...acc, [key]: value}
}
return this.reduce(addKeyValuePair, {})
}
@Sidd27
Sidd27 / scan.js
Created September 25, 2019 07:40
Init+each+accumulate+push -> scan
Array.prototype.scan = function (callback, initialValue) {
const appendAggregate = (acc, item) => {
const aggregate = acc[acc.length-1] //get last item
const newAggregate = callback(aggregate, item)
return [...acc, newAggregate]
}
const accumulator = [initialValue]
return this.reduce(appendAggregate, accumulator)
@Sidd27
Sidd27 / debounce.js
Created February 21, 2020 09:39
ES5 Javascript Debounce Function
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
let getFileName = function (header) {
let contentDispostion = header.split(';');
const fileNameToken = `filename*=UTF-8''`;
let fileName = 'downloaded.pdf';
for (let thisValue of contentDispostion) {
if (thisValue.trim().indexOf(fileNameToken) === 0) {
fileName = decodeURIComponent(thisValue.trim().replace(fileNameToken, ''));
break;
}
@Sidd27
Sidd27 / MyPomise.js
Created June 15, 2023 07:43
Custom Promise Implementation in Javascript
function MyPromise(executor) {
let onResolve, onReject;
let fulfilled = false,
rejected = false,
called = false,
value;
function resolve(v) {
fulfilled = true;
value = v;