Skip to content

Commit

Permalink
document new helpers, update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
esheyw committed Jan 20, 2024
1 parent 555e209 commit 86eaa01
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
## Release 0.2.0
- add `updateInitiativeSkillsDialog()` macro
- add `MHDialog` class
- various tidyup
- various tidyup
- add `isEmpty()` helper
- add `log` and `mhlog` helpers
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,18 @@ Returns an `Error` with the message having been passed through `localize()` as a
Localizes `str` with `data`, preprends `prefix`, and calls `ui.notifications[type]` with the result and `console`. Errors if `type` is not `info`, `warn`, or `error`. If `notify` is nullish, falls back on the module setting, as above. If `log` is provided and is an object, it will be passed to `console[type]()`. `notify` functions as above.
#### `MHLError(str, data = {}, { notify = null, prefix = "MacroHelperLibrary: ", log = {}, func = null } = {})`
A simple wrapper on `localizedError` above, pre-fills the prefix for this library's calls, and provides the `func` variable which, if provided, is inserted between the prefix and the rest of the error string, for more a more granular 'where did this error come from' report.

#### `isEmpty(value)`
Checks if value is empty. Deep-checks arrays and objects.
```js
isEmpty([]) == true
isEmpty({}) == true
isEmpty([{0:false},"",0]) == true
isEmpty({0:1}) == false
```
#### `log(loggable, type = null, prefix = null)`
Passes `loggable` to `console[type]()`, with `prefix` as a separate argument first for ease of console filtering.
#### `mhlog(loggable, type = null, prefix="MHL |") `
Simple wrapper on the above with a set prefix.
---
### PF2e-specific Helpers
#### `levelBasedDC(level)`
Expand Down
20 changes: 10 additions & 10 deletions scripts/helpers/errorHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ export function localizedError(str, data = {}, { notify = null, prefix = "", log
return Error(errorstr);
}

export function localizedBanner(str, data = {}, { notify = null, prefix = "", log = {}, type = "info", console=true} = {}) {
export function localizedBanner(
str,
data = {},
{ notify = null, prefix = "", log = {}, type = "info", console = true } = {}
) {
const func = "localizedBanner";
notify ??= NOTIFY();
if (!notify) return false;
if (!BANNER_TYPES.includes(type)) throw MHLError(`MHL.Error.BannerType`, null, { func, log: { type } });
let bannerstr = "" + prefix;
if (typeof log === "object" && Object.keys(log).length) mhlog(log,type);
if (typeof log === "object" && Object.keys(log).length) mhlog(log, type);
if (typeof str !== "string") {
throw MHLError(`MHL.Error.Type.String`, { var: "str" }, { func, log: { str } });
}
bannerstr += localize(str, data);
return ui.notifications[type](bannerstr, {console});
return ui.notifications[type](bannerstr, { console });
}

export function MHLError(
str,
data = {},
{ notify = null, prefix = "MHL | ", log = {}, func = null } = {}
) {
export function MHLError(str, data = {}, { notify = null, prefix = "MHL | ", log = {}, func = null } = {}) {
if (func && typeof func === "string") prefix += func;
return localizedError(str, data, { notify, prefix, log });
}
Expand Down Expand Up @@ -70,6 +70,6 @@ export function log(loggable, type = null, prefix = null) {
return console[type](prefix, loggable);
}

export function mhlog(loggable, type = null) {
return log(loggable, type, "MacroHelperLibrary |");
export function mhlog(loggable, type = null, prefix = "MHL |") {
return log(loggable, type, prefix);
}

0 comments on commit 86eaa01

Please sign in to comment.