-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathattribute_converters.ts
43 lines (39 loc) · 1.33 KB
/
attribute_converters.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {ComplexAttributeConverter} from 'lit';
type LatLngLiteral = google.maps.LatLngLiteral;
/**
* Converter that transforms an optional `google.maps.LatLngLiteral` property
* to/from an attribute string literal in the `lat,lng` format.
*/
export const LAT_LNG_LITERAL_ATTRIBUTE_CONVERTER:
ComplexAttributeConverter<LatLngLiteral|undefined> = {
fromAttribute(attr: string|null): LatLngLiteral |
undefined {
if (attr === null) return undefined;
const [lat, lng] = attr.split(',').map((s) => Number(s.trim()));
return {lat: lat || 0, lng: lng || 0};
},
toAttribute(prop?: LatLngLiteral): string |
null {
return prop ? `${prop.lat},${prop.lng}` : null;
},
};
/**
* Converter that transforms an optional string-array property to/from a
* space-delimited attribute value.
*/
export const STRING_ARRAY_ATTRIBUTE_CONVERTER:
ComplexAttributeConverter<string[]|undefined> = {
fromAttribute(attr: string|null): string[] |
undefined {
return attr?.split(/\s+/).filter((s) => s !== '') ?? undefined;
},
toAttribute(prop?: string[]): string |
null {
return prop?.join(' ') ?? null;
},
};