From 86eaa01448f0544e816dfdb3d2d3f59adcd16efd Mon Sep 17 00:00:00 2001 From: esheyw Date: Fri, 19 Jan 2024 16:29:19 -0800 Subject: [PATCH] document new helpers, update changelog --- CHANGELOG.md | 4 +++- README.md | 13 ++++++++++++- scripts/helpers/errorHelpers.mjs | 20 ++++++++++---------- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f1eda2..1a0e6d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,4 +18,6 @@ ## Release 0.2.0 - add `updateInitiativeSkillsDialog()` macro - add `MHDialog` class -- various tidyup \ No newline at end of file +- various tidyup +- add `isEmpty()` helper +- add `log` and `mhlog` helpers \ No newline at end of file diff --git a/README.md b/README.md index 5d1d275..d43871d 100644 --- a/README.md +++ b/README.md @@ -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)` diff --git a/scripts/helpers/errorHelpers.mjs b/scripts/helpers/errorHelpers.mjs index d490b90..c475890 100644 --- a/scripts/helpers/errorHelpers.mjs +++ b/scripts/helpers/errorHelpers.mjs @@ -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 }); } @@ -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); }