Skip to content

Commit

Permalink
chore: Refactor code to improve readability and remove unnecessary co…
Browse files Browse the repository at this point in the history
…nditional checks
  • Loading branch information
hoangnhan2ka3 committed Aug 30, 2024
1 parent 71c525c commit 86a110c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function toVal(mix: ClassValue, options?: TWGOptions): string {
}
} else {
for (y in mix) {
if (mix[y] && typeof mix[y] !== "string" && typeof mix[y] !== "object") {
if (mix[y] && typeof mix[y] !== "string" && !Array.isArray(mix[y])) {
str && (str += " ")
str += y
} else {
Expand Down
5 changes: 0 additions & 5 deletions src/lite/processor/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export function transformer(
path.traverse({
ObjectExpression(innerPath) {
try {
// 2. Find outer Object(s) inside callee function
if (!innerPath.findParent(parentPath => parentPath.isObjectExpression() && parentPath.node === innerPath.node)) {
// 3. First parse ternary and logical conditionals
innerPath.traverse({
ConditionalExpression(innerPath) {
if (innerPath.findParent((parent) => parent.isTemplateLiteral())) {
Expand All @@ -55,7 +53,6 @@ export function transformer(
}
})

// 4. And parse the values if it is conditional
innerPath.traverse({
ObjectProperty(innerPath) {
if (
Expand All @@ -67,10 +64,8 @@ export function transformer(
}
})

// 5. Then take the outer Object(s) out
const largestObject = generate(innerPath.node).code

// DONE. Final replace the original outer Object(s) with parsed one
innerPath.replaceWith(types.stringLiteral(
parser(...new Function(
`return [${(/['"`]|:\s*1/g).test(largestObject) ? largestObject : ""}]`
Expand Down
2 changes: 1 addition & 1 deletion src/processor/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function reducer(args: ClassValue[], options?: TWGOptions) {
/**
* Transforms the inputs. Map key to each values inside the Object zones.
* @param {TWGOptions} [options = {separator=":"}] separator. See [docs](https://github.com/hoangnhan2ka3/twg?tab=readme-ov-file#twg-options).
* @param {ClassValue[]} args The inputs class values.
* @param {...ClassValue[]} args The inputs class values.
* @returns string
*/
export function parser(options?: TWGOptions) {
Expand Down
7 changes: 4 additions & 3 deletions tests/dev/replacer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,13 @@ describe("replacer()", () => {
var1: true
},
{
var2: 0
var2: 0,
var3: 1
},
className
)} />
`,
expected: `<div className={twg("multiple classes", "var1", "var2", className)} />;`
expected: `<div className={twg("multiple classes", "var1", "var2 var3", className)} />;`
},
{
contents: `
Expand All @@ -486,7 +487,7 @@ describe("replacer()", () => {
{
var1: true,
var2: "multiple classes",
var3: true,
var3: 1,
var4: true
},
{
Expand Down

0 comments on commit 86a110c

Please sign in to comment.