Skip to content

Commit

Permalink
#87/fix: fixed handling undefined and null in the configurations (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
szikszail authored Dec 4, 2023
1 parent bbecbf9 commit 1f000f2
Show file tree
Hide file tree
Showing 5 changed files with 498 additions and 401 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## 1.2.0 2023-11-11
## 1.2.1 - 2023-11-24

### Fixed

-Fixed issue with undefined and null values ([#87](https://github.com/gherking/gherking/issues/87))

## 1.2.0 - 2023-11-11

### Added

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gherkin-formatter",
"version": "1.2.0",
"version": "1.2.1",
"description": "Tool to format gherkin-ast model to gherkin string",
"main": "index.js",
"types": "index.d.ts",
Expand Down
71 changes: 38 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,47 @@ export interface FormatOptions {
}

const DEFAULT_OPTIONS: FormatOptions = {
oneTagPerLine: false,
separateStepGroups: false,
compact: false,
lineBreak: null,
indentation: " ",
tagFormat: TagFormat.FUNCTIONAL,
oneTagPerLine: false,
separateStepGroups: false,
compact: false,
lineBreak: null,
indentation: " ",
tagFormat: TagFormat.FUNCTIONAL,
};

export const config = (options: Partial<FormatOptions>): FormatOptions => {
debug("config(options: %o)", options);
return {
...DEFAULT_OPTIONS,
...(options || {}),
};
export const config = (options?: Partial<FormatOptions>): FormatOptions => {
debug("config(options: %o)", options);
options = options ?? {};
return {
oneTagPerLine: options.oneTagPerLine ?? DEFAULT_OPTIONS.oneTagPerLine,
separateStepGroups: options.separateStepGroups ?? DEFAULT_OPTIONS.separateStepGroups,
compact: options.compact ?? DEFAULT_OPTIONS.compact,
lineBreak: options.lineBreak ?? DEFAULT_OPTIONS.lineBreak,
indentation: options.indentation ?? DEFAULT_OPTIONS.indentation,
tagFormat: options.tagFormat ?? DEFAULT_OPTIONS.tagFormat,
};
};

export const format = (document: Document, options?: Partial<FormatOptions>): string => {
debug("format(document: %s, options: %o)", document?.constructor.name, options);
if (!document) {
throw new Error("Document must be set!");
}
if (!(document instanceof Document)) {
throw new TypeError(`The passed object is not a GherkinDocument! ${document}`);
}
options = config(options);
astConfig.set({
tagFormat: options.tagFormat,
});
setDefaultOptions({
eol: options.lineBreak,
indent: options.indentation,
skipEmpty: options.compact,
skipFirstLevelIndent: true,
indentEmpty: false,
trimLeft: false,
trimRight: true,
});
return formatGherkinDocument(document, options);
debug("format(document: %s, options: %o)", document?.constructor.name, options);
if (!document) {
throw new Error("Document must be set!");
}
if (!(document instanceof Document)) {
throw new TypeError(`The passed object is not a GherkinDocument! ${document}`);
}
options = config(options);
astConfig.set({
tagFormat: options.tagFormat,
});
setDefaultOptions({
eol: options.lineBreak,
indent: options.indentation,
skipEmpty: options.compact,
skipFirstLevelIndent: true,
indentEmpty: false,
trimLeft: false,
trimRight: true,
});
return formatGherkinDocument(document, options);
};
Loading

0 comments on commit 1f000f2

Please sign in to comment.