Skip to content

Instantly share code, notes, and snippets.

View ravid7000's full-sized avatar
🎯
Focusing

Ravi Dhiman ravid7000

🎯
Focusing
View GitHub Profile
@ravid7000
ravid7000 / ConnectHOC.js
Last active August 14, 2018 07:46
Connect Redux with component using Higher Order Component
import React, { Component, createElement } from 'react'
import Store from './store'; // using createStore of redux
// hoc
function connect(WrappedComponent) {
return class extends Component {
constructor(props) {
super(props);
this.state = {
data: Store.getState()
@ravid7000
ravid7000 / queryParser.js
Created April 13, 2018 07:49
URL Search query parameter parser
function queryParamsParser(s) {
var params = {};
if (typeof s === 'string' && s.indexOf('?') >= 0) {
var a = s.substr(1).split('&');
for (var i = 0; i < a.length; i++) {
var b = a[i].split('=');
if (b[0] !== '')
params[b[0]] = b[1];
}
}
@ravid7000
ravid7000 / validateEmail.js
Created April 13, 2018 07:51
JS Email validater
function validateEmail(text) {
if (typeof text === 'string' && text !== '') {
return (/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[A-Za-z]+$/).test(text);
}
return false;
}
@ravid7000
ravid7000 / scrollTo.js
Created April 13, 2018 07:54
Pure JS smooth scroll with easing
Math.easeInOutQuad = function (t, b, c, d) {
t /= d/2;
if (t < 1) return c/2*t*t + b;
t--;
return -c/2 * (t*(t-2) - 1) + b;
};
/*
scrollTo(element.scrollTop || 200, 400)
@ravid7000
ravid7000 / classUtils.js
Created April 13, 2018 08:13
Pure JS add/remove class from element
/*
var elm = new classUtils('body');
elm.add('modal-open');
elm.add('bg-white fg-dark'); // add multple class with space seprated
elm.removeAll();
elm.remove('modal-open');
*/
function classUtils(elm) {
@ravid7000
ravid7000 / find-length-of-number.js
Created April 23, 2018 13:47
Find length of a number using Math
/*
findNumberLen(25)
*/
function findNumberLen(num) {
return Math.ceil(Math.log10(Math.abs(num) + 1))
}
@ravid7000
ravid7000 / ceil-to-nearest.js
Created April 23, 2018 13:49
Find closest number multiple of 10, 100, 1000, 10000,...
/*
ceilToNearest(5)
> 10
*/
function ceilToNearest(num) {
let len = Math.ceil(Math.log10(Math.abs(num) + 1))
let divisor = Math.pow(10, (len - 1) === 0 ? 1 : (len - 1))
if (num % divisor > 0) {
return (Math.round(num / divisor) + 1) * divisor
@ravid7000
ravid7000 / asyncComponent.js
Created May 25, 2018 13:53
React dynamic import component
/*
import AsyncComponent from './asyncComponent'
<AsyncComponent load={() => import('./componentPath')}>
{Component => (<Component />)}
</AsyncComponent>
*/
import React from 'react'
@ravid7000
ravid7000 / Javascript_interview_questions.md
Last active January 7, 2020 12:06
Popular JavaScript Interview Questions.

Popular JavaScript Interview Questions.

Question #1. What will be the output of following code?

Note: A closure is basically when an inner function has access to variables outside of its scope. Closures can be used for things like implementing privacy and creating function factories.

  const arr = [10, 12, 15, 21];
  for (var i = 0; i < arr.length; i++) {
    setTimeout(function() {
@ravid7000
ravid7000 / webpack.config.js
Last active September 3, 2018 12:39
Webpack 4 configuration for ReactJS. Single webpack 4 config for React, includes loader for Sass/SCSS (Autoprefix), Eslint, BrowserSync.
/*
* Author: Ravi Dhiman
* Email: [email protected]
*
* Usage: Add scripts in `package.json` file
*
* "start": "webpack-dev-server --config=webpack.config",
* "build": "webpack --config=webpack.config --prod"
*
* Dependencies: