Skip to content

Instantly share code, notes, and snippets.

@identw
Created April 5, 2024 18:55
Show Gist options
  • Save identw/7239bcf4a995a9e37f05ca78ddd00837 to your computer and use it in GitHub Desktop.
Save identw/7239bcf4a995a9e37f05ca78ddd00837 to your computer and use it in GitHub Desktop.
helpers.jsonnet
{
mapArrayByField(f, mixinField):: {
local m = super[mixinField],
[mixinField]: std.prune(std.map(f, m))
},
mapArrayWithField(fieldValues, fieldName, mixinField, f):: (
local fieldValuesSet = std.set(fieldValues);
local inFieldValuesSet(value) = std.length(std.setInter(fieldValuesSet, std.set([value]))) > 0;
self.mapArrayByField(function(i) if std.objectHas(i, fieldName) && inFieldValuesSet(i[fieldName]) then f(i) else i, mixinField)
),
mergeArrayWithField(fieldValues, fieldName, mixinField, patch):: (
self.mapArrayWithField(fieldValues, fieldName, mixinField, function(i) (i + patch))
),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment