Skip to content

Commit

Permalink
Slugify handle empty strings (#912)
Browse files Browse the repository at this point in the history
* Handle empty strings in slugify macro

* update changelog with PR number

* fix slugify test

* return empty string rather than none

---------

Co-authored-by: Andrew Vaccaro <[email protected]>
  • Loading branch information
dbeatty10 and atvaccaro authored Jun 4, 2024
1 parent af64dbe commit cb8dd4b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion integration_tests/tests/jinja_helpers/test_slugify.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
with comparisons as (
select '{{ dbt_utils.slugify("") }}' as output, '' as expected
union all
select '{{ dbt_utils.slugify(None) }}' as output, '' as expected
union all
select '{{ dbt_utils.slugify("!Hell0 world-hi") }}' as output, 'hell0_world_hi' as expected
union all
select '{{ dbt_utils.slugify("0Hell0 world-hi") }}' as output, '_0hell0_world_hi' as expected
)

select *
from comparisons
where output != expected
where output != expected
6 changes: 5 additions & 1 deletion macros/jinja_helpers/slugify.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{% macro slugify(string) %}

{% if not string %}
{{ return('') }}
{% endif %}

{#- Lower case the string -#}
{% set string = string | lower %}
{#- Replace spaces and dashes with underscores -#}
Expand All @@ -11,4 +15,4 @@

{{ return(string) }}

{% endmacro %}
{% endmacro %}

0 comments on commit cb8dd4b

Please sign in to comment.