money_parser 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 17061821eaee57290483af05d265f37d828bc6d8
4
+ data.tar.gz: 75952160c7f5bb973714f05b3c369e700d215631
5
+ SHA512:
6
+ metadata.gz: 4cc1f9b20736daed4180792a83c98cfc7907cf6fc4d4c175dd2c53628e1e54a6d971ab5fdf52be5cad8ac2574f5a8ebc033db1c9a546eb30babfbc4ca1521985
7
+ data.tar.gz: 4211f0e1172937021ee438575dd52bcf3ab6b77a0066919c2867aeaf90d89c418e3749e348849121f689c364917aaf7cb9fb83d4e0b0fa3e7be26cd5b57e4a9a
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .rbenv-version
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+ gem 'rspec'
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Yuri Leikind
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,29 @@
1
+ Author:: Yuri Leikind
2
+
3
+ A simple function in Ruby and a similar function in Javascript which try
4
+ to parse a string containing
5
+ a money amount in any format and return a float for javascript and a <tt>BigDecimal</tt> for Ruby.
6
+
7
+ The functions return <tt>nil</tt>/<tt>null</tt> if the input is not a a money amount.
8
+
9
+ <tt>specification.rb</tt> contains input/output pairs.
10
+
11
+ The tests are generated by <tt>rake generate_tests</tt> from <tt>specification.rb</tt>.
12
+
13
+ To install add
14
+
15
+ gem "money_parser"
16
+
17
+ or
18
+
19
+ gem "money_parser", :git => 'git://github.com/leikind/money_parser.git'
20
+
21
+ to your <tt>Gemfile</tt>, and
22
+
23
+ //= require parse_money
24
+
25
+ to your <tt>app/assets/javascripts/application.js</tt>, and run <tt>bundle install</tt>.
26
+
27
+ After that you can call JS function <tt>parseMoney()</tt> on the client side,
28
+ and Ruby function <tt>MoneyParser.parse</tt> on the server side.
29
+
data/Rakefile ADDED
@@ -0,0 +1,101 @@
1
+ # © Yuri Leikind 2014
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'rdoc/task'
5
+
6
+ task :default => :rdoc
7
+
8
+ desc 'Generate documentation for the plugin.'
9
+ Rake::RDocTask.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'money_parser'
12
+ rdoc.options << '--line-numbers' << '--inline-source'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ def random_amount_of_spaces
18
+ ' ' * rand(2)
19
+ end
20
+
21
+ def randomly_replace_zeros_by_o(string)
22
+ indexes = string.
23
+ each_char.
24
+ each_with_index.
25
+ to_a.
26
+ select{|l, idx| l == '0'}.
27
+ map{|_, idx| idx }
28
+
29
+ return nil if indexes.empty?
30
+ idx = indexes[rand(indexes.size)]
31
+ idx2 = indexes[rand(indexes.size)]
32
+
33
+ string.clone.tap do |cloned|
34
+ cloned[idx] = 'O'
35
+ cloned[idx] = 'o'
36
+ end
37
+ end
38
+
39
+ def generate_tests specs
40
+ ruby_code = "require 'spec_helper'\ndescribe MoneyParser do\n"
41
+
42
+ js_code = "var assert = require(\"assert\")\n" +
43
+ "var parseMoney = require('../vendor/assets/javascripts/parseMoney');\n\n" +
44
+ "describe('moneyParse', function(){\n"
45
+
46
+
47
+ specs.inject([]) {|accu, pair|
48
+
49
+ money_string, result = pair
50
+
51
+ accu << [money_string, result]
52
+
53
+ unless result.nil?
54
+ accu << [random_amount_of_spaces + "-" + random_amount_of_spaces + money_string, result * -1, '(negative amount)']
55
+ end
56
+
57
+ tuple_with_o = [randomly_replace_zeros_by_o(money_string), result, '(with O instead of 0)']
58
+
59
+ accu << tuple_with_o unless tuple_with_o[0].nil?
60
+
61
+ accu << ['$' + money_string, result, '(with a dollar sign)']
62
+ accu << ['€' + money_string, result, '(with a euro sign)']
63
+ accu << ['£' + money_string, result, '(with a pound sign)']
64
+ accu << ['₤' + money_string, result, '(with a pound sign)']
65
+
66
+ accu
67
+ }.each {|money_string, result, comment|
68
+
69
+ expecting = if result
70
+ "BigDecimal.new(\"#{result.inspect}\")"
71
+ else
72
+ result.inspect
73
+ end
74
+ ruby_code << %!
75
+
76
+ it '\"#{money_string}\" should be parsed as #{result.inspect} #{comment}' do
77
+ MoneyParser.parse(\"#{money_string}\").should == #{expecting}
78
+ end\n!
79
+
80
+ js_code << %!
81
+ it('\"#{money_string}\" should be parsed as #{nil_to_null(result.inspect)} #{comment}', function(){
82
+ assert.equal(#{nil_to_null(result.inspect)}, parseMoney(\"#{money_string}\"));
83
+ });\n!
84
+
85
+
86
+ }
87
+ [ruby_code + "end\n", js_code + "})\n"]
88
+ end
89
+
90
+ def nil_to_null(null_or_other)
91
+ null_or_other == 'nil' ? 'null' : null_or_other
92
+ end
93
+
94
+ desc 'Generate tests'
95
+ task :generate_tests do |rdoc|
96
+ require './specification.rb'
97
+ ruby_code, js_code = generate_tests(SPECIFICATION)
98
+ File.open('./spec/money_parser_spec.rb', 'w'){|f| f.write ruby_code }
99
+ File.open('./test/money_parser_spec.js', 'w'){|f| f.write js_code }
100
+ end
101
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,40 @@
1
+ # encoding: UTF-8
2
+ # © Yuri Leikind 2014
3
+
4
+ require 'bigdecimal'
5
+
6
+ module MoneyParser
7
+
8
+ if Kernel.const_defined?('Rails')
9
+ class Engine < ::Rails::Engine
10
+ end
11
+ end
12
+
13
+ def self.parse(money_string)
14
+
15
+ cleaned_up = money_string.
16
+ gsub(/\s+/,'').
17
+ gsub(/(\d)o/i){$1 + '0'}.
18
+ gsub(/o(\d)/i){'0' + $1}.
19
+ gsub(/\s+/,'').
20
+ tr(',', '.').
21
+ gsub(/[^\d\.-]/, '')
22
+
23
+ return nil if cleaned_up.empty?
24
+
25
+ chunks = cleaned_up.split('.')
26
+
27
+ normalized = if chunks.size == 1
28
+ cleaned_up
29
+ else
30
+ if chunks[-1].size > 2
31
+ chunks.join
32
+ else
33
+ chunks[0..-2].join + '.' + chunks[-1]
34
+ end
35
+ end
36
+
37
+ normalized ? BigDecimal.new(normalized) : normalized
38
+ end
39
+
40
+ end
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'money_parser'
3
+ s.version = '0.0.1'
4
+ s.homepage = 'https://github.com/leikind/money_parser'
5
+ s.date = '2014-01-05'
6
+ s.summary = 'Tries to parse various crazy money formats'
7
+ s.description = 'Tries to parse various crazy money formats'
8
+ s.authors = ['Yuri Leikind']
9
+ s.email = 'yuri.leikind@gmail.com'
10
+ s.files = `git ls-files`.split($/)
11
+ s.license = 'MIT'
12
+
13
+ s.add_development_dependency "rspec"
14
+ end