Skip to content

Commit

Permalink
Fix createThemeVars when using null values (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcompiles authored Apr 26, 2021
1 parent ccd7c58 commit e154028
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/dirty-brooms-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@vanilla-extract/private': patch
'@vanilla-extract/css': patch
---

Fix `createThemeVars` when using null values
27 changes: 19 additions & 8 deletions packages/private/src/walkObject.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import { MapLeafNodes } from './types';

export function walkObject<T, MapTo>(
type Primitive = string | number | null | undefined;

type Walkable = {
[Key in string | number]: Primitive | Walkable;
};

export function walkObject<T extends Walkable, MapTo>(
obj: T,
fn: (value: string | number, path: Array<string>) => MapTo,
fn: (value: Primitive, path: Array<string>) => MapTo,
path: Array<string> = [],
): MapLeafNodes<T, MapTo> {
// @ts-expect-error
const clone = obj.constructor();

for (let key in obj) {
const value = obj[key];
const currentPath = [...path, key];

if (typeof value === 'object') {
clone[key] = value ? walkObject(value, fn, currentPath) : value;
} else if (typeof value === 'string' || typeof value === 'number') {
clone[key] = fn(value, currentPath);
if (
typeof value === 'string' ||
typeof value === 'number' ||
value == null
) {
clone[key] = fn(value as Primitive, currentPath);
} else if (typeof value === 'object' && !Array.isArray(value)) {
clone[key] = walkObject(value as Walkable, fn, currentPath);
} else {
console.warn(
`Skipping invalid key "${currentPath.join(
'.',
)}". Should be a string, number or object. Received: "${typeof value}"`,
)}". Should be a string, number, null or object. Received: "${
Array.isArray(value) ? 'Array' : typeof value
}"`,
);
}
}
Expand Down

0 comments on commit e154028

Please sign in to comment.