From e41473da8002e0b36bc25ee15c06aee452ae2386 Mon Sep 17 00:00:00 2001 From: Nedyalko Nikolov Date: Fri, 8 Jul 2016 11:51:35 +0300 Subject: [PATCH] Updated test for API 24. --- package.json | 2 +- src/README.md | 37 +++++- src/package.json | 8 +- tests/app/tests/example.js | 34 ++--- tests/getFormatsFromChrome.js | 232 ++++++++++++++++++++++++++++++++++ tests/package.json | 12 +- 6 files changed, 298 insertions(+), 27 deletions(-) create mode 100644 tests/getFormatsFromChrome.js diff --git a/package.json b/package.json index 6bf6d0e..dc91e39 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "devDependencies": { - "typescript": "^1.8.7" + "typescript": "^1.8.9" } } diff --git a/src/README.md b/src/README.md index b711554..59d7f0b 100644 --- a/src/README.md +++ b/src/README.md @@ -2,8 +2,43 @@ Welcome to the `nativescript-intl` plugin for NativeScript framework # Prerequisites +- installed [NativeScript](https://github.com/NativeScript/NativeScript) - installed [NativeScript-CLI](https://github.com/NativeScript/nativescript-cli) # Install `nativescript-intl` plugin. -Navigate to project folder and run `NativeScript-CLI` command `tns plugin add nativescript-intl`. \ No newline at end of file +Navigate to project folder and run `NativeScript-CLI` command `tns plugin add nativescript-intl`. + +This plugin provides similar to [Intl.js](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Intl) API for date, time and number formatting on a mobile device (Android and iOS). + +## How to use + +```JavaScript +var intl = require("nativescript-intl"); +var dateFormat = new intl.DateTimeFormat('en-US', {'year': 'numeric', 'month': 'short', 'day': 'numeric'}).format(new Date(2016, 2, 23)); +var numberFormat = new intl.NumberFormat('en-US', {'style': 'currency', 'currency': 'USD', 'currencyDisplay': 'symbol'}).format(123456.789); + +console.log("dateFormat: " + dateFormat); +console.log("numberFormat: " + numberFormat); +// prints Mar 23, 2016 +// $123456.79 +``` + +Since localization formatting is not exactly same on Android, iOS and [Intl.js](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Intl) there is another option to use this API. + +## Custom pattern + +```JavaScript +var intl = require("nativescript-intl"); +var dateFormat = new intl.DateTimeFormat(null, null, 'y MMMM d, EEEE HH:mm:ss').format(new Date(2016, 2, 23, 12, 35, 40)); +var numberFormat = new intl.NumberFormat('en-US', {'style': 'currency', 'currency': 'USD', 'currencyDisplay': 'symbol'}, '#,##0.00 ¤').format(123456.789); + +console.log("dateFormat: " + dateFormat); +console.log("numberFormat: " + numberFormat); +// prints 2016 March 23, Wednesday 2016 12:35:40 +// 123,456.79 $ +``` + +> Omitting locale will use the current locale setting on device. + +> Keep in mind that ICU versions differ accross device OS versions, so this plugin could produce different results on a different API level (Android) and OS version (iOS). diff --git a/src/package.json b/src/package.json index 9e57b36..162225a 100644 --- a/src/package.json +++ b/src/package.json @@ -1,16 +1,16 @@ { "name": "nativescript-intl", - "version": "0.0.1", + "version": "0.0.3", "description": "Provides API for using Native date, time and number formatting with an API similar to Intl.js", "main": "nativescript-intl.js", "nativescript": { "platforms": { - "android": "1.6.2", - "ios": "1.6.0" + "android": "2.1.0", + "ios": "2.1.0" } }, "keywords": [ - "NativeScript", "localization", "globalization", "formatting" + "NativeScript", "localization", "globalization", "formatting", "currency", "nativescript-angular" ], "author": "Nedyalko Nikolov " } diff --git a/tests/app/tests/example.js b/tests/app/tests/example.js index f7ad5e6..c911421 100644 --- a/tests/app/tests/example.js +++ b/tests/app/tests/example.js @@ -95,7 +95,7 @@ var mediumDateTimeResultsAndroid = [ "22 mar 2016 09:40:39", //22 mar 2016, 09:40:39 "22 de mar de 2016 09:40:39", //22/03/2016, 09:40:39 "22 mar. 2016 09:40:39", //22 mar. 2016, 09:40:39 - "22 марта 2016 г. 09:40:39", //22 марта 2016 г., 9:40:39 + "22 мар. 2016 г. 09:40:39", //22 мар. 2016 г., 9:40:39 "22. мар 2016. 09.40.39", "22. mar 2016 09:40:39", //22. 3. 2016, 9:40:39 "22. mar. 2016 09:40:39", //22. mar. 2016 09.40.39 @@ -275,7 +275,7 @@ var mediumDateResultsAndroid = [ "22 mar 2016", "22 de mar de 2016", //22/03/2016 "22 mar. 2016", - "22 марта 2016 г.", + "22 мар. 2016 г.", "22. мар 2016.", "22. mar 2016", //22. 3. 2016 "22. mar. 2016", @@ -867,7 +867,7 @@ var currencyCodes = [ "UAH", "GBP", "USD", - "NOK" + "NOK" ]; var currencyCodesResultsAndroid = [ @@ -886,7 +886,7 @@ var currencyCodesResultsAndroid = [ "123456,79 EUR", "123456,79 HUF", "123456,79 EUR", - "EUR123456,79", + "123456,79 EUR", "123456,79 MDL", "EUR 123456,79", "123456,79 PLN", @@ -943,7 +943,7 @@ var currencyCodesResultsIOS = [ ]; var currencyCodesGroupingResultsAndroid = [ - "EUR 123.456,79", + "EUR 123 456,79", "123.456,79 EUR", //EUR 123.456,79" "123.456,79 EUR", //123 456,79 EUR "123 456,79 BGN", @@ -958,7 +958,7 @@ var currencyCodesGroupingResultsAndroid = [ "123.456,79 EUR", "123 456,79 HUF", "123.456,79 EUR", - "EUR123456,79", //EUR123 456,79 + "123456,79 EUR", "123.456,79 MDL", "EUR 123.456,79", "123 456,79 PLN", @@ -1028,15 +1028,15 @@ var currencyCodesSymbolResultsAndroid = [ "123456,79 €", "123456,79 €", "123456,79 €", - "123456,79 HUF", + "123456,79 Ft", + "123456,79 €", "123456,79 €", - "€123456,79", "123456,79 L", //123456,79 MDL "€ 123456,79", "123456,79 zł", "123456,79 €", "123456,79 RON", - "123456,79 руб.", + "123456,79 ₽", // 123456,79 RUB "123457 RSD", "123456,79 €", "123457 SLL", //123456,79 € @@ -1087,7 +1087,7 @@ var currencyCodesSymbolResultsIOS = [ ]; var percentResults = [ - "12.345.679 %", + "12 345 679 %", "12.345.679%", "12.345.679 %", //12 345 679 % "12 345 679%", @@ -1114,7 +1114,7 @@ var percentResults = [ "12.345.679%", "12.345.679 %", "12 345 679 %", - "12'345'679 %", + "12'345'679%", "%12.345.679", "12 345 679%", "12,345,679%", @@ -1123,7 +1123,7 @@ var percentResults = [ ]; var decimalResults = [ - "123.456,79", + "123 456,79", "123.456,79", "123.456,79", //123 456,79 "123 456,79", @@ -1159,7 +1159,7 @@ var decimalResults = [ ]; var customNumberPatternResultsAndroid = [ - "123.456,79 €", + "123 456,79 €", "123.456,79 €", "123.456,79 €", "123 456,79 лв.", @@ -1172,7 +1172,7 @@ var customNumberPatternResultsAndroid = [ "123 456,79 €", "123.456,79 €", "123.456,79 €", - "123 456,79 HUF", + "123 456,79 Ft", "123.456,79 €", "123 456,79 €", "123.456,79 L", @@ -1180,10 +1180,10 @@ var customNumberPatternResultsAndroid = [ "123 456,79 zł", "123 456,79 €", "123.456,79 RON", - "123 456,79 руб.", - "123.456,79 RSD", + "123 456,79 ₽", + "123.457 RSD", "123 456,79 €", - "123.456,79 SLL", + "123.457 SLL", "123.456,79 €", "123 456,79 kr", "123'456.79 CHF", diff --git a/tests/getFormatsFromChrome.js b/tests/getFormatsFromChrome.js new file mode 100644 index 0000000..d497eba --- /dev/null +++ b/tests/getFormatsFromChrome.js @@ -0,0 +1,232 @@ +var locales = [ + "de-AT", + "nl-BE", + "fr-BE", + "bg-BG", + "hr-HR", + "el-CY", + "cs-CZ", + "da-DK", + "et-EE", + "fi-FI", + "fr-FR", + "de-DE", + "el-GR", + "hu-HU", + "it-IT", + "lv-LV", + "ro-MD", + "nl-NL", + "pl-PL", + "pt-PT", + "ro-RO", + "ru-RU", + "sr-RS", + "sk-SK", + "sl-SL", + "es-ES", + "sv-SE", + "de-CH", + "tr-TR", + "uk-UA", + "en-GB", + "en-US", + "nb-NO" +]; + +var dateTimeOptions = [ + { + "optionsName": "longDateOptions", + "options": { + 'year': 'numeric', + 'month': 'long', + 'day': 'numeric' + } + }, + { + "optionsName": "mediumDateTimeOptions", + "options": { + 'year': 'numeric', + 'month': 'short', + 'day': 'numeric', + 'hour': 'numeric', + 'minute': '2-digit', + 'second': 'numeric' + } + }, + { + "optionsName": "shortDateTimeOptions", + "options": { + 'year': 'numeric', + 'month': 'numeric', + 'day': 'numeric', + 'hour': 'numeric', + 'minute': 'numeric' + } + }, + { + "optionsName": "fullDateOptions", + "options": { + 'year': 'numeric', + 'month': 'long', + 'day': 'numeric', + 'weekday': 'long' + } + }, + { + "optionsName": "mediumDateOptions", + "options": { + 'year': 'numeric', + 'month': 'short', + 'day': 'numeric' + } + }, + { + "optionsName": "shortDateOptions", + "options": { + 'year': 'numeric', + 'month': 'numeric', + 'day': 'numeric' + } + }, + { + "optionsName": "mediumTimeOptions", + "options": { + 'hour': 'numeric', + 'minute': 'numeric', + 'second': 'numeric' + } + }, + { + "optionsName": "shortTimeOptions", + "options": { + 'hour': 'numeric', + 'minute': 'numeric' + } + } +]; + +var currencyCodes = [ + 'EUR', + 'EUR', + 'EUR', + 'BGN', + 'HRK', + 'EUR', + 'CZK', + 'DKK', + 'EUR', + 'EUR', + 'EUR', + "EUR", + "EUR", + "HUF", + "EUR", + "EUR", + "MDL", + "EUR", + "PLN", + "EUR", + "RON", + "RUB", + "RSD", + "EUR", + "SLL", + "EUR", + "SEK", + "CHF", + "TRY", + "UAH", + "GBP", + "USD", + "NOK" +]; + +var currencyOptions = [ + { + "optionsName": "currencyCodes", + "options": { + 'style': 'currency', + 'useGrouping': false, + 'currencyDisplay': 'code' + } + }, + { + "optionsName": "currencyCodesGrouping", + "options": { + 'style': 'currency', + 'currencyDisplay': 'code' + } + }, + { + "optionsName": "currencyCodesSymbol", + "options": { + 'style': 'currency', + 'useGrouping': false, + 'currencyDisplay': 'symbol' + } + } +]; + +var numberOptions = [ + { + "optionsName": "percent", + "options": { + 'style': 'percent' + } + }, + { + "optionsName": "decimal", + "options": { + 'style': 'decimal', + 'minimumIntegerDigits': 2, + 'minimumFractionDigits': 1, + 'maximumFractionDigits': 2 + } + }, + { + "optionsName": "noLocale", + "options": { + 'style': 'currency', + 'currency': 'USD', + 'currencyDisplay': 'code' + } + } +]; + +var getDateTimeFormatForLocales = function () { + var i; + var j; + for (j = 0; j < dateTimeOptions.length; j++) { + console.log(dateTimeOptions[j].optionsName); + for (i = 0; i < locales.length; i++) { + console.log(locales[i] + '"' + new Intl.DateTimeFormat(locales[i], dateTimeOptions[j].options).format(new Date(2016, 2, 22, 9, 40, 39)) + '",'); + } + console.log(""); + } +} + +var getNumberFormatForLocales = function () { + var i; + var j; + var testNumber = 123456.789; + for (j = 0; j < currencyOptions.length; j++) { + console.log(currencyOptions[j].optionsName); + for (i = 0; i < locales.length; i++) { + var updatedCurrencyOptions = currencyOptions[j].options; + updatedCurrencyOptions.currency = currencyCodes[i]; + console.log(locales[i] + '"' + new Intl.NumberFormat(locales[i], updatedCurrencyOptions).format(testNumber) + '",'); + } + console.log(""); + } + for (j = 0; j < numberOptions.length; j++) { + console.log(numberOptions[j].optionsName); + for (i = 0; i < locales.length; i++) { + console.log(locales[i] + '"' + new Intl.NumberFormat(locales[i], numberOptions[j].options).format(testNumber) + '",'); + } + console.log(""); + } +} + +getDateTimeFormatForLocales(); +getNumberFormatForLocales(); \ No newline at end of file diff --git a/tests/package.json b/tests/package.json index 6870cfc..9755b61 100644 --- a/tests/package.json +++ b/tests/package.json @@ -2,21 +2,25 @@ "nativescript": { "id": "org.nativescript.tests", "tns-android": { - "version": "1.7.1" + "version": "2.1.1" } }, "dependencies": { - "nativescript-intl": "file:..\\dist\\nativescript-intl-0.0.1.tgz", + "nativescript-intl": "file:..\\dist\\nativescript-intl-0.0.3.tgz", "nativescript-unit-test-runner": "^0.3.3", - "tns-core-modules": "1.7.0" + "tns-core-modules": "2.1.0" }, "devDependencies": { + "babel-traverse": "6.10.4", + "babel-types": "6.11.1", + "babylon": "6.8.4", "chai": "^3.5.0", "karma": "^0.13.22", "karma-chai": "^0.1.0", "karma-mocha": "^0.2.2", "karma-mocha-reporter": "^2.0.0", "karma-nativescript-launcher": "^0.4.0", + "lazy": "1.0.11", "mocha": "^2.4.5" } -} +} \ No newline at end of file