-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Collapses `reducer()` into `parser()` function. - Improves `reducer()` function, does not need to handle Array anymore. refactor: - Refactor lite `parser()` function. chore: - Test also lite `replacer()` and `twg()` function. - Add more test cases.
- Loading branch information
1 parent
6e1c9b3
commit 1dfddbc
Showing
12 changed files
with
260 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "twg", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "A utility function for grouping TailwindCSS variants.", | ||
"author": "Nguyễn Hoàng Nhân <[email protected]> (https://github.com/hoangnhan2ka3)", | ||
"homepage": "https://github.com/hoangnhan2ka3/twg", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,32 @@ | ||
import { type ClassValue } from "src/lite" | ||
|
||
function reducer(args: ClassValue[]) { | ||
return ( | ||
args.reduce<string[]>((acc: string[], cur: ClassValue) => { | ||
if (cur === undefined || cur === null || cur === false) return acc | ||
if (Array.isArray(cur)) { | ||
acc.push(...cur.filter(Boolean).map(String)) | ||
} else if (typeof cur === "object") { | ||
export function parser() { | ||
function reducer(args: ClassValue[]): string[] { | ||
return args.reduce<string[]>((acc, cur) => { | ||
if (cur && typeof cur === "object") { | ||
Object.entries(cur).forEach(([key, values]) => { | ||
const func = parser[key] as (...args: ClassValue[]) => string | ||
if (Array.isArray(values)) { | ||
values.flat(Infinity).forEach((value: ClassValue) => { | ||
acc.push(func(value)) | ||
}) | ||
} else { | ||
acc.push(func(values as ClassValue)) | ||
} | ||
(Array.isArray(values) ? values.flat(Infinity) : [values]).forEach( | ||
value => acc.push( | ||
(parser()[key] as (...args: ClassValue[]) => string)(value as ClassValue) | ||
) | ||
) | ||
}) | ||
} else { | ||
acc.push(...String(cur).split(" ")) | ||
} | ||
return acc | ||
}, []) | ||
).flat() | ||
} | ||
|
||
export const parser = new Proxy((...args: ClassValue[]) => { | ||
return reducer(args).join(" ") | ||
}, { | ||
get: function (obj, key: string) { | ||
return key ? ( | ||
...args: ClassValue[] | ||
) => reducer(args).filter(values => values.trim() !== "").map((values) => ( | ||
`${key}:${values}`.trim() | ||
)) : obj | ||
}, []).flat() | ||
} | ||
}) as Record<string, (...args: ClassValue[]) => string> & ((...args: ClassValue[]) => string) | ||
|
||
return new Proxy((...args: ClassValue[]) => { | ||
return reducer(args).join(" ") | ||
}, { | ||
get: function (obj, key: string) { | ||
return key ? ( | ||
...args: ClassValue[] | ||
) => reducer(args).filter(values => values.trim() !== "").map((values) => ( | ||
`${key}:${values}`.trim() | ||
)) : obj | ||
} | ||
}) as Record<string, (...args: ClassValue[]) => string> & ((...args: ClassValue[]) => string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,51 @@ | ||
import { type ClassValue, type TWGOptions } from "src/index" | ||
|
||
/** | ||
* Focusing on handling arrays and objects, looping them until all are flattened. | ||
* @param args The inputs class values | ||
* @param separator The separator used to join the classes | ||
* @returns string[] | ||
* @author `easy-tailwind` [Noriller] see <[reference](https://github.com/Noriller/easy-tailwind/blob/master/src/index.ts#L65C1-L89C2)> | ||
*/ | ||
function reducer(args: ClassValue[], options?: TWGOptions) { | ||
return ( | ||
args.reduce<string[]>((acc: string[], cur: ClassValue) => { | ||
if (cur === undefined || cur === null || cur === false) return acc | ||
if (Array.isArray(cur)) { | ||
acc.push(...cur.filter(Boolean).map(String)) | ||
} else if (typeof cur === "object") { | ||
Object.entries(cur).forEach(([key, values]) => { | ||
const func = parser(options)[key] as (...args: ClassValue[]) => string | ||
if (Array.isArray(values)) { | ||
values.flat(Infinity).forEach((value: ClassValue) => { | ||
acc.push(func(value)) | ||
}) | ||
} else { | ||
acc.push(func(values as ClassValue)) | ||
} | ||
}) | ||
} else { | ||
acc.push(...String(cur).split(" ")) | ||
} | ||
return acc | ||
}, []) | ||
).flat() | ||
} | ||
|
||
/** | ||
* Transforms the inputs. Map key to each values inside the Object zones. | ||
* @param options see [docs](https://github.com/hoangnhan2ka3/twg?tab=readme-ov-file#twg-options). | ||
* @param args The inputs class values | ||
* @returns string | ||
* @author `easy-tailwind` [Noriller] see <[reference](https://github.com/Noriller/easy-tailwind/blob/master/src/index.ts#L57C1-L63C4)> | ||
*/ | ||
export function parser(options?: TWGOptions) { | ||
const divider = (options?.separator !== undefined) | ||
? typeof options.separator === "string" | ||
? options.separator | ||
: "" | ||
: ":" | ||
|
||
/** | ||
* Focusing on handling arrays and objects, looping them until all are flattened. | ||
* @param args The inputs class values | ||
* @param separator The separator used to join the classes | ||
* @returns string[] | ||
*/ | ||
function reducer(args: ClassValue[]): string[] { | ||
return args.reduce<string[]>((acc, cur) => { | ||
if (cur && typeof cur === "object") { | ||
Object.entries(cur).forEach(([key, values]) => { | ||
(Array.isArray(values) ? values.flat(Infinity) : [values]).forEach( | ||
value => acc.push( | ||
(parser(options)[key] as (...args: ClassValue[]) => string)(value as ClassValue) | ||
) | ||
) | ||
}) | ||
} else { | ||
acc.push(...String(cur).split(" ")) | ||
} | ||
return acc | ||
}, []).flat() | ||
} | ||
|
||
return new Proxy((...args: ClassValue[]) => { | ||
return reducer(args, options).join(" ") | ||
return reducer(args).join(" ") | ||
}, { | ||
get: function (obj, key: string) { | ||
return key ? ( | ||
...args: ClassValue[] | ||
// filter out empty strings/spaces | ||
) => reducer(args, options).filter(values => values.trim() !== "").map((values) => ( | ||
) => reducer(args).filter(values => values.trim() !== "").map((values) => ( | ||
`${key}${divider}${values}`.trim() | ||
)) : obj | ||
} | ||
}) as Record<string, (...args: ClassValue[]) => string> & ((...args: ClassValue[]) => string) | ||
} | ||
|
||
export default parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.