@nera-static/plugin-utils 1.6.1 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/values.js ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Value checks for parsed YAML and frontmatter, shared by every Nera plugin
3
+ * and tool (`@nera-static/plugin-utils/values`).
4
+ */
5
+
6
+ /**
7
+ * Whether `value` is a plain object: a mapping as YAML or JSON produce it,
8
+ * or `Object.create(null)`. Arrays, `null`, `Date`, `Map` and class
9
+ * instances are not.
10
+ *
11
+ * An object's prototype must be `null` or an `Object.prototype` — also one
12
+ * of another realm (a `node:vm` context), recognised as the end of its
13
+ * prototype chain, so the check does not depend on this realm's `Object`.
14
+ *
15
+ * @param {unknown} value
16
+ * @returns {boolean}
17
+ */
18
+ export function isPlainObject(value) {
19
+ if (value === null || typeof value !== 'object') return false
20
+ const prototype = Object.getPrototypeOf(value)
21
+ return prototype === null || Object.getPrototypeOf(prototype) === null
22
+ }
23
+
24
+ /**
25
+ * Whether `value` is a string with at least one non-whitespace character.
26
+ *
27
+ * @param {unknown} value
28
+ * @returns {boolean}
29
+ */
30
+ export function isText(value) {
31
+ return typeof value === 'string' && value.trim() !== ''
32
+ }
33
+
34
+ /**
35
+ * `value` trimmed when it is text (see `isText`), otherwise `fallback`.
36
+ *
37
+ * @template T
38
+ * @param {unknown} value
39
+ * @param {T} [fallback] returned for a non-string or a blank string
40
+ * @returns {string | T}
41
+ */
42
+ export function textOr(value, fallback = undefined) {
43
+ return isText(value) ? value.trim() : fallback
44
+ }