Skip to content

Commit

Permalink
Add substring function
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Mar 18, 2024
1 parent 235da3e commit a27e001
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe("Expression", () => {
flavor
)
).toEqual('(YEAR("2024-01-01") - YEAR("2025-01-01"))');
expect(Fn.substring("year", 1, 10).toSQL(flavor)).toEqual(
"SUBSTRING(`year`,1,10)"
);
});
it("should support value composition", () => {
expect(Fn.sum(Fn.ifnull("foo", Q.S`123`)).toSQL(flavor)).toEqual(
Expand Down
12 changes: 12 additions & 0 deletions src/Function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ export const Function = {
formatDate: (date: Dayjs) => {
return Q.expr(`'${formatDayjs(date)}'`);
},
substring: (
string: string,
start: ExpressionValue,
length: ExpressionValue
) => {
return new FunctionExpression(
"SUBSTRING",
string,
Q.value(start),
Q.value(length)
);
},
string: (value: string) => {
return Q.expr(Expression.escapeString(`${value}`));
},
Expand Down
8 changes: 8 additions & 0 deletions src/stories/3_Function.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ Compute a single result from a set of input values:

<QueryPreview code={`Fn.count('column');`} />

### String Functions

Manipulate and format date values:

**Year**

<QueryPreview code={`Fn.substring('created_at',1,4);`} />

### Date Functions

Manipulate and format date values:
Expand Down

0 comments on commit a27e001

Please sign in to comment.