util.js 1.1 KB

12345678910111213141516171819202122232425262728
  1. module.exports = {
  2. // 一维数组转二维数组
  3. listToMatrix(list, elementsPerSubArray) {
  4. let matrix = [], i, col, row;
  5. for (i = 0, row = -1; i < list.length; i += 1) {
  6. col = i % elementsPerSubArray;
  7. row = Math.floor(i / elementsPerSubArray);
  8. if (!matrix[row]) matrix[row] = [0, 0, 0];
  9. matrix[row][col] = list[i];
  10. }
  11. return matrix;
  12. },
  13. // 选中文件之后,计算一个随机的短文件名
  14. getRandFileName: function (filePath) {
  15. var extIndex = filePath.lastIndexOf('.');
  16. var extName = extIndex === -1 ? '' : filePath.substr(extIndex);
  17. return parseInt('' + Date.now() + Math.floor(Math.random() * 900 + 100), 10).toString(36) + extName;
  18. },
  19. // 对更多字符编码的 url encode 格式
  20. camSafeUrlEncode: function (str) {
  21. return encodeURIComponent(str)
  22. .replace(/!/g, '%21')
  23. .replace(/'/g, '%27')
  24. .replace(/\(/g, '%28')
  25. .replace(/\)/g, '%29')
  26. .replace(/\*/g, '%2A');
  27. },
  28. };