/ / image stabilization
function debounce (fn, time = 1000) {
  let timeId = null;
  return function (. args) {
    if (timeId) clearTimeout(timeId);
    timeId = setTimeout(() = > {
     fn.apply(this, args);
     timeId = null; }}})/ / throttling
function throttle (fn, time = 16.67) {
  const { performance, Date } = window || globalThis;
  const getDate = performance || Date;
  let cacheTime = getDate.now();
  return function (. args) {
    const curTime = getDate.now();
    if (curTime - cacheTime > time) {
      fn.apply(this, args); cacheTime = curTime; }}}// Anti-shake and throttling
function douM (fn, debounceTime = 1000, throttleTime = 16.67) {
  const d = debounce(fn, debounceTime);
  const t = throttle(fn, throttleTime);
  return function (. args) {
    d.apply(this, args);
    t.apply(this, args); }}/ / search tree
function searchToNewTree (arr, value, { label = 'label', children = 'children' }) {
  const newTree = [];
  arr.forEach(item= > {
   if (~item[label].indexOf(value)) {
    newTree.push(item);
   } else {
    const child = searchToNewTree(item[children], value, { label, children });
    if(child.length) { newTree.push({ ... item, [children]: child }); }}});return newTree;
}

// Object deep copy
function deep (obj, objCache = new WeakMap(a)) {
  if (obj instanceof Array) {
    const newArray = [];
    for (const item of obj) {
      newArray.push(deep(item, objCache));
    }
    return newArray;
  } else if (obj instanceof Object) {
    let newObj = objCache.get(obj);
    if (newObj) {
      return newObj
    } else {
      newObj = {};
      Reflect.ownKeys(obj).forEach(key= > {
        newObj[key] = deep(obj[key], objCache)
      })
      objCache.set(obj, newObj);
      returnnewObj; }}else {
    returnobj; }}// Convert a one-dimensional array to a tree
function arrToTree (arr, { id = 'id', pid = 'pid', children = 'children' }) {
  const newArr = JSON.parse(JSON.stringify(arr));
  const objCache = {};
  newArr.forEach(item= > objCache[item[id]] = item)
  const result = newArr.filter(item= >! objCache[item[pid]]); newArr.forEach(item= > {
    const parent = objCache[item[pid]];
    if (parent) {
      if (parent[children]) {
        parent[children].push(item);
      } else{ parent[children] = [item]; }}})return result;
}

/ / the whole arrangement
var _allSort = (function () {
  const result = [];
  function findDiff (temp, nums, length) {
    if(! length) { result.push([...temp]) }for (let i = 0; i < length; ++i) {
      temp.push(nums.splice(i, 1) [0])
      findDiff(temp, nums, nums.length);
      nums.splice(i, 0, temp.pop()); }}return function (nums) {
    result.length = 0;
    findDiff([], nums, nums.length)
    return [...result];
  }
})()

// Array flattening
function flatten (arr, dep = 1) {
  const newArr = [];
  dep-- ? arr.forEach(item= > item instanceof Array? newArr.push(... flatten(item)) : newArr.push(item)) : newArr.push(... arr);return newArr;
}


// Depth traversal
function dfs (arr) {
  arr.forEach(item= > {
    item.children && dfs(item.children)
  })
}

// width traversal
function bfs (arr) {
  const list = [...arr]
  while (list.length) {
    constitem = list.shift(); item.children && list.push(... item.children) } }// Fibonacci
const fbnq = (function () {
  const cache = [0.1.2];
  return function (num) {
    return cache[num] || (cache[num] = arguments.callee(num - 1) + arguments.callee(num - 2))}}) ()/ / written Promise
const status = Symbol('status');

class MyPromise {
  [status] = 'pending';
  #value;
  #onFulFillCallbacks = [];
  #onRejectCallbacks = [];
  constructor (fn) {
    const resolve = (value) = > {
      if (this[status] === 'pending') {
        this.#value = value;
        this[status] = 'fulfilled';
        this.#onFulFillCallbacks.forEach(fn= >fn()); }}const reject = (value) = > {
      this.#value = value;
      this[status] = 'rejected';
      this.#onRejectCallbacks.forEach(fn= > fn());
    }
    try {
      fn(resolve, reject)
    } catch(err) { reject(err); }}static resolve (value) {
    return new MyPromise((res) = > res(value))
  }

  then (resolve, reject) {
    const promise = new MyPromise((res, rej) = >  {
      if (this[status] === 'fulfilled') queueMicrotask(() = > resolvePromise(promise, resolve(this.#value), res, rej))
      else if (this[status] === 'rejected') queueMicrotask(() = > resolvePromise(promise, reject(this.#value), res, rej))
      else if (this[status] === 'pending') {
        this.#onFulFillCallbacks.push(() = > queueMicrotask(() = > resolvePromise(promise, resolve(this.#value), res, rej)))
        this.#onRejectCallbacks.push(() = > queueMicrotask(() = > resolvePromise(promise, reject(this.#value), res, rej)))
      }
    })
    returnpromise; }}function resolvePromise (promise, value, resolve, reject) {
  try {
    if (value instanceof Promise) {
      promise.then.call(value, res= > resolvePromise(promise, res, resolve, reject))
    } else{ resolve(value); }}catch (err) {
    reject(err)
  }
}
Copy the code