Skip to content

Instantly share code, notes, and snippets.

Avatar

OMKAR AGRAWAL Omkaragrawal

View GitHub Profile
@Omkaragrawal
Omkaragrawal / generateRandomString.ts
Last active Apr 21, 2022
Use nanoid to generate random unique string. This function has lots of available formatting.
View generateRandomString.ts
import { customAlphabet, nanoid } from 'nanoid';
import nanoidDictionary from 'nanoid-dictionary';
export enum NanoidStringEnum {
NUMERIC = 'NUMERIC',
LOWERCASE = 'LOWERCASE',
UPPERCASE = 'UPPERCASE',
ALPHABETS = 'ALPHABETS',
ALPHANUMERIC = 'ALPHANUMERIC',
LOWERCASE_NUMERIC = 'LOWERCASE_NUMERIC',
View index.js
let data;
let currentPage = -1;
let currentArticle = 0;
let loaderElement, articleContainer;
let totalPages;
const toggleLoader = () => {
if (loaderElement.classList.contains('hidden')) {
loaderElement.classList.remove('hidden');
} else {
View code-for-using-useState-in-axios-React.jsx
import React, { useEffect, useState } from 'react';
import axios from 'axios';
const App = () => {
let [list, setList] = useState(<>LOADING</>);
useEffect(() => {
// You can use your link here
// I have created corsenabled.herokuapp.com just to bypass the CORS issue. It's only for testing and educational purpose only. No intention to infringe any copyrights or other legal matters
View without-strict-mode.js
ction f1() {
return this;
}
// In a browser:
f1() === window; // true
// In Node:
f1() === globalThis; // true
View strict-mode.js
'use strict'; // see strict mode
function f2() {
// 'use strict'; or else here
return this;
}
console.log(f2() === undefined) // true
View call-apply-method.js
// An object can be passed as the first argument to call or apply and this will be bound to it.
var obj = {a: 'Custom'};
// We declare a variable and the variable is assigned to the global window as its property.
var a = 'Global';
function whatsThis() {
return this.a; // The value of this is dependent on how the function is called
}
View bind-method.js
function f() {
return this.a;
}
var g = f.bind({a: 'azerty'});
console.log(g()); // azerty
var h = g.bind({a: 'yoo'}); // bind only works once!
console.log(h()); // azerty
@Omkaragrawal
Omkaragrawal / getter-setter.js
Created May 18, 2020
scope of "this" in getters and setters of regular function
View getter-setter.js
function sum() {
return this.a + this.b + this.c;
}
var o = {
a: 1,
b: 2,
c: 3,
get average() {
return (this.a + this.b + this.c) / 3;
@Omkaragrawal
Omkaragrawal / Solution.js
Created May 18, 2020
Solution to HakerRank Inheritance Tutorials of/in 10 Days of Javascript
View Solution.js
class Rectangle {
constructor(w, h) {
this.w = w;
this.h = h;
}
}
/*
* Write code that adds an 'area' method to the Rectangle class' prototype
*/
@Omkaragrawal
Omkaragrawal / Problematic-Code.js
Created May 18, 2020
Problematic-code.js file
View Problematic-Code.js
class Rectangle {
constructor(w, h) {
this.w = w;
this.h = h;
}
}
/*
* Write code that adds an 'area' method to the Rectangle class' prototype
*/