Skip to content

Instantly share code, notes, and snippets.

Avatar

Ajay Poshak AjayPoshak

View GitHub Profile
View networkDelayTime.js
// This is an example of Dijkstra's algorithm. https://leetcode.com/problems/network-delay-time/
function createAdjList(edges, n) {
const adjList = new Map()
for(let i=0; i<n; i++) adjList.set(i, [])
for(let i=0; i<edges.length; i++) {
const [from, to, weight] = edges[i]
const neighbors = adjList.get(from)
neighbors.push({ node: to, weight })
adjList.set(from, neighbors)
}
View minCostConnect.js
function MinHeap() {
this.arr = new Array()
}
MinHeap.prototype.push = function push(data, weight) {
this.arr.push({ data, weight })
if(this.arr.length <= 1) return
let current = this.arr.length - 1
let parent = Math.floor(current/2)
@AjayPoshak
AjayPoshak / newFunction.js
Created Jul 1, 2019
Function to implement new keyword
View newFunction.js
function newFunction(funcName, args) {
// Copy the function prototype to object
let obj = Object.create(funcName.prototype)
// Call constructor function with supplied arguments, and assign this to newly created object
funcName.call(obj, args)
// return newly created object
return obj
}
View webpack.client.config.js
const path = require('path'),
webpack = require('webpack'),
AssetsPlugin = require('assets-webpack-plugin'),
BrotliPlugin = require('brotli-webpack-plugin'),
HtmlWebpackPlugin = require('html-webpack-plugin'),
UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const isProd = process.env.NODE_ENV === 'production';
/**
View vendor-chunk.js
...
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
}
}
View brottli.compression.js
...
plugins: [
new BrotliPlugin({
asset: '[path].br[query]',
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8
})
],
...
View remove-dev-tools-mapping.js
...
devtool: '', // Removed dev-tools mapping
entry: './client/index.js',
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'build/client'),
publicPath: 'build/client'
},
...
@AjayPoshak
AjayPoshak / webpack.config.js
Last active Jul 14, 2018
Initial webpack config
View webpack.config.js
const path = require('path'),
AssetsPlugin = require('assets-webpack-plugin'),
HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
devtool: 'inline-source-map',
entry: './client/index.js',
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'build/client'),
@AjayPoshak
AjayPoshak / prototype-inst.js
Created Jan 20, 2018
instantiate prototypes
View prototype-inst.js
var eventsNotifs = new Notification();
console.log(eventsNotifs.notifs); // Output: A, B, C
eventsNotifs.markAllNotifsRead(); // Mark all notification as read
@AjayPoshak
AjayPoshak / prototype-methods.js
Last active Mar 25, 2018
Adding methods to prototypes
View prototype-methods.js
var Notification = function () {
this.notifs = ['A', 'B', 'C']
this.readNotifs = ['B']
this.isPublic = false
}
Notification.prototype = {
markNotifsRead: function() {
//Mark notification read
}