PageRenderTime 69ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/_octopress/source/functions/is_int/_comments.html

https://gitlab.com/orvi2014/phpjs
HTML | 212 lines | 158 code | 53 blank | 1 comment | 0 complexity | 8dbbc64c1e6f046c0b9bfc04c64b5c87 MD5 | raw file
  1. <!-- Generated by Rakefile:build -->
  2. <strong>
  3. <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
  4. </strong>
  5. on 2012-07-04 17:02:04 <br />
  6. @CoursesWeb: Is there some case where your code catches something ours does not?
  7. <hr />
  8. <strong>
  9. <a href="http://www.coursesweb.net" rel="nofollow">CoursesWeb</a>
  10. </strong>
  11. on 2012-04-30 08:20:25 <br />
  12. Hi,
  13. For is_int i use this version:
  14. <pre><code>
  15. function is_int(n) {
  16. return typeof(n)===&quot;number&quot; &amp;&amp; Math.round(n) == n;
  17. }
  18. </code></pre>
  19. <hr />
  20. <strong>
  21. <a href="http://blog.kukawski.pl" rel="nofollow">Rafa? Kukawski</a>
  22. </strong>
  23. on 2011-03-23 14:55:02 <br />
  24. even shorter
  25. <pre><code>return mixed_var === (mixed_var | 0);</code></pre>
  26. <hr />
  27. <strong>
  28. <a href="http://www.ita.es" rel="nofollow">Enrique Melendez</a>
  29. </strong>
  30. on 2011-03-23 11:07:30 <br />
  31. version in one line:
  32. <pre><code>
  33. return typeof mixed_var !== 'number' ? false : !(mixed_var % 1);
  34. </code></pre>
  35. <hr />
  36. <strong>
  37. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  38. </strong>
  39. on 2009-12-14 15:54:34 <br />
  40. @ WebDevHobo: Thanks I fixed the link to the php docs throughout the site. As for is_int: yeah that's a real bummer. We can get close to PHP but sometimes it just isn't possible to nail it.
  41. Did fix the jslint warning though, will disappear soon ; )
  42. <hr />
  43. <strong>
  44. <a href="http://webdevhobo.blogspot.com" rel="nofollow">WebDevHobo</a>
  45. </strong>
  46. on 2009-12-13 09:24:43 <br />
  47. Nevermind my comment on the 0.0 issue, I should read the documentation before saying stuff.
  48. <hr />
  49. <strong>
  50. <a href="http://webdevhobo.blogspot.com" rel="nofollow">WebDevHobo</a>
  51. </strong>
  52. on 2009-12-13 01:12:01 <br />
  53. Also, the link to the PHP is_int function(to the official PHP docs) is broken. Apparently, not an underscore, but a minus sign is used in the link.
  54. My guess: they want to reserve underscores for replacing spaces.
  55. <hr />
  56. <strong>
  57. <a href="http://webdevhobo.blogspot.com" rel="nofollow">WebDevHobo</a>
  58. </strong>
  59. on 2009-12-13 01:09:14 <br />
  60. <pre><code>is_int(0.0)</code></pre> returns true
  61. Screenshot: http://i.imgur.com/rvtAc.png
  62. Any number of 0's may be added after the . sign, result stays true. The moment you add any other number somewhere behind the . sign, result becomes false.
  63. Supposed bugfix: Check for the . sign and return false whenever encountered. Not sure how to do that, which is why I didn't code it myself.
  64. That is, if we [b]want[/b] this to return false? 0.0 is the exact same as 0, far as I know.
  65. Also: &quot;On line: #25: Confusing use of '!'.&quot;
  66. Far as I know, what happens here is the &quot;order of execution&quot;. The parser will first execute that within the () and then the ! will be applied to the result, which is what is needed. Not very confusing.
  67. <hr />
  68. <strong>
  69. <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
  70. </strong>
  71. on 2009-11-07 13:25:46 <br />
  72. @WebDevHobo: Thanks for pointing out those useful functions. We needed the extra checks in our is_numeric(), though I realized later (see http://github.com/kvz/phpjs/commit/eb83b48c940e4f8c5548d63c178c2fae2c0ba729 ) that we also needed to prevent arrays being treated as numeric. As far as integer checking, while we still needed our check for type (e.g., your isInteger() will return true for true), the use of modulus is I believe a sound replacement for the more complex comparison we had earlier. Fixed at http://github.com/kvz/phpjs/commit/06388a893c7a5bcb1876ada68997c04241fc6d52 . Thanks for the functions!
  73. <hr />
  74. <strong>
  75. <a href="http://webdevhobo.blogspot.com" rel="nofollow">WebDevHobo</a>
  76. </strong>
  77. on 2009-10-30 02:50:16 <br />
  78. Interesting implementation. I'm not that big with Javascript, so this is real nice.
  79. Here's 2 functions that I usually use for this kind of stuff:
  80. <pre><code>function isInteger(value){
  81. return (!(value % 1));
  82. }
  83. function isNumber(n){
  84. return n != null &amp;&amp; n != &quot;&quot; &amp;&amp; typeof(n) != &quot;boolean&quot; &amp;&amp; !isNaN(n);
  85. }</code></pre>
  86. Don't know what flaws might be in them, but they sure did the trick uptill now.
  87. <hr />
  88. <strong>
  89. <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
  90. </strong>
  91. on 2009-09-08 14:17:27 <br />
  92. @Jordan: It's false for me (as it should be)--not sure what you mean...
  93. <hr />
  94. <strong>
  95. Jordan
  96. </strong>
  97. on 2009-09-08 13:09:10 <br />
  98. <pre><code>
  99. is_int('23,5') return true...
  100. </code></pre>
  101. <hr />
  102. <strong>
  103. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  104. </strong>
  105. on 2009-01-25 14:13:05 <br />
  106. @ Matt Bradley: Thanks for you function. The current implementation did as well. But in fact, I checked the PHP manual: and that's not supposed to happen. So I've actually changed your implementation that also the type is looked at. And now all the examples in the php manual workout with our function as well.
  107. <hr />
  108. <strong>
  109. <a href="http://www.inventpartners" rel="nofollow">Matt Bradley</a>
  110. </strong>
  111. on 2009-01-20 15:15:26 <br />
  112. This version will also work with strings
  113. function is_int(value){
  114. if(parseFloat(value) == parseInt(value)){
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. }
  120. <hr />
  121. <strong>
  122. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  123. </strong>
  124. on 2008-12-03 13:13:23 <br />
  125. @ Paulo Ricardo F. Santos: OK, I've added a note to the is_int code, we'll leave it at that then.
  126. <hr />
  127. <strong>
  128. Paulo Ricardo F. Santos
  129. </strong>
  130. on 2008-12-03 12:47:44 <br />
  131. @ Kevin: Yep, the value is evaluated before it is assigned - this should occurs through the Number object constructor. A time that JS lacks different objects to Float/Integer values, I think that, unfortunately, there's no way to match this approach. ;/
  132. <hr />
  133. <strong>
  134. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  135. </strong>
  136. on 2008-12-01 09:29:32 <br />
  137. @ Paulo Ricardo F. Santos: thanks. I believe that 1.0 is simplified to 1 as soon before it can be accessed by the function. if I echo mixed_var in the function's first statement, it's already 1. So if anybody knows a good way to get this example working:
  138. <pre><code>
  139. // * example 3: is_int(1.0);
  140. // * returns 3: false
  141. </code></pre>
  142. please let me know!
  143. <hr />
  144. <strong>
  145. Paulo Ricardo F. Santos
  146. </strong>
  147. on 2008-11-29 01:38:03 <br />
  148. Note: both current/my implementation isn't PHP compliant at all, since in JS '1.0 === 1' is a True comparison.
  149. <hr />
  150. <strong>
  151. Paulo Ricardo F. Santos
  152. </strong>
  153. on 2008-11-28 23:19:12 <br />
  154. KISS way: just do value/type comparison with parseInt() result. :)
  155. <pre><code>function is_int(mixed_var)
  156. {
  157. return mixed_var === parseInt(mixed_var * 1);
  158. }</code></pre>
  159. <hr />