Skip to content

Instantly share code, notes, and snippets.

View imvpn22's full-sized avatar
:octocat:
Playing with JS

Vipin Yadav imvpn22

:octocat:
Playing with JS
View GitHub Profile
const withCache = (fn, limit) => {
const cache = {};
const cacheQueue = new Array(limit);
return (params) => {
// console.log('cache', cacheQueue, cache)
const key = fn.name + '$$' + JSON.stringify(params);
if (cache.hasOwnProperty(key)) {
import React, { useRef } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const API_TOKEN = "MfwZLqWeh1YPrvrlULOqgzZfOlRkIR7r";
const BASE_URL = "https://api.giphy.com/v1/gifs/search?api_key=";
const SEARCH_URL = BASE_URL + API_TOKEN + "&q=";
/*
// Callback to Promise
const printMessage = (param, callback) => {
console.log('Executing...', param);
callback('err', {param: param, done: 'yes'});
}
const proisify = fn => params => new Promise((resolve, reject) => {
fn(params, (err, data) => {
if (err) reject(err);
// Implement a runOnlyOnce function
const runOnlyOnce = fn => {
let flag = false;
let result = null;
return (...args ) => {
if (flag) return result;
result = fn(args);
flag = true;
const str1 = 'my--name---is-vipin-'; // myNameIsVipin
const str2 = '---this-is-awesome----day---too'; // ThisIsAwesomeDayToo
const snakeToCamel = (str) => {
const strArr = str.split(/-+/);
const flag = str.startsWith('-')
return strArr.map((st, i) => {
let firstChar = st.charAt(0);
// Possible sum without neighbours
const arr = [4,1,5,7,2,3,8,6];
const arr2 = [1,1,1,4,1,2,4]
function maxSum(arr) {
if (!arr.length) return 0;
if (arr.length === 1) {
return arr[0];
@imvpn22
imvpn22 / myCloneDeep.js
Created October 6, 2022 14:20
Lodash cloneDeep implementation
// Implement lodash cloneDeep
const a = {
val: 1,
prop: {
val2: 0,
val3: { name: 'a', condition: false, myArr: [1,2,3,4]}
}
};
@imvpn22
imvpn22 / simpleJSONStringify.js
Last active October 6, 2022 14:22
Simple implementation to JSON Stringify function
const obj = {asdf: "jkl", num: 2, bool: false, subObj: {a: 1, b: true}, arr: [1, 'abc', true]};
const str = JSON.stringify(obj);
console.log(str);
const myStringify = (value) => {
let str = '';
if (typeof value === 'string') {
str += '"' + value + '"';
} else if (typeof value === 'number') {
const S = "timetopractice";
const P = "toc";
const P_arr = P.split('');
const matchedSubStr = [];
let i = 0;
let win = P.length;
while (i + win <= S.length) {
const subStr = S.substring(i, i+win);
@imvpn22
imvpn22 / decimalToRoman.js
Created August 18, 2019 06:27
Convert given decimal numbers into roman
const romanMap = {
1: 'I',
2: 'II',
3: 'III',
4: 'IV',
5: 'V',
6: 'VI',
7: 'VII',
8: 'VIII',
9: 'IX',