PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/_legacy/php/var/settype.js

http://github.com/kvz/phpjs
JavaScript | 123 lines | 106 code | 3 blank | 14 comment | 58 complexity | f3200af44c1cd25c161ffdfe305e24fb MD5 | raw file
  1. module.exports = function settype (vr, type) {
  2. // discuss at: https://locutus.io/php/settype/
  3. // original by: Waldo Malqui Silva (https://waldo.malqui.info)
  4. // improved by: Kevin van Zonneveld (https://kvz.io)
  5. // revised by: Brett Zamir (https://brett-zamir.me)
  6. // note: Credits to Crockford also
  7. // note: only works on global variables, and "vr" must be passed in as a string
  8. // example 1: var $foo = '5bar'
  9. // example 1: settype('$foo', 'integer')
  10. // example 1: $result = $foo
  11. // returns 1: 5
  12. // example 2: var $foo = true
  13. // example 2: settype('$foo', 'string')
  14. // example 2: $result = $foo
  15. // returns 2: '1'
  16. var isArray = function (arr) {
  17. return typeof arr === 'object' && typeof arr.length === 'number' && !(arr.propertyIsEnumerable('length')) &&
  18. typeof arr.splice === 'function'
  19. }
  20. var v, mtch, i, obj
  21. v = this[vr] ? this[vr] : vr
  22. try {
  23. switch (type) {
  24. case 'boolean':
  25. if (isArray(v) && v.length === 0) {
  26. this[vr] = false
  27. } else if (v === '0') {
  28. this[vr] = false
  29. } else if (typeof v === 'object' && !isArray(v)) {
  30. var _, lgth = false
  31. for (_ in v) {
  32. lgth = true
  33. break;
  34. }
  35. this[vr] = lgth
  36. } else {
  37. this[vr] = !!v
  38. }
  39. break
  40. case 'integer':
  41. if (typeof v === 'number') {
  42. this[vr] = parseInt(v, 10)
  43. } else if (typeof v === 'string') {
  44. mtch = v.match(/^([+\-]?)(\d+)/)
  45. if (!mtch) {
  46. this[vr] = 0
  47. } else {
  48. this[vr] = parseInt(v, 10)
  49. }
  50. } else if (v === true) {
  51. this[vr] = 1
  52. } else if (v === false || v === null) {
  53. this[vr] = 0
  54. } else if (isArray(v) && v.length === 0) {
  55. this[vr] = 0
  56. } else if (typeof v === 'object') {
  57. this[vr] = 1
  58. }
  59. break
  60. case 'float':
  61. if (typeof v === 'string') {
  62. mtch = v.match(/^([+\-]?)(\d+(\.\d+)?|\.\d+)([eE][+\-]?\d+)?/)
  63. if (!mtch) {
  64. this[vr] = 0
  65. } else {
  66. this[vr] = parseFloat(v)
  67. }
  68. } else if (v === true) {
  69. this[vr] = 1
  70. } else if (v === false || v === null) {
  71. this[vr] = 0
  72. } else if (isArray(v) && v.length === 0) {
  73. this[vr] = 0
  74. } else if (typeof v === 'object') {
  75. this[vr] = 1
  76. }
  77. break
  78. case 'string':
  79. if (v === null || v === false) {
  80. this[vr] = ''
  81. } else if (isArray(v)) {
  82. this[vr] = 'Array'
  83. } else if (typeof v === 'object') {
  84. this[vr] = 'Object'
  85. } else if (v === true) {
  86. this[vr] = '1'
  87. } else {
  88. this[vr] += ''
  89. } // numbers (and functions?)
  90. break
  91. case 'array':
  92. if (v === null) {
  93. this[vr] = []
  94. } else if (typeof v !== 'object') {
  95. this[vr] = [v]
  96. }
  97. break
  98. case 'object':
  99. if (v === null) {
  100. this[vr] = {}
  101. } else if (isArray(v)) {
  102. for (i = 0, obj = {}; i < v.length; i++) {
  103. obj[i] = v
  104. }
  105. this[vr] = obj
  106. } else if (typeof v !== 'object') {
  107. this[vr] = {
  108. scalar: v
  109. }
  110. }
  111. break
  112. case 'null':
  113. delete this[vr]
  114. break
  115. }
  116. return true
  117. } catch (e) {
  118. return false
  119. }
  120. }