valuta 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +25 -0
- data/lib/valuta/version.rb +3 -0
- data/lib/valuta.rb +46 -0
- data/makefile +2 -0
- data/test/valuta_test.rb +43 -0
- data/valuta.gemspec +17 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5d6e02da5a1e808187168f79d71ed33b1e114b4d
|
4
|
+
data.tar.gz: d2f5d88c4de41bab14aaf41bdd75341377cc6ba7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c4940e1ee3262fa4306b91f8ad73fdca6ddfd2518f0139624de9ed734154571cf7e6586ef5929a705657edb6edea07cfa02f3265ae3f54219e942801220e2a6
|
7
|
+
data.tar.gz: 9dc3619b09b2b3b71e312b207dc9842c38fa64f1ae5411e051b96fc9c219a2dcecc2a04ed513a46b3edb72a776e5f2d41e548f49d1c517082d0fc11a13c00cd1
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Mayn Kjær
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# valuta
|
2
|
+
|
3
|
+
Formats a number into a currency string (e.g., $1,567.65).
|
4
|
+
You can customize the format in the options hash.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
require "valuta"
|
10
|
+
|
11
|
+
Valuta.convert(1458) # => 1,458
|
12
|
+
Valuta.convert(1458.65) # => 1,458.65
|
13
|
+
Valuta.convert("1458.65") # => 1,458.65
|
14
|
+
Valuta.convert(1458, prefix: "$") # => $1458
|
15
|
+
Valuta.convert(1458, suffix: " NOK") # => 1458 NOK
|
16
|
+
|
17
|
+
Valuta.convert(100458.65, delimiter: ".", separator: ",")
|
18
|
+
# => 100.458,65
|
19
|
+
```
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
```
|
24
|
+
$ gem install valuta
|
25
|
+
```
|
data/lib/valuta.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative "valuta/version"
|
2
|
+
|
3
|
+
class Valuta
|
4
|
+
FORMAT = /(\d{3})(?=\d)/
|
5
|
+
SEPARATOR = ".".freeze
|
6
|
+
|
7
|
+
DEFAULTS = {
|
8
|
+
delimiter: ",",
|
9
|
+
separator: ".",
|
10
|
+
suffix: nil,
|
11
|
+
prefix: nil
|
12
|
+
}
|
13
|
+
|
14
|
+
def self.convert(number, options={})
|
15
|
+
return new(options).convert(number)
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(options = {})
|
19
|
+
@options = DEFAULTS.merge(options)
|
20
|
+
end
|
21
|
+
|
22
|
+
def convert(number)
|
23
|
+
prefix = @options[:prefix]
|
24
|
+
suffix = @options[:suffix]
|
25
|
+
|
26
|
+
return [prefix, format(number.to_s), suffix].join
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def format(str)
|
32
|
+
return parts(str.strip).join(@options[:separator])
|
33
|
+
end
|
34
|
+
|
35
|
+
def parts(str)
|
36
|
+
left, right = str.split(SEPARATOR)
|
37
|
+
|
38
|
+
if left.length > 3
|
39
|
+
left.reverse!
|
40
|
+
left.gsub!(FORMAT, "\\1#{ @options[:delimiter] }")
|
41
|
+
left.reverse!
|
42
|
+
end
|
43
|
+
|
44
|
+
return [left, right].compact
|
45
|
+
end
|
46
|
+
end
|
data/makefile
ADDED
data/test/valuta_test.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "cutest"
|
3
|
+
|
4
|
+
require_relative "../lib/valuta"
|
5
|
+
|
6
|
+
scope do
|
7
|
+
test "convert default" do
|
8
|
+
number = 1567945
|
9
|
+
|
10
|
+
assert_equal("1,567,945", Valuta.convert(number))
|
11
|
+
end
|
12
|
+
|
13
|
+
test "convert decimal" do
|
14
|
+
number = 1674.765
|
15
|
+
|
16
|
+
assert_equal("1,674.765", Valuta.convert(number))
|
17
|
+
end
|
18
|
+
|
19
|
+
test "covert with custom delimiter and seperator" do
|
20
|
+
number = 1674.765
|
21
|
+
options = { delimiter: ".", separator: "," }
|
22
|
+
|
23
|
+
assert_equal("1.674,765", Valuta.convert(number, options))
|
24
|
+
end
|
25
|
+
|
26
|
+
test "convert with prefix" do
|
27
|
+
number = 1567945
|
28
|
+
|
29
|
+
assert_equal("$1,567,945", Valuta.convert(number, prefix: "$"))
|
30
|
+
end
|
31
|
+
|
32
|
+
test "convert with suffix" do
|
33
|
+
number = 1567945
|
34
|
+
|
35
|
+
assert_equal("1,567,945 NOK", Valuta.convert(number, suffix: " NOK"))
|
36
|
+
end
|
37
|
+
|
38
|
+
test "covert negative number" do
|
39
|
+
number = -1563.654
|
40
|
+
|
41
|
+
assert_equal("-1,563.654", Valuta.convert(number))
|
42
|
+
end
|
43
|
+
end
|
data/valuta.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative "lib/valuta/version"
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "valuta"
|
5
|
+
s.version = Valuta::VERSION
|
6
|
+
s.summary = "Formats a number into a currency string (e.g., $1,567.65)."
|
7
|
+
s.description = s.summary
|
8
|
+
s.authors = ["Mayn Ektvedt Kjær"]
|
9
|
+
s.email = ["mayn.kjaer@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/harmoni/valuta"
|
11
|
+
s.license = "MIT"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
|
15
|
+
s.add_development_dependency "bundler", "~> 1.10"
|
16
|
+
s.add_development_dependency "cutest", "~> 1.2"
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: valuta
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mayn Ektvedt Kjær
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cutest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
description: Formats a number into a currency string (e.g., $1,567.65).
|
42
|
+
email:
|
43
|
+
- mayn.kjaer@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- lib/valuta.rb
|
53
|
+
- lib/valuta/version.rb
|
54
|
+
- makefile
|
55
|
+
- test/valuta_test.rb
|
56
|
+
- valuta.gemspec
|
57
|
+
homepage: https://github.com/harmoni/valuta
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.4.5.1
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Formats a number into a currency string (e.g., $1,567.65).
|
81
|
+
test_files: []
|