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

feat(tools-language): add keysOf as a type-safe alt to Object.keys #1975

Merged
merged 1 commit into from
Oct 26, 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
6 changes: 6 additions & 0 deletions .changeset/nine-terms-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rnx-kit/tools-language": minor
"@rnx-kit/cli": patch
---

Add `keysOf` to `@rnx-kit/tools-language`, a type-safe wrapper around `Object.keys`
2 changes: 1 addition & 1 deletion packages/align-deps/src/capabilities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Capability } from "@rnx-kit/config";
import { warn } from "@rnx-kit/console";
import { keysOf } from "@rnx-kit/tools-language/properties";
import type { PackageManifest } from "@rnx-kit/tools-node/package";
import { keysOf } from "./helpers";
import type { MetaPackage, Package, Preset, Profile } from "./types";

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/align-deps/src/commands/setVersion.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { keysOf } from "@rnx-kit/tools-language/properties";
import type { PackageManifest } from "@rnx-kit/tools-node/package";
import isString from "lodash/isString";
import prompts from "prompts";
import semverCoerce from "semver/functions/coerce";
import { transformConfig } from "../compatibility/config";
import { defaultConfig, loadConfig } from "../config";
import { isError } from "../errors";
import { keysOf, modifyManifest } from "../helpers";
import { modifyManifest } from "../helpers";
import defaultPreset from "../presets/microsoft/react-native";
import type {
AlignDepsConfig,
Expand Down
3 changes: 2 additions & 1 deletion packages/align-deps/src/commands/vigilant.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Capability } from "@rnx-kit/config";
import { error } from "@rnx-kit/console";
import { keysOf } from "@rnx-kit/tools-language/properties";
import type { PackageManifest } from "@rnx-kit/tools-node/package";
import * as path from "path";
import semverSubset from "semver/ranges/subset";
import { resolveCapabilities } from "../capabilities";
import { keysOf, modifyManifest } from "../helpers";
import { modifyManifest } from "../helpers";
import { updateDependencies } from "../manifest";
import { filterPreset, mergePresets } from "../preset";
import type {
Expand Down
4 changes: 0 additions & 4 deletions packages/align-deps/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ export function dropPatchFromVersion(version: string): string {
.join(" || ");
}

export function keysOf<T extends Record<string, unknown>>(obj: T): (keyof T)[] {
return Object.keys(obj);
}

export function modifyManifest(
pkgPath: string,
manifest: PackageManifest
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/copy-assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Config as CLIConfig } from "@react-native-community/cli-types";
import { error, info, warn } from "@rnx-kit/console";
import { isNonEmptyArray } from "@rnx-kit/tools-language/array";
import { keysOf } from "@rnx-kit/tools-language/properties";
import type { PackageManifest } from "@rnx-kit/tools-node/package";
import {
findPackageDependencyDir,
Expand Down Expand Up @@ -93,10 +94,6 @@ function isAssetsConfig(config: unknown): config is AssetsConfig {
return typeof config === "object" && config !== null && "getAssets" in config;
}

function keysOf(record: Record<string, unknown> | undefined): string[] {
return record ? Object.keys(record) : [];
}

export function versionOf(pkgName: string): string {
const packageDir = findPackageDependencyDir(pkgName);
if (!packageDir) {
Expand Down
1 change: 1 addition & 0 deletions packages/tools-language/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import * from "@rnx-kit/tools-language/properties";
| properties | `extendObject(obj, extendedProps)` | Add properties to an object, changing it from its current type to an extended type. |
| properties | `extendObjectArray(arr, extendedProps)` | Add properties to each object in an array, changing the object from its current type to an extended type. |
| properties | `hasProperty(obj, property)` | Returns whether `property` exists in `obj`. |
| properties | `keysOf(obj)` | Returns the names of the enumerable string properties of an object. Equivalent to calling `Object.keys()`, but type safe. |
| properties | `pickValue(obj, key, name)` | Pick the value for property `key` from `obj` and return it in a new object. If `name` is given, use it in the new object, instead of `key`. |
| properties | `pickValues(obj, keys, names)` | Pick the value for each `key` property from `obj` and return each one in a new object. If `names` are given, use them in the new object, instead of `keys`. |

Expand Down
12 changes: 12 additions & 0 deletions packages/tools-language/src/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,15 @@ export function hasProperty<Property extends string>(
): obj is Record<Property, unknown> {
return typeof obj === "object" && obj !== null && property in obj;
}

/**
* Returns the names of the enumerable string properties of an object.
* Equivalent to calling `Object.keys()`, but type safe.
* @param obj Object that contains the properties
* @returns Type-safe array of properties of the specified object
*/
export function keysOf<T extends Record<string, unknown>>(
obj: T | undefined
): (keyof T)[] {
return obj ? Object.keys(obj) : [];
}