crop

Table of Contents

Function

crop — remove fields from input value that are missing in a specified type

Synopsis

crop(val: any, t: type) -> any

Description

The crop function operates on record values (or records within a nested value) and returns a result such that any fields that are present in val but not in record type t are removed. Cropping is a useful when you want records to “fit” a schema tightly.

If val is a record (or if any of its nested values is a record):

  • absent fields are ignored and omitted from the result,
  • fields are matched by name and are order independent and the input order is retained, and
  • leaf types are ignored, i.e., no casting occurs.

If val is not a record, it is returned unmodified.

Examples

Crop a record

crop(this, <{a:int64}>)
{a:1,b:2}
Loading...

produces

Crop an array of records

crop(this, <[{a:int64}]>)
[{a:1,b:2},{a:3,b:4}]
Loading...

produces

Cropped primitives are returned unmodified

crop(this, <{a:int64}>)
10.0.0.1
1
"foo"
Loading...

produces

Next: error

SuperDB