Skip to content

Commit 077fb31

Browse files
author
Canmert Celikler
committed
feat: add narrowSymbol currency display support
(perf) move instance creation to the constructor. (feat) expose native formatter instance. (fix) merge NativeScript#24
1 parent 82cd6fa commit 077fb31

6 files changed

+104
-89
lines changed

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"tns-platform-declarations": "~6.5.8",
1111
"tslint": "^6.1.0",
1212
"typescript": "^3.9.0"
13+
},
14+
"dependencies": {
15+
"currency-symbol-map": "^5.1.0"
1316
}
1417
}

src/nativescript-intl.android.d.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import common = require("./nativescript-intl-common");
22
export declare class DateTimeFormat extends common.DateTimeFormat {
3-
getNativePattern(patternDefinition: {
4-
date?: string;
5-
time?: string;
6-
}, locale?: string): string;
3+
getNativePattern(
4+
patternDefinition: {
5+
date?: string;
6+
time?: string;
7+
},
8+
locale?: string
9+
): string;
710
formatNative(pattern: string, locale?: string, date?: Date): string;
811
}
912
export declare class NumberFormat extends common.NumberFormat {
10-
formatNative(value: number, locale?: string, options?: Intl.NumberFormatOptions, pattern?: string): any;
13+
constructor(locale?: string, options?: Intl.NumberFormatOptions, pattern?: string);
14+
formatNative(value: number): any;
1115
}

src/nativescript-intl.android.ts

+49-42
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import {
2-
DateTimeFormat as commonDateTimeFormat,
3-
NumberFormat as commonNumberFormat,
4-
FULL
5-
} from "./nativescript-intl-common";
1+
import { DateTimeFormat as commonDateTimeFormat, NumberFormat as commonNumberFormat, FULL } from "./nativescript-intl-common";
62
import { NumberFormatOptions } from "./nativescript-intl";
3+
import getSymbolFromCurrency from "currency-symbol-map";
74

85
let localesCache: Map<string, any> = new Map<string, any>();
96

@@ -20,7 +17,7 @@ function getNativeLocale(locale?: string) {
2017
if (firstHypenIndex > -1) {
2118
lang = locale.substr(0, firstHypenIndex);
2219
let nextHypenIndex = locale.substr(firstHypenIndex + 1).indexOf("-");
23-
country = locale.substr(firstHypenIndex + 1, (nextHypenIndex > -1) ? nextHypenIndex : undefined);
20+
country = locale.substr(firstHypenIndex + 1, nextHypenIndex > -1 ? nextHypenIndex : undefined);
2421
} else {
2522
lang = locale;
2623
}
@@ -37,7 +34,13 @@ function getNativeLocale(locale?: string) {
3734
}
3835

3936
export class DateTimeFormat extends commonDateTimeFormat {
40-
public getNativePattern(patternDefinition: {date?: string, time?: string}, locale?: string): string {
37+
public getNativePattern(
38+
patternDefinition: {
39+
date?: string;
40+
time?: string;
41+
},
42+
locale?: string
43+
): string {
4144
let result = "";
4245
let flag = 0;
4346
let nativeLocale;
@@ -69,8 +72,7 @@ export class DateTimeFormat extends commonDateTimeFormat {
6972
break;
7073
case 3:
7174
// date + locale
72-
dateFormat =
73-
java.text.DateFormat.getDateInstance(patternDefinition.date === FULL ? 0 : 3, nativeLocale);
75+
dateFormat = java.text.DateFormat.getDateInstance(patternDefinition.date === FULL ? 0 : 3, nativeLocale);
7476
break;
7577
case 4:
7678
// only time we always use long pattern using default locale
@@ -86,8 +88,7 @@ export class DateTimeFormat extends commonDateTimeFormat {
8688
break;
8789
case 7:
8890
// locale + date + time
89-
dateFormat =
90-
java.text.DateFormat.getDateTimeInstance(patternDefinition.date === FULL ? 0 : 3, 1, nativeLocale);
91+
dateFormat = java.text.DateFormat.getDateTimeInstance(patternDefinition.date === FULL ? 0 : 3, 1, nativeLocale);
9192
break;
9293
default:
9394
break;
@@ -97,14 +98,11 @@ export class DateTimeFormat extends commonDateTimeFormat {
9798
}
9899

99100
public formatNative(pattern: string, locale?: string, date?: Date): string {
100-
let sdf = locale ?
101-
new java.text.SimpleDateFormat(pattern, getNativeLocale(locale)) :
102-
new java.text.SimpleDateFormat(pattern);
101+
let sdf = locale ? new java.text.SimpleDateFormat(pattern, getNativeLocale(locale)) : new java.text.SimpleDateFormat(pattern);
103102
return sdf.format(date ? new java.util.Date(date.valueOf()) : new java.util.Date()).toString();
104103
}
105104
}
106105

107-
108106
// style?: string;
109107
// currency?: string;
110108
// currencyDisplay?: string;
@@ -113,69 +111,78 @@ export class DateTimeFormat extends commonDateTimeFormat {
113111
// minimumFractionDigits?: number;
114112
// maximumFractionDigits?: number;
115113
export class NumberFormat extends commonNumberFormat {
116-
public formatNative(value: number, locale?: string, options?: NumberFormatOptions, pattern?: string) {
117-
let numberFormat;
114+
public numberFormat: java.text.DecimalFormat | java.text.NumberFormat | any;
115+
constructor(locale?: string, options?: NumberFormatOptions, pattern?: string) {
116+
super(locale, options, pattern);
118117
if (pattern) {
119-
numberFormat = new java.text.DecimalFormat(pattern);
118+
this.numberFormat = new java.text.DecimalFormat(pattern);
120119
} else {
121-
if (options) {
120+
if (options && options.style) {
122121
switch (options.style.toLowerCase()) {
123122
case "decimal":
124-
numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
123+
this.numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
125124
break;
126125
case "percent":
127-
numberFormat = java.text.NumberFormat.getPercentInstance(getNativeLocale(locale));
126+
this.numberFormat = java.text.NumberFormat.getPercentInstance(getNativeLocale(locale));
128127
break;
129128
case "currency":
130-
numberFormat = java.text.NumberFormat.getCurrencyInstance(getNativeLocale(locale));
129+
this.numberFormat = java.text.NumberFormat.getCurrencyInstance(getNativeLocale(locale));
131130
if (options.currency !== void 0) {
132-
numberFormat.setCurrency(java.util.Currency.getInstance(options.currency));
131+
this.numberFormat.setCurrency(java.util.Currency.getInstance(options.currency));
133132
}
134133
break;
135134
default:
136-
numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
135+
this.numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
137136
break;
138137
}
139138
} else {
140-
numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
139+
this.numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
141140
}
142141
}
143142

144143
if (options && options.minimumIntegerDigits !== void 0) {
145-
numberFormat.setMinimumIntegerDigits(options.minimumIntegerDigits);
144+
this.numberFormat.setMinimumIntegerDigits(options.minimumIntegerDigits);
146145
}
147146

148147
if (options && options.minimumFractionDigits !== void 0) {
149-
numberFormat.setMinimumFractionDigits(options.minimumFractionDigits);
148+
this.numberFormat.setMinimumFractionDigits(options.minimumFractionDigits);
150149
}
151150

152151
if (options && options.maximumFractionDigits !== void 0) {
153-
numberFormat.setMaximumFractionDigits(options.maximumFractionDigits);
152+
this.numberFormat.setMaximumFractionDigits(options.maximumFractionDigits);
154153
}
155154

156155
if (options && options.useGrouping !== void 0) {
157-
numberFormat.setGroupingUsed(options.useGrouping);
156+
this.numberFormat.setGroupingUsed(options.useGrouping);
157+
}
158+
159+
let decimalFormatSymbols = locale
160+
? new java.text.DecimalFormatSymbols(getNativeLocale(locale))
161+
: new java.text.DecimalFormatSymbols();
162+
163+
if (options && options.currency !== void 0) {
164+
decimalFormatSymbols.setCurrency(java.util.Currency.getInstance(options.currency));
165+
// Use a narrow format symbol ("$100" rather than "US$100") (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#narrowsymbol)
166+
if (options && options.currencyDisplay === "narrowSymbol") {
167+
const currencySymbol = getSymbolFromCurrency(options.currency);
168+
if (currencySymbol) decimalFormatSymbols.setCurrencySymbol(currencySymbol);
169+
}
158170
}
159171

160-
let decimalFormatSymbols = locale ?
161-
new java.text.DecimalFormatSymbols(getNativeLocale(locale)) :
162-
new java.text.DecimalFormatSymbols();
163-
numberFormat.setDecimalFormatSymbols(decimalFormatSymbols);
172+
this.numberFormat.setDecimalFormatSymbols(decimalFormatSymbols);
164173

165-
if (options && (options.style.toLowerCase() === "currency" && options.currencyDisplay === "code")) {
174+
if (options && options.style.toLowerCase() === "currency" && options.currencyDisplay === "code") {
166175
if (!pattern) {
167-
let currrentPattern = numberFormat.toPattern();
176+
let currrentPattern = this.numberFormat.toPattern();
168177
// this will display currency code instead of currency symbol
169178
currrentPattern = currrentPattern.replace("¤", "¤¤");
170-
numberFormat = new java.text.DecimalFormat(currrentPattern);
171-
numberFormat.setDecimalFormatSymbols(decimalFormatSymbols);
172-
}
173-
174-
if (options.currency !== void 0) {
175-
decimalFormatSymbols.setCurrency(java.util.Currency.getInstance(options.currency));
179+
this.numberFormat = new java.text.DecimalFormat(currrentPattern);
180+
this.numberFormat.setDecimalFormatSymbols(decimalFormatSymbols);
176181
}
177182
}
183+
}
178184

179-
return numberFormat.format(value);
185+
public formatNative(value: number) {
186+
return this.numberFormat.format(value);
180187
}
181188
}

src/nativescript-intl.ios.d.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import common = require("./nativescript-intl-common");
22
export declare class DateTimeFormat extends common.DateTimeFormat {
3-
getNativePattern(patternDefinition: {
4-
date?: string;
5-
time?: string;
6-
}, locale?: string): string;
3+
getNativePattern(
4+
patternDefinition: {
5+
date?: string;
6+
time?: string;
7+
},
8+
locale?: string
9+
): string;
710
formatNative(pattern: string, locale?: string, date?: Date): string;
811
}
912
export declare class NumberFormat extends common.NumberFormat {
10-
formatNative(value: number, locale?: string, options?: Intl.NumberFormatOptions, pattern?: string): string;
13+
constructor(locale?: string, options?: Intl.NumberFormatOptions, pattern?: string);
14+
formatNative(value: number): string;
1115
}

src/nativescript-intl.ios.ts

+31-29
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import {
2-
DateTimeFormat as commonDateTimeFormat,
3-
NumberFormat as commonNumberFormat,
4-
FULL
5-
} from "./nativescript-intl-common";
1+
import { DateTimeFormat as commonDateTimeFormat, NumberFormat as commonNumberFormat, FULL } from "./nativescript-intl-common";
62
import { NumberFormatOptions } from "./nativescript-intl";
3+
import getSymbolFromCurrency from "currency-symbol-map";
74

85
export class DateTimeFormat extends commonDateTimeFormat {
9-
public getNativePattern(patternDefinition: {date?: string, time?: string}, locale?: string): string {
6+
public getNativePattern(patternDefinition: { date?: string; time?: string }, locale?: string): string {
107
let dateFormatter = NSDateFormatter.new();
118
if (locale) {
129
dateFormatter.locale = NSLocale.alloc().initWithLocaleIdentifier(locale);
1310
}
1411
if (patternDefinition.date) {
15-
dateFormatter.dateStyle = patternDefinition.date === FULL ?
16-
NSDateFormatterStyle.FullStyle :
17-
NSDateFormatterStyle.ShortStyle;
12+
dateFormatter.dateStyle = patternDefinition.date === FULL ? NSDateFormatterStyle.FullStyle : NSDateFormatterStyle.ShortStyle;
1813
}
1914
if (patternDefinition.time) {
2015
dateFormatter.timeStyle = NSDateFormatterStyle.LongStyle;
@@ -43,61 +38,68 @@ export class DateTimeFormat extends commonDateTimeFormat {
4338
// minimumFractionDigits?: number;
4439
// maximumFractionDigits?: number;
4540
export class NumberFormat extends commonNumberFormat {
46-
public formatNative(value: number, locale?: string, options?: NumberFormatOptions, pattern?: string) {
47-
let numberFormat = NSNumberFormatter.new();
41+
public numberFormat: NSNumberFormatter;
42+
constructor(locale?: string, options?: NumberFormatOptions, pattern?: string) {
43+
super(locale, options, pattern);
44+
this.numberFormat = NSNumberFormatter.new();
4845
if (locale) {
49-
numberFormat.locale = NSLocale.alloc().initWithLocaleIdentifier(locale);
46+
this.numberFormat.locale = NSLocale.alloc().initWithLocaleIdentifier(locale);
5047
}
51-
if (options) {
48+
if (options && options.style) {
5249
switch (options.style.toLowerCase()) {
5350
case "decimal":
54-
numberFormat.numberStyle = NSNumberFormatterStyle.DecimalStyle;
51+
this.numberFormat.numberStyle = NSNumberFormatterStyle.DecimalStyle;
5552
break;
5653
case "percent":
57-
numberFormat.numberStyle = NSNumberFormatterStyle.PercentStyle;
54+
this.numberFormat.numberStyle = NSNumberFormatterStyle.PercentStyle;
5855
break;
5956
case "currency":
60-
numberFormat.numberStyle = NSNumberFormatterStyle.CurrencyStyle;
57+
this.numberFormat.numberStyle = NSNumberFormatterStyle.CurrencyStyle;
6158
if (options.currency !== void 0) {
62-
numberFormat.currencyCode = options.currency;
59+
this.numberFormat.currencyCode = options.currency;
60+
if (options && options.currencyDisplay === "narrowSymbol") {
61+
const currencySymbol = getSymbolFromCurrency(options.currency);
62+
if (currencySymbol) this.numberFormat.currencySymbol = currencySymbol;
63+
}
6364
}
6465
break;
6566
default:
66-
numberFormat.numberStyle = NSNumberFormatterStyle.DecimalStyle;
67+
this.numberFormat.numberStyle = NSNumberFormatterStyle.DecimalStyle;
6768
break;
6869
}
6970
} else {
70-
numberFormat.numberStyle = NSNumberFormatterStyle.DecimalStyle;
71+
this.numberFormat.numberStyle = NSNumberFormatterStyle.DecimalStyle;
7172
}
7273

7374
if (options && options.minimumIntegerDigits !== void 0) {
74-
numberFormat.minimumIntegerDigits = options.minimumIntegerDigits;
75+
this.numberFormat.minimumIntegerDigits = options.minimumIntegerDigits;
7576
}
7677

7778
if (options && options.minimumFractionDigits !== void 0) {
78-
numberFormat.minimumFractionDigits = options.minimumFractionDigits;
79+
this.numberFormat.minimumFractionDigits = options.minimumFractionDigits;
7980
}
8081

8182
if (options && options.maximumFractionDigits !== void 0) {
82-
numberFormat.maximumFractionDigits = options.maximumFractionDigits;
83+
this.numberFormat.maximumFractionDigits = options.maximumFractionDigits;
8384
}
8485

8586
if (options && options.useGrouping !== void 0) {
86-
numberFormat.usesGroupingSeparator = options.useGrouping;
87+
this.numberFormat.usesGroupingSeparator = options.useGrouping;
8788
}
8889

8990
if (pattern) {
90-
numberFormat.positiveFormat = pattern;
91+
this.numberFormat.positiveFormat = pattern;
9192
} else {
92-
if (options && (options.style.toLowerCase() === "currency" && options.currencyDisplay === "code")) {
93-
let tempPattern = numberFormat.positiveFormat;
93+
if (options && options.style && options.style.toLowerCase() === "currency" && options.currencyDisplay === "code") {
94+
let tempPattern = this.numberFormat.positiveFormat;
9495
// this will display currency code instead of currency symbol
9596
tempPattern = tempPattern.replace("¤", "¤¤");
96-
numberFormat.positiveFormat = tempPattern;
97+
this.numberFormat.positiveFormat = tempPattern;
9798
}
9899
}
100+
}
99101

100-
// return numberFormat.stringFromNumber(NSNumber.alloc().initWithDouble(value));
101-
return numberFormat.stringFromNumber(value);
102+
public formatNative(value: number) {
103+
return this.numberFormat.stringFromNumber(value);
102104
}
103105
}

src/tsconfig.json

+3-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99
"preserveConstEnums": true,
1010
"sourceMap": false,
1111
"declaration": false,
12-
"lib": [
13-
"es2017"
14-
]
12+
"allowSyntheticDefaultImports": true,
13+
"lib": ["es2017"]
1514
},
16-
"include": [
17-
"./**/*.ts",
18-
"./**/*.d.ts",
19-
"!./node_modules/**/*.ts"
20-
],
15+
"include": ["./**/*.ts", "./**/*.d.ts", "!./node_modules/**/*.ts"],
2116
"exclude": []
2217
}

0 commit comments

Comments
 (0)