typeTool.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. let matchNumberStr = ['0',]
  2. // 部分关键字转换为boolean值
  3. let falseField = ['false','False','FALSE','null','Null','NULL','undefined','Undefined'];
  4. let trueField = ['true','True','TRUE'];
  5. // 类型转换
  6. function toString(){
  7. // 情况1 非对象,直接调用内部值
  8. }
  9. // 转换为number类型,先用toBoolean转换为布尔类型
  10. function toNumber(v){
  11. if(isString(v)){
  12. if(falseField.includes(v)){
  13. v = toBoolean(v)
  14. }else if (trueField.includes(v)){
  15. v = toBoolean(v)
  16. }
  17. }
  18. return Number(v)
  19. }
  20. // 考虑字段类型
  21. function toBoolean(v){
  22. // 字符串类型
  23. if (isString(v)){
  24. if(falseField.includes(v)){
  25. return false
  26. }else if (trueField.includes(v)){
  27. return true
  28. }else{
  29. Boolean(v)
  30. }
  31. }
  32. }
  33. function isArray(v){
  34. return v instanceof Array
  35. }
  36. function isString(v){
  37. return v instanceof String || typeof v === 'string'
  38. }
  39. function isNumber(v){
  40. return v instanceof Number
  41. }
  42. function isObject(v){
  43. return v instanceof Object
  44. }
  45. module.exports = {
  46. toNumber,
  47. toString,
  48. toBoolean
  49. }