Skip to content

Instantly share code, notes, and snippets.

View imparvez's full-sized avatar

Parvez Shaikh imparvez

  • 1Spatial
  • Newcastle Upon Tyne, United Kingdom
View GitHub Profile
@imparvez
imparvez / script.js
Created July 18, 2020 17:24
What are falsy values in JavaScript?
// Falsy values in Javascript
// The falsy values in JavaScript are 0, 0n, null, undefined, false, NaN, and the empty string "".
// A false value is a value that is false when encountered in Boolean context.
Boolean(0)
> false
Boolean(0n)
> false
Boolean(null)
> false
Boolean(undefined)
@imparvez
imparvez / actionCreators.js
Created July 18, 2020 15:58
React Redux Async Await
import axios from 'axios'
import * as actions from './actions'
export function suggestedSearch() {
return async dispatch => {
try {
dispatch(actions.suggestedSearchLoading())
const url = 'https://0b6c4e73-c7fd-4eab-b3ad-89e11db59c58.mock.pstmn.io/search'
const json = await axios.get(url)
@imparvez
imparvez / index.js
Created July 18, 2020 15:48
Splitting an array into two.
// Splitting an array into two.
// Use case:
// Given array: [1,2,3,4,5]
// Expected result: [1, 2] and [3, 4, 5]
var arr = [1, 2, 3, 4, 5]
var arrLength = arr.length
var arrMidSection = Math.floor(arrLength/2)
var leftPart = arr.slice(0, arrMidSection)
var rightPart = arr.slice(arrMidSection)
// leftPart
@imparvez
imparvez / index.html
Created July 8, 2019 18:02
Clone Item with and without content using JavaScript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="container">
<div class="col left-col">
@imparvez
imparvez / index.html
Created June 23, 2019 15:18
Simple Javascript Form Validation
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Simple Form Validation</title>
</head>
<body>
<form>
@imparvez
imparvez / fetchData.js
Created June 17, 2019 06:38
Fetch data using useEffect hooks in React
import React, { useState, useEffect } from 'react';
const useFetch = (url) => {
const [data, setData] = useState(null)
const [loading, setLoading] = useState(true)
useEffect( async () => {
const response = await fetch(url)
const data = await response.json()
const [ item ] = data.results
@imparvez
imparvez / useEffect.js
Created June 17, 2019 06:22
useEffect in React Hooks
import React, { useState, useEffect } from 'react';
function App() {
const [count, setCount] = useState(0)
useEffect(() => {
document.title = `You clicked ${count} times`
console.log('component did mount')
}, []); // componentDidMount, only when the component has been rendered at start
@imparvez
imparvez / useState.js
Created June 17, 2019 06:04
useState in React Hooks
import React, { useState } from "react";
import ReactDOM from "react-dom";
function App() {
const [count, setCount] = useState(0);
const increaseCount = () => {
setCount(count + 1)
}
const decreaseCount = () => {
@imparvez
imparvez / formatNumber.js
Created June 16, 2019 17:01
Format Phone Number using JavaScript
function formatNumber(number, format = 'xxx-xxx-xxx') {
let result = '',
curIndex = 0;
format.split('').forEach(char => {
if(char.toLowerCase() === 'x') {
result += number.charAt(curIndex)
curIndex++
} else {
result += char
@imparvez
imparvez / maskCC.js
Created June 12, 2019 12:24
Masking Credit Card Number using JavaScript
function maskCC(number) {
// If given parameter doesn't fall under below category, then return
// 1. Should be numbers(Every element should be a number)
// 2. Should be of length 16
if(!(typeof number === 'string' && number.length === 16 && number.split('').map(Number).every(n => !isNaN(n)) )) {
return;
}
return [...new Array(3).fill('****'), number.slice(-4)].join('-')
}