This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function fetchUtility({ url, headers, method }) { | |
try { | |
const response = await fetch(url, { ...headers }, method); | |
return response.json(); | |
} catch(err) { | |
console.log("error", err); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://twitter.com/wesbos/status/1063515277911052290/photo/1 | |
async function soFetch(input, settings = {}) { | |
const response = await fetch(input, { | |
headers: { | |
Accept: 'application/json, text/plain, */*', | |
'Content-Type': 'application/json' | |
}, | |
...settings | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let subscribers = {}; | |
module.exports = { | |
publish(event, data) { | |
if (!subscribers[event]) return; | |
subscribers.forEach(callback => { | |
callback(data); | |
}) | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wrong way, bcoz variables are exposed. | |
// var foo = { | |
// o: {bar: 'fghj'}, | |
// test() { | |
// console.log('hello'); | |
// } | |
// } | |
// foo.test(); | |
// console.log(foo.o); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using bind | |
var sum = function(x, y) { | |
return x+y | |
} | |
var sumCall = sum.bind(this); | |
sumCall(3, 2); | |
var sumCall = sum.bind(this, 3); | |
sumCall(2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let name = { | |
firstname: 'jyoti', | |
lastname: 'arora' | |
} | |
var printFullName = function (city, state) { | |
console.log(`${this.firstname} ${this.lastname} from ${city}, ${state}`); | |
} | |
var printMyName = printFullName.bind(name, 'MZN'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function throttle(func, wait) { | |
let flag = true; | |
return function() { | |
let context = this; | |
let args = arguments; | |
if (flag) { | |
func.apply(this, args); | |
flag = false; | |
} | |
else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for single object | |
function testFunction(a, b, c, d, {name, company, isUsingLaptop}) { | |
console.log(a, b, c, d) | |
console.log(name, company, isUsingLaptop) | |
} | |
var a = 10 | |
var b = 20 | |
var c = true | |
var d = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function shiftArr (n, rotation, arr) { | |
for (let i= 0; i < rotation ; i++) { | |
let sElement = arr.shift() | |
arr.push(sElement) | |
} | |
} | |
shiftArr(5, 4, [1, 2, 3, 4, 5]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function candies(n, arr) { | |
var ans = 0 | |
var values = [] | |
values[0] = 1 | |
for (let i=1; i< n; i++) { | |
if (arr[i-1] < arr[i]) { | |
values[i] = values[i-1] +1 | |
} else { | |
values[i] = 1 | |
} |
NewerOlder