All files utils.tsx

34.38% Statements 11/32
0% Branches 0/10
25% Functions 3/12
36.67% Lines 11/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 961x               1x 1x                   1x 1x                   1x 1x               1x                   1x                       1x                     1x                                              
const fs = require('fs')
 
/**
 * 十六进制转十进制,获取码值
 * @param {*} code 十六进制
 * utf-8 编码格式 如 0x2001  unicode 编码格式 \u2001
 * str.charCodeAt(0) Unicode 编码值 20013
 */
export function unicodeToCodePoint (code) {
  return parseInt(code, 16)
}
 
 
/**
 * 十进制转十六进制
 * @param {*} code 十进制
 * utf-8 编码格式 如 0x2001  unicode 编码格式 \u2001
 * str.charCodeAt(0) Unicode 编码值 20013
 */
export function codePointToUnicode (codePoint) {
  return codePoint.toString(16)
}
 
 
/**
 * unicode 转 string,根据码值获取对应字符
 * @param {*} code 十六进制
 * utf-8 编码格式 如 0x2001  unicode 编码格式 \u2001
 * str.charCodeAt(0) Unicode 编码值 20013
 */
export function unicodeToString (code) {
  return String.fromCharCode(code)
}
 
/**
 * utf-8 转十进制字符串
 * @param {*} code 十六进制
 * utf-8 编码格式 如 0x2001  unicode 编码格式 \u2001
 */
export function utf8ToString (code) {
  return String.fromCharCode(parseInt(code, 16))
}
 
/*
 * [writeToFile description]
 * @param  {[type]} data [数组数据列表]
 * @param  {[type]} path [写入的路径]
 */
// eslint-disable-next-line no-unused-vars
export function writeToFile (data, path, calllback?:() => void) {
  if(typeof data === 'object')data = JSON.stringify(data, null, '\t')
  fs.writeFile(path, data, 'utf-8', err => {
    if (err) throw err
    calllback && calllback()
  })
}
 
/*
 * [readToFile 读取文件]
 * @param  {[type]} path [读取路径]
 */
export function readToFile (path, calllback?:() => void) {
  const data = fs.readFileSync(path, 'UTF-8')
  calllback && calllback()
  return data
}
 
 
/*
 * [readToFile 读取文件]
 * @param  {[type]} path [读取路径]
 */
export function readStreamToFile (path, calllback?:(dataString:any) => void) {
  const promise = new Promise((resolve, reject) => {
    const stream = fs.createReadStream(path, {
      'encoding': 'utf8'
    })
 
    let dataString = ''
 
    stream.on('data', data => {
      dataString += data
    })
 
    stream.on('error', err => {
      reject(err)
    })
 
    stream.on('end', () => {
      resolve(dataString)
      calllback && calllback(dataString)
    })
  })
  return promise
}