获取文件后缀名

  1. const getExt = (filename) => {
  2. if (typeof filename == 'string') {
  3. return filename.split('.').pop().toLowerCase()
  4. } else {
  5. throw new Error('filename must be a string type')
  6. }
  7. }

生成随机字符串

  1. const uuid = (length, chars) => {
  2. chars =
  3. chars ||
  4. '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  5. length = length || 8
  6. let result = ''
  7. for (let i = length; i > 0; --i) {
  8. result +=
  9. // chars[Math.floor(Math.random() * chars.length)]
  10. chars[(Math.random() * chars.length) | 0]
  11. }
  12. return result
  13. }

image.png

保留小数点后n位

  1. const cutNumber = (num, no) => {
  2. if (typeof num != 'number') {
  3. num = Number(num)
  4. }
  5. return Number(num.toFixed(no))
  6. }
  7. const a = 38 / 3
  8. console.log(cutNumber(a, 2))//12.67