You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(mapping-kit): update README to match current implementation (#3714)
* docs(mapping-kit): update README to match current implementation
- Fix @if `blank` conditional description (runs `then` when NOT blank, not when blank)
- Fix @if `blank` example result for undefined path
- Fix @replace examples to include the required `value` key
- Document @replace `pattern2`/`replacement2` dual-pattern feature
- Add missing @flatten directive documentation
- Add missing @JSON directive documentation
- Add missing @liquid directive documentation with disabled tags/filters note
- Update table of contents with new directives
- Fix broken link to test suite (was pointing to old fab-5-engine repo)
* docs(mapping-kit): clarify @if blank coercion behavior for 0 and false
suite](./__tests__/index.iso.test.ts) is a good source-of-truth for current implementation behavior.
214
215
215
216
## Removing values from object
216
217
@@ -252,7 +253,10 @@ The supported conditional values are:
252
253
253
254
- "exists": If the given value is not undefined or null, the @if directive resolves to the "then"
254
255
value. Otherwise, the "else" value is used.
255
-
- "blank": If the given value is undefined or null, the @if directive resolves to the "then" value. Otherwise, the "else" value is used.
256
+
- "blank": If the given value is not undefined, not null, and does not loosely equal an empty
257
+
string (`''`) (i.e., is not blank), the @if directive resolves to the "then" value. Otherwise,
258
+
the "else" value is used. Because this check uses JavaScript loose equality semantics, values
259
+
such as `0` and `false` are currently treated as blank and will use the "else" branch.
256
260
257
261
```json
258
262
Input:
@@ -292,7 +296,7 @@ Mappings:
292
296
}
293
297
}
294
298
=>
295
-
"yep"
299
+
"nope"
296
300
```
297
301
298
302
If "then" or "else" are not defined and the conditional indicates that their value should be used,
@@ -490,57 +494,55 @@ Result:
490
494
491
495
### @replace
492
496
493
-
The @replace directive replaces to the given pattern value with a replacement string. Both "pattern" and "replacement"
494
-
fields are required but replacement can be an empty string.
497
+
The @replace directive replaces occurrences of a pattern in a string with a replacement string.
498
+
The `value` field specifies the input string (may be a directive or raw value). The `pattern` field
499
+
is required; `replacement` defaults to an empty string if omitted.
500
+
501
+
By default, replacement is global (all occurrences) and case-sensitive. Use `global: false` to
502
+
replace only the first occurrence, and `ignorecase: true` for case-insensitive matching.
495
503
496
-
````json
504
+
```json
497
505
Input:
498
506
499
507
{
500
-
"a": "cool-story",
508
+
"a": "cool-story"
501
509
}
502
510
503
511
Mappings:
504
512
505
513
{
506
514
"@replace": {
515
+
"value": { "@path": "$.a" },
507
516
"pattern": "-",
508
517
"replacement": ""
509
518
}
510
519
}
511
520
=>
512
521
"coolstory"
513
522
514
-
```json
515
-
Input:
516
-
517
-
{
518
-
"a": "cool-story",
519
-
}
520
-
521
-
Mappings:
522
-
523
523
{
524
524
"@replace": {
525
+
"value": { "@path": "$.a" },
525
526
"pattern": "-",
526
527
"replacement": "nice"
527
528
}
528
529
}
529
530
=>
530
531
"coolnicestory"
531
-
````
532
+
```
532
533
533
534
```json
534
535
Input:
535
536
536
537
{
537
-
"a": "cWWl-story-ww",
538
+
"a": "cWWl-story-ww"
538
539
}
539
540
540
541
Mappings:
541
542
542
543
{
543
544
"@replace": {
545
+
"value": { "@path": "$.a" },
544
546
"pattern": "WW",
545
547
"replacement": "oo",
546
548
"ignorecase": false
@@ -554,13 +556,14 @@ Mappings:
554
556
Input:
555
557
556
558
{
557
-
"a": "just-the-first",
559
+
"a": "just-the-first"
558
560
}
559
561
560
562
Mappings:
561
563
562
564
{
563
565
"@replace": {
566
+
"value": { "@path": "$.a" },
564
567
"pattern": "-",
565
568
"replacement": "@",
566
569
"global": false
@@ -570,6 +573,33 @@ Mappings:
570
573
"just@the-first"
571
574
```
572
575
576
+
A second pattern/replacement pair (`pattern2` / `replacement2`) can be provided to apply a second
577
+
substitution on the result of the first:
578
+
579
+
```json
580
+
Input:
581
+
582
+
{
583
+
"a": "something-great!"
584
+
}
585
+
586
+
Mapping:
587
+
588
+
{
589
+
"@replace": {
590
+
"value": { "@path": "$.a" },
591
+
"pattern": "-",
592
+
"replacement": "",
593
+
"pattern2": "great",
594
+
"replacement2": "awesome"
595
+
}
596
+
}
597
+
598
+
Output:
599
+
600
+
"something awesome!"
601
+
```
602
+
573
603
### @merge
574
604
575
605
The @merge directive resolves a list of objects to a single object. It accepts a list of one or more objects (either raw objects or directives that resolve to objects), and a direction that determines how overwrites will be applied for duplicate keys. The resolved object is built by combining each object in turn, moving in the specified direction, overwriting any duplicate keys.
@@ -689,6 +719,141 @@ Mappings:
689
719
}
690
720
```
691
721
722
+
### @flatten
723
+
724
+
The @flatten directive flattens a nested object into a single-level object using a separator string
725
+
for the keys. The `value` field specifies the object to flatten and the `separator` field (required)
726
+
specifies the string used to join nested keys. Set `omitArrays: true` to leave array values
727
+
un-flattened.
728
+
729
+
```json
730
+
Input:
731
+
732
+
{
733
+
"foo": {
734
+
"bar": "baz",
735
+
"aces": { "a": 1, "b": 2 }
736
+
}
737
+
}
738
+
739
+
Mapping:
740
+
741
+
{
742
+
"@flatten": {
743
+
"value": { "@path": "$.foo" },
744
+
"separator": "."
745
+
}
746
+
}
747
+
748
+
Output:
749
+
750
+
{
751
+
"bar": "baz",
752
+
"aces.a": 1,
753
+
"aces.b": 2
754
+
}
755
+
```
756
+
757
+
With `omitArrays: true`, array values are preserved as-is instead of being flattened:
758
+
759
+
```json
760
+
Input:
761
+
762
+
{
763
+
"foo": {
764
+
"bar": "baz",
765
+
"tags": [1, 2]
766
+
}
767
+
}
768
+
769
+
Mapping:
770
+
771
+
{
772
+
"@flatten": {
773
+
"value": { "@path": "$.foo" },
774
+
"separator": ".",
775
+
"omitArrays": true
776
+
}
777
+
}
778
+
779
+
Output:
780
+
781
+
{
782
+
"bar": "baz",
783
+
"tags": [1, 2]
784
+
}
785
+
```
786
+
787
+
### @json
788
+
789
+
The @json directive encodes a value to a JSON string or decodes a JSON string to a value. The `mode`
790
+
field must be either `"encode"` or `"decode"`, and the `value` field specifies the input.
0 commit comments