Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Siddharth Pandey Sidd27

🏠
Working from home
View GitHub Profile
View DownloadPDF.js
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 / debounce.js
Created Feb 21, 2020
ES5 Javascript Debounce Function
View debounce.js
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);
@Sidd27
Sidd27 / scan.js
Created Sep 25, 2019
Init+each+accumulate+push -> scan
View scan.js
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 / mash.js
Created Sep 25, 2019
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.
View mash.js
Array.prototype.mash = function(callback) {
const addKeyValuePair = (acc, item) => {
const [key, value] = callback ? callback(item) : item
return {...acc, [key]: value}
}
return this.reduce(addKeyValuePair, {})
}
View mongo-unique-count.js
db.getCollection('companydetails').aggregate([
{ "$group": {
"_id": { "$toLower": "$state" },
"count": { "$sum": 1 }
} },
{ "$group": {
"_id": null,
"counts": {
"$push": { "k": "$_id", "v": "$count" }
}
@Sidd27
Sidd27 / indian-currency-pipe.js
Created Apr 2, 2019
Indian Currency Pipe for Angular
View indian-currency-pipe.js
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 = '₹ ';
@Sidd27
Sidd27 / curry.js
Last active Mar 16, 2019
Curry Function in ES5 (Javascript) It returns the curried function
View curry.js
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);