PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/_legacy/php/var/settype.js

https://github.com/andriuspetrauskis/phpjs
JavaScript | 122 lines | 105 code | 3 blank | 14 comment | 58 complexity | 4592988b781abca82e94b4b034b48968 MD5 | raw file
  1. module.exports = function settype (vr, type) {
  2. // discuss at: http://locutus.io/php/settype/
  3. // original by: Waldo Malqui Silva (http://waldo.malqui.info)
  4. // improved by: Kevin van Zonneveld (http://kvz.io)
  5. // revised by: Brett Zamir (http://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 (i in v) {
  32. lgth = true
  33. }
  34. this[vr] = lgth
  35. } else {
  36. this[vr] = !!v
  37. }
  38. break
  39. case 'integer':
  40. if (typeof v === 'number') {
  41. this[vr] = parseInt(v, 10)
  42. } else if (typeof v === 'string') {
  43. mtch = v.match(/^([+\-]?)(\d+)/)
  44. if (!mtch) {
  45. this[vr] = 0
  46. } else {
  47. this[vr] = parseInt(v, 10)
  48. }
  49. } else if (v === true) {
  50. this[vr] = 1
  51. } else if (v === false || v === null) {
  52. this[vr] = 0
  53. } else if (isArray(v) && v.length === 0) {
  54. this[vr] = 0
  55. } else if (typeof v === 'object') {
  56. this[vr] = 1
  57. }
  58. break
  59. case 'float':
  60. if (typeof v === 'string') {
  61. mtch = v.match(/^([+\-]?)(\d+(\.\d+)?|\.\d+)([eE][+\-]?\d+)?/)
  62. if (!mtch) {
  63. this[vr] = 0
  64. } else {
  65. this[vr] = parseFloat(v, 10)
  66. }
  67. } else if (v === true) {
  68. this[vr] = 1
  69. } else if (v === false || v === null) {
  70. this[vr] = 0
  71. } else if (isArray(v) && v.length === 0) {
  72. this[vr] = 0
  73. } else if (typeof v === 'object') {
  74. this[vr] = 1
  75. }
  76. break
  77. case 'string':
  78. if (v === null || v === false) {
  79. this[vr] = ''
  80. } else if (isArray(v)) {
  81. this[vr] = 'Array'
  82. } else if (typeof v === 'object') {
  83. this[vr] = 'Object'
  84. } else if (v === true) {
  85. this[vr] = '1'
  86. } else {
  87. this[vr] += ''
  88. } // numbers (and functions?)
  89. break
  90. case 'array':
  91. if (v === null) {
  92. this[vr] = []
  93. } else if (typeof v !== 'object') {
  94. this[vr] = [v]
  95. }
  96. break
  97. case 'object':
  98. if (v === null) {
  99. this[vr] = {}
  100. } else if (isArray(v)) {
  101. for (i = 0, obj = {}; i < v.length; i++) {
  102. obj[i] = v
  103. }
  104. this[vr] = obj
  105. } else if (typeof v !== 'object') {
  106. this[vr] = {
  107. scalar: v
  108. }
  109. }
  110. break
  111. case 'null':
  112. delete this[vr]
  113. break
  114. }
  115. return true
  116. } catch (e) {
  117. return false
  118. }
  119. }