Skip to content

Commit

Permalink
feat(android): add locale methods to String type
Browse files Browse the repository at this point in the history
- Added locale support to:
  * String.localeCompare()
  * String.toLocaleLowerCase()
  * String.toLocaleUpperCase()

Fixes TIMOB-27892
  • Loading branch information
jquick-axway authored and sgtcoolguy committed Jul 19, 2020
1 parent e4fdd5e commit 683adaf
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ public double parseDecimal(String text, @Kroll.argument(optional = true) String
return result;
}

@Kroll.method
public String makeLowerCase(String text, @Kroll.argument(optional = true) Object locales)
{
if (text == null) {
return null;
}
Locale locale = getLocaleFrom(locales, Locale.getDefault());
return text.toLowerCase(locale);
}

@Kroll.method
public String makeUpperCase(String text, @Kroll.argument(optional = true) Object locales)
{
if (text == null) {
return null;
}
Locale locale = getLocaleFrom(locales, Locale.getDefault());
return text.toUpperCase(locale);
}

@Kroll.method
@Kroll.setProperty
public void setLanguage(String language)
Expand Down Expand Up @@ -239,6 +259,22 @@ public String getString(String key, @Kroll.argument(optional = true) String defa
}
}

private Locale getLocaleFrom(Object value, Locale defaultLocale)
{
String localeName = null;
if (value instanceof String) {
localeName = (String) value;
} else if ((value != null) && value.getClass().isArray()) {
String[] stringArray = TiConvert.toStringArray((Object[]) value);
if (stringArray.length > 0) {
localeName = stringArray[0];
}
}

Locale locale = TiPlatformHelper.getInstance().getLocale(localeName);
return (locale != null) ? locale : defaultLocale;
}

private String[] getLocaleStringArrayFrom(Object value)
{
String[] stringArray = null;
Expand Down
22 changes: 22 additions & 0 deletions common/Resources/ti.internal/extensions/js/String.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2020 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.
*/
/* globals OS_ANDROID */

if (OS_ANDROID) {
String.prototype.localeCompare = function (compareString, locales, options) {
const collator = new Intl.Collator(locales, options);
return collator.compare(this, compareString);
};

String.prototype.toLocaleLowerCase = function (locale) {
return Ti.Locale.makeLowerCase(this, locale);
};

String.prototype.toLocaleUpperCase = function (locale) {
return Ti.Locale.makeUpperCase(this, locale);
};
}
1 change: 1 addition & 0 deletions common/Resources/ti.internal/extensions/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './Date';
import './Error';
import './Intl';
import './Number';
import './String';

// hook our implementations to get loaded by require
import { register } from '../binding';
Expand Down
53 changes: 53 additions & 0 deletions tests/Resources/string.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2018-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';
const should = require('./utilities/assertions');

describe('String', function () {
it('#localeCompare()', () => {
const string1 = 'réservé';
const string2 = 'RESERVE';
should(string1.localeCompare).not.be.undefined();
should(string1.localeCompare).be.a.Function();
should(string1.localeCompare(string2)).be.a.Number();
should(string1.localeCompare(string2)).be.above(0);
should(string1.localeCompare(string2, 'en')).be.above(0);
should(string1.localeCompare(string2, [ 'en' ])).be.above(0);
should(string1.localeCompare(string2, [ 'en', 'de' ])).be.above(0);
should(string1.localeCompare(string2, undefined, { sensitivity: 'base' })).be.eql(0);
should(string1.localeCompare(string2, 'en', { sensitivity: 'base' })).be.eql(0);
should(string1.localeCompare(string2, [ 'en' ], { sensitivity: 'base' })).be.eql(0);
should(string1.localeCompare(string2, [ 'en', 'de' ], { sensitivity: 'base' })).be.eql(0);
});

it('#toLocaleLowerCase()', () => {
const text = 'İstanbul';
should(text.toLocaleLowerCase).not.be.undefined();
should(text.toLocaleLowerCase).be.a.Function();
should(text.toLocaleLowerCase()).be.a.String();
should(text.toLocaleLowerCase('en-US')).be.eql('i̇stanbul');
should(text.toLocaleLowerCase([ 'en-US' ])).be.eql('i̇stanbul');
should(text.toLocaleLowerCase([ 'en-US', 'de-DE' ])).be.eql('i̇stanbul');
should(text.toLocaleLowerCase('tr-TR')).be.eql('istanbul');
should(text.toLocaleLowerCase([ 'tr-TR' ])).be.eql('istanbul');
});

it('#toLocaleUpperCase()', () => {
const text = 'istanbul';
should(text.toLocaleUpperCase).not.be.undefined();
should(text.toLocaleUpperCase).be.a.Function();
should(text.toLocaleUpperCase()).be.a.String();
should(text.toLocaleUpperCase('en-US')).be.eql('ISTANBUL');
should(text.toLocaleUpperCase([ 'en-US' ])).be.eql('ISTANBUL');
should(text.toLocaleUpperCase([ 'en-US', 'de-DE' ])).be.eql('ISTANBUL');
should(text.toLocaleUpperCase('tr-TR')).be.eql('İSTANBUL');
should(text.toLocaleUpperCase([ 'tr-TR' ])).be.eql('İSTANBUL');
});
});

0 comments on commit 683adaf

Please sign in to comment.