function allSettled(promises) {
return new Promise(resolve => {
const data = [], len = promises.length;
let count = len;
for (let i = 0; i < len; i += 1) {
const promise = promises[i];
promise.then(res => {
data[i] = { status: 'fulfilled', value: res };
}, error => {
data[i] = { status: 'rejected', reason: error };
}).finally(() => {
if (!--count) {
resolve(data);
}
});
}
});
}
function all(promises) {
let len = promises.length, res = []
if (len) {
return new Promise(function (resolve, reject) {
for(let i=0; i<len; i++) {
let promise = promises[i];
promise.then(response => {
res[i] = response
if (res.length === len) {
resolve(res)
}
}, error => {
reject(error)
})
}
})
}
}
function debounce(func, wait, immediate) {
let timout;
return function () {
let context = this
let args = arguments
if (timout) clearTimeout(timout)
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait)
if (callNow) func.apply(context, args)
} else {
timeout = setTimeout(function(){
func.apply(context, args)
}, wait);
}
}
}
function throttle1(func, delay) {
let timer = null,
startTime = Date.now();
return function() {
let curTime = Date.now(),
remaining = delay - (curTime - startTime),
context = this,
args = arguments;
clearTimeout(timer);
if (remaining <= 0) {
func.apply(context, args);
startTime = Date.now();
} else {
timer = setTimeout(() => {
func.apply(context, args);
}, remaining);
}
}
}
function flat(arr, deep = 1) {
return deep > 0 ? arr.reduce((pre, cur) => {
return pre.concat(Array.isArray(cur) ? flat(cur, --deep) : cur);
}, [])
: []
}
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
let pivotIndex = Math.floor(arr.length / 2);
let pivot = arr.splice(pivotIndex, 1)[0];
let left = [];
let right = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot], quickSort(right));
};
function ReverseList(pHead) {
let pPre=null,
pNext=null;
while(pHead!==null){
pNext=pHead.next;
pHead.next=pPre;
pPre=pHead;
pHead=pNext;
}
return pPre;
}
function reverseTree(root) {
if (root !== null) {
let temp = root.left
root.left = root.right
root.right = temp
reverseTree(root.left)
reverseTree(root.right)
}
return root
}
function cloneDeep (obj) {
let newObj = Array.isArray(obj) ? [] : {};
if (obj && typeof obj === "object") {
for (let i in obj) {
let prop = obj[i];
if (prop == obj) {
continue;
}
if (obj.hasOwnProperty(i)) {
if (prop && typeof prop === "object") {
newObj[i] = (prop.constructor === Array) ? [] : {};
arguments.callee(prop, newObj[i]);
} else {
newObj[i] = prop;
}
}
}
}
return newObj;
}
function add () {
let args = Array.prototype.slice.call(arguments)
let temp = function () {
args.push(...arguments)
return temp
}
temp.toString = function () {
return args.reduce(function(a, b) {
return a+b
})
}
return temp
}
console