common.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * 换算数据大小
  3. * @param {number} size
  4. * @returns {string} 换算后的数据大小,两位小数带单位
  5. */
  6. export function conversionSize(size) {
  7. let _size = size;
  8. const units = ['B', 'KB', 'MB', 'GB'];
  9. let factor = 0;
  10. while (_size > 1024 && factor < units.length - 1) {
  11. _size /= 1024;
  12. factor += 1;
  13. }
  14. return `${_size.toFixed(2)}${units[factor]}`;
  15. }
  16. export const tone_data = [
  17. ['ā', 'á', 'ǎ', 'à', 'a'],
  18. ['ō', 'ó', 'ǒ', 'ò', 'o'],
  19. ['ē', 'é', 'ě', 'è', 'e'],
  20. ['ī', 'í', 'ǐ', 'ì', 'i'],
  21. ['ū', 'ú', 'ǔ', 'ù', 'u'],
  22. ['ǖ', 'ǘ', 'ǚ', 'ǜ', 'ü'],
  23. ['ǖ', 'ǘ', 'ǚ', 'ǜ', 'ü'],
  24. ['Ā', 'Á', 'Â', 'À', 'A'],
  25. ['Ō', 'Ó', 'Ô', 'Ò', 'O'],
  26. ['Ē', 'É', 'Ê', 'È', 'E'],
  27. ['Ī', 'Í', 'Î', 'Ì', 'I'],
  28. ['Ū', 'Ú', 'Û', 'Ù', 'U'],
  29. ];
  30. /**
  31. * 添加声调
  32. * @param {Number} number
  33. * @param {String} con
  34. * @returns String
  35. */
  36. export function addTone(number, con) {
  37. const zmList = ['a', 'o', 'e', 'i', 'u', 'v', 'ü', 'A', 'O', 'E', 'I', 'U'];
  38. let cons = con;
  39. if (number) {
  40. for (let i = 0; i < zmList.length; i++) {
  41. let zm = zmList[i];
  42. if (con.includes(zm)) {
  43. let zm2 = tone_data[i][number - 1];
  44. if (con.includes('iu')) {
  45. zm2 = tone_data[4][number - 1];
  46. cons = con.replace('u', zm2);
  47. } else if (con.includes('ui')) {
  48. zm2 = tone_data[3][number - 1];
  49. cons = con.replace('i', zm2);
  50. } else if (/yv|jv|qv|xv/.test(con)) {
  51. zm2 = tone_data[4][number - 1];
  52. cons = con.replace('v', zm2);
  53. } else if (/yü|jü|qü|xü/.test(con)) {
  54. zm2 = tone_data[4][number - 1];
  55. cons = con.replace('ü', zm2);
  56. } else {
  57. cons = con.replace(zm, zm2);
  58. }
  59. break;
  60. }
  61. }
  62. }
  63. return cons;
  64. }
  65. export function handleToneValue(valItem) {
  66. let numList = [];
  67. if (/[A-Za-zü]+\d/g.test(valItem)) {
  68. valItem.split('').forEach((item, i) => {
  69. if (/\d/.test(item)) {
  70. let numIndex = numList.length === 0 ? 0 : numList[numList.length - 1].index;
  71. let con = valItem.substring(numIndex, i).replace(/\d/g, '');
  72. numList.push({
  73. number: item,
  74. con,
  75. });
  76. }
  77. });
  78. } else {
  79. numList = [];
  80. }
  81. return numList.length === 0 ? [{ con: valItem }] : numList;
  82. }
  83. /**
  84. * @description 是否为 true
  85. * @param {'true'|'false'} value
  86. * @returns {boolean}
  87. */
  88. export function isTrue(value) {
  89. return value === 'true';
  90. }