Skip to content

Instantly share code, notes, and snippets.

View rajatjain-21's full-sized avatar
💭
Alive in the world of code

Rajat Jain rajatjain-21

💭
Alive in the world of code
View GitHub Profile
@rajatjain-21
rajatjain-21 / partition.js
Created September 7, 2020 17:19
This is a custom method to partition any array based on the logic provided
/**
* A function that partitions an array based on the passed logic
* @param arr The array to be partitioned
* @param callback The logic function to base the partition on
*/
const partition = (arr, callback) => {
// The result object which will ultimately have two properties
// positive and negative
const result = {};
arr.forEach((item, index) => {
// Function to be called on click
const mainFunc = () => {
console.log("Called me?😃");
}
const oncifyHelper = () => {
let clickCount = 0;
const oncifyHelperInner = (func) => {
if(clickCount===0) {
func();
// Method for creation of array
function array(...arguments) {
let arr = Object.create(array.prototype);
Object.defineProperty(arr, "length", {
value: 0,
enumerable: false,
writable: true
})
for(let i=0;i<arguments.length;i++) {
function curry(func, length = func.length) {
// passing a second paramenter as the number of arguments
// passed to the function
return function (...args) {
if (args.length >= length) {
// if the function call contains arguments which are
// greater than or equal to the max, just the return the func
return func(...args);
} else {
// otherwise call the curry again, binding the already
import React, { useState, useCallback } from "react";
import "./styles.css";
import { debounce } from "lodash";
export default function App() {
const [countryName, setCountryName] = useState("");
const [countries, setCountries] = useState([]);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
function normalFunc(x,y,callback) {
const result = x + y;
callback(result);
}
async function run() {
const promisedFn = promisify(normalFunc);
const result = await promisedFn(4,3).then(data => data*data);
console.log(result);
}
run()
let obj = {
a: 32,
b: 34
}
Object.defineProperty(obj, Symbol.iterator, {
configurable: false,
enumerable: false,
writable: false,
value: function() {
Function.prototype.bind = function(...args) {
let obj = this;
let params = args.slice(1);
return function(...args2) {
obj.apply(args[0], [...params, ...args2]);
}
}
class MyPromise {
static all(promises) {
let results = []; // to be resolved array of values
let completedPromises = 0;
// return a promise
return new Promise((resolve, reject) => {
promises.forEach((promise, index) => {
promise.then((value) => {
results[index] = value;
completedPromises += 1;
const input = [
1, 2, 3,
[4],
[5, 6, [7], [8, [9, [10]]]],
11, 12, 13,
[14, [[[[[15, [16]]]]]]],
17, 18,
[19, [20, [21, [22, [23, [24, [[[[[25]]]]]]]]]]]
];