@clementine-solutions/jane-io 1.0.1 → 1.0.3
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/dist/core/analysis/diff.js +88 -0
- package/dist/core/analysis/explain.js +117 -0
- package/dist/core/analysis/index.js +26 -0
- package/dist/core/analysis/replay.js +68 -0
- package/dist/core/analysis/telemetry.js +123 -0
- package/dist/core/boundary-rules/at-most-one.js +38 -0
- package/dist/core/boundary-rules/conditionally-required.js +44 -0
- package/dist/core/boundary-rules/date-range.js +39 -0
- package/dist/core/boundary-rules/index.js +21 -0
- package/dist/core/boundary-rules/mutually-exclusive.js +38 -0
- package/dist/core/boundary-rules/no-unknown-fields.js +39 -0
- package/dist/core/boundary-rules/require-all.js +39 -0
- package/dist/core/boundary-rules/require-one.js +41 -0
- package/dist/core/common/events.js +82 -0
- package/dist/core/common/fluent.js +429 -0
- package/dist/core/common/index.js +31 -0
- package/dist/core/common/policy.js +550 -0
- package/dist/core/common/utilities.js +177 -0
- package/dist/core/common/wildcard.js +63 -0
- package/dist/core/field-path/construct.js +189 -0
- package/dist/core/field-path/format.js +154 -0
- package/dist/core/field-path/index.js +26 -0
- package/dist/core/field-path/utilities.js +138 -0
- package/dist/core/field-path/walk.js +80 -0
- package/dist/core/fluent-registry.js +151 -0
- package/dist/core/normalizers/array/compact-sparse-array.js +40 -0
- package/dist/core/normalizers/array/flatten-one-level.js +45 -0
- package/dist/core/normalizers/array/remove-empty-string-items.js +34 -0
- package/dist/core/normalizers/array/remove-null-items.js +34 -0
- package/dist/core/normalizers/array/remove-undefined-items.js +34 -0
- package/dist/core/normalizers/date/invalid-date-to-undefined.js +35 -0
- package/dist/core/normalizers/index.js +46 -0
- package/dist/core/normalizers/normalizer-register.js +41 -0
- package/dist/core/normalizers/number/infinity-to-undefined.js +35 -0
- package/dist/core/normalizers/number/nan-to-undefined.js +34 -0
- package/dist/core/normalizers/number/normalize-negative-zero.js +33 -0
- package/dist/core/normalizers/object/remove-empty-array-keys.js +38 -0
- package/dist/core/normalizers/object/remove-empty-object-keys.js +42 -0
- package/dist/core/normalizers/object/remove-empty-string-keys.js +37 -0
- package/dist/core/normalizers/object/remove-null-keys.js +37 -0
- package/dist/core/normalizers/object/remove-undefined-keys.js +37 -0
- package/dist/core/normalizers/string/collapse-whitespace.js +35 -0
- package/dist/core/normalizers/string/empty-to-undefined.js +33 -0
- package/dist/core/normalizers/string/trim.js +34 -0
- package/dist/core/parsers/index.js +43 -0
- package/dist/core/parsers/parse-array-string.js +36 -0
- package/dist/core/parsers/parse-bigint-string.js +33 -0
- package/dist/core/parsers/parse-binary-string.js +33 -0
- package/dist/core/parsers/parse-boolean-string.js +27 -0
- package/dist/core/parsers/parse-date-string.js +30 -0
- package/dist/core/parsers/parse-duration-string.js +37 -0
- package/dist/core/parsers/parse-hex-string.js +29 -0
- package/dist/core/parsers/parse-integer-string.js +30 -0
- package/dist/core/parsers/parse-json-string.js +35 -0
- package/dist/core/parsers/parse-numeric-string.js +29 -0
- package/dist/core/parsers/parse-object-string.js +36 -0
- package/dist/core/parsers/parse-octal-string.js +33 -0
- package/dist/core/parsers/parse-scientific-notation-string.js +30 -0
- package/dist/core/parsers/parse-url-string.js +36 -0
- package/dist/core/pipeline/boundary.js +256 -0
- package/dist/core/pipeline/contain.js +339 -0
- package/dist/core/pipeline/index.js +37 -0
- package/dist/core/pipeline/normalize.js +76 -0
- package/dist/core/pipeline/parse.js +91 -0
- package/dist/core/pipeline/pipeline.js +418 -0
- package/dist/core/pipeline/scan.js +115 -0
- package/dist/core/pipeline/validate.js +74 -0
- package/dist/core/scanners/any/scan-for-sentinels.js +35 -0
- package/dist/core/scanners/array/array-is-deep.js +38 -0
- package/dist/core/scanners/array/array-is-heterogenous.js +39 -0
- package/dist/core/scanners/array/array-is-large.js +32 -0
- package/dist/core/scanners/bigint/bigint-is-large.js +34 -0
- package/dist/core/scanners/bigint/bigint-not-safe.js +34 -0
- package/dist/core/scanners/date/date-is-before-epoch.js +32 -0
- package/dist/core/scanners/date/date-is-far-future.js +32 -0
- package/dist/core/scanners/date/date-is-invalid.js +31 -0
- package/dist/core/scanners/index.js +58 -0
- package/dist/core/scanners/number/number-is-infinite.js +31 -0
- package/dist/core/scanners/number/number-is-nan.js +31 -0
- package/dist/core/scanners/number/number-is-too-large.js +33 -0
- package/dist/core/scanners/number/number-is-unsafe-integer.js +31 -0
- package/dist/core/scanners/object/object-has-circular-references.js +43 -0
- package/dist/core/scanners/object/object-has-many-keys.js +33 -0
- package/dist/core/scanners/object/object-is-deep.js +38 -0
- package/dist/core/scanners/scanner-registry.js +36 -0
- package/dist/core/scanners/string/string-has-unsafe-unicode.js +32 -0
- package/dist/core/scanners/string/string-has-whitespace-edges.js +31 -0
- package/dist/core/scanners/string/string-is-long.js +32 -0
- package/dist/core/scanners/unknown/unknown-not-scannable.js +34 -0
- package/dist/core/shapes/analysis.js +11 -0
- package/dist/core/shapes/boundary.js +11 -0
- package/dist/core/shapes/events.js +10 -0
- package/dist/core/shapes/field-path.js +10 -0
- package/dist/core/shapes/index.js +11 -0
- package/dist/core/shapes/normalize.js +11 -0
- package/dist/core/shapes/parse.js +11 -0
- package/dist/core/shapes/pipeline.js +11 -0
- package/dist/core/shapes/policy.js +11 -0
- package/dist/core/shapes/public.js +10 -0
- package/dist/core/shapes/scan.js +11 -0
- package/dist/core/shapes/validate.js +11 -0
- package/dist/core/validators/array/array-max-items.js +42 -0
- package/dist/core/validators/array/array-min-items.js +42 -0
- package/dist/core/validators/array/array.js +34 -0
- package/dist/core/validators/array/excludes.js +47 -0
- package/dist/core/validators/array/has-unique-items.js +46 -0
- package/dist/core/validators/array/includes.js +46 -0
- package/dist/core/validators/array/items-equal.js +42 -0
- package/dist/core/validators/array/no-empty-string-items.js +46 -0
- package/dist/core/validators/array/no-null-items.js +46 -0
- package/dist/core/validators/array/no-undefined-items.js +45 -0
- package/dist/core/validators/array/non-empty-array.js +45 -0
- package/dist/core/validators/array/not-sparse.js +44 -0
- package/dist/core/validators/bigint/bigint-equals.js +57 -0
- package/dist/core/validators/bigint/bigint-max.js +63 -0
- package/dist/core/validators/bigint/bigint-min.js +87 -0
- package/dist/core/validators/bigint/bigint-negative.js +73 -0
- package/dist/core/validators/bigint/bigint-non-negative.js +72 -0
- package/dist/core/validators/bigint/bigint-non-positive.js +72 -0
- package/dist/core/validators/bigint/bigint-positive.js +72 -0
- package/dist/core/validators/bigint/bigint-safe.js +75 -0
- package/dist/core/validators/bigint/bigint.js +38 -0
- package/dist/core/validators/boolean/boolean.js +39 -0
- package/dist/core/validators/boolean/is-false.js +48 -0
- package/dist/core/validators/boolean/is-true.js +48 -0
- package/dist/core/validators/common/is-country-code.js +36 -0
- package/dist/core/validators/common/is-currency-code.js +36 -0
- package/dist/core/validators/common/is-email-strict.js +36 -0
- package/dist/core/validators/common/is-email.js +36 -0
- package/dist/core/validators/common/is-ip.js +37 -0
- package/dist/core/validators/common/is-phone-strict.js +36 -0
- package/dist/core/validators/common/is-phone.js +36 -0
- package/dist/core/validators/common/is-port.js +35 -0
- package/dist/core/validators/common/is-postal-code.js +36 -0
- package/dist/core/validators/common/is-url.js +38 -0
- package/dist/core/validators/common/is-uuid.js +36 -0
- package/dist/core/validators/date/before-epoch.js +56 -0
- package/dist/core/validators/date/date-now-required.js +48 -0
- package/dist/core/validators/date/is-date.js +47 -0
- package/dist/core/validators/date/is-far-future.js +46 -0
- package/dist/core/validators/date/is-future.js +59 -0
- package/dist/core/validators/date/is-past.js +59 -0
- package/dist/core/validators/date/not-after.js +66 -0
- package/dist/core/validators/date/not-before.js +66 -0
- package/dist/core/validators/date/same-day.js +60 -0
- package/dist/core/validators/date/same-month.js +59 -0
- package/dist/core/validators/date/same-year.js +56 -0
- package/dist/core/validators/date/too-early.js +57 -0
- package/dist/core/validators/date/too-late.js +57 -0
- package/dist/core/validators/date/weekday.js +65 -0
- package/dist/core/validators/date/weekend.js +56 -0
- package/dist/core/validators/index.js +139 -0
- package/dist/core/validators/nullish/is-null-or-undefined.js +40 -0
- package/dist/core/validators/nullish/is-null.js +39 -0
- package/dist/core/validators/nullish/is-undefined.js +39 -0
- package/dist/core/validators/number/finite.js +40 -0
- package/dist/core/validators/number/integer.js +40 -0
- package/dist/core/validators/number/less-than.js +39 -0
- package/dist/core/validators/number/max.js +39 -0
- package/dist/core/validators/number/min.js +39 -0
- package/dist/core/validators/number/more-than.js +39 -0
- package/dist/core/validators/number/negative.js +38 -0
- package/dist/core/validators/number/non-negative.js +37 -0
- package/dist/core/validators/number/non-positive.js +37 -0
- package/dist/core/validators/number/number.js +31 -0
- package/dist/core/validators/number/positive.js +38 -0
- package/dist/core/validators/number/safe-integer.js +42 -0
- package/dist/core/validators/object/deep-equals.js +47 -0
- package/dist/core/validators/object/has-key.js +42 -0
- package/dist/core/validators/object/has-value.js +59 -0
- package/dist/core/validators/object/keys-equal.js +47 -0
- package/dist/core/validators/object/max-keys.js +43 -0
- package/dist/core/validators/object/min-keys.js +43 -0
- package/dist/core/validators/object/missing-key.js +42 -0
- package/dist/core/validators/object/no-empty-array-values.js +44 -0
- package/dist/core/validators/object/no-empty-object-values.js +44 -0
- package/dist/core/validators/object/no-null-values.js +44 -0
- package/dist/core/validators/object/no-undefined-values.js +44 -0
- package/dist/core/validators/object/non-empty-object.js +40 -0
- package/dist/core/validators/object/only-keys.js +43 -0
- package/dist/core/validators/object/plain-object.js +35 -0
- package/dist/core/validators/string/alpha-num.js +50 -0
- package/dist/core/validators/string/alpha.js +51 -0
- package/dist/core/validators/string/chars-equal.js +49 -0
- package/dist/core/validators/string/ends-with.js +50 -0
- package/dist/core/validators/string/is-ascii.js +53 -0
- package/dist/core/validators/string/is-printable.js +53 -0
- package/dist/core/validators/string/matches.js +50 -0
- package/dist/core/validators/string/max-length.js +50 -0
- package/dist/core/validators/string/min-length.js +50 -0
- package/dist/core/validators/string/no-lead-space.js +50 -0
- package/dist/core/validators/string/no-repeat-space.js +52 -0
- package/dist/core/validators/string/no-space.js +51 -0
- package/dist/core/validators/string/no-trail-space.js +50 -0
- package/dist/core/validators/string/non-empty.js +48 -0
- package/dist/core/validators/string/not-one-of.js +51 -0
- package/dist/core/validators/string/num-string.js +50 -0
- package/dist/core/validators/string/one-of.js +50 -0
- package/dist/core/validators/string/starts-with.js +50 -0
- package/dist/core/validators/string/string.js +39 -0
- package/dist/core/validators/string/trimmed.js +51 -0
- package/dist/index.js +26 -0
- package/package.json +28 -3
- package/dist/test.d.ts +0 -1
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Public | Normalizer Registry
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Exposes the full mapping of structural types to their
|
|
7
|
+
* associated normalization rules, defining Jane’s public
|
|
8
|
+
* normalization pipeline surface.
|
|
9
|
+
* @see https://jane-io.com
|
|
10
|
+
* ----------------------------------------------------------------------------
|
|
11
|
+
*/
|
|
12
|
+
import { collapseWhitespace, compactSparseArray, emptyToUndefined, flattenOneLevel, infinityToUndefined, invalidDateToUndefined, nanToUndefined, normalizeNegativeZero, removeEmptyArrayKeys, removeEmptyObjectKeys, removeEmptyStringItems, removeEmptyStringKeys, removeNullItems, removeNullKeys, removeUndefinedItems, removeUndefinedKeys, trim, } from '.';
|
|
13
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
14
|
+
|* Normalization Rule Registry *|
|
|
15
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
16
|
+
/**
|
|
17
|
+
* Maps each structural type to the normalization rules that apply to it,
|
|
18
|
+
* defining the full normalization pipeline Jane runs for incoming values.
|
|
19
|
+
*/
|
|
20
|
+
export const normalizationRuleRegistry = {
|
|
21
|
+
string: [trim, collapseWhitespace, emptyToUndefined],
|
|
22
|
+
number: [normalizeNegativeZero, nanToUndefined, infinityToUndefined],
|
|
23
|
+
boolean: [],
|
|
24
|
+
array: [
|
|
25
|
+
compactSparseArray,
|
|
26
|
+
flattenOneLevel,
|
|
27
|
+
removeEmptyStringItems,
|
|
28
|
+
removeNullItems,
|
|
29
|
+
removeUndefinedItems,
|
|
30
|
+
],
|
|
31
|
+
object: [
|
|
32
|
+
removeUndefinedKeys,
|
|
33
|
+
removeNullKeys,
|
|
34
|
+
removeEmptyStringKeys,
|
|
35
|
+
removeEmptyArrayKeys,
|
|
36
|
+
removeEmptyObjectKeys,
|
|
37
|
+
],
|
|
38
|
+
date: [invalidDateToUndefined],
|
|
39
|
+
unknown: [],
|
|
40
|
+
bigint: [],
|
|
41
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Infinity To Undefined
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Converts `Infinity` and `-Infinity` to `undefined`, removing
|
|
7
|
+
* non‑finite numeric values that cannot be represented or
|
|
8
|
+
* validated safely.
|
|
9
|
+
* @see https://jane-io.com
|
|
10
|
+
* ----------------------------------------------------------------------------
|
|
11
|
+
*/
|
|
12
|
+
import { normalizationEvent } from '../../pipeline';
|
|
13
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
14
|
+
|* Implementation *|
|
|
15
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
16
|
+
/**
|
|
17
|
+
* Converts `Infinity` and `-Infinity` to `undefined`, removing non‑finite
|
|
18
|
+
* numeric values that cannot be represented safely across serialization or
|
|
19
|
+
* downstream validation.
|
|
20
|
+
*/
|
|
21
|
+
export const infinityToUndefined = (value, _mode, path) => {
|
|
22
|
+
if (typeof value !== 'number')
|
|
23
|
+
return [];
|
|
24
|
+
if (value !== Infinity && value !== -Infinity)
|
|
25
|
+
return [];
|
|
26
|
+
const event = normalizationEvent('info', 'number.now.undefined', path, 'Converted Infinity or -Infinity to undefined.', 'Infinite values were removed.', { before: value, after: undefined });
|
|
27
|
+
return [
|
|
28
|
+
{
|
|
29
|
+
path,
|
|
30
|
+
nextValue: undefined,
|
|
31
|
+
lossy: 'lossy',
|
|
32
|
+
events: [event],
|
|
33
|
+
},
|
|
34
|
+
];
|
|
35
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | NaN to Undefined
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Converts `NaN` to `undefined`, eliminating invalid numeric
|
|
7
|
+
* states that cannot be compared, serialized, or reasoned
|
|
8
|
+
* about reliably.
|
|
9
|
+
* @see https://jane-io.com
|
|
10
|
+
* ----------------------------------------------------------------------------
|
|
11
|
+
*/
|
|
12
|
+
import { normalizationEvent } from '../../pipeline';
|
|
13
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
14
|
+
|* Implementation *|
|
|
15
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
16
|
+
/**
|
|
17
|
+
* Converts `NaN` to `undefined`, eliminating invalid numeric states that cannot
|
|
18
|
+
* be meaningfully compared, serialized, or validated.
|
|
19
|
+
*/
|
|
20
|
+
export const nanToUndefined = (value, _mode, path) => {
|
|
21
|
+
if (typeof value !== 'number')
|
|
22
|
+
return [];
|
|
23
|
+
if (!Number.isNaN(value))
|
|
24
|
+
return [];
|
|
25
|
+
const event = normalizationEvent('info', 'number.now.undefined', path, 'Converted NaN to undefined.', 'Invalid numeric values were removed.', { before: value, after: undefined });
|
|
26
|
+
return [
|
|
27
|
+
{
|
|
28
|
+
path,
|
|
29
|
+
nextValue: undefined,
|
|
30
|
+
lossy: 'lossy',
|
|
31
|
+
events: [event],
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Normalize Negative Zero
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Normalizes `-0` to `0`, removing a JavaScript‑specific edge
|
|
7
|
+
* case while preserving the value’s intended semantics.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { normalizationEvent } from '../../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Normalizes `-0` to `0`, removing a JavaScript‑specific numeric edge case
|
|
17
|
+
* while preserving the value’s intended semantics.
|
|
18
|
+
*/
|
|
19
|
+
export const normalizeNegativeZero = (value, _mode, path) => {
|
|
20
|
+
if (typeof value !== 'number')
|
|
21
|
+
return [];
|
|
22
|
+
if (!Object.is(value, -0))
|
|
23
|
+
return [];
|
|
24
|
+
const event = normalizationEvent('info', 'number.now.positive-zero', path, 'Normalized -0 to 0.', 'Negative zero was corrected.', { before: value, after: 0 });
|
|
25
|
+
return [
|
|
26
|
+
{
|
|
27
|
+
path,
|
|
28
|
+
nextValue: 0,
|
|
29
|
+
lossy: 'lossless',
|
|
30
|
+
events: [event],
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Remove Empty Array Keys
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Removes keys whose values are empty arrays, eliminating
|
|
7
|
+
* structurally empty list fields while preserving all
|
|
8
|
+
* non‑empty array data.
|
|
9
|
+
* @see https://jane-io.com
|
|
10
|
+
* ----------------------------------------------------------------------------
|
|
11
|
+
*/
|
|
12
|
+
import { normalizationEvent } from '../../pipeline';
|
|
13
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
14
|
+
|* Implementation *|
|
|
15
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
16
|
+
/**
|
|
17
|
+
* Removes keys whose values are empty arrays, eliminating structurally empty
|
|
18
|
+
* list fields while preserving all non‑empty array values.
|
|
19
|
+
*/
|
|
20
|
+
export const removeEmptyArrayKeys = (value, _mode, path) => {
|
|
21
|
+
if (value === null || typeof value !== 'object')
|
|
22
|
+
return [];
|
|
23
|
+
if (Array.isArray(value))
|
|
24
|
+
return [];
|
|
25
|
+
const entries = Object.entries(value).filter(([_, v]) => !Array.isArray(v) || v.length > 0);
|
|
26
|
+
if (entries.length === Object.keys(value).length)
|
|
27
|
+
return [];
|
|
28
|
+
const nextValue = Object.fromEntries(entries);
|
|
29
|
+
const event = normalizationEvent('info', 'object.without.empty-arrays', path, 'Removed keys whose values were empty arrays.', 'Empty lists were removed.', { before: value, after: nextValue });
|
|
30
|
+
return [
|
|
31
|
+
{
|
|
32
|
+
path,
|
|
33
|
+
nextValue,
|
|
34
|
+
lossy: 'lossy',
|
|
35
|
+
events: [event],
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Remove Empty Object Keys
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Removes keys whose values are empty plain objects, clearing
|
|
7
|
+
* fields that carry no structural or semantic content.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { normalizationEvent } from '../../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Removes keys whose values are empty plain objects, filtering out fields that
|
|
17
|
+
* carry no structural or semantic content.
|
|
18
|
+
*/
|
|
19
|
+
export const removeEmptyObjectKeys = (value, _mode, path) => {
|
|
20
|
+
if (value === null || typeof value !== 'object')
|
|
21
|
+
return [];
|
|
22
|
+
if (Array.isArray(value))
|
|
23
|
+
return [];
|
|
24
|
+
const entries = Object.entries(value).filter(([_, v]) => {
|
|
25
|
+
if (v && typeof v === 'object' && !Array.isArray(v)) {
|
|
26
|
+
return Object.keys(v).length > 0;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
});
|
|
30
|
+
if (entries.length === Object.keys(value).length)
|
|
31
|
+
return [];
|
|
32
|
+
const nextValue = Object.fromEntries(entries);
|
|
33
|
+
const event = normalizationEvent('info', 'object.without.empty-objects', path, 'Removed keys whose values were empty objects.', 'Empty objects were removed.', { before: value, after: nextValue });
|
|
34
|
+
return [
|
|
35
|
+
{
|
|
36
|
+
path,
|
|
37
|
+
nextValue,
|
|
38
|
+
lossy: 'lossy',
|
|
39
|
+
events: [event],
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Remove Empty String Keys
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Removes keys whose values are empty strings, eliminating
|
|
7
|
+
* placeholder or accidental text fields.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { normalizationEvent } from '../../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Removes keys whose values are empty strings, clearing out placeholder or
|
|
17
|
+
* accidental text fields while retaining all meaningful values.
|
|
18
|
+
*/
|
|
19
|
+
export const removeEmptyStringKeys = (value, _mode, path) => {
|
|
20
|
+
if (value === null || typeof value !== 'object')
|
|
21
|
+
return [];
|
|
22
|
+
if (Array.isArray(value))
|
|
23
|
+
return [];
|
|
24
|
+
const entries = Object.entries(value).filter(([_, v]) => v !== '');
|
|
25
|
+
if (entries.length === Object.keys(value).length)
|
|
26
|
+
return [];
|
|
27
|
+
const nextValue = Object.fromEntries(entries);
|
|
28
|
+
const event = normalizationEvent('info', 'object.without.empty-strings', path, 'Removed keys whose values were empty strings.', 'Empty text values were removed.', { before: value, after: nextValue });
|
|
29
|
+
return [
|
|
30
|
+
{
|
|
31
|
+
path,
|
|
32
|
+
nextValue,
|
|
33
|
+
lossy: 'lossy',
|
|
34
|
+
events: [event],
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Remove Null Keys
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Removes keys whose values are null, stripping explicitly
|
|
7
|
+
* empty fields while preserving all defined data.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { normalizationEvent } from '../../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Removes keys whose values are null, eliminating explicitly empty fields while
|
|
17
|
+
* preserving all defined data.
|
|
18
|
+
*/
|
|
19
|
+
export const removeNullKeys = (value, _mode, path) => {
|
|
20
|
+
if (value === null || typeof value !== 'object')
|
|
21
|
+
return [];
|
|
22
|
+
if (Array.isArray(value))
|
|
23
|
+
return [];
|
|
24
|
+
const entries = Object.entries(value).filter(([_, v]) => v !== null);
|
|
25
|
+
if (entries.length === Object.keys(value).length)
|
|
26
|
+
return [];
|
|
27
|
+
const nextValue = Object.fromEntries(entries);
|
|
28
|
+
const event = normalizationEvent('info', 'object.without.null-keys', path, 'Removed keys whose values were null.', 'Null values were removed.', { before: value, after: nextValue });
|
|
29
|
+
return [
|
|
30
|
+
{
|
|
31
|
+
path,
|
|
32
|
+
nextValue,
|
|
33
|
+
lossy: 'lossy',
|
|
34
|
+
events: [event],
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Remove Undefined Keys
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Removes keys whose values are undefined, producing a cleaner
|
|
7
|
+
* and more predictable object shape.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { normalizationEvent } from '../../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Removes keys whose values are undefined, producing a cleaner and more
|
|
17
|
+
* predictable object shape by stripping absent fields.
|
|
18
|
+
*/
|
|
19
|
+
export const removeUndefinedKeys = (value, _mode, path) => {
|
|
20
|
+
if (value === null || typeof value !== 'object')
|
|
21
|
+
return [];
|
|
22
|
+
if (Array.isArray(value))
|
|
23
|
+
return [];
|
|
24
|
+
const entries = Object.entries(value).filter(([_, v]) => v !== undefined);
|
|
25
|
+
if (entries.length === Object.keys(value).length)
|
|
26
|
+
return [];
|
|
27
|
+
const nextValue = Object.fromEntries(entries);
|
|
28
|
+
const event = normalizationEvent('info', 'object.without.undefined-keys', path, 'Removed keys whose values were undefined.', 'Undefined values were removed.', { before: value, after: nextValue });
|
|
29
|
+
return [
|
|
30
|
+
{
|
|
31
|
+
path,
|
|
32
|
+
nextValue,
|
|
33
|
+
lossy: 'lossy',
|
|
34
|
+
events: [event],
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Collapse Whitespace
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Collapses repeated internal whitespace into a single space,
|
|
7
|
+
* producing cleaner, more consistent text without altering
|
|
8
|
+
* meaning.
|
|
9
|
+
* @see https://jane-io.com
|
|
10
|
+
* ----------------------------------------------------------------------------
|
|
11
|
+
*/
|
|
12
|
+
import { normalizationEvent } from '../../pipeline';
|
|
13
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
14
|
+
|* Implementation *|
|
|
15
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
16
|
+
/**
|
|
17
|
+
* Collapses repeated internal whitespace into a single space, producing a
|
|
18
|
+
* cleaner and more consistent text structure without altering meaning.
|
|
19
|
+
*/
|
|
20
|
+
export const collapseWhitespace = (value, _mode, path) => {
|
|
21
|
+
if (typeof value !== 'string')
|
|
22
|
+
return [];
|
|
23
|
+
const collapsed = value.replace(/\s+/g, ' ');
|
|
24
|
+
if (collapsed === value)
|
|
25
|
+
return [];
|
|
26
|
+
const event = normalizationEvent('info', 'string.now.collapsed', path, 'Collapsed repeated internal whitespace into a single space.', 'Extra spaces inside the text were cleaned up.', { before: value, after: collapsed });
|
|
27
|
+
return [
|
|
28
|
+
{
|
|
29
|
+
path,
|
|
30
|
+
nextValue: collapsed,
|
|
31
|
+
lossy: 'lossy',
|
|
32
|
+
events: [event],
|
|
33
|
+
},
|
|
34
|
+
];
|
|
35
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Empty To Undefined
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Converts empty strings to undefined, removing text fields
|
|
7
|
+
* that contain no meaningful content.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { normalizationEvent } from '../../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Converts empty strings to undefined, removing text fields that contain no
|
|
17
|
+
* meaningful content.
|
|
18
|
+
*/
|
|
19
|
+
export const emptyToUndefined = (value, _mode, path) => {
|
|
20
|
+
if (typeof value !== 'string')
|
|
21
|
+
return [];
|
|
22
|
+
if (value.length > 0)
|
|
23
|
+
return [];
|
|
24
|
+
const event = normalizationEvent('info', 'string.now.undefined', path, 'Converted empty string to undefined.', 'Empty text was removed.', { before: value, after: undefined });
|
|
25
|
+
return [
|
|
26
|
+
{
|
|
27
|
+
path,
|
|
28
|
+
nextValue: undefined,
|
|
29
|
+
lossy: 'lossy',
|
|
30
|
+
events: [event],
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Normalizers | Trim
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Removes leading and trailing whitespace, normalizing text
|
|
7
|
+
* edges while preserving internal content exactly as provided.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { normalizationEvent } from '../../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Trims leading and trailing whitespace, normalizing text edges while
|
|
17
|
+
* preserving the internal content exactly as provided.
|
|
18
|
+
*/
|
|
19
|
+
export const trim = (value, _mode, path) => {
|
|
20
|
+
if (typeof value !== 'string')
|
|
21
|
+
return [];
|
|
22
|
+
const trimmed = value.trim();
|
|
23
|
+
if (trimmed === value)
|
|
24
|
+
return [];
|
|
25
|
+
const event = normalizationEvent('info', 'string.now.trimmed', path, 'Removed leading and trailing whitespace.', 'Extra spaces at the beginning or end were removed.', { before: value, after: trimmed });
|
|
26
|
+
return [
|
|
27
|
+
{
|
|
28
|
+
path,
|
|
29
|
+
nextValue: trimmed,
|
|
30
|
+
lossy: 'lossless',
|
|
31
|
+
events: [event],
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Parsers | Barrel File
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Re‑exports all parser implementations, providing a unified
|
|
7
|
+
* entry point for Jane’s parsing subsystem.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
12
|
+
|* Array Values *|
|
|
13
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
14
|
+
export { parseArrayString } from './parse-array-string';
|
|
15
|
+
export { parseJsonString } from './parse-json-string';
|
|
16
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
17
|
+
|* Bigint Values *|
|
|
18
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
19
|
+
export { parseBigIntString } from './parse-bigint-string';
|
|
20
|
+
export { parseBinaryString } from './parse-binary-string';
|
|
21
|
+
export { parseHexString } from './parse-hex-string';
|
|
22
|
+
export { parseOctalString } from './parse-octal-string';
|
|
23
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
24
|
+
|* Boolean Values *|
|
|
25
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
26
|
+
export { parseBooleanString } from './parse-boolean-string';
|
|
27
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
28
|
+
|* Date Values *|
|
|
29
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
30
|
+
export { parseIsoDateString } from './parse-date-string';
|
|
31
|
+
export { parseDurationString } from './parse-duration-string';
|
|
32
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
33
|
+
|* Number Values *|
|
|
34
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
35
|
+
export { parseNumericString } from './parse-numeric-string';
|
|
36
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
37
|
+
|* Object Values *|
|
|
38
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
39
|
+
export { parseObjectString } from './parse-object-string';
|
|
40
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
41
|
+
|* URL Values *|
|
|
42
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
43
|
+
export { parseUrlString } from './parse-url-string';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Parsers | Parse Array String
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Parses JSON‑compatible array strings into actual arrays,
|
|
7
|
+
* rejecting ambiguous or non‑array input.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { parseEvent } from '../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Parses JSON‑compatible array strings into actual arrays, rejecting anything
|
|
17
|
+
* that is not a syntactically valid array literal.
|
|
18
|
+
*/
|
|
19
|
+
export const parseArrayString = (value, path) => {
|
|
20
|
+
if (typeof value !== 'string')
|
|
21
|
+
return [];
|
|
22
|
+
const trimmed = value.trim();
|
|
23
|
+
if (!trimmed.startsWith('[') || !trimmed.endsWith(']'))
|
|
24
|
+
return [];
|
|
25
|
+
let parsed;
|
|
26
|
+
try {
|
|
27
|
+
parsed = JSON.parse(trimmed);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
if (!Array.isArray(parsed))
|
|
33
|
+
return [];
|
|
34
|
+
const event = parseEvent('info', 'string.now.array', 'Parsed string into a JSON array.', path, 'Converted text into a list.', { before: value, after: parsed });
|
|
35
|
+
return [{ path, nextValue: parsed, events: [event] }];
|
|
36
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Parsers | Parse Bigint String
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Parses canonical decimal bigint literals into `bigint`
|
|
7
|
+
* values, enforcing strict, non‑guessing numeric rules.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { parseEvent } from '../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Parses canonical decimal bigint strings into `bigint` values, enforcing a
|
|
17
|
+
* strict digits‑only format with an optional leading minus.
|
|
18
|
+
*/
|
|
19
|
+
export const parseBigIntString = (value, path) => {
|
|
20
|
+
if (typeof value !== 'string')
|
|
21
|
+
return [];
|
|
22
|
+
if (!/^-?\d+$/.test(value))
|
|
23
|
+
return [];
|
|
24
|
+
let parsed;
|
|
25
|
+
try {
|
|
26
|
+
parsed = BigInt(value);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
const event = parseEvent('info', 'string.now.bigint', `String parsed into bigint ${parsed}.`, path, 'Converted text into a large integer.', { before: value, after: parsed });
|
|
32
|
+
return [{ path, nextValue: parsed, events: [event] }];
|
|
33
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Parsers | Parse Binary String
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Parses binary string literals (0b…) into `bigint` values,
|
|
7
|
+
* validating format and rejecting malformed input.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { parseEvent } from '../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Parses binary string literals (`0b…`) into `bigint` values, validating format
|
|
17
|
+
* and rejecting malformed or non‑binary input.
|
|
18
|
+
*/
|
|
19
|
+
export const parseBinaryString = (value, path) => {
|
|
20
|
+
if (typeof value !== 'string')
|
|
21
|
+
return [];
|
|
22
|
+
if (!/^0b[01]+$/.test(value))
|
|
23
|
+
return [];
|
|
24
|
+
let parsed;
|
|
25
|
+
try {
|
|
26
|
+
parsed = BigInt(value);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
const event = parseEvent('info', 'string.now.binary', `Binary string parsed into bigint ${parsed}.`, path, 'Converted binary text into a number.', { before: value, after: parsed });
|
|
32
|
+
return [{ path, nextValue: parsed, events: [event] }];
|
|
33
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ----------------------------------------------------------------------------
|
|
3
|
+
* Parsers | Parse Boolean String
|
|
4
|
+
* ----------------------------------------------------------------------------
|
|
5
|
+
* @package @clementine-solutions/jane
|
|
6
|
+
* @description Parses strict boolean strings (`"true"` / `"false"`) into
|
|
7
|
+
* boolean values without accepting loose or coercive forms.
|
|
8
|
+
* @see https://jane-io.com
|
|
9
|
+
* ----------------------------------------------------------------------------
|
|
10
|
+
*/
|
|
11
|
+
import { parseEvent } from '../pipeline';
|
|
12
|
+
/* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— *\
|
|
13
|
+
|* Implementation *|
|
|
14
|
+
\* ——— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ————— * ——— */
|
|
15
|
+
/**
|
|
16
|
+
* Parses strict boolean strings (`"true"` or `"false"`) into boolean values,
|
|
17
|
+
* without accepting loose or coercive forms.
|
|
18
|
+
*/
|
|
19
|
+
export const parseBooleanString = (value, path) => {
|
|
20
|
+
if (typeof value !== 'string')
|
|
21
|
+
return [];
|
|
22
|
+
if (value !== 'true' && value !== 'false')
|
|
23
|
+
return [];
|
|
24
|
+
const parsed = value === 'true';
|
|
25
|
+
const event = parseEvent('info', 'string.now.boolean', `String parsed into boolean ${parsed}.`, path, 'Converted text into true or false.', { before: value, after: parsed });
|
|
26
|
+
return [{ path, nextValue: parsed, events: [event] }];
|
|
27
|
+
};
|