human_number 0.1.9 → 0.2.0
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 +4 -4
- data/CHANGELOG.md +39 -92
- data/README.md +97 -4
- data/lib/human_number/formatters/number.rb +11 -398
- data/lib/human_number/number_system.rb +657 -0
- data/lib/human_number/version.rb +1 -1
- data/lib/human_number.rb +65 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93e874d0c4c80b94891c99059eea6c14bc1d6f9d3931c6d589559f6282b2b835
|
4
|
+
data.tar.gz: 4eb1a4bc258bb90a25d4ebb908c115cf2aa2ecca7878cf3faacc028d3e418432
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1102a52bea931b847a7886904e05497cbab49d4d770fefd9bb0d6b1d6f9538283e0b8a7e99e363a2e76b48d8af40022b56b5791d160e9943ca98df9b6fca355c
|
7
|
+
data.tar.gz: 5dbffd0891fc1945b8a20b2ed569671aa8836c78a1026711b667a2ab0adebb0d798ec89e8e9f1895da98dd704290a3575454fbc34359db36c1549cd7131b529d
|
data/CHANGELOG.md
CHANGED
@@ -5,111 +5,58 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [0.2.0] - 2025-08-25
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- `HumanNumber.number_system(locale:)` - Detects number system (`:default`, `:east_asian`, `:indian`) by locale
|
12
|
+
- `HumanNumber.currency_number_system(currency_code:)` - Detects number system by currency code
|
13
|
+
- New `HumanNumber::NumberSystem` module with centralized system logic
|
14
|
+
|
15
|
+
### Changed
|
16
|
+
- Moved system classes (`DefaultSystem`, `EastAsianSystem`, `IndianSystem`) to `NumberSystem` module
|
17
|
+
- Reduced `number.rb` formatter from 475 to 93 lines (80% reduction)
|
18
|
+
- Updated I18n test patterns to use `I18n.with_locale` for thread safety
|
19
|
+
- Enhanced README with new API documentation and examples
|
20
|
+
|
21
|
+
## [0.1.10] - 2025-08-19
|
22
|
+
|
23
|
+
### Changed
|
24
|
+
- **BREAKING CHANGE**: Changed `max_digits` default from `2` to `nil`
|
25
|
+
- Numbers now show complete breakdown by default (`"1M 234K 567"` instead of `"1.2M"`)
|
26
|
+
- Previous abbreviated behavior available with explicit `max_digits: 2`
|
27
|
+
|
8
28
|
## [0.1.9] - 2025-08-05
|
9
29
|
|
10
30
|
### Changed
|
11
|
-
-
|
12
|
-
|
13
|
-
|
14
|
-
- **Locale-based separator configuration**: Added `number.human.decimal_units.separator` for customizable unit separators
|
31
|
+
- Japanese numbers use culturally appropriate spacing (no spaces between units)
|
32
|
+
- Korean/Chinese maintain spaces between units as default
|
33
|
+
- Added `number.human.decimal_units.separator` for customizable separators
|
15
34
|
|
16
35
|
## [0.1.8] - 2025-08-05
|
17
36
|
|
18
37
|
### Fixed
|
19
|
-
-
|
20
|
-
-
|
21
|
-
-
|
38
|
+
- I18n fallback currency symbol contamination (Korean won symbols appearing for other currencies)
|
39
|
+
- Missing translation errors for various currency/locale combinations
|
40
|
+
- Rails configuration warnings in test environment
|
22
41
|
|
23
42
|
### Changed
|
24
|
-
-
|
25
|
-
-
|
26
|
-
|
27
|
-
### Improved
|
28
|
-
- **Rails-i18n integration**: Enhanced loading order and initialization to prevent locale conflicts
|
29
|
-
- **Test infrastructure**: Updated RSpec mocks and test expectations for better reliability
|
30
|
-
- **I18n isolation**: Added `fallback: false` parameter to prevent cross-locale pollution
|
43
|
+
- Corrected CURRENCY_NATIVE_LOCALES mappings for accuracy
|
44
|
+
- Moved locale files from `config/locales/` to `lib/locales/`
|
31
45
|
|
32
46
|
## [0.1.0] - 2025-07-31
|
33
47
|
|
34
48
|
### Added
|
35
|
-
|
36
|
-
|
37
|
-
-
|
38
|
-
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
-
|
45
|
-
|
46
|
-
- **40+ currency mappings** with native locale associations for accurate formatting
|
47
|
-
- **Human-readable currency formatting** combining cultural abbreviations with currency symbols
|
48
|
-
|
49
|
-
#### API Methods
|
50
|
-
- `HumanNumber.human_number(number, **options)` - Formats numbers with cultural abbreviations
|
51
|
-
- `HumanNumber.currency(number, currency_code:, locale:)` - Standard currency formatting
|
52
|
-
- `HumanNumber.human_currency(number, currency_code:, locale:, **options)` - Currency with abbreviations
|
53
|
-
|
54
|
-
#### Formatting Options
|
55
|
-
- **Precision control**: `max_digits` parameter for significant digit control (1-4 digits or nil for complete mode)
|
56
|
-
- **Unit preferences**: `abbr_units` to choose between abbreviated (K/M) vs full words (thousand/million)
|
57
|
-
- **Minimum thresholds**: `min_unit` to prevent abbreviation below specified amounts
|
58
|
-
- **Zero trimming**: `trim_zeros` to control trailing decimal zero display
|
59
|
-
- **Complete mode**: `max_digits: nil` shows full breakdown across multiple units (e.g., "1M 234K 567")
|
60
|
-
|
61
|
-
#### Rails Integration
|
62
|
-
- **Automatic Rails detection** and helper loading via Railtie
|
63
|
-
- **View helpers**: `human_number`, `currency`, `human_currency` with Rails-friendly parameter handling
|
64
|
-
- **I18n integration**: Automatic locale detection from `I18n.locale`
|
65
|
-
|
66
|
-
#### Locale Data System
|
67
|
-
- **Hybrid locale approach**: Combines rails-i18n for base formatting with custom locale files for unit symbols
|
68
|
-
- **14 custom locale files**: bn, de, en-GB, en-IN, en, es, fr, hi, ja, ko, ur, zh-CN, zh-TW, zh
|
69
|
-
- **Rails-i18n structure**: `number.human.decimal_units.units` for unit translations
|
70
|
-
- **Automatic locale file loading** from `config/locales/` directory
|
71
|
-
|
72
|
-
#### Architecture & Performance
|
73
|
-
- **Configuration-free design**: No global state, explicit parameters for predictable behavior
|
74
|
-
- **Direct class methods**: Zero object instantiation overhead for optimal performance
|
75
|
-
- **Thread-safe implementation**: No shared mutable state between calls
|
76
|
-
- **Two-stage formatting**: Clean separation between number formatting and currency application
|
77
|
-
- **Efficient I18n**: Leverages Rails I18n caching mechanisms
|
78
|
-
|
79
|
-
#### Standards Compliance
|
80
|
-
- **Microsoft Globalization documentation** compliance for cultural number formatting
|
81
|
-
- **Unicode CLDR standards** for locale-specific formatting rules
|
82
|
-
- **ISO 4217 standard** for currency codes and precision rules
|
83
|
-
- **rails-i18n integration** for verified locale data
|
84
|
-
|
85
|
-
#### Development Infrastructure
|
86
|
-
- **Comprehensive test suite**: 106 test examples with 100% code coverage
|
87
|
-
- **RuboCop compliance**: Complete adherence to Ruby style guidelines
|
88
|
-
- **Rake tasks**: Integrated testing and linting workflow
|
89
|
-
- **Gem packaging**: Ready for RubyGems distribution
|
90
|
-
|
91
|
-
#### Dependencies
|
92
|
-
- **Runtime dependencies**: `actionview` (>= 5.2), `i18n` (>= 1.6, < 2), `rails-i18n` (>= 5.1)
|
93
|
-
- **Ruby requirement**: >= 3.1.0
|
94
|
-
- **Development dependencies**: `rspec`, `rubocop`, `simplecov`, `yard`
|
95
|
-
|
96
|
-
### Technical Details
|
97
|
-
|
98
|
-
#### Number System Architecture
|
99
|
-
- **DefaultSystem**: Handles Western locales with standard thousand/million/billion/trillion units
|
100
|
-
- **EastAsianSystem**: Implements East Asian counting with ten-thousand (만/万) and hundred-million (억/億) bases
|
101
|
-
- **IndianSystem**: Supports Indian numbering with lakh (100,000) and crore (10,000,000) units
|
102
|
-
|
103
|
-
#### Currency Precision System
|
104
|
-
- **LocaleSupport module**: Centralized currency-to-locale mapping for consistent precision
|
105
|
-
- **Native locale detection**: Automatic selection of appropriate formatting locale for each currency
|
106
|
-
- **Precision inheritance**: Currency formatting inherits precision rules from native locales
|
107
|
-
|
108
|
-
#### Formatter Pattern
|
109
|
-
- **Formatters::Number**: Core number formatting with intelligent unit system selection
|
110
|
-
- **Formatters::Currency**: Currency symbol and format string application
|
111
|
-
- **Clean separation**: Number formatting independent of currency concerns
|
112
|
-
|
49
|
+
- Human-readable number formatting with cultural abbreviations
|
50
|
+
- Three number systems: Western (K/M/B/T), East Asian (만/억/조), Indian (lakh/crore)
|
51
|
+
- ISO 4217 currency code support with native locale precision
|
52
|
+
- API methods: `human_number`, `currency`, `human_currency`
|
53
|
+
- Rails integration with automatic helper loading
|
54
|
+
- Configuration-free design with explicit parameters
|
55
|
+
- 14 custom locale files with rails-i18n integration
|
56
|
+
- Thread-safe implementation
|
57
|
+
|
58
|
+
[0.2.0]: https://github.com/ether-moon/human_number/releases/tag/v0.2.0
|
59
|
+
[0.1.10]: https://github.com/ether-moon/human_number/releases/tag/v0.1.10
|
113
60
|
[0.1.9]: https://github.com/ether-moon/human_number/releases/tag/v0.1.9
|
114
61
|
[0.1.8]: https://github.com/ether-moon/human_number/releases/tag/v0.1.8
|
115
62
|
[0.1.0]: https://github.com/ether-moon/human_number/releases/tag/v0.1.0
|
data/README.md
CHANGED
@@ -123,6 +123,76 @@ HumanNumber.human_currency(1_234_567, currency_code: 'USD', locale: :en, max_dig
|
|
123
123
|
HumanNumber.human_currency(1_234_567, currency_code: 'USD', locale: :en, max_digits: nil) #=> "$1M 234K 567"
|
124
124
|
```
|
125
125
|
|
126
|
+
### System Detection Methods
|
127
|
+
|
128
|
+
#### `HumanNumber.number_system(locale:)`
|
129
|
+
|
130
|
+
Determines which number system is used for a given locale. Returns a symbol indicating the cultural number formatting system.
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
# Western/Default system (K/M/B/T)
|
134
|
+
HumanNumber.number_system(locale: :en) #=> :default
|
135
|
+
HumanNumber.number_system(locale: :fr) #=> :default
|
136
|
+
HumanNumber.number_system(locale: :de) #=> :default
|
137
|
+
|
138
|
+
# East Asian system (만/억/조 or 万/億/兆)
|
139
|
+
HumanNumber.number_system(locale: :ko) #=> :east_asian
|
140
|
+
HumanNumber.number_system(locale: :ja) #=> :east_asian
|
141
|
+
HumanNumber.number_system(locale: :zh) #=> :east_asian
|
142
|
+
|
143
|
+
# Indian system (thousand/lakh/crore)
|
144
|
+
HumanNumber.number_system(locale: :hi) #=> :indian
|
145
|
+
HumanNumber.number_system(locale: :'en-IN') #=> :indian
|
146
|
+
|
147
|
+
# Defaults to current I18n.locale when not specified
|
148
|
+
HumanNumber.number_system #=> (uses I18n.locale)
|
149
|
+
```
|
150
|
+
|
151
|
+
**Return Values:**
|
152
|
+
- `:default` - Western system using K/M/B/T (thousand/million/billion/trillion)
|
153
|
+
- `:east_asian` - Asian system using 만/억/조 or 万/億/兆 patterns
|
154
|
+
- `:indian` - South Asian system using thousand/lakh/crore
|
155
|
+
|
156
|
+
#### `HumanNumber.currency_number_system(currency_code:)`
|
157
|
+
|
158
|
+
Determines which number system is used for a given currency code. Returns a symbol indicating the culturally appropriate formatting system for that currency's region.
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
# Default system currencies
|
162
|
+
HumanNumber.currency_number_system(currency_code: 'USD') #=> :default
|
163
|
+
HumanNumber.currency_number_system(currency_code: 'EUR') #=> :default
|
164
|
+
HumanNumber.currency_number_system(currency_code: 'GBP') #=> :default
|
165
|
+
|
166
|
+
# East Asian system currencies
|
167
|
+
HumanNumber.currency_number_system(currency_code: 'KRW') #=> :east_asian # Korean Won
|
168
|
+
HumanNumber.currency_number_system(currency_code: 'JPY') #=> :east_asian # Japanese Yen
|
169
|
+
HumanNumber.currency_number_system(currency_code: 'CNY') #=> :east_asian # Chinese Yuan
|
170
|
+
|
171
|
+
# Indian system currencies
|
172
|
+
HumanNumber.currency_number_system(currency_code: 'INR') #=> :indian # Indian Rupee
|
173
|
+
|
174
|
+
# Case-insensitive and handles whitespace
|
175
|
+
HumanNumber.currency_number_system(currency_code: 'krw') #=> :east_asian
|
176
|
+
HumanNumber.currency_number_system(currency_code: ' USD ') #=> :default
|
177
|
+
```
|
178
|
+
|
179
|
+
**Practical Usage:**
|
180
|
+
These methods are useful for understanding which formatting system will be applied, or for building custom formatting logic:
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
# Check system before formatting
|
184
|
+
system = HumanNumber.number_system(locale: :ko)
|
185
|
+
if system == :east_asian
|
186
|
+
puts "Will use 만/억/조 units"
|
187
|
+
result = HumanNumber.human_number(50_000, locale: :ko) #=> "5만"
|
188
|
+
end
|
189
|
+
|
190
|
+
# Currency-aware system detection
|
191
|
+
currency_system = HumanNumber.currency_number_system(currency_code: 'KRW')
|
192
|
+
locale_system = HumanNumber.number_system(locale: :ko)
|
193
|
+
puts "Consistent systems!" if currency_system == locale_system #=> true
|
194
|
+
```
|
195
|
+
|
126
196
|
### Rails Integration
|
127
197
|
|
128
198
|
In Rails applications, helper methods are automatically available:
|
@@ -300,10 +370,33 @@ $ rake # Runs both spec and rubocop
|
|
300
370
|
|
301
371
|
To add support for a new locale:
|
302
372
|
|
303
|
-
1. Add locale to appropriate
|
304
|
-
|
305
|
-
|
306
|
-
|
373
|
+
1. Add locale to appropriate system mapping in `lib/human_number/number_system.rb`:
|
374
|
+
- Update `LOCALE_SYSTEM_MAPPING` to include the new locale
|
375
|
+
- If needed, add currency mapping to `CURRENCY_SYSTEM_MAPPING`
|
376
|
+
2. Create locale file in `config/locales/` with unit translations following rails-i18n structure
|
377
|
+
3. Add currency precision mapping to `CURRENCY_NATIVE_LOCALES` in `lib/human_number/locale_support.rb` if applicable
|
378
|
+
4. Add comprehensive tests covering the new locale in both `spec/human_number_spec.rb` and `spec/human_number/number_system_spec.rb`
|
379
|
+
|
380
|
+
**Example:** Adding Portuguese (Brazil) support:
|
381
|
+
```ruby
|
382
|
+
# In lib/human_number/number_system.rb
|
383
|
+
LOCALE_SYSTEM_MAPPING = {
|
384
|
+
east_asian: %i[ko ja zh zh-CN zh-TW].freeze,
|
385
|
+
indian: %i[hi ur bn en-IN].freeze,
|
386
|
+
# Add pt-BR to default system (no entry needed)
|
387
|
+
}.freeze
|
388
|
+
|
389
|
+
# In config/locales/pt-BR.yml
|
390
|
+
pt-BR:
|
391
|
+
number:
|
392
|
+
human:
|
393
|
+
decimal_units:
|
394
|
+
abbr_units:
|
395
|
+
thousand: "K"
|
396
|
+
million: "M"
|
397
|
+
billion: "B"
|
398
|
+
trillion: "T"
|
399
|
+
```
|
307
400
|
|
308
401
|
## License
|
309
402
|
|