Permalink
Browse files

Add $.fn.numericValue()

  • Loading branch information...
1 parent eb20e20 commit 73d445fe28ed907241ecaf423ea4df6fe2d198fc @noniq noniq committed May 23, 2012
View
@@ -1,5 +1,8 @@
+## Devel
+* Add $.fn.numericValue().
+
## Version 0.1.0
-* Add conditionalVisibility().
+* Add $.fn.conditionalVisibility().
* Now requires CoffeeScript.
## Version 0.0.2
@@ -0,0 +1,34 @@
+@javascript
+Feature: Numeric value of input elements
+
+ Background:
+ Given I visit the test page for "numeric value"
+
+ Scenario Outline: English locale
+ When I fill in "textfield" with "<String>"
+ Then the numeric value of "textfield" should be "<Value>"
+
+ Examples:
+ | String | Value |
+ | 1 | 1 |
+ | 1.2 | 1.2 |
+ | 1234.56 | 1234.56 |
+ | 1,234.56 | 1234.56 |
+ | 1,234 | 1234 |
+ | abc | 0 |
+ | | 0 |
+
+ Scenario Outline: German locale
+ Given the HTML element has a lang attribute set to "de"
+ When I fill in "textfield" with "<String>"
+ Then the numeric value of "textfield" should be "<Value>"
+
+ Examples:
+ | String | Value |
+ | 1 | 1 |
+ | 1,2 | 1.2 |
+ | 1234,56 | 1234.56 |
+ | 1.234,56 | 1234.56 |
+ | 1.234 | 1234 |
+ | abc | 0 |
+ | | 0 |
@@ -0,0 +1,7 @@
+Given /^the HTML element has a lang attribute set to "([^"]*)"$/ do |lang|
+ page.execute_script("$('html').attr('lang', '#{lang}')")
+end
+
+Then /^the numeric value of "([^"]*)" should be "([^"]*)"$/ do |input, value|
+ page.evaluate_script("$('##{input}').numericValue()").should be_within(0.0001).of value.to_f
+end
@@ -3,3 +3,8 @@
When /^I visit the test page for "([^"]*)"$/ do |feature|
visit "/#{feature.parameterize '_'}"
end
+
+When /^I fill in "([^"]*)" with "([^"]*)"$/ do |input, value|
+ fill_in input, :with => value
+end
+
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Test</title>
+ <%= "<script src='../vendor/jquery-#{@jquery_version}.js'></script>" %>
+ <script src="/assets/javascripts/da-js.js"></script>
+ <script>$.fx.off = true;</script>
+ </head>
+ <body>
+ <h1>Test: Numeric Value</h1>
+ <form>
+ <input id="textfield">
+ </form>
+ </body>
+</html>
@@ -0,0 +1,16 @@
+# Like val(), but return the value parsed as float (or 0 if the value is not numeric).
+#
+# Thousands delimiters (",") are handled correctly.
+# German thousands delimiters (".") and separators (",") are handled correctly if the lang attribtue of the HTML element is set to "de".
+
+$.fn.numericValue = ->
+
+ delocalize = (val) ->
+ if $("html").attr("lang") == "de"
+ val.replace(".", "").replace(",", ".")
+ else
+ val.replace(",", "")
+
+ val = delocalize($(this).val())
+ return 0 unless $.isNumeric(val)
+ parseFloat(val)

0 comments on commit 73d445f

Please sign in to comment.