Skip to content

Commit

Permalink
feat: support json properties on reearth/core (#412)
Browse files Browse the repository at this point in the history
* feat: support json properties

* fix: copy
  • Loading branch information
keiya01 committed Feb 1, 2023
1 parent 07a9359 commit ac79868
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
48 changes: 48 additions & 0 deletions src/core/mantle/evaluator/simple/index.test.ts
Expand Up @@ -41,6 +41,54 @@ test("evalSimpleLayer", async () => {
});
});

test("evaluate json properties", async () => {
expect(
await evalSimpleLayer(
{
id: "x",
type: "simple",
data: {
type: "geojson",
jsonProperties: ["key1", "key2"],
},
},
{
getAllFeatures: async () => [
{
type: "feature",
id: "a",
},
{
type: "feature",
id: "b",
properties: {
key1: `["hoge", "fuga"]`,
key2: "abc",
},
},
],
getFeatures: async () => undefined,
},
),
).toEqual({
layer: {},
features: [
{
type: "computedFeature",
id: "a",
},
{
type: "computedFeature",
id: "b",
properties: {
key1: ["hoge", "fuga"],
key2: "abc",
},
},
],
});
});

describe("Conditional styling", () => {
test("conditions with variables from properties, members and Strictly Equals", () => {
expect(
Expand Down
35 changes: 28 additions & 7 deletions src/core/mantle/evaluator/simple/index.ts
Expand Up @@ -23,19 +23,16 @@ export async function evalSimpleLayer(
const appearances: Partial<LayerAppearanceTypes> = pick(layer, appearanceKeys);
return {
layer: evalLayerAppearances(appearances, layer),
features: features?.map(f => ({
...f,
...evalLayerAppearances(appearances, layer, f),
type: "computedFeature",
})),
features: features?.map(f => evalSimpleLayerFeature(layer, f)),
};
}

export const evalSimpleLayerFeature = (layer: LayerSimple, feature: Feature): ComputedFeature => {
const appearances: Partial<LayerAppearanceTypes> = pick(layer, appearanceKeys);
const nextFeature = evalJsonProperties(layer, feature);
return {
...feature,
...evalLayerAppearances(appearances, layer, feature),
...nextFeature,
...evalLayerAppearances(appearances, layer, nextFeature),
type: "computedFeature",
};
};
Expand Down Expand Up @@ -77,3 +74,27 @@ function evalExpression(expressionContainer: any, layer: LayerSimple, feature?:
}
return expressionContainer;
}

function evalJsonProperties(layer: LayerSimple, feature: Feature): Feature {
const keys = layer.data?.jsonProperties;
if (!feature.properties || !keys || !keys.length) {
return feature;
}

const next = {
...feature,
...(feature?.properties ? { properties: { ...feature.properties } } : {}),
};
keys.forEach(k => {
next.properties[k] = (() => {
const p = next.properties[k];
try {
return JSON.parse(p);
} catch {
return p;
}
})();
});

return next;
}
1 change: 1 addition & 0 deletions src/core/mantle/types/index.ts
Expand Up @@ -62,6 +62,7 @@ export type Data = {
url?: string;
value?: any;
layers?: string | string[];
jsonProperties?: string[];
csv?: {
idColumn?: string | number;
latColumn?: string | number;
Expand Down

0 comments on commit ac79868

Please sign in to comment.