-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Ti.Local.parseDecimal() method #11683
Changes from 3 commits
461a9dd
6ebf268
4b79d9e
ee912e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,37 @@ methods: | |
// for Spanish locales, displays "You say adios and I say hola!" | ||
Ti.API.info(String.format(L('phrase'), L('greeting', i18nMissingMsg), L('signoff', i18nMissingMsg))); | ||
|
||
- name: parseDecimal | ||
summary: Parses a number from the given string using the current or given locale. | ||
platforms: [android, iphone, ipad] | ||
returns: | ||
type: Number | ||
summary: Returns the parsed number. Returns `NaN` (Not-a-Number) if given string is invalid. | ||
parameters: | ||
- name: text | ||
summary: The string to be parsed. | ||
type: String | ||
|
||
- name: locale | ||
summary: | | ||
Locale, as a combination of ISO 2-letter language and country codes. For example, | ||
`en-US` or `en-GB`. See the | ||
[ISO 639-1](http://en.wikipedia.org/wiki/ISO_639-1) and | ||
[ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements) | ||
sections of wikipedia for reference. | ||
type: String | ||
optional: true | ||
examples: | ||
- title: Parsing Localized Numeric Strings | ||
example: | | ||
// You can convert to/from a localized numeric string like this. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use a fenced code block now: // this is some example code |
||
var numericString = String.formatDecimal(1234.5); | ||
var numericValue = Ti.Locale.parseDecimal(numericString); | ||
|
||
// The numbers returned will be the same for the 2 different locales. | ||
var number1 = Ti.Locale.parseDecimal('1,234,567.8', 'en-US'); | ||
var number2 = Ti.Locale.parseDecimal('1.234.567,8', 'de-DE'); | ||
|
||
events: | ||
- name: change | ||
summary: Fired when the device locale changes. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,10 @@ JSExportAs(getString, | |
-(NSString *)getString | ||
: (NSString *)key withHint | ||
: (id)hint); | ||
JSExportAs(parseDecimal, | ||
-(NSNumber *)parseDecimal | ||
: (NSString *)text withLocaleId | ||
: (id)localeId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I'm a bit conflicted over the use of So I guess we have to either declare as |
||
- (void)setLanguage:(NSString *)language; | ||
|
||
@end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Appcelerator Titanium Mobile | ||
* Copyright (c) 2015-Present by Axway, Inc. All Rights Reserved. | ||
* Licensed under the terms of the Apache Public License | ||
* Please see the LICENSE included with this distribution for details. | ||
*/ | ||
/* eslint-env mocha */ | ||
/* eslint no-unused-expressions: "off" */ | ||
'use strict'; | ||
var should = require('./utilities/assertions'); | ||
|
||
describe('Titanium.Locale', function () { | ||
describe('#parseDecimal()', () => { | ||
it('compared with String.formatDecimal()', () => { | ||
should(Ti.Locale.parseDecimal).be.a.Function(); | ||
|
||
const numericValue = 1234567.8; | ||
|
||
let numericString = String.formatDecimal(numericValue); | ||
let parsedValue = Ti.Locale.parseDecimal(numericString); | ||
should(parsedValue).be.a.Number(); | ||
should(Math.abs(parsedValue - numericValue)).be.lessThan(Number.EPSILON); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat trick - never knew about the use of EPSILON to compare floats. May be easier to write this as: should(parsedValue).be.approximately(numericValue, Number.EPSILON); |
||
|
||
numericString = String.formatDecimal(numericValue, 'de-DE'); | ||
parsedValue = Ti.Locale.parseDecimal(numericString, 'de-DE'); | ||
should(Math.abs(parsedValue - numericValue)).be.lessThan(Number.EPSILON); | ||
|
||
numericString = String.formatDecimal(numericValue, 'fr-FR'); | ||
parsedValue = Ti.Locale.parseDecimal(numericString, 'fr-FR'); | ||
should(Math.abs(parsedValue - numericValue)).be.lessThan(Number.EPSILON); | ||
|
||
numericString = String.formatDecimal(numericValue, 'ar-EG'); | ||
parsedValue = Ti.Locale.parseDecimal(numericString, 'ar-EG'); | ||
should(Math.abs(parsedValue - numericValue)).be.lessThan(Number.EPSILON); | ||
}); | ||
|
||
it('localized values', () => { | ||
let result = Ti.Locale.parseDecimal('1,234,567.8', 'en-US'); | ||
should(Math.abs(result - 1234567.8)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('1.234.567,8', 'de-DE'); | ||
should(Math.abs(result - 1234567.8)).be.lessThan(Number.EPSILON); | ||
|
||
// France uses non-breaking unicode spaces for thousands separator. | ||
result = Ti.Locale.parseDecimal('1\u00A0234\u00A0567,8', 'fr-FR'); | ||
should(Math.abs(result - 1234567.8)).be.lessThan(Number.EPSILON); | ||
}); | ||
|
||
it('various values', () => { | ||
let result = Ti.Locale.parseDecimal('0', 'en-US'); | ||
should(Math.abs(result)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('0.', 'en-US'); | ||
should(Math.abs(result)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('.0', 'en-US'); | ||
should(Math.abs(result)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('00.00123', 'en-US'); | ||
should(Math.abs(result - 0.00123)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('+0', 'en-US'); | ||
should(Math.abs(result)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('-0', 'en-US'); | ||
should(Math.abs(result)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('+1234.5', 'en-US'); | ||
should(Math.abs(result - 1234.5)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('-1234.5', 'en-US'); | ||
should(Math.abs(result + 1234.5)).be.lessThan(Number.EPSILON); | ||
}); | ||
|
||
it('scientific values', () => { | ||
let result = Ti.Locale.parseDecimal('1.2E+3', 'en-US'); | ||
should(Math.abs(result - 1200.0)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('-1.2E+3', 'en-US'); | ||
should(Math.abs(result + 1200.0)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('1.2E-3', 'en-US'); | ||
should(Math.abs(result - 0.0012)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal('-1.2E-3', 'en-US'); | ||
should(Math.abs(result + 0.0012)).be.lessThan(Number.EPSILON); | ||
}); | ||
|
||
it('padded with spaces', () => { | ||
let result = Ti.Locale.parseDecimal(' 123 ', 'en-US'); | ||
should(Math.abs(result - 123)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal(' +123 ', 'en-US'); | ||
should(Math.abs(result - 123)).be.lessThan(Number.EPSILON); | ||
|
||
result = Ti.Locale.parseDecimal(' -123 ', 'en-US'); | ||
should(Math.abs(result + 123)).be.lessThan(Number.EPSILON); | ||
}); | ||
|
||
it('NaN', () => { | ||
let result = Ti.Locale.parseDecimal('ThisShouldFail'); | ||
should(result).be.a.Number(); | ||
should(result).be.eql(Number.NaN); | ||
|
||
result = Ti.Locale.parseDecimal(''); | ||
should(result).be.eql(Number.NaN); | ||
|
||
result = Ti.Locale.parseDecimal(' '); | ||
should(result).be.eql(Number.NaN); | ||
|
||
result = Ti.Locale.parseDecimal(null); | ||
should(result).be.eql(Number.NaN); | ||
|
||
result = Ti.Locale.parseDecimal(undefined); | ||
should(result).be.eql(Number.NaN); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't need the null check since you're already trimming without checking above. I assume our bridge layer enforces non-null if the argument is not marked optional? If not, the null check needs to move to the very top.