cstr.mjs

import isSymbol from 'lodash-es/isSymbol.js'
import isestr from './isestr.mjs'
import isnum from './isnum.mjs'


/**
 * 輸入轉字串
 * 若輸入不是數字或字串時則回傳空字串
 *
 * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/cstr.test.mjs Github}
 * @memberOf wsemi
 * @param {Number|String} v 輸入資料
 * @returns {String} 回傳字串
 * @example
 *
 * console.log(cstr(2.25))
 * // => '2.25'
 *
 */
function cstr(v) {

    //check
    if (!isestr(v) && !isnum(v) && !isSymbol(v)) {
        return ''
    }

    let r = ''
    try {
        r = String(v)
    }
    catch (err) {
    }
    try {
        r = v.toString()
    }
    catch (err) {
    }

    return r
}


export default cstr