Skip to content

Commit 73e445c

Browse files
committed
Make the Options class more robust
- Includes default values. - Can be constructed from a partial.
1 parent 85e995f commit 73e445c

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/Config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export type DuplicatesOption = 'preserve' | 'remove';
2+
export type FillOption = 'fill' | 'no-fill';
3+
export type ImportsOption = 'include' | 'style-only';
4+
export type MergeNthOption = 'merge' | 'no-merge';
5+
export type SanitizationOption = 'all' | 'imports' | 'off';
6+
7+
export class Options {
8+
duplicates?: DuplicatesOption = 'remove';
9+
fill?: FillOption = 'fill';
10+
imports?: ImportsOption = 'style-only';
11+
mergeNth?: MergeNthOption = 'merge';
12+
sanitize?: SanitizationOption = 'all';
13+
14+
constructor (options?: Partial<Options>) {
15+
if (typeof options?.duplicates === 'string') this.duplicates = options.duplicates;
16+
if (typeof options?.fill === 'string') this.fill = options.fill;
17+
if (typeof options?.imports === 'string') this.imports = options.imports;
18+
if (typeof options?.mergeNth === 'string') this.mergeNth = options.mergeNth;
19+
if (typeof options?.sanitize === 'string') this.sanitize = options.sanitize;
20+
}
21+
}

src/Generator.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import { createParser } from 'css-selector-parser';
22
import type { AstRule, AstSelector } from 'css-selector-parser';
3+
import { Options } from './Config.js';
34
import { Descriptor } from './Descriptor.js';
45
import { createCSSOM, elementsAreComparable, mergeElements, sanitizeElement } from './Utility.js';
56

6-
export class Options {
7-
duplicates?: 'preserve' | 'remove';
8-
fill?: 'fill' | 'no-fill';
9-
imports?: 'include' | 'style-only';
10-
mergeNth?: 'merge' | 'no-merge';
11-
sanitize?: 'all' | 'imports' | 'off';
12-
}
13-
147
const parse = createParser({ syntax: 'progressive' });
158

169
class Rule {
@@ -31,7 +24,8 @@ class Rule {
3124
* @param options `(Optional)` Options with which to configure the generator.
3225
* @returns An HTML body element containing the generated DOM.
3326
*/
34-
export async function cssToHtml (css: CSSRuleList | string, options: Options = {}): Promise<HTMLBodyElement> {
27+
export async function cssToHtml (css: CSSRuleList | string, options: Partial<Options> = {}): Promise<HTMLBodyElement> {
28+
options = new Options(options);
3529
const output = document.createElement('body');
3630

3731
const fillerElements = new Array<HTMLElement>();

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export { cssToHtml, Options } from './Generator.js';
1+
export { Options } from './Config';
2+
export { cssToHtml } from './Generator.js';

0 commit comments

Comments
 (0)