Skip to content

Commit

Permalink
Add $.isNumeric()
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jul 13, 2016
1 parent 0f2ab27 commit 6ea6dd8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/zepto.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ var Zepto = (function() {
return true
}

$.isNumeric = function(val) {
var num = Number(val), type = typeof val
return val != null && type != 'boolean' &&
(type != 'string' || val.length) &&
!isNaN(num) && isFinite(num) || false
}

$.inArray = function(elem, array, i){
return emptyArray.indexOf.call(array, elem, i)
}
Expand Down
21 changes: 21 additions & 0 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,27 @@ <h1>Zepto Core unit tests</h1>
t.assertTrue($.isWindow($('iframe').get(0).contentWindow))
},

testDollarIsNumeric: function(t){
t.assertTrue($.isNumeric( '-10' ))
t.assertTrue($.isNumeric( '0' ))
t.assertTrue($.isNumeric( 0xFF ))
t.assertTrue($.isNumeric( '0xFF' ))
t.assertTrue($.isNumeric( '8e5' ))
t.assertTrue($.isNumeric( '3.1415' ))
t.assertTrue($.isNumeric( +10 ))
t.assertTrue($.isNumeric( 0144 ))

t.assertFalse($.isNumeric( '-0x42' ), '-0x42')
t.assertFalse($.isNumeric( '7.2acdgs' ), '7.2acdgs')
t.assertFalse($.isNumeric( '' ), 'empty string')
t.assertFalse($.isNumeric( {} ), 'empty hash')
t.assertFalse($.isNumeric( NaN ), 'NaN')
t.assertFalse($.isNumeric( null ), 'null')
t.assertFalse($.isNumeric( true ), 'true')
t.assertFalse($.isNumeric( Infinity ), 'Infinity')
t.assertFalse($.isNumeric( undefined ), 'undefined')
},

// test to see if we augment iOS 3.2 with String#trim()
testTrim: function(t){
t.assertEqual("blah", " blah ".trim())
Expand Down

0 comments on commit 6ea6dd8

Please sign in to comment.