Skip to content

Commit a96c7ce

Browse files
author
Nedyalko Nikolov
committed
Intl plugin outputs es6.
1 parent 2413d8f commit a96c7ce

18 files changed

+545
-327
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ tests/node_modules/
55
example/node_modules/
66
example/platforms/
77
dist/
8+
src/**/*.js

compile.sh

+18-7
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@
33
SOURCE_DIR=src;
44
PACK_DIR=dist/package/;
55
DIST_DIR=dist;
6+
ANDROID=android;
7+
IOS=ios;
68

79
copy_package_files() {
8-
cp "$SOURCE_DIR"/package.json $PACK_DIR
9-
cp "$SOURCE_DIR"/*.md $PACK_DIR
10-
cp "$SOURCE_DIR"/*.d.ts $PACK_DIR
10+
cp "$SOURCE_DIR"/package.json "$PACK_DIR"
11+
cp "$SOURCE_DIR"/*.md "$PACK_DIR"
12+
cp "$SOURCE_DIR"/*.d.ts "$PACK_DIR"
1113
}
1214

1315
#clean dist folder from previous compilation
14-
rm -rf $DIST_DIR
16+
rm -rf "$DIST_DIR"
1517

1618
#create package folder
17-
mkdir -p $PACK_DIR
19+
mkdir -p "$PACK_DIR"
20+
21+
# compile ts
22+
node_modules/.bin/tsc -p "$SOURCE_DIR" --outDir "$PACK_DIR"
23+
24+
node_modules/tslint/bin/tslint --project "$SOURCE_DIR"/tsconfig.json --config "$SOURCE_DIR"/tslint.json
25+
26+
# make commonjs bundle for ANDROID
27+
node_modules/rollup/bin/rollup -c --environment PLATFORM:$ANDROID
28+
29+
# make commonjs bundle for IOS
30+
node_modules/rollup/bin/rollup -c --environment PLATFORM:$IOS
1831

19-
#compile package and copy file required by npm
20-
node_modules/.bin/tsc -p $SOURCE_DIR --outDir $PACK_DIR
2132
copy_package_files

package.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
2-
"devDependencies": {
3-
"typescript": "^1.8.9"
4-
}
2+
"scripts": {
3+
"tslint": "tslint --project src/tsconfig.json --config src/tslint.json",
4+
"tsc": "tsc -p src/tsconfig.json",
5+
"create": "bash ./create.sh"
6+
},
7+
"devDependencies": {
8+
"rollup": "^0.36.4",
9+
"tns-platform-declarations": "^2.4.0-2016-09-28-1",
10+
"tslint": "^4.0.2",
11+
"typescript": "^2.0.6"
12+
}
513
}

rollup.config.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as path from "path";
2+
import * as fs from "fs";
3+
4+
class NsPathResolve {
5+
constructor(options){
6+
this.options = options;
7+
}
8+
resolveId(id, from){
9+
//console.log(`id: ${id}, from: ${from}`);
10+
if (from) {
11+
var filePath = path.join(path.dirname(from), `${id}.js`);
12+
//console.log(`filePath: ${filePath}`);
13+
if (!fs.existsSync(filePath)) {
14+
return path.join(path.dirname(from), `${id}.${process.env.PLATFORM}.js`);
15+
}
16+
}
17+
}
18+
}
19+
20+
const nsPathResolve = (config) => new NsPathResolve(config);
21+
22+
export default {
23+
entry: "dist/package/index.js",
24+
dest: `dist/package/bundle.${process.env.PLATFORM}.js`,
25+
format: "cjs",
26+
sourceMap: "inline",
27+
treeshake: false,
28+
plugins: [
29+
nsPathResolve()
30+
]
31+
}

src/index.d.ts

-1
This file was deleted.

src/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// important since typescript compiler does not recognize DateTimeFormat and NumberFormat
2+
import { DateTimeFormat, NumberFormat } from "./nativescript-intl";
3+
4+
export { DateTimeFormat, NumberFormat } from "./nativescript-intl";
5+
6+
declare var global: any;
7+
8+
if (!global.Intl) {
9+
global.Intl = {};
10+
}
11+
global.Intl.DateTimeFormat = DateTimeFormat;
12+
global.Intl.NumberFormat = NumberFormat;
13+

src/nativescript-intl-common.js

+87-97
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
"use strict";
2-
exports.NUMERIC = "numeric";
3-
exports.LONG = "long";
4-
exports.SHORT = "short";
5-
exports.TWODIGIT = "2-digit";
6-
exports.FULL = "full";
7-
var DateTimeFormat = (function () {
8-
function DateTimeFormat(locale, options, pattern) {
1+
export const NUMERIC = "numeric";
2+
export const LONG = "long";
3+
export const SHORT = "short";
4+
export const TWODIGIT = "2-digit";
5+
export const FULL = "full";
6+
export class DateTimeFormat {
7+
constructor(locale, options, pattern) {
98
this.locale = locale;
109
this.options = options;
1110
this.pattern = pattern;
@@ -23,50 +22,52 @@ var DateTimeFormat = (function () {
2322
"G": "era",
2423
"a": "hour12"
2524
};
26-
if (options && options.minute === exports.NUMERIC) {
27-
this.options.minute = exports.TWODIGIT;
25+
if (options && options.minute === NUMERIC) {
26+
this.options.minute = TWODIGIT;
2827
}
29-
if (options && options.hour === exports.NUMERIC) {
30-
this.options.hour = exports.TWODIGIT;
28+
if (options && options.hour === NUMERIC) {
29+
this.options.hour = TWODIGIT;
3130
}
3231
}
33-
DateTimeFormat.prototype.hasTimeOptions = function (options) {
32+
hasTimeOptions(options) {
3433
return options.hour !== undefined || options.minute !== undefined || options.second !== undefined;
35-
};
36-
DateTimeFormat.prototype.hasDateOptions = function (options) {
37-
return options.weekday !== undefined || options.year !== undefined || options.month !== undefined || options.day !== undefined;
38-
};
39-
DateTimeFormat.prototype.useFullDatePattern = function (intlOptions) {
40-
var i;
41-
var propsArray = Object.keys(intlOptions);
42-
var propsArrayLength = propsArray.length;
43-
var result = false;
34+
}
35+
hasDateOptions(options) {
36+
return options.weekday !== undefined ||
37+
options.year !== undefined ||
38+
options.month !== undefined ||
39+
options.day !== undefined;
40+
}
41+
useFullDatePattern(intlOptions) {
42+
let i;
43+
let propsArray = Object.keys(intlOptions);
44+
let propsArrayLength = propsArray.length;
45+
let result = false;
4446
for (i = 0; i < propsArrayLength; i++) {
45-
if (intlOptions[propsArray[i]] === exports.LONG || intlOptions[propsArray[i]] === exports.SHORT) {
47+
if (intlOptions[propsArray[i]] === LONG || intlOptions[propsArray[i]] === SHORT) {
4648
result = true;
4749
break;
4850
}
4951
}
5052
return result;
51-
};
52-
DateTimeFormat.prototype.getNativePattern = function (patternDefinition, locale) {
53+
}
54+
getNativePattern(patternDefinition, locale) {
5355
return "";
54-
};
55-
DateTimeFormat.prototype.getCorrectPatternForLocale = function () {
56-
var dateTimePatternOptions = {};
56+
}
57+
getCorrectPatternForLocale() {
58+
let dateTimePatternOptions = {};
5759
if (this.hasDateOptions(this.options)) {
5860
if (this.useFullDatePattern(this.options)) {
59-
dateTimePatternOptions.date = exports.FULL;
61+
dateTimePatternOptions.date = FULL;
6062
}
6163
else {
62-
dateTimePatternOptions.date = exports.SHORT;
64+
dateTimePatternOptions.date = SHORT;
6365
}
6466
}
6567
if (this.hasTimeOptions(this.options)) {
66-
dateTimePatternOptions.time = exports.FULL;
68+
dateTimePatternOptions.time = FULL;
6769
}
68-
var result = this.getNativePattern(dateTimePatternOptions, this.locale);
69-
console.log("nativePattern: " + result);
70+
let result = this.getNativePattern(dateTimePatternOptions, this.locale);
7071
if (this.options.hour) {
7172
if (this.options.hour12 !== undefined) {
7273
result = this.options.hour12 ? result.replace(/H/g, "h") : result.replace(/h/g, "H");
@@ -76,19 +77,19 @@ var DateTimeFormat = (function () {
7677
}
7778
}
7879
return result;
79-
};
80-
DateTimeFormat.prototype.getDateElementsFromPattern = function (pattern) {
81-
var result = [];
82-
var patternLength = pattern.length;
83-
var i = 0;
84-
var stringInsidePattern = false;
80+
}
81+
getDateElementsFromPattern(pattern) {
82+
let result = [];
83+
let patternLength = pattern.length;
84+
let i = 0;
85+
let stringInsidePattern = false;
8586
while (i < patternLength) {
8687
if (pattern[i] === '"' || pattern[i] === "'") {
87-
var p = i + 1;
88+
let p = i + 1;
8889
while (p < patternLength && pattern[i] !== pattern[p]) {
8990
p++;
9091
}
91-
for (var j = i; j < p + 1; j++) {
92+
for (let j = i; j < p + 1; j++) {
9293
result.push({
9394
"isDateElement": false,
9495
"patternValue": pattern[j]
@@ -98,7 +99,7 @@ var DateTimeFormat = (function () {
9899
continue;
99100
}
100101
if (this.dateTimeFormatElements.hasOwnProperty(pattern[i])) {
101-
var j = i;
102+
let j = i;
102103
while (i < patternLength && pattern[i] === pattern[j]) {
103104
i++;
104105
}
@@ -117,16 +118,16 @@ var DateTimeFormat = (function () {
117118
}
118119
}
119120
return result;
120-
};
121-
DateTimeFormat.prototype.prepareDateElement = function (intlOption, dateElement) {
121+
}
122+
prepareDateElement(intlOption, dateElement) {
122123
switch (intlOption) {
123-
case exports.NUMERIC:
124+
case NUMERIC:
124125
return dateElement;
125-
case exports.TWODIGIT:
126+
case TWODIGIT:
126127
return dateElement.repeat(2);
127-
case exports.SHORT:
128+
case SHORT:
128129
return dateElement.repeat(3);
129-
case exports.LONG:
130+
case LONG:
130131
return dateElement.repeat(4);
131132
case true:
132133
return dateElement;
@@ -135,22 +136,21 @@ var DateTimeFormat = (function () {
135136
default:
136137
return dateElement;
137138
}
138-
};
139-
DateTimeFormat.prototype.preparePattern = function (pattern, options) {
140-
console.log("preparePattern input - " + pattern);
141-
var patternOptions = this.getDateElementsFromPattern(pattern);
142-
var patternOptionsLength = patternOptions.length;
143-
for (var i_1 = 0; i_1 < patternOptionsLength; i_1++) {
144-
if (patternOptions[i_1].isDateElement) {
145-
var formatChar = patternOptions[i_1].patternValue[0];
146-
var intlOptionValue = options[patternOptions[i_1].intlOption];
139+
}
140+
preparePattern(pattern, options) {
141+
let patternOptions = this.getDateElementsFromPattern(pattern);
142+
let patternOptionsLength = patternOptions.length;
143+
for (let i = 0; i < patternOptionsLength; i++) {
144+
if (patternOptions[i].isDateElement) {
145+
let formatChar = patternOptions[i].patternValue[0];
146+
let intlOptionValue = options[patternOptions[i].intlOption];
147147
if (intlOptionValue !== undefined) {
148-
var newPatternValue = this.prepareDateElement(intlOptionValue, formatChar);
149-
patternOptions[i_1].patternValue = newPatternValue;
148+
let newPatternValue = this.prepareDateElement(intlOptionValue, formatChar);
149+
patternOptions[i].patternValue = newPatternValue;
150150
}
151151
else {
152-
if (i_1 > 0) {
153-
var j = i_1 - 1;
152+
if (i > 0) {
153+
let j = i - 1;
154154
while (patternOptions[j] && patternOptions[j].isDateElement === false) {
155155
if (patternOptions[j].patternValue !== " ") {
156156
if (patternOptions[j].patternValue !== '"' && patternOptions[j].patternValue !== "'") {
@@ -164,58 +164,48 @@ var DateTimeFormat = (function () {
164164
j--;
165165
}
166166
}
167-
patternOptions[i_1].patternValue = "";
167+
patternOptions[i].patternValue = "";
168168
}
169169
}
170170
}
171-
var result = [];
172-
var i = 0;
171+
let result = [];
172+
let i = 0;
173173
while (patternOptions[i].patternValue === "" || patternOptions[i].isDateElement === false) {
174174
i++;
175175
}
176176
for (i; i < patternOptionsLength; i++) {
177177
result.push(patternOptions[i].patternValue);
178178
}
179-
console.log("preparePattern output - " + result.join(""));
180179
return result.join("");
181-
};
182-
DateTimeFormat.prototype.formatNative = function (pattern, locale, date) {
180+
}
181+
formatNative(pattern, locale, date) {
183182
return "";
184-
};
185-
Object.defineProperty(DateTimeFormat.prototype, "preparedPattern", {
186-
get: function () {
187-
if (!this._preparedPattern) {
188-
if (this.pattern) {
189-
this._preparedPattern = this.pattern;
190-
}
191-
else {
192-
this._preparedPattern = this.preparePattern(this.getCorrectPatternForLocale(), this.options);
193-
}
183+
}
184+
get preparedPattern() {
185+
if (!this._preparedPattern) {
186+
if (this.pattern) {
187+
this._preparedPattern = this.pattern;
194188
}
195-
console.log("preparedPattern: " + this._preparedPattern);
196-
return this._preparedPattern;
197-
},
198-
enumerable: true,
199-
configurable: true
200-
});
201-
DateTimeFormat.prototype.format = function (date) {
189+
else {
190+
this._preparedPattern = this.preparePattern(this.getCorrectPatternForLocale(), this.options);
191+
}
192+
}
193+
return this._preparedPattern;
194+
}
195+
format(date) {
202196
return this.formatNative(this.preparedPattern, this.locale, date);
203-
};
204-
return DateTimeFormat;
205-
}());
206-
exports.DateTimeFormat = DateTimeFormat;
207-
var NumberFormat = (function () {
208-
function NumberFormat(locale, options, pattern) {
197+
}
198+
}
199+
export class NumberFormat {
200+
constructor(locale, options, pattern) {
209201
this.locale = locale;
210202
this.options = options;
211203
this.pattern = pattern;
212204
}
213-
NumberFormat.prototype.formatNative = function (value, locale, options, pattern) {
205+
formatNative(value, locale, options, pattern) {
214206
return "";
215-
};
216-
NumberFormat.prototype.format = function (value) {
207+
}
208+
format(value) {
217209
return this.formatNative(value, this.locale, this.options, this.pattern);
218-
};
219-
return NumberFormat;
220-
}());
221-
exports.NumberFormat = NumberFormat;
210+
}
211+
}

0 commit comments

Comments
 (0)