Skip to content

Commit

Permalink
Add Record.remove() to remove by key
Browse files Browse the repository at this point in the history
For this we make the assumption that the keys are unique.

Note: this does not hold universally, yet. Implementations in
`nu-protocol` use an all key removal on the basis of retain in certain
places. (e.g. used by `reject` under the hood)

See nushell#8446 for this defensiveness
  • Loading branch information
sholderbach committed Oct 29, 2023
1 parent 2e68e6d commit 88ec098
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/nu-protocol/src/value/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ impl Record {
Some((self.cols.get(idx)?, self.vals.get(idx)?))
}

/// Remove single value by key
///
/// Returns `None` if key not found
///
/// Note: makes strong assumption that keys are unique
pub fn remove(&mut self, col: impl AsRef<str>) -> Option<Value> {
let idx = self.index_of(col)?;
self.cols.remove(idx);
Some(self.vals.remove(idx))
}

pub fn columns(&self) -> Columns {
Columns {
iter: self.cols.iter(),
Expand Down

0 comments on commit 88ec098

Please sign in to comment.