@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/CHANGELOG.md +303 -0
- package/LICENSE +1 -1
- package/README.md +538 -8
- package/index.js +20 -31
- package/package.json +22 -6
- package/src/config.js +160 -0
- package/src/dates.js +155 -0
- package/src/frontmatter.js +50 -0
- package/src/glob.js +79 -0
- package/src/guard.js +105 -0
- package/src/html.js +76 -0
- package/src/lang.js +103 -0
- package/src/log.js +29 -0
- package/src/packages.js +143 -0
- package/src/pages.js +34 -0
- package/src/sort.js +38 -0
- package/src/urls.js +243 -0
- package/src/values.js +44 -0
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
|
+
}
|