git.delta.rocks / jrsonnet / refs/commits / 3e814e5a718d

difftreelog

feat add std.remove and std.removeAt

Paweł Bęza2023-07-15parent: #aee64ce.patch.diff
in: master
Upstream issue: https://github.com/google/go-jsonnet/pull/689

2 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -253,3 +253,26 @@
 	}
 	Ok(Val::Num(arr.iter().sum::<f64>() / (arr.len() as f64)))
 }
+
+#[builtin]
+pub fn builtin_remove_at(
+	arr: ArrValue,
+	index: usize,
+) -> Result<ArrValue> {
+	let newArrLeft = arr.clone().slice(None, Some(index), None);
+	let newArrRight = arr.clone().slice(Some(index + 1), None, None);
+	return Ok(ArrValue::extended(
+		newArrLeft.unwrap_or(ArrValue::empty()),
+		newArrRight.unwrap_or(ArrValue::empty()))
+	);
+}
+
+#[builtin]
+pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {
+	for (index, item) in arr.iter().enumerate() {
+		if equals(&item?, &elem)? {
+			return builtin_remove_at(arr.clone(), index) 
+		}
+	}
+	Ok(arr)
+}
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
83 ("contains", builtin_member::INST),83 ("contains", builtin_member::INST),
84 ("count", builtin_count::INST),84 ("count", builtin_count::INST),
85 ("avg", builtin_avg::INST),85 ("avg", builtin_avg::INST),
86 ("removeAt", builtin_remove_at::INST),
87 ("remove", builtin_remove::INST),
86 // Math88 // Math
87 ("abs", builtin_abs::INST),89 ("abs", builtin_abs::INST),
88 ("sign", builtin_sign::INST),90 ("sign", builtin_sign::INST),