Skip to content

Commit 26cce82

Browse files
committed
fix: some style property conversion fixes
1 parent 728e79c commit 26cce82

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/layers/parser/property-parser.android.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1+
import { Color } from '@nativescript/core';
2+
13
function toCamelCase(s) {
24
return s.replace(/([-_][a-z])/gi, ($1) => $1.toUpperCase().replace('-', '').replace('_', ''));
35
}
6+
7+
const Expression = com.mapbox.mapboxsdk.style.expressions.Expression;
8+
function transformValue(key, value) {
9+
let nValue = value;
10+
if (Array.isArray(value)) {
11+
nValue = Expression.Converter.convert(JSON.stringify(value));
12+
}
13+
if (key.indexOf('-color') !== -1 && !Array.isArray(value)) {
14+
const color = value instanceof Color ? value : new Color(value);
15+
nValue = color.android;
16+
} else if (typeof value === 'number') {
17+
nValue = new java.lang.Float(value);
18+
} else if (typeof value === 'boolean') {
19+
nValue = new java.lang.Boolean(value);
20+
}
21+
return nValue;
22+
}
423
export class PropertyParser {
524
static parsePropertiesForLayer(propertiesObject) {
625
const nProperties = [];
726

827
const PropertyFactory = com.mapbox.mapboxsdk.style.layers.PropertyFactory;
9-
const Expression = com.mapbox.mapboxsdk.style.expressions.Expression;
1028
if (propertiesObject) {
1129
Object.keys(propertiesObject).forEach((k) => {
1230
const actualKey = toCamelCase(k);
1331
const value = propertiesObject[k];
14-
let nValue = value;
15-
if (Array.isArray(value)) {
16-
nValue = Expression.Converter.convert(JSON.stringify(value));
17-
} else if (typeof value === 'number') {
18-
nValue = new java.lang.Float(value);
19-
}
32+
const nValue = transformValue(k, value);
2033
nProperties.push(PropertyFactory[actualKey](nValue));
2134
});
2235
}

src/layers/parser/property-parser.ios.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ export class PropertyParser {
6767
Object.keys(propertiesObject).forEach((k) => {
6868
const actualKey = keysMap[k] || toCamelCase(k);
6969
const value = propertiesObject[k];
70+
const rValue = transformValue(k, value);
7071
if (Array.isArray(value)) {
71-
nProperties[actualKey] = (NSExpression as any).expressionWithMGLJSONObject(transformValue(k, value));
72+
nProperties[actualKey] = (NSExpression as any).expressionWithMGLJSONObject(rValue);
7273
} else {
73-
nProperties[actualKey] = NSExpression.expressionForConstantValue(transformValue(k, value));
74+
nProperties[actualKey] = NSExpression.expressionForConstantValue(rValue);
7475
}
7576
});
7677
}

0 commit comments

Comments
 (0)