Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ricn/zarex
Filename sanitization for Elixir
https://github.com/ricn/zarex
elixir
Last synced: about 1 month ago
JSON representation
Filename sanitization for Elixir
- Host: GitHub
- URL: https://github.com/ricn/zarex
- Owner: ricn
- License: mit
- Created: 2015-03-23T22:54:21.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-01-16T22:31:27.000Z (11 months ago)
- Last Synced: 2024-11-01T07:33:23.604Z (about 1 month ago)
- Topics: elixir
- Language: Elixir
- Homepage:
- Size: 50.8 KB
- Stars: 28
- Watchers: 4
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- freaking_awesome_elixir - Elixir - Filename sanitization for Elixir. (Files and Directories)
- fucking-awesome-elixir - zarex - Filename sanitization for Elixir. (Files and Directories)
- awesome-elixir - zarex - Filename sanitization for Elixir. (Files and Directories)
README
Zarex
=====[![Elixir CI](https://github.com/ricn/zarex/actions/workflows/elixir.yml/badge.svg)](https://github.com/ricn/zarex/actions/workflows/elixir.yml)
[![Hex.pm](https://img.shields.io/hexpm/v/zarex.svg)](https://hex.pm/packages/zarex)Filename sanitization for Elixir. This is useful when you generate filenames for downloads from user input. The library is heavily inspired by
[Zaru](https://github.com/madrobby/zaru) which does the same thing but in Ruby.## Installation
Add this to your `mix.exs` file, then run `mix do deps.get, deps.compile`:
```elixir
{:zarex, "~> 1.0"}
```## Examples
```elixir
Zarex.sanitize(" what\ēver//wëird:user:înput:")
# => "whatēverwëirduserînput"
```Zarex takes a given filename and normalizes, filters and truncates it.
It deletes the bad stuff and leaves unicode characters in place, so users can use whatever alphabets they want to. Zarex doesn't remove whitespace—instead, any sequence of whitespace that is 1 or more characters in length is collapsed to a single space. Filenames are truncated so that they are at maximum 255 characters long.
If extra space is required (for example to add your own filename extension later), you can leave extra padding:
```elixir
Zarex.sanitize(String.duplicate("a", 400), padding: 10)
# returns a string with 245 chars. First the binary of 400 chars will be truncated
# to 255 chars and then the extra padding of 10 will truncate the binary to 245 chars.
```## Credits
The following people have contributed ideas, documentation, or code to Zarex:
* Richard Nyström
* Thomas Fuchs (the creator of Zaru, the Ruby library Zarex is heavily based on)## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request