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

chore(utils): add comments + cleanup util functions #10628

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions packages/core/utils/src/common/filter-object-by-keys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { isDefined } from "./is-defined"

/*
Given an array of keys that are object paths (like product.categories.id), this function will
return an object that contains keys from those object paths

Given an object: testObject = {
product: {
id: "test-product",
name: "Test Product",
categories: [{
id: "test-category",
name: "Test Category"
}]
}
}

filterObjectByKeys(testObject, ['product.categories.id']) will return

{
product: {
categories: [{
id: "test-category"
}]
}
}
*/
export function filterObjectByKeys(obj, paths) {
function buildObject(paths) {
const result = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import { isObject } from "./is-object"

type NestedObject = {
[key: string]: any
}

/*
Given a deeply nested object that can be arrays or objects, this function will flatten
it to an object that is 1 level deep.

Given an object: testObject = {
product: {
id: "test-product",
name: "Test Product",
categories: [{
id: "test-category",
name: "Test Category"
}]
}
}

flattenObjectToKeyValuePairs(testObject) will return

{
"product.id": "test-product",
"product.name": "Test Product",
"product.categories.id": ["test-category"],
"product.categories.name": ["Test Category"]
}
*/
export function flattenObjectToKeyValuePairs(obj: NestedObject): NestedObject {
const result: NestedObject = {}

Expand All @@ -12,17 +38,17 @@ export function flattenObjectToKeyValuePairs(obj: NestedObject): NestedObject {
): string[][] {
const paths: string[][] = []

if (!obj || typeof obj !== "object") {
if (!obj || !isObject(obj)) {
return paths
}

// If it's an array of objects, add this path
if (Array.isArray(obj) && obj.length > 0 && typeof obj[0] === "object") {
if (Array.isArray(obj) && obj.length > 0 && isObject(obj[0])) {
paths.push(currentPath)
}

// Check all properties
if (typeof obj === "object") {
if (isObject(obj)) {
Object.entries(obj as Record<string, unknown>).forEach(([key, value]) => {
const newPath = [...currentPath, key]
paths.push(...findArrayPaths(value, newPath))
Expand All @@ -35,7 +61,7 @@ export function flattenObjectToKeyValuePairs(obj: NestedObject): NestedObject {
// Extract array values at a specific path
function getArrayValues(obj: unknown, path: string[]): unknown[] {
const arrayObj = path.reduce((acc: unknown, key: string) => {
if (acc && typeof acc === "object") {
if (isObject(acc)) {
return (acc as Record<string, unknown>)[key]
}
return undefined
Expand All @@ -48,7 +74,7 @@ export function flattenObjectToKeyValuePairs(obj: NestedObject): NestedObject {

// Process non-array paths
function processRegularPaths(obj: unknown, prefix = ""): void {
if (!obj || typeof obj !== "object") {
if (!obj || !isObject(obj)) {
result[prefix] = obj
return
}
Expand All @@ -57,7 +83,7 @@ export function flattenObjectToKeyValuePairs(obj: NestedObject): NestedObject {

Object.entries(obj as Record<string, unknown>).forEach(([key, value]) => {
const newPrefix = prefix ? `${prefix}.${key}` : key
if (value && typeof value === "object" && !Array.isArray(value)) {
if (isObject(obj) && !Array.isArray(value)) {
processRegularPaths(value, newPrefix)
} else if (!Array.isArray(value)) {
result[newPrefix] = value
Expand All @@ -78,7 +104,7 @@ export function flattenObjectToKeyValuePairs(obj: NestedObject): NestedObject {
// Get all possible keys from the array objects
const keys = new Set<string>()
arrayObjects.forEach((item) => {
if (item && typeof item === "object") {
if (isObject(item)) {
Object.keys(item as object).forEach((k) => keys.add(k))
}
})
Expand All @@ -87,7 +113,7 @@ export function flattenObjectToKeyValuePairs(obj: NestedObject): NestedObject {
keys.forEach((key) => {
const values = arrayObjects
.map((item) => {
if (item && typeof item === "object") {
if (isObject(item)) {
return (item as Record<string, unknown>)[key]
}
return undefined
Expand All @@ -96,7 +122,7 @@ export function flattenObjectToKeyValuePairs(obj: NestedObject): NestedObject {

if (values.length > 0) {
const newPath = `${pathStr}.${key}`
if (values.every((v) => typeof v === "object" && !Array.isArray(v))) {
if (values.every((v) => isObject(v) && !Array.isArray(v))) {
// If these are all objects, recursively process them
const subObj = { [key]: values }
const subResult = flattenObjectToKeyValuePairs(subObj)
Expand Down
Loading