Skip to content

Commit

Permalink
Adds #2176 slice() (#2238)
Browse files Browse the repository at this point in the history
Adds a slice() function with documentation to the query language.
  • Loading branch information
holroy committed Mar 17, 2024
1 parent 733cadd commit f2d9c62
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/docs/reference/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,18 @@ flat(list(1, list(21, 22), list(list (311, 312, 313))), 4) => list(1, 21, 22, 31
flat(rows.file.outlinks)) => All the file outlinks at first level in output
```

### `slice(array, [start, [end]])`

Returns a shallow copy of a portion of an array into a new array object selected from `start`
to `end` (`end` not included) where `start` and `end` represents the index of items in that array.

```js
slice([1, 2, 3, 4, 5], 3) = [4, 5] => All items from given position, 0 as first
slice(["ant", "bison", "camel", "duck", "elephant"], 0, 2) = ["ant", "bison"] => First two items
slice([1, 2, 3, 4, 5], -2) = [4, 5] => counts from the end, last two items
slice(someArray) => a copy of someArray
```

---

## String Operations
Expand Down
15 changes: 15 additions & 0 deletions src/expression/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,20 @@ export namespace DefaultFunctions {
})
.add1("null", () => null)
.build();

// Slices the array into a new array
export const slice = new FunctionBuilder("slice")
.add1("array", a => {
return a.slice();
})
.add2("array", "number", (a, start) => {
return a.slice(start);
})
.add3("array", "number", "number", (a, start, end) => {
return a.slice(start, end);
})
.add1("null", () => null)
.build();
}

/** Default function implementations for the expression evaluator. */
Expand Down Expand Up @@ -889,6 +903,7 @@ export const DEFAULT_FUNCTIONS: Record<string, FunctionImpl> = {
reverse: DefaultFunctions.reverse,
sort: DefaultFunctions.sort,
flat: DefaultFunctions.flat,
slice: DefaultFunctions.slice,

// Aggregation operations like reduce.
reduce: DefaultFunctions.reduce,
Expand Down
19 changes: 19 additions & 0 deletions src/test/function/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ test("Evaluate flat()", () => {
expect(parseEval("flat(list(1, list(2, list(3, list(4, list(5))))), 10)")).toEqual(parseEval("list(1,2,3,4,5)")); // flat(..., 10)
});

// <-- slice() -->

test("Evaluate slice()", () => {
expect(parseEval("slice(list(1, 2, 3, 4, 5), 3)")).toEqual(parseEval("list(4, 5)")); // slice(..., 3)
expect(parseEval("slice(list(1, 2, 3, 4, 5), 0, 2)")).toEqual(parseEval("list(1, 2)")); // slice(..., 0, 2)
expect(parseEval("slice(list(1, 2, 3, 4, 5), -2)")).toEqual(parseEval("list(4, 5)")); // slice(..., -2)
expect(parseEval("slice(list(1, 2, 3, 4, 5), -1, 1)")).toEqual(parseEval("list()")); // slice(..., -1, 1)
expect(parseEval("slice(list(1, 2, 3, 4, 5))")).toEqual(parseEval("list(1, 2, 3, 4, 5)")); // slice(...)
expect(parseEval('slice(list(date("2021-01-01"), date("2022-02-02"), date("2023-03-03")), -2)')).toEqual([
DateTime.fromObject({ year: 2022, month: 2, day: 2 }),
DateTime.fromObject({ year: 2023, month: 3, day: 3 }),
]); // slice(date list, -2)
expect(parseEval('slice(["ant", "bison", "camel", "duck", "elephant"], -3)')).toEqual([
"camel",
"duck",
"elephant",
]); // slice(string list, -3)
});

// <-- sort() -->

describe("sort()", () => {
Expand Down

0 comments on commit f2d9c62

Please sign in to comment.