style: replace Number/String/parseInt/parseFloat with shorter equivalents
Replace Number(x) with +x, String(x) with template literal, parseInt(x, 10) with +, and parseFloat(x || "0") with +(x ?? 0). Fix operator precedence with parentheses around ?? expressions.
This commit is contained in:
parent
7ae092ac98
commit
11cae8c89f
@ -16,7 +16,7 @@ export default class LeafletControlScale extends WithProps(HTMLElement, PROPS) {
|
||||
connectedCallback() {
|
||||
this.#obj = new Control.Scale({
|
||||
position: this.getAttribute('position') as ControlPosition | undefined,
|
||||
maxWidth: parseInt(this.getAttribute('max-width') ?? '100', 10),
|
||||
maxWidth: +(this.getAttribute('max-width') ?? 100),
|
||||
metric: !this.hasAttribute('metric') || this.getAttribute('metric') !== 'false',
|
||||
imperial: !this.hasAttribute('imperial') || this.getAttribute('imperial') !== 'false',
|
||||
updateWhenIdle:
|
||||
|
||||
@ -4,10 +4,7 @@ export default class LeafletLine extends HTMLElement {
|
||||
}
|
||||
|
||||
get latlng(): [number, number] {
|
||||
return [
|
||||
parseFloat(this.getAttribute('lat') || '0'),
|
||||
parseFloat(this.getAttribute('lng') || '0'),
|
||||
];
|
||||
return [+(this.getAttribute('lat') ?? 0), +(this.getAttribute('lng') ?? 0)];
|
||||
}
|
||||
|
||||
attributeChangedCallback() {
|
||||
|
||||
@ -113,13 +113,13 @@ export default class LeafletMap extends TypedBase {
|
||||
Object.defineProperty(LeafletMap.prototype, propName, {
|
||||
get(this: LeafletMap) {
|
||||
const val = spec.mapGet && this.#map ? spec.mapGet(this.#map) : undefined;
|
||||
return val !== undefined ? val : Number(this.getAttribute(spec.attr) ?? spec.default);
|
||||
return val !== undefined ? val : +(this.getAttribute(spec.attr) ?? spec.default);
|
||||
},
|
||||
set(this: LeafletMap, v: number) {
|
||||
if (spec.event) {
|
||||
this.#syncAttr(spec.attr, String(v));
|
||||
this.#syncAttr(spec.attr, `${v}`);
|
||||
} else {
|
||||
this.setAttribute(spec.attr, String(v));
|
||||
this.setAttribute(spec.attr, `${v}`);
|
||||
}
|
||||
},
|
||||
configurable: true,
|
||||
@ -166,9 +166,9 @@ export default class LeafletMap extends TypedBase {
|
||||
|
||||
// Read view-state from attributes directly — the getters call getCenter()/getZoom()
|
||||
// which throw if invoked before setView(), so we can't use them here.
|
||||
const lat = Number(this.getAttribute('lat') ?? 0);
|
||||
const lng = Number(this.getAttribute('lng') ?? 0);
|
||||
const zoom = Number(this.getAttribute('zoom') ?? 2);
|
||||
const lat = +(this.getAttribute('lat') ?? 0);
|
||||
const lng = +(this.getAttribute('lng') ?? 0);
|
||||
const zoom = +(this.getAttribute('zoom') ?? 2);
|
||||
|
||||
// Assign #map only after setView so the getters' `this.#map` guard is
|
||||
// equivalent to "map is ready" — getCenter()/getZoom() throw before setView.
|
||||
@ -193,7 +193,7 @@ export default class LeafletMap extends TypedBase {
|
||||
for (const s of specs) {
|
||||
if (this.#map && s.mapGet) {
|
||||
const v = s.mapGet(this.#map);
|
||||
if (v !== undefined) this.#syncAttr(s.attr, String(v));
|
||||
if (v !== undefined) this.#syncAttr(s.attr, `${v}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -233,7 +233,7 @@ export default class LeafletMap extends TypedBase {
|
||||
if (!propName) return;
|
||||
const spec = PROPS[propName] as PropDef;
|
||||
if (spec.kind === 'num') {
|
||||
if (spec.mapSet && newValue !== null) spec.mapSet(this.#map, Number(newValue));
|
||||
if (spec.mapSet && newValue !== null) spec.mapSet(this.#map, +newValue);
|
||||
} else if (spec.kind === 'bool-off') {
|
||||
spec.mapSet?.(this.#map, newValue === null);
|
||||
}
|
||||
@ -292,7 +292,7 @@ export default class LeafletMap extends TypedBase {
|
||||
if (spec.kind === 'num') {
|
||||
if (spec.viewState) continue;
|
||||
const v = this.getAttribute(spec.attr);
|
||||
if (v !== null) o[propName] = Number(v);
|
||||
if (v !== null) o[propName] = +v;
|
||||
} else if (spec.kind === 'bool-off') {
|
||||
if (this.hasAttribute(spec.attr)) o[propName] = false;
|
||||
} else {
|
||||
|
||||
@ -57,8 +57,8 @@ export default class LeafletMarker extends WithProps(HTMLElement, PROPS) {
|
||||
if (!this.#obj || this.#syncing) return;
|
||||
this.#syncing = true;
|
||||
const pos = this.#obj.getLatLng();
|
||||
this.setAttribute('lat', String(pos.lat));
|
||||
this.setAttribute('lng', String(pos.lng));
|
||||
this.setAttribute('lat', `${pos.lat}`);
|
||||
this.setAttribute('lng', `${pos.lng}`);
|
||||
this.#syncing = false;
|
||||
};
|
||||
}
|
||||
|
||||
@ -31,14 +31,14 @@ function definePropAccessors(proto: object, props: Record<string, PropDef>) {
|
||||
get() {
|
||||
const el = this as HTMLElement;
|
||||
const val = el.getAttribute(spec.attr);
|
||||
if (spec.kind === 'num') return val !== null ? Number(val) : spec.default;
|
||||
if (spec.kind === 'num') return val !== null ? +val : spec.default;
|
||||
if (spec.kind === 'bool-on') return el.hasAttribute(spec.attr);
|
||||
return val ?? (spec as { default: string }).default;
|
||||
},
|
||||
set(v: unknown) {
|
||||
const el = this as HTMLElement;
|
||||
if (spec.kind === 'bool-on') el.toggleAttribute(spec.attr, !!v);
|
||||
else el.setAttribute(spec.attr, String(v));
|
||||
else el.setAttribute(spec.attr, `${v}`);
|
||||
},
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
@ -51,7 +51,7 @@ function definePropAccessors(proto: object, props: Record<string, PropDef>) {
|
||||
// reading lat/lng/radius/opacity with a guaranteed number return.
|
||||
export function numAttr(el: HTMLElement, props: Record<string, PropDef>, name: string): number {
|
||||
const v = el.getAttribute(name);
|
||||
if (v !== null) return Number(v);
|
||||
if (v !== null) return +v;
|
||||
return (props[name] as { default: number }).default;
|
||||
}
|
||||
|
||||
@ -107,7 +107,7 @@ export function parseAttributeValue(value: string | null): unknown {
|
||||
if (value === null) return null;
|
||||
if (value === 'true') return true;
|
||||
if (value === 'false') return false;
|
||||
const num = Number(value);
|
||||
const num = +value;
|
||||
if (!isNaN(num) && value !== '') return num;
|
||||
try {
|
||||
return JSON.parse(value);
|
||||
@ -143,7 +143,7 @@ export function buildOptions<
|
||||
if ('default' in spec && spec.default !== '') opts[propName] = spec.default;
|
||||
continue;
|
||||
}
|
||||
if (spec.kind === 'num') opts[propName] = Number(val);
|
||||
if (spec.kind === 'num') opts[propName] = +val;
|
||||
else if (spec.kind === 'bool-on') opts[propName] = true;
|
||||
else if (spec.kind === 'bool-off') opts[propName] = false;
|
||||
else opts[propName] = val;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user