Consistent wrap (#7159)

* Consistent wrap

* Signature fix
This commit is contained in:
Nano 2022-11-19 09:39:40 +12:00 committed by GitHub
parent 2dd4cb9f7d
commit bd30ea723e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,7 +20,10 @@ impl Command for Wrap {
fn signature(&self) -> nu_protocol::Signature {
Signature::build("wrap")
.input_output_types(vec![(Type::List(Box::new(Type::Any)), Type::Table(vec![]))])
.input_output_types(vec![
(Type::List(Box::new(Type::Any)), Type::Table(vec![])),
(Type::Range, Type::Table(vec![])),
])
.required("name", SyntaxShape::String, "the name of the column")
.category(Category::Filters)
}
@ -36,7 +39,9 @@ impl Command for Wrap {
let name: String = call.req(engine_state, stack, 0)?;
match input {
PipelineData::Value(Value::List { vals, .. }, ..) => Ok(vals
PipelineData::Value(Value::Range { .. }, ..)
| PipelineData::Value(Value::List { .. }, ..)
| PipelineData::ListStream { .. } => Ok(input
.into_iter()
.map(move |x| Value::Record {
cols: vec![name.clone()],
@ -44,13 +49,6 @@ impl Command for Wrap {
span,
})
.into_pipeline_data(engine_state.ctrlc.clone())),
PipelineData::ListStream(stream, ..) => Ok(stream
.map(move |x| Value::Record {
cols: vec![name.clone()],
vals: vec![x],
span,
})
.into_pipeline_data(engine_state.ctrlc.clone())),
PipelineData::ExternalStream { .. } => Ok(Value::Record {
cols: vec![name],
vals: vec![input.into_value(call.head)],
@ -67,7 +65,8 @@ impl Command for Wrap {
}
fn examples(&self) -> Vec<Example> {
vec![Example {
vec![
Example {
description: "Wrap a list into a table with a given column name",
example: "[1 2 3] | wrap num",
result: Some(Value::List {
@ -90,7 +89,32 @@ impl Command for Wrap {
],
span: Span::test_data(),
}),
}]
},
Example {
description: "Wrap a range into a table with a given column name",
example: "1..3 | wrap num",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(2)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(3)],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
]
}
}