Skip to content

Commit

Permalink
feat: remove empty keys from final YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
tensor5 committed Apr 29, 2024
1 parent 5f71fdd commit 86f3327
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/app/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import YAML from "yaml";
import { Col, Container, Row } from "design-react-kit";
import EditorContacts from "./EditorContacts";
import EditorContractors from "./EditorContractors";
import linter from "../linter";

const resolver: Resolver<PublicCode> = async (values) => {
const res = await validator(JSON.stringify(values), "main");
Expand Down Expand Up @@ -241,7 +242,7 @@ export default function Editor() {
/>
<InfoBox />
<YamlModal
yaml={YAML.stringify(getValues())}
yaml={YAML.stringify(linter(getValues()))}
display={isYamlModalVisible}
toggle={() => setYamlModalVisibility(!isYamlModalVisible)}
/>
Expand Down
24 changes: 24 additions & 0 deletions src/app/linter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { cloneDeep, isArray, isEmpty, isObjectLike, isUndefined } from "lodash";
import PublicCode from "./contents/publiccode";

export default function linter(pc: PublicCode): PublicCode {
return removeEmpty(cloneDeep(pc));
}

function removeEmpty<T>(obj: T): T {
if (typeof obj !== "object") return obj;

const ret = obj;

for (const key in ret) {
const val = removeEmpty(ret[key]);
if (
((isArray(val) || isObjectLike(val)) && isEmpty(val)) ||
isUndefined(val)
) {
delete ret[key];
}
}

return ret;
}

0 comments on commit 86f3327

Please sign in to comment.