utils.js 4.41 KB
Newer Older
1 2
import moment from 'moment';

偏右's avatar
偏右 committed
3
export function fixedZero(val) {
4 5 6
  return val * 1 < 10 ? `0${val}` : val;
}

偏右's avatar
偏右 committed
7
export function getTimeDistance(type) {
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  const now = new Date();
  const oneDay = 1000 * 60 * 60 * 24;

  if (type === 'today') {
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);
    return [moment(now), moment(now.getTime() + (oneDay - 1000))];
  }

  if (type === 'week') {
    let day = now.getDay();
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);

    if (day === 0) {
      day = 6;
    } else {
      day -= 1;
    }

jim's avatar
jim committed
30
    const beginTime = now.getTime() - day * oneDay;
31

jim's avatar
jim committed
32
    return [moment(beginTime), moment(beginTime + (7 * oneDay - 1000))];
33 34 35 36 37 38 39 40 41
  }

  if (type === 'month') {
    const year = now.getFullYear();
    const month = now.getMonth();
    const nextDate = moment(now).add(1, 'months');
    const nextYear = nextDate.year();
    const nextMonth = nextDate.month();

jim's avatar
jim committed
42 43 44 45
    return [
      moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`),
      moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000),
    ];
46 47 48 49 50 51 52 53
  }

  if (type === 'year') {
    const year = now.getFullYear();

    return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
  }
}
ddcat1115's avatar
ddcat1115 committed
54

55
export function getPlainNode(nodeList, parentPath = '') {
ddcat1115's avatar
ddcat1115 committed
56
  const arr = [];
jim's avatar
jim committed
57
  nodeList.forEach(node => {
ddcat1115's avatar
ddcat1115 committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    const item = node;
    item.path = `${parentPath}/${item.path || ''}`.replace(/\/+/g, '/');
    item.exact = true;
    if (item.children && !item.component) {
      arr.push(...getPlainNode(item.children, item.path));
    } else {
      if (item.children && item.component) {
        item.exact = false;
      }
      arr.push(item);
    }
  });
  return arr;
}

73 74 75
export function digitUppercase(n) {
  const fraction = ['θ§’', 'εˆ†'];
  const digit = ['ι›Ά', 'ε£Ή', 'θ΄°', '叁', '肆', '伍', '陆', 'ζŸ’', '捌', 'ηŽ–'];
jim's avatar
jim committed
76
  const unit = [['ε…ƒ', 'δΈ‡', 'δΊΏ'], ['', 'ζ‹Ύ', 'δ½°', '仟']];
77 78 79
  let num = Math.abs(n);
  let s = '';
  fraction.forEach((item, index) => {
jim's avatar
jim committed
80
    s += (digit[Math.floor(num * 10 * 10 ** index) % 10] + item).replace(/ι›Ά./, '');
81 82 83 84 85 86 87 88 89 90 91 92
  });
  s = s || 'ζ•΄';
  num = Math.floor(num);
  for (let i = 0; i < unit[0].length && num > 0; i += 1) {
    let p = '';
    for (let j = 0; j < unit[1].length && num > 0; j += 1) {
      p = digit[num % 10] + unit[1][j] + p;
      num = Math.floor(num / 10);
    }
    s = p.replace(/(ι›Ά.)*ι›Ά$/, '').replace(/^$/, 'ι›Ά') + unit[0][i] + s;
  }

jim's avatar
jim committed
93 94 95 96
  return s
    .replace(/(ι›Ά.)*ι›Άε…ƒ/, 'ε…ƒ')
    .replace(/(ι›Ά.)+/g, 'ι›Ά')
    .replace(/^ζ•΄$/, 'ι›Άε…ƒζ•΄');
97
}
ddcat1115's avatar
ddcat1115 committed
98 99 100

function getRelation(str1, str2) {
  if (str1 === str2) {
jim's avatar
jim committed
101
    console.warn('Two path are equal!'); // eslint-disable-line
ddcat1115's avatar
ddcat1115 committed
102 103 104 105 106 107 108 109 110 111 112
  }
  const arr1 = str1.split('/');
  const arr2 = str2.split('/');
  if (arr2.every((item, index) => item === arr1[index])) {
    return 1;
  } else if (arr1.every((item, index) => item === arr2[index])) {
    return 2;
  }
  return 3;
}

113
function getRenderArr(routes) {
ddcat1115's avatar
ddcat1115 committed
114 115 116 117
  let renderArr = [];
  renderArr.push(routes[0]);
  for (let i = 1; i < routes.length; i += 1) {
    let isAdd = false;
118
    // ζ˜―ε¦εŒ…ε«
ddcat1115's avatar
ddcat1115 committed
119
    isAdd = renderArr.every(item => getRelation(item, routes[i]) === 3);
120
    // εŽ»ι‡
ddcat1115's avatar
ddcat1115 committed
121 122 123 124 125
    renderArr = renderArr.filter(item => getRelation(item, routes[i]) !== 1);
    if (isAdd) {
      renderArr.push(routes[i]);
    }
  }
126 127 128 129 130 131 132 133 134 135
  return renderArr;
}

/**
 * Get router routing configuration
 * { path:{name,...param}}=>Array<{name,path ...param}>
 * @param {string} path
 * @param {routerData} routerData
 */
export function getRoutes(path, routerData) {
jim's avatar
jim committed
136 137 138
  let routes = Object.keys(routerData).filter(
    routePath => routePath.indexOf(path) === 0 && routePath !== path
  );
139 140 141 142 143
  // Replace path to '' eg. path='user' /user/name => name
  routes = routes.map(item => item.replace(path, ''));
  // Get the route to be rendered to remove the deep rendering
  const renderArr = getRenderArr(routes);
  // Conversion and stitching parameters
jim's avatar
jim committed
144
  const renderRoutes = renderArr.map(item => {
ddcat1115's avatar
ddcat1115 committed
145 146
    const exact = !routes.some(route => route !== item && getRelation(route, item) === 1);
    return {
147
      exact,
ddcat1115's avatar
ddcat1115 committed
148
      ...routerData[`${path}${item}`],
ddcat1115's avatar
ddcat1115 committed
149 150 151 152 153 154
      key: `${path}${item}`,
      path: `${path}${item}`,
    };
  });
  return renderRoutes;
}
155 156 157 158 159 160 161

/* eslint no-useless-escape:0 */
const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g;

export function isUrl(path) {
  return reg.test(path);
}