diff --git a/TaskFiles/Tests/JSON/Transformations/Collection/bind.nebs b/TaskFiles/Tests/JSON/Transformations/Collection/bind.nebs
new file mode 100644
index 000000000..03b01c61f
--- /dev/null
+++ b/TaskFiles/Tests/JSON/Transformations/Collection/bind.nebs
@@ -0,0 +1,28 @@
+# Test bind functionality
+# a bind-transformation takes an array and binds its values sequentially to the specified keys
+
+# Array
+set arr[0] 1
+set arr[1] 2
+set arr[2] 3
+
+# Bind: Arr->Obj
+assign self:result.bind.a = {self:arr|bind key1 key2 key3}
+assign self:result.bind.b = {self:arr|bind key1 key2}
+assign self:result.bind.c = {self:arr|bind key1}
+
+# Verify
+eval nop {self:result.bind.a|assert type object}
+eval nop {self:result.bind.a|listMembers|length|assert equals int 3}
+eval nop {self:result.bind.a.key1|assert equals int 1}
+eval nop {self:result.bind.a.key2|assert equals int 2}
+eval nop {self:result.bind.a.key3|assert equals int 3}
+eval nop {self:result.bind.b|assert type object}
+eval nop {self:result.bind.b|listMembers|length|assert equals int 2}
+eval nop {self:result.bind.b.key1|assert equals int 1}
+eval nop {self:result.bind.b.key2|assert equals int 2}
+eval nop {self:result.bind.c|assert type object}
+eval nop {self:result.bind.c|listMembers|length|assert equals int 1}
+eval nop {self:result.bind.c.key1|assert equals int 1}
+
+exit
diff --git a/TaskFiles/Tests/JSON/Transformations/Collection/bundleToArray.nebs b/TaskFiles/Tests/JSON/Transformations/Collection/bundle.nebs
similarity index 82%
rename from TaskFiles/Tests/JSON/Transformations/Collection/bundleToArray.nebs
rename to TaskFiles/Tests/JSON/Transformations/Collection/bundle.nebs
index b4c6fc8c4..b85c67862 100644
--- a/TaskFiles/Tests/JSON/Transformations/Collection/bundleToArray.nebs
+++ b/TaskFiles/Tests/JSON/Transformations/Collection/bundle.nebs
@@ -11,8 +11,8 @@ set obj.key2 value2
set obj.key3 value3
# Bundle
-assign global:result.arr = {global:arr|bundleToArray [0] [1]}
-assign global:result.obj = {global:obj|bundleToArray key1 key2}
+assign global:result.arr = {global:arr|bundle [0] [1]}
+assign global:result.obj = {global:obj|bundle key1 key2}
# Verify
eval nop {global:result.arr|assert type array}
diff --git a/TaskFiles/Tests/JSON/Transformations/Collection/enumerate.nebs b/TaskFiles/Tests/JSON/Transformations/Collection/enumerate.nebs
new file mode 100644
index 000000000..a086c015b
--- /dev/null
+++ b/TaskFiles/Tests/JSON/Transformations/Collection/enumerate.nebs
@@ -0,0 +1,48 @@
+# Test enumerate functionality
+
+set arr[0] a
+set arr[1] b
+set arr[2] c
+set arr[3] d
+
+set arrObj[0].value a
+set arrObj[1].value b
+set arrObj[2].value c
+set arrObj[3].value d
+
+# Enumerate: Best option, as it does not overwrite values but moves them
+# [0]: index
+# [1]: value
+assign self:result.enumerate.arr = {self:arr|enumerate}
+assign self:result.enumerate.arrObj = {self:arrObj|enumerate}
+
+# Inline enumerate
+# Faster, but can overwrite values if not careful. Use with caution.
+assign self:result.inline.arr = {self:arr|enumerateInline idx} # Not recommended, overwrites values
+assign self:result.inline.arrObj = {self:arrObj|enumerateInline idx} # Recommended, adds idx to each object
+
+# Verify: enumerate
+for i 0 3 eval nop {self:result.enumerate.arr[{i}][0]|assert equals int {i}}
+eval nop {self:result.enumerate.arr[0][1]|assert equals string a}
+eval nop {self:result.enumerate.arr[1][1]|assert equals string b}
+eval nop {self:result.enumerate.arr[2][1]|assert equals string c}
+eval nop {self:result.enumerate.arr[3][1]|assert equals string d}
+for i 0 3 eval nop {self:result.enumerate.arrObj[{i}][0]|assert equals int {i}}
+eval nop {self:result.enumerate.arrObj[0][1].value|assert equals string a}
+eval nop {self:result.enumerate.arrObj[1][1].value|assert equals string b}
+eval nop {self:result.enumerate.arrObj[2][1].value|assert equals string c}
+eval nop {self:result.enumerate.arrObj[3][1].value|assert equals string d}
+
+# Verify: enumerateInline
+for i 0 3 eval nop {self:result.inline.arr[{i}].idx|assert equals int {i}}
+eval nop {self:result.inline.arr[0]|length|assert equals int 1}
+eval nop {self:result.inline.arr[1]|length|assert equals int 1}
+eval nop {self:result.inline.arr[2]|length|assert equals int 1}
+eval nop {self:result.inline.arr[3]|length|assert equals int 1}
+for i 0 3 eval nop {self:result.inline.arrObj[{i}].idx|assert equals int {i}}
+eval nop {self:result.inline.arrObj[0].value|assert equals string a}
+eval nop {self:result.inline.arrObj[1].value|assert equals string b}
+eval nop {self:result.inline.arrObj[2].value|assert equals string c}
+eval nop {self:result.inline.arrObj[3].value|assert equals string d}
+
+exit
diff --git a/Tools/Tests/JSON/Transformations/Collection.json b/Tools/Tests/JSON/Transformations/Collection.json
index 67b8f8193..357e759ab 100644
--- a/Tools/Tests/JSON/Transformations/Collection.json
+++ b/Tools/Tests/JSON/Transformations/Collection.json
@@ -16,7 +16,15 @@
"expected": { "cout": [], "cerr": [] }
},
{
- "command": "task TaskFiles/Tests/JSON/Transformations/Collection/bundleToArray.nebs",
+ "command": "task TaskFiles/Tests/JSON/Transformations/Collection/enumerate.nebs",
+ "expected": { "cout": [], "cerr": [] }
+ },
+ {
+ "command": "task TaskFiles/Tests/JSON/Transformations/Collection/bundle.nebs",
+ "expected": { "cout": [], "cerr": [] }
+ },
+ {
+ "command": "task TaskFiles/Tests/JSON/Transformations/Collection/bind.nebs",
"expected": { "cout": [], "cerr": [] }
}
]
\ No newline at end of file
diff --git a/doc/Commands.md b/doc/Commands.md
index 6bacda2f8..ac257b557 100644
--- a/doc/Commands.md
+++ b/doc/Commands.md
@@ -2,7 +2,7 @@
This documentation is automatically generated.
-Generated on: Sun Aug 9 11:46:19 CEST 2026
+Generated on: Thu Aug 27 08:43:11 CEST 2026
## Table of Contents
@@ -75,6 +75,7 @@ Available Functions
| `query` | Functions to manipulate JSON data via SQL query results |
| `reparse` | Commands for forwarding function calls to other contexts (other or global) while switching context. |
| `rmlui` | Functions for managing RmlUI elements. |
+| `rng` | Random number generation functions. |
| `ruleset` | Functions for managing rulesets. |
| `selected-object` | Functions to select and interact with a selected RenderObject |
| `set` | Set a key to a string value in the JSON document. |
@@ -959,6 +960,28 @@ Removes a loaded RmlUI document from the renderer's context by its name.
Usage: rmlui document remove
```
+#### `rng`
+
+Available Functions
+
+| Function | Description |
+|----------|-------------|
+| `help` | Show available commands and their descriptions |
+| `off` | Disables RNG updates. Useful for TAS scripts to avoid RNG state changes, or benchmarking to avoid RNG state changes. |
+| `on` | Re-enables RNG updates. |
+
+##### `rng off`
+
+```
+Disables RNG updates. Useful for TAS scripts to avoid RNG state changes, or benchmarking to avoid RNG state changes.
+```
+
+##### `rng on`
+
+```
+Re-enables RNG updates.
+```
+
#### `ruleset`
Available Functions
@@ -2066,7 +2089,10 @@ Available Functions
| `assign` | Assigns a value based on the result of an expression. |
| `at` | Gets the element at the specified index from the array in the current JSON value. |
| `average` | Calculates the average of the elements of the array in the current JSON value. |
-| `bundleToArray` | Gathers all members from the provided keys into an array. |
+| `batch` | Batches the array in the current JSON value into subarrays of the specified size. |
+| `batchPadded` | Batches the array in the current JSON value into subarrays of the specified size, padding the last batch with empty objects if necessary. |
+| `bind` | Binds the elements of an array to the provided keys. |
+| `bundle` | Gathers all members from the provided keys into an array. |
| `capitalize` | Capitalizes the current JSON string. |
| `ceiling` | Rounds the current JSON numeric value down to the nearest integer. |
| `complexAbs` | Calculates the absolute value (magnitude) of a complex number. |
@@ -2078,6 +2104,7 @@ Available Functions
| `echo` | Echoes the provided arguments to the console, with newline. |
| `ensureArray` | Ensures the current JSON value is an array. |
| `enumerate` | Enumerates the array in the current JSON value. |
+| `enumerateInline` | Enumerates the array in the current JSON value using a provided key. |
| `eq` | Checks if the current JSON value is equal to the specified value. |
| `error` | Echoes the provided arguments to the console as an error message, with newline. |
| `exists` | Checks if a specified key exists in the current JSON object. |
@@ -2119,6 +2146,7 @@ Available Functions
| `neq` | Checks if the current JSON value is not equal to the specified value. |
| `not` | Logical NOT operation on the current JSON value. |
| `numberToComplex` | Converts a given number to a complex number. |
+| `pad` | Pads the array in the current JSON value to the specified length with empty objects. |
| `pow` | Raises the current JSON value to the power of a numeric value. |
| `print` | Prints the current JSON value to the console. |
| `product` | Multiplies the elements of the array in the current JSON value. |
@@ -2141,6 +2169,7 @@ Available Functions
| `setDouble` | Sets a double value at the specified key in the JSON document. |
| `setInt` | Sets an integer value at the specified key in the JSON document. |
| `setString` | Sets a string value at the specified key in the JSON document. |
+| `slide` | Slides the array in the current JSON value by the specified size, generating overlapping windows. |
| `sort` | Sorting transformation functions |
| `split` | Splits the current JSON string value into an array of substrings based on a specified character delimiter. |
| `sqrt` | Calculates the square root of the current JSON value. |
@@ -2149,6 +2178,7 @@ Available Functions
| `strCompare` | Functions for comparing string values. |
| `strCountAppearance` | Counts the number of occurrences of a specified substring in the current JSON string value. |
| `strLen` | Calculates the length of the current JSON string value. |
+| `stride` | Strides the array in the current JSON value by the specified size. |
| `strip` | Strips whitespace from both ends of the current JSON string value. |
| `sub` | Subtracts a numeric value from the current JSON value. |
| `subspan` | Gets a subarray from the array in the current JSON value. |
@@ -2357,11 +2387,36 @@ Input must be an array of numbers. If any element is not a number, the current v
Usage: |average -> {number}
```
-#### `bundleToArray`
+#### `batch`
+
+```
+Batches the array in the current JSON value into subarrays of the specified size.
+Usage: |batch -> {array}
+```
+
+#### `batchPadded`
+
+```
+Batches the array in the current JSON value into subarrays of the specified size, padding the last batch with empty objects if necessary.
+Usage: |batchPadded -> {array}
+```
+
+#### `bind`
+
+```
+Binds the elements of an array to the provided keys.
+think of C++ structured bindings: auto [key1, key2, key3] = arr;
+Fails if the given scope is not an array or if no keys are provided.
+Inserts empty objects for missing values in the array.
+Usage: |bind ... -> {object}
+```
+
+#### `bundle`
```
Gathers all members from the provided keys into an array.
-Usage: |bundleToArray ... -> {array}
+Inserts empty objects for missing values in the object.
+Usage: |bundle ... -> {array}
```
#### `capitalize`
@@ -2447,7 +2502,18 @@ Usage: |ensureArray -> {array}
```
Enumerates the array in the current JSON value.
-Usage: |enumerate -> {array}
+Creates another array for each element, where [0] is the index and [1] is the value.
+Recommended over enumerateInline if the values stored are not objects.
+Usage: |enumerate -> {array}
+Where indexKey is the key of each array element to populate with the index of the element in the array.
+```
+
+#### `enumerateInline`
+
+```
+Enumerates the array in the current JSON value using a provided key.
+Recommended over enumerate if the values stored are objects.
+Usage: |enumerateInline -> {array}
Where indexKey is the key of each array element to populate with the index of the element in the array.
```
@@ -2771,6 +2837,14 @@ Allowed values are:
The transformation fails if the current JSON value is not convertible to a complex number.
```
+#### `pad`
+
+```
+Pads the array in the current JSON value to the specified length with empty objects.
+If the current value is larger than the specified length, it is not modified.
+Usage: |pad -> {array}
+```
+
#### `pow`
```
@@ -3096,6 +3170,13 @@ Expects two arguments: and .
Usage: |setString -> {json}
```
+#### `slide`
+
+```
+Slides the array in the current JSON value by the specified size, generating overlapping windows.
+Usage: |slide -> {array}
+```
+
#### `sort`
Available Functions
@@ -3244,6 +3325,13 @@ Calculates the length of the current JSON string value.
Usage: |strLen -> {int}
```
+#### `stride`
+
+```
+Strides the array in the current JSON value by the specified size.
+Usage: |stride -> {array}
+```
+
#### `strip`
```
diff --git a/include/Nebulite/Data/Document/JsonScope.hpp b/include/Nebulite/Data/Document/JsonScope.hpp
index 16a7fb3dd..2d9cc9fdc 100644
--- a/include/Nebulite/Data/Document/JsonScope.hpp
+++ b/include/Nebulite/Data/Document/JsonScope.hpp
@@ -46,7 +46,8 @@ namespace Nebulite::Data {
* @details It allows for modifications to a JSON document within a specific scope,
* that is a key-prefixed section of the document. This is useful for modular data management,
* where different parts of a JSON document can be managed independently.
- * Holds little data itself, mostly acts as a scoped view over an existing JSON document or another JsonScope.
+ * This class also includes an ordered cache list system for fast caching of numeric value references,
+ * breaking down the complexity of set/get operations into a more efficient structure for hot paths.
*/
class JsonScope final {
public:
@@ -72,6 +73,7 @@ class JsonScope final {
/**
* @brief Mapped ordered double pointers intended for non-locking access
+ * @todo Is a lazy init possible? This could reduce the memory footprint of JsonScope instances and make copying potentially cheaper.
*/
alignas(Constants::Alignment::simdAlign) std::array odpCache;
@@ -307,7 +309,7 @@ class JsonScope final {
[[nodiscard]] std::vector listAvailableKeys(ScopedKeyView const& key) const ;
[[nodiscard]] std::vector listAvailableKeys(ScopedKey const& key) const ;
- // Helper struct for holding a member as well as its full key
+ // Helper struct for holding a member name as well as its full key
struct MemberAndKey {
std::string member;
ScopedKey key;
diff --git a/include/Nebulite/Module/Transformation/Array.hpp b/include/Nebulite/Module/Transformation/Array.hpp
index d18c7cbcd..1dfa3d3d5 100644
--- a/include/Nebulite/Module/Transformation/Array.hpp
+++ b/include/Nebulite/Module/Transformation/Array.hpp
@@ -68,7 +68,7 @@ class Array final : public Base::TransformationModule {
// TODO: dedupe, pick, omit
- // Metadata
+ // Dimensionality
static bool flatten(Data::JsonScope& jsonDoc);
static auto constexpr flattenName = "flatten";
@@ -80,14 +80,6 @@ class Array final : public Base::TransformationModule {
"If the current value is not an array, it is first wrapped into a single-element array.\n"
"Usage: |reverse -> {array}\n";
- // TODO: no index name passing. Instead: turn into object with index and value
- // even better: a canonical representation of tuples for the Json/JsonScope class
- static bool enumerate(std::span args, Data::JsonScope& jsonDoc);
- static auto constexpr enumerateName = "enumerate";
- static auto constexpr enumerateDesc = "Enumerates the array in the current JSON value.\n"
- "Usage: |enumerate -> {array}\n"
- "Where indexKey is the key of each array element to populate with the index of the element in the array.\n";
-
static bool batch(std::span args, Data::JsonScope& jsonDoc);
static auto constexpr batchName = "batch";
static auto constexpr batchDesc = "Batches the array in the current JSON value into subarrays of the specified size.\n"
diff --git a/include/Nebulite/Module/Transformation/Collection.hpp b/include/Nebulite/Module/Transformation/Collection.hpp
index 59a26ca48..0ea76c97f 100644
--- a/include/Nebulite/Module/Transformation/Collection.hpp
+++ b/include/Nebulite/Module/Transformation/Collection.hpp
@@ -59,10 +59,34 @@ class Collection final : public Base::TransformationModule {
"If the current value is an array, it lists the indices as strings.\n"
"Usage: |listMembersAndValues -> {array of {key, value} objects}\n";
- static bool bundleToArray(std::span args, Data::JsonScope& jsonDoc);
- static auto constexpr bundleToArrayName = "bundleToArray";
- static auto constexpr bundleToArrayDesc = "Gathers all members from the provided keys into an array.\n"
- "Usage: |bundleToArray ... -> {array}\n";
+ static bool enumerate(std::span args, Data::JsonScope& jsonDoc);
+ static auto constexpr enumerateName = "enumerate";
+ static auto constexpr enumerateDesc = "Enumerates the array in the current JSON value.\n"
+ "Creates another array for each element, where [0] is the index and [1] is the value.\n"
+ "Recommended over enumerateInline if the values stored are not objects.\n"
+ "Usage: |enumerate -> {array}\n"
+ "Where indexKey is the key of each array element to populate with the index of the element in the array.\n";
+
+ static bool enumerateInline(std::span args, Data::JsonScope& jsonDoc);
+ static auto constexpr enumerateInlineName = "enumerateInline";
+ static auto constexpr enumerateInlineDesc = "Enumerates the array in the current JSON value using a provided key.\n"
+ "Recommended over enumerate if the values stored are objects.\n"
+ "Usage: |enumerateInline -> {array}\n"
+ "Where indexKey is the key of each array element to populate with the index of the element in the array.\n";
+
+ static bool bundle(std::span args, Data::JsonScope& jsonDoc);
+ static auto constexpr bundleName = "bundle";
+ static auto constexpr bundleDesc = "Gathers all members from the provided keys into an array.\n"
+ "Inserts empty objects for missing values in the object.\n"
+ "Usage: |bundle ... -> {array}\n";
+
+ static bool bind(std::span args, Data::JsonScope& jsonDoc);
+ static auto constexpr bindName = "bind";
+ static auto constexpr bindDesc = "Binds the elements of an array to the provided keys.\n"
+ "think of C++ structured bindings: auto [key1, key2, key3] = arr;\n"
+ "Fails if the given scope is not an array or if no keys are provided.\n"
+ "Inserts empty objects for missing values in the array.\n"
+ "Usage: |bind ... -> {object}\n";
};
} // namespace Nebulite::Module::Transformation
#endif // NEBULITE_MODULE_TRANSFORMATION_COLLECTION_HPP
diff --git a/src/Nebulite/Module/Transformation/Array.cpp b/src/Nebulite/Module/Transformation/Array.cpp
index 46e610857..294abac60 100644
--- a/src/Nebulite/Module/Transformation/Array.cpp
+++ b/src/Nebulite/Module/Transformation/Array.cpp
@@ -30,10 +30,9 @@ void Array::bindTransformations() {
bindTransformation(&Array::length, lengthName, lengthDesc);
bindTransformation(&Array::subspan, subspanName, subspanDesc);
- // Metadata
+ // Dimensionality
bindTransformation(&Array::flatten, flattenName, flattenDesc);
bindTransformation(&Array::reverse, reverseName, reverseDesc);
- bindTransformation(&Array::enumerate, enumerateName, enumerateDesc);
bindTransformation(&Array::batch, batchName, batchDesc);
bindTransformation(&Array::batchPadded, batchPaddedName, batchPaddedDesc);
bindTransformation(&Array::stride, strideName, strideDesc);
@@ -230,20 +229,6 @@ bool Array::reverse(Data::JsonScope& jsonDoc) {
return true;
}
-bool Array::enumerate(std::span const args, Data::JsonScope& jsonDoc){
- if (args.size() < 2) return false;
- if (jsonDoc.memberType(rootKey) != Data::KeyType::array) return false;
- auto const& indexKey = args.at(1);
- std::ranges::for_each(
- std::views::iota(std::size_t{0}, jsonDoc.memberSize(rootKey)),
- [&](std::size_t const i) {
- auto const key = rootKey.addIndex(i).addMember(indexKey);
- jsonDoc.set(key, i);
- }
- );
- return true;
-}
-
bool Array::batch(std::span const args, Data::JsonScope& jsonDoc){
// Validate arguments and input
if (args.size() < 2) return false;
diff --git a/src/Nebulite/Module/Transformation/Collection.cpp b/src/Nebulite/Module/Transformation/Collection.cpp
index 9b6748f88..9ff06d1ad 100644
--- a/src/Nebulite/Module/Transformation/Collection.cpp
+++ b/src/Nebulite/Module/Transformation/Collection.cpp
@@ -8,7 +8,6 @@
#include
#include
#include
-#include
#include
// Nebulite
@@ -26,7 +25,10 @@ void Collection::bindTransformations() {
bindTransformation(&Collection::get, getName, getDesc);
bindTransformation(&Collection::listMembers, listMembersName, listMembersDesc);
bindTransformation(&Collection::listMembersAndValues, listMembersAndValuesName, listMembersAndValuesDesc);
- bindTransformation(&Collection::bundleToArray, bundleToArrayName, bundleToArrayDesc);
+ bindTransformation(&Collection::enumerate, enumerateName, enumerateDesc);
+ bindTransformation(&Collection::enumerateInline, enumerateInlineName, enumerateInlineDesc);
+ bindTransformation(&Collection::bundle, bundleName, bundleDesc);
+ bindTransformation(&Collection::bind, bindName, bindDesc);
}
bool Collection::map(std::span const args, Data::JsonScope& jsonDoc) {
@@ -40,7 +42,7 @@ bool Collection::map(std::span const args, Data::JsonSco
return false; // still not an array, something went wrong
}
- std::size_t const arraySize = jsonDoc.memberSize(rootKey);
+ auto const arraySize = jsonDoc.memberSize(rootKey);
for (std::uint32_t idx = 0; idx < arraySize; ++idx) {
// Set temp document with current element
auto const elementKey = rootKey.addIndex(idx);
@@ -81,27 +83,31 @@ bool Collection::listMembers(Data::JsonScope& jsonDoc){
}
bool Collection::listMembersAndValues(Data::JsonScope& jsonDoc){
- // Copy values
auto const membersAndKeys = jsonDoc.listAvailableMembersAndKeys(rootKey);
- std::vector values;
- std::ranges::for_each(
- membersAndKeys,
- [&](auto const& memberAndKey) {
- auto const& [member, key] = memberAndKey;
- Data::Json newObject;
- newObject.deserialize(jsonDoc.serialize(key));
- values.push_back(std::move(newObject));
- }
- );
+ auto const members = membersAndKeys
+ | std::views::transform([]([[clang::lifetimebound]] auto const& memberAndKey) -> auto const& {
+ return memberAndKey.member;
+ });
+
+ auto const keys = membersAndKeys
+ | std::views::transform([]([[clang::lifetimebound]] auto const& memberAndKey) -> auto const& {
+ return memberAndKey.key;
+ });
+
+ // Copy values
+ auto const values = keys
+ | std::views::transform([&](auto const& key) {
+ return jsonDoc.getSubDoc(key);
+ })
+ | std::ranges::to();
// Reinsert, enumerated
jsonDoc.removeMember(rootKey);
jsonDoc.setEmptyArray(rootKey);
std::ranges::for_each(
- membersAndKeys | Utility::Ranges::enumerate,
- [&](auto const& enumeratedMemberAndKey) {
- auto const& [index, memberAndKey] = enumeratedMemberAndKey;
- auto const& [member, key] = memberAndKey;
+ members | Utility::Ranges::enumerate,
+ [&](auto const& enumeratedMember) {
+ auto const& [index, member] = enumeratedMember;
auto const key1 = rootKey.addIndex(index).addMember("key");
auto const key2 = rootKey.addIndex(index).addMember("value");
jsonDoc.set(key1,member);
@@ -111,13 +117,68 @@ bool Collection::listMembersAndValues(Data::JsonScope& jsonDoc){
return true;
}
-bool Collection::bundleToArray(std::span const args, Data::JsonScope& jsonDoc) {
+bool Collection::enumerateInline(std::span const args, Data::JsonScope& jsonDoc){
+ if (args.size() < 2) return false;
+ if (jsonDoc.memberType(rootKey) != Data::KeyType::array) return false;
+ auto const& indexKey = args.at(1);
+ std::ranges::for_each(
+ std::views::iota(std::size_t{0}, jsonDoc.memberSize(rootKey)),
+ [&](std::size_t const i) {
+ auto const key = rootKey.addIndex(i).addMember(indexKey);
+ jsonDoc.set(key, i);
+ }
+ );
+ return true;
+}
+
+bool Collection::enumerate(std::span args, Data::JsonScope& jsonDoc){
+ if (args.size() != 1) return false;
+ if (jsonDoc.memberType(rootKey) != Data::KeyType::array) return false;
+ Data::Json tmp;
+ std::ranges::for_each(
+ std::views::iota(std::size_t{0}, jsonDoc.memberSize(rootKey)),
+ [&](std::size_t const i) {
+ auto const key = rootKey.addIndex(i);
+ tmp.set(key.addIndex(0).toString(), i);
+ tmp.setSubDoc(key.addIndex(1).toString(), jsonDoc.getSubDoc(key));
+ }
+ );
+ jsonDoc.setSubDoc(rootKey, tmp);
+ return true;
+}
+
+// Obj->Array: bundle
+bool Collection::bundle(std::span const args, Data::JsonScope& jsonDoc) {
if (args.size() < 2) {
return false;
}
Data::Json tmp;
- for (auto [idx, key] : args.subspan(1) | std::views::enumerate) {
- tmp.setSubDoc("[" + std::to_string(idx) + "]", jsonDoc.getSubDoc(rootKey.addMember(key)));
+ for (auto [idx, key] : args.subspan(1) | Utility::Ranges::enumerate) {
+ auto element = jsonDoc.getSubDoc(rootKey.addMember(key));
+ if (element.memberType("") == Data::KeyType::null) { // Default to empty object instead of null
+ element.setEmptyObject("");
+ }
+ tmp.setSubDoc(rootKey.addIndex(idx).toString(), element);
+ }
+ jsonDoc.setSubDoc(rootKey, tmp);
+ return true;
+}
+
+// Array->Obj: bind
+bool Collection::bind(std::span args, Data::JsonScope& jsonDoc){
+ if (args.size() < 2) {
+ return false;
+ }
+ if (jsonDoc.memberType(rootKey) != Data::KeyType::array) {
+ return false;
+ }
+ Data::Json tmp;
+ for (auto [idx, key] : args.subspan(1) | Utility::Ranges::enumerate) {
+ auto element = jsonDoc.getSubDoc(rootKey.addIndex(idx));
+ if (element.memberType("") == Data::KeyType::null) { // Default to empty object instead of null
+ element.setEmptyObject("");
+ }
+ tmp.setSubDoc(key, element);
}
jsonDoc.setSubDoc(rootKey, tmp);
return true;
diff --git a/src/Nebulite/Module/Transformation/Filter.cpp b/src/Nebulite/Module/Transformation/Filter.cpp
index 358eb5bdf..9eef323c8 100644
--- a/src/Nebulite/Module/Transformation/Filter.cpp
+++ b/src/Nebulite/Module/Transformation/Filter.cpp
@@ -169,7 +169,7 @@ bool Filter::filterNulls(Data::JsonScope& jsonDoc) {
if (memberType != Data::KeyType::null) {
if (rootType == Data::KeyType::array) {
// For arrays, we need to reindex the keys
- filteredObject.setSubDoc("[" + std::to_string(arrayIndex) + "]", memberValue);
+ filteredObject.setSubDoc(rootKey.addIndex(arrayIndex).toString(), memberValue);
arrayIndex++;
} else {
filteredObject.setSubDoc(member, memberValue);