Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: eliminate Object.entries polyfill #4859

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/cc/src/cc/ColorSwitchCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type { ZWaveApplicationHost, ZWaveHost } from "@zwave-js/host/safe";
import { getEnumMemberName, keysOf, pick } from "@zwave-js/shared/safe";
import { validateArgs } from "@zwave-js/transformers";
import { clamp } from "alcalzone-shared/math";
import { entries } from "alcalzone-shared/objects";
import { isObject } from "alcalzone-shared/typeguards";
import {
CCAPI,
Expand Down Expand Up @@ -273,7 +272,7 @@ export class ColorSwitchCCAPI extends CCAPI {
targetColorValueId,
) ?? {};

for (const [key, value] of entries(colorTable)) {
for (const [key, value] of Object.entries(colorTable)) {
const component = colorTableKeyToComponent(key);
if (
component === ColorComponent.Red ||
Expand Down Expand Up @@ -421,7 +420,7 @@ export class ColorSwitchCCAPI extends CCAPI {
}

// Ensure that each property is numeric
for (const [key, val] of entries(value)) {
for (const [key, val] of Object.entries(value)) {
if (typeof val !== "number") {
throwWrongValueType(
this.ccId,
Expand Down Expand Up @@ -901,7 +900,7 @@ export class ColorSwitchCCSet extends ColorSwitchCC {
);
this.payload[0] = populatedColorCount & 0b11111;
let i = 1;
for (const [key, value] of entries(this.colorTable)) {
for (const [key, value] of Object.entries(this.colorTable)) {
const component = colorTableKeyToComponent(key);
this.payload[i] = component;
this.payload[i + 1] = clamp(value, 0, 0xff);
Expand Down
3 changes: 1 addition & 2 deletions packages/cc/src/lib/serializers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ZWaveError, ZWaveErrorCodes } from "@zwave-js/core";
import { clamp } from "alcalzone-shared/math";
import { entries } from "alcalzone-shared/objects";
import type { SetbackSpecialState, SetbackState, Switchpoint } from "./_Types";

export const setbackSpecialStateValues: Record<SetbackSpecialState, number> = {
Expand All @@ -26,7 +25,7 @@ export function encodeSetbackState(state: SetbackState): number {
export function decodeSetbackState(val: number): SetbackState | undefined {
if (val > 120) {
// Special state, try to look it up
const foundEntry = entries(setbackSpecialStateValues).find(
const foundEntry = Object.entries(setbackSpecialStateValues).find(
([, v]) => val === v,
);
if (!foundEntry) return;
Expand Down
42 changes: 30 additions & 12 deletions packages/config/src/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ZWaveLogContainer,
} from "@zwave-js/core";
import { getErrorMessage, JSONObject, num2hex } from "@zwave-js/shared";
import { entries } from "alcalzone-shared/objects";
import { isObject } from "alcalzone-shared/typeguards";
import { pathExists, readFile } from "fs-extra";
import JSON5 from "json5";
Expand Down Expand Up @@ -792,19 +791,27 @@ export async function loadDeviceClassesInternal(
}

const basicDeviceClasses = new Map<number, string>();
for (const [key, basicClass] of entries(definition.basic)) {
for (const [key, basicClass] of Object.entries(definition.basic)) {
if (!hexKeyRegexNDigits.test(key)) {
throwInvalidConfig(
"device classes",
`found invalid key "${key}" in the basic device class definition. Device classes must have lowercase hexadecimal IDs.`,
);
}
if (typeof basicClass !== "string") {
throwInvalidConfig(
"device classes",
`basic device class "${key}" must be a string`,
);
}
const keyNum = parseInt(key.slice(2), 16);
basicDeviceClasses.set(keyNum, basicClass);
}

const genericDeviceClasses = new Map<number, GenericDeviceClass>();
for (const [key, genericDefinition] of entries(definition.generic)) {
for (const [key, genericDefinition] of Object.entries(
definition.generic,
)) {
if (!hexKeyRegexNDigits.test(key)) {
throwInvalidConfig(
"device classes",
Expand All @@ -814,7 +821,7 @@ export async function loadDeviceClassesInternal(
const keyNum = parseInt(key.slice(2), 16);
genericDeviceClasses.set(
keyNum,
new GenericDeviceClass(keyNum, genericDefinition),
new GenericDeviceClass(keyNum, genericDefinition as any),
);
}

Expand Down Expand Up @@ -867,27 +874,38 @@ export async function loadIndicatorsInternal(
}

const indicators = new Map<number, string>();
for (const [id, label] of entries(definition.indicators)) {
for (const [id, label] of Object.entries(definition.indicators)) {
if (!hexKeyRegexNDigits.test(id)) {
throwInvalidConfig(
"indicators",
`found invalid key "${id}" in "indicators". Indicators must have lowercase hexadecimal IDs.`,
);
}
if (typeof label !== "string") {
throwInvalidConfig(
"indicators",
`indicator "${id}" must be a string`,
);
}
const idNum = parseInt(id.slice(2), 16);
indicators.set(idNum, label);
}

const properties = new Map<number, IndicatorProperty>();
for (const [id, propDefinition] of entries(definition.properties)) {
for (const [id, propDefinition] of Object.entries(
definition.properties,
)) {
if (!hexKeyRegexNDigits.test(id)) {
throwInvalidConfig(
"indicators",
`found invalid key "${id}" in "properties". Indicator properties must have lowercase hexadecimal IDs.`,
);
}
const idNum = parseInt(id.slice(2), 16);
properties.set(idNum, new IndicatorProperty(idNum, propDefinition));
properties.set(
idNum,
new IndicatorProperty(idNum, propDefinition as any),
);
}

return { indicators, properties };
Expand Down Expand Up @@ -924,7 +942,7 @@ export async function loadMetersInternal(
}

const meters = new Map();
for (const [id, meterDefinition] of entries(definition)) {
for (const [id, meterDefinition] of Object.entries(definition)) {
if (!hexKeyRegexNDigits.test(id)) {
throwInvalidConfig(
"meters",
Expand Down Expand Up @@ -971,7 +989,7 @@ export async function loadNotificationsInternal(
}

const notifications = new Map();
for (const [id, ntfcnDefinition] of entries(definition)) {
for (const [id, ntfcnDefinition] of Object.entries(definition)) {
if (!hexKeyRegexNDigits.test(id)) {
throwInvalidConfig(
"notifications",
Expand Down Expand Up @@ -1021,7 +1039,7 @@ export async function loadNamedScalesInternal(
}

const namedScales = new Map<string, ScaleGroup>();
for (const [name, scales] of entries(definition)) {
for (const [name, scales] of Object.entries(definition)) {
if (!/[\w\d]+/.test(name)) {
throwInvalidConfig(
"named scales",
Expand All @@ -1033,7 +1051,7 @@ export async function loadNamedScalesInternal(
Scale
>();
named.name = name;
for (const [key, scaleDefinition] of entries(
for (const [key, scaleDefinition] of Object.entries(
scales as JSONObject,
)) {
if (!hexKeyRegexNDigits.test(key)) {
Expand Down Expand Up @@ -1085,7 +1103,7 @@ export async function loadSensorTypesInternal(
}

const sensorTypes = new Map();
for (const [key, sensorDefinition] of entries(definition)) {
for (const [key, sensorDefinition] of Object.entries(definition)) {
if (!hexKeyRegexNDigits.test(key)) {
throwInvalidConfig(
"sensor types",
Expand Down
5 changes: 2 additions & 3 deletions packages/config/src/DeviceClasses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CommandClasses } from "@zwave-js/core/safe";
import { JSONObject, num2hex } from "@zwave-js/shared/safe";
import { distinct } from "alcalzone-shared/arrays";
import { entries } from "alcalzone-shared/objects";
import { isArray, isObject } from "alcalzone-shared/typeguards";
import { hexKeyRegexNDigits, throwInvalidConfig } from "./utils_safe";

Expand Down Expand Up @@ -144,7 +143,7 @@ export class GenericDeviceClass {

const specific = new Map<number, SpecificDeviceClass>();
if (isObject(definition.specific)) {
for (const [specificKey, specificDefinition] of entries(
for (const [specificKey, specificDefinition] of Object.entries(
definition.specific,
)) {
if (!hexKeyRegexNDigits.test(specificKey))
Expand All @@ -161,7 +160,7 @@ export class GenericDeviceClass {
specificKeyNum,
new SpecificDeviceClass(
specificKeyNum,
specificDefinition,
specificDefinition as any,
this,
),
);
Expand Down
3 changes: 1 addition & 2 deletions packages/config/src/Manufacturers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isZWaveError, ZWaveError, ZWaveErrorCodes } from "@zwave-js/core";
import { formatId, stringify } from "@zwave-js/shared";
import { entries } from "alcalzone-shared/objects";
import { isObject } from "alcalzone-shared/typeguards";
import { pathExists, readFile, writeFile } from "fs-extra";
import JSON5 from "json5";
Expand Down Expand Up @@ -36,7 +35,7 @@ export async function loadManufacturersInternal(
}

const manufacturers = new Map();
for (const [id, name] of entries(definition)) {
for (const [id, name] of Object.entries(definition)) {
if (!hexKeyRegex4Digits.test(id)) {
throwInvalidConfig(
"manufacturers",
Expand Down
3 changes: 1 addition & 2 deletions packages/config/src/Meters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JSONObject, num2hex } from "@zwave-js/shared/safe";
import { entries } from "alcalzone-shared/objects";
import { isObject } from "alcalzone-shared/typeguards";
import { hexKeyRegexNDigits, throwInvalidConfig } from "./utils_safe";

Expand All @@ -12,7 +11,7 @@ export class Meter {

const scales = new Map<number, MeterScale>();
if (isObject(definition.scales)) {
for (const [scaleId, scaleDefinition] of entries(
for (const [scaleId, scaleDefinition] of Object.entries(
definition.scales,
)) {
if (!hexKeyRegexNDigits.test(scaleId)) {
Expand Down
11 changes: 6 additions & 5 deletions packages/config/src/Notifications.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JSONObject, num2hex } from "@zwave-js/shared/safe";
import { entries } from "alcalzone-shared/objects";
import { isArray, isObject } from "alcalzone-shared/typeguards";
import { hexKeyRegexNDigits, throwInvalidConfig } from "./utils_safe";

Expand Down Expand Up @@ -34,7 +33,7 @@ export class Notification {
: [];
const events = new Map<number, NotificationEvent>();
if (isObject(definition.events)) {
for (const [eventId, eventDefinition] of entries(
for (const [eventId, eventDefinition] of Object.entries(
definition.events,
)) {
if (!hexKeyRegexNDigits.test(eventId)) {
Expand All @@ -48,7 +47,7 @@ export class Notification {
const eventIdNum = parseInt(eventId.slice(2), 16);
events.set(
eventIdNum,
new NotificationEvent(eventIdNum, eventDefinition),
new NotificationEvent(eventIdNum, eventDefinition as any),
);
}
}
Expand Down Expand Up @@ -100,7 +99,9 @@ export class NotificationVariable {
);
}
const states = new Map<number, NotificationState>();
for (const [stateId, stateDefinition] of entries(definition.states)) {
for (const [stateId, stateDefinition] of Object.entries(
definition.states,
)) {
if (!hexKeyRegexNDigits.test(stateId)) {
throwInvalidConfig(
"notifications",
Expand All @@ -110,7 +111,7 @@ export class NotificationVariable {
const stateIdNum = parseInt(stateId.slice(2), 16);
states.set(
stateIdNum,
new NotificationState(stateIdNum, stateDefinition),
new NotificationState(stateIdNum, stateDefinition as any),
);
}
this.states = states;
Expand Down
5 changes: 2 additions & 3 deletions packages/config/src/SensorTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ZWaveError, ZWaveErrorCodes } from "@zwave-js/core/safe";
import { JSONObject, num2hex } from "@zwave-js/shared/safe";
import { entries } from "alcalzone-shared/objects";
import { isObject } from "alcalzone-shared/typeguards";
import type { ConfigManager } from "./ConfigManager";
import { Scale, ScaleGroup } from "./Scales";
Expand Down Expand Up @@ -50,7 +49,7 @@ export class SensorType {
"sensor types",
`scale definition for ${num2hex(key)} is not an object`,
);
for (const [scaleKey, scaleDefinition] of entries(
for (const [scaleKey, scaleDefinition] of Object.entries(
definition.scales,
)) {
if (!hexKeyRegexNDigits.test(scaleKey))
Expand All @@ -63,7 +62,7 @@ export class SensorType {
const scaleKeyNum = parseInt(scaleKey.slice(2), 16);
scales.set(
scaleKeyNum,
new Scale(scaleKeyNum, scaleDefinition),
new Scale(scaleKeyNum, scaleDefinition as any),
);
}
this.scales = scales;
Expand Down
11 changes: 5 additions & 6 deletions packages/config/src/devices/DeviceConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ReadonlyObjectKeyMap,
stringify,
} from "@zwave-js/shared";
import { entries } from "alcalzone-shared/objects";
import { isArray, isObject } from "alcalzone-shared/typeguards";
import * as fs from "fs-extra";
import { pathExists, readFile, writeFile } from "fs-extra";
Expand Down Expand Up @@ -451,7 +450,7 @@ firmwareVersion is malformed or invalid. Must be x.y or x.y.z where x, y, and z
endpoints is not an object`,
);
}
for (const [key, ep] of entries(definition.endpoints)) {
for (const [key, ep] of Object.entries(definition.endpoints)) {
if (!/^\d+$/.test(key)) {
throwInvalidConfig(
`device`,
Expand All @@ -463,7 +462,7 @@ found non-numeric endpoint index "${key}" in endpoints`,
const epIndex = parseInt(key, 10);
endpoints.set(
epIndex,
new ConditionalEndpointConfig(filename, epIndex, ep),
new ConditionalEndpointConfig(filename, epIndex, ep as any),
);
}
this.endpoints = endpoints;
Expand All @@ -481,7 +480,7 @@ found non-numeric endpoint index "${key}" in endpoints`,
associations is not an object`,
);
}
for (const [key, assocDefinition] of entries(
for (const [key, assocDefinition] of Object.entries(
definition.associations,
)) {
if (!/^[1-9][0-9]*$/.test(key)) {
Expand All @@ -498,7 +497,7 @@ found non-numeric group id "${key}" in associations`,
new ConditionalAssociationConfig(
filename,
keyNum,
assocDefinition,
assocDefinition as any,
),
);
}
Expand Down Expand Up @@ -577,7 +576,7 @@ found invalid param number "${paramNo}" in paramInformation`,
// We need to support parsing legacy files because users might have custom configs
// However, we don't allow this on CI or during tests/lint

for (const [key, paramDefinition] of entries(
for (const [key, paramDefinition] of Object.entries(
definition.paramInformation,
)) {
const match = /^(\d+)(?:\[0x([0-9a-fA-F]+)\])?$/.exec(key);
Expand Down
5 changes: 2 additions & 3 deletions packages/config/src/devices/EndpointConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { JSONObject } from "@zwave-js/shared/safe";
import { entries } from "alcalzone-shared/objects";
import { isObject } from "alcalzone-shared/typeguards";
import { throwInvalidConfig } from "../utils_safe";
import {
Expand Down Expand Up @@ -43,7 +42,7 @@ export class ConditionalEndpointConfig
Endpoint ${index}: associations is not an object`,
);
}
for (const [key, assocDefinition] of entries(
for (const [key, assocDefinition] of Object.entries(
definition.associations,
)) {
if (!/^[1-9][0-9]*$/.test(key)) {
Expand All @@ -60,7 +59,7 @@ Endpoint ${index}: found non-numeric group id "${key}" in associations`,
new ConditionalAssociationConfig(
filename,
keyNum,
assocDefinition,
assocDefinition as any,
),
);
}
Expand Down
Loading