Skip to content

Instantly share code, notes, and snippets.

View ANUPAMCHAUDHARY1117's full-sized avatar

Anupam Chaudhary ANUPAMCHAUDHARY1117

View GitHub Profile
const one = +true; //This would return 1 (Unary and boolean operator)
const zero = +false; // This would return 0 (Unary and boolean operator)
//Using string operations apending 1 and 0 and 0 to form "100"
const hundredInString = one.toString() + zero.toString() + zero.toString();
// To save the day for not using the loop, we are using recursion function.
const printInRecursion = (index) => {
if (index <= parseInt(hundredInString)) {
console.log(index);
printInRecursion(index + one);
const app = require('../app');
const jwt = require('jsonwebtoken');
const request = require('supertest');
const testApi = () => {
it('It should verify the access token and respond with status 200', async () => {
const jwtSpy = jest.spyOn(jwt, 'verify');
jwtSpy.mockReturnValue('Some decoded token');
const res = await request(app)
const express = require('express');
const app = express();
const PORT = process.env.PORT || 8000;
const jwt = require('jsonwebtoken');
const secretAccessKey = process.env.secretAccessKey || 'SecretAccessKey';
// To mock any NPM module make sure that you are not destructuring them while requiring them in your file.
// For example - If we use 
// const {verify} = require('jwt'); in place of const jwt = require();
// Jest would not be able to mock destructured modules. So, always keep in mind while writing your code to not to use destructured imports or require.
import React, { useEffect } from 'react';
import IndexedDb from './indexedDb'
const Test = () => {
useEffect(() => {
const runIndexDb = async () => {
const indexedDb = new IndexedDb('test');
await indexedDb.createObjectStore(['books', 'students']);
await indexedDb.putValue('books', { name: 'A Game of Thrones' });
await indexedDb.putBulkValue('books', [{ name: 'A Song of Fire and Ice' }, { name: 'Harry Potter and the Chamber of Secrets' }]);
import { IDBPDatabase, openDB } from 'idb';
class IndexedDb {
private database: string;
private db: any;
constructor(database: string) {
this.database = database;
}
import React from 'react';
const ImageComponent = ({ src }) => {
return <img src={src} alt='Avatar' style={{ width: '50%' }} />;
};
export default ImageComponent;
import React, { useState, useEffect, Suspense } from 'react';
import imageFile from './img_avatar.png';
import './list.css';
const ImageComponent = React.lazy(() => import('../image/image'));
const List = () => {
const [listItems, setListItems] = useState([]);
const [isFetching, setIsFetching] = useState(false);
const [page, setPage] = useState(1);
const cookieParser = require('cookie-parser');
const jwt = require('jwt');
app.get('/checkLogin', (req, res) => {
const token = cookieParser.signedCookie(req.signedCookies['x-access-token'], process.env.COOKIE_SECRET_KEY);
jwt.verify(token, process.env.SECRET_KEY, function (err, decoded) {
if (err) {
throw new Error(err);
}
return res.status(200).json({ message: 'JWT VERFIED' });
const jwt = require('jsonwebtoken');
const jwtOperation = {
sign: function(id) {
return jwt.sign({ id }, process.env.SECRET_KEY, {
expiresIn: 864,
});
},
verify: function(token) {
return jwt.verify(token, process.env.SECRET_KEY, function(err, decoded) {
function addZeroToString(str1, str2){
while (str1.length > str2.length) {
str2 = "0" + str2;
}
return str2;
}
function addTwoBugNumbers(a, b) {
if (a.length > b.length) {
b = addZeroToString(a,b);