Skip to content

Instantly share code, notes, and snippets.

View prashantsani's full-sized avatar

Prashant Sani prashantsani

View GitHub Profile
@prashantsani
prashantsani / basic-node-server.js
Created March 6, 2024 15:11
Basic Node Server
/* Prompts */
// npm init -y
// npm install express
// touch index.js
/// ------------------------
// Contents of `index.js` file
import express from "express";
@prashantsani
prashantsani / js-array.js
Last active March 21, 2024 08:56
JS Array Input and Output
'hello'. includes('ell'); // true
'hello'.startswith('hell'); // true
'hello' endsWith('ello'); // true
'&' . repeat (3); //
'hello' .codePointAt(1); // 101
@prashantsani
prashantsani / js
Created February 10, 2024 12:55
using-js-promise-to-delay-execution-context
// Using Promise to delay Execution
function delay(duration) {
return new Promise((resolve) => {
setTimeout(() => resolve(), duration);
});
}
@prashantsani
prashantsani / symbol.iterator.txt
Created April 3, 2023 11:32
Using Symbol.iterator to update how (for of) loop iterates through an object
const testingTeam = {
lead: 'Alex',
engineer: 'Bill',
// This is generator function
[Symbol.iterator]: function* () {
yield this.lead;
yield this.engineer
}
}
@prashantsani
prashantsani / gist:fea14a251c20beac32838d8b4702093a
Created February 20, 2023 09:31
Book Store using Factory Function vs Constructor Function
// Factory Function
function CreateBookStore(inventory) {
return {
inventory,
getInventoryValue() {
// if arrow function is used here, this will return `undefined`,
// hence using short-hand/object literal for function
// this is similar to `getInventoryValue: function () { ......... }`
return this.inventory.reduce((total, book) => total + book.price, 0);
},
@prashantsani
prashantsani / Classical Inheritance
Created February 17, 2023 10:41
Classical Inheritance
function Car(options) {
this.title = options.title;
}
Car.prototype.drive = function () {
return 'drive'
}
function Toyota(options) {
Car.call(this, options);
@prashantsani
prashantsani / array-flat.js
Created March 5, 2020 15:03
Flatten An array of Integers not using Array.flat()
// Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers.
// e.g. [[1,2,[3]],4] -> [1,2,3,4].
function flatten(arr){
let newArray = [];
for(let i=0; i< arr.length; i++){
if(Array.isArray(arr[i])){
newArray = newArray.concat(flatten(arr[i]))
}else{
newArray.push(arr[i])
@prashantsani
prashantsani / VanillaJS_ScrollTo
Last active March 12, 2024 10:37
Native Alternate(Vanilla JS) to jQuery's ScrollTo Plugins
const supportsNativeSmoothScroll = 'scrollBehavior' in document.documentElement.style;
function scrollToView(elem) {
if(supportsNativeSmoothScroll){
// https://caniuse.com/#feat=mdn-api_window_scrollto
// As of publish date of this gist,
// this is only supported in 52% browsers,
// So, the next section (`else{`) is a fallback
window.scrollTo({
behavior: 'smooth',
@prashantsani
prashantsani / IntersectionObserver
Last active November 6, 2019 07:41
JavaScript IntersectionObserver Utility Function - Trigger When Element in View
// View Comment section for explanation
function _$(elem){ return d.querySelectorAll(elem) }
function observer(trigger, func_vis, func_hidden, threshold){
var t = threshold ? threshold : 1,
observable_var;
@prashantsani
prashantsani / fav-js-libraries.md
Created October 23, 2017 01:52
Favorite JS Libraries & Examples