Skip to content

๐Ÿ“ Fast extensible file name sanitizer that works in Windows/Linux

License

Notifications You must be signed in to change notification settings

dreamscached/sanity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

12 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Sanity

Sanity is a fast and easily extensible file name (and in fact any other string) sanitizer.

Usage

Built-in rule set

Sanity provides a sensible default rule set for Windows and Linux file names that will be enough in most cases:

package main

import (
	"fmt"

	"github.com/dreamscached/sanity/filename"
)

func main() {
	// Prints 'con_.txt'
	fmt.Println(filename.Windows.Sanitize("con.txt"))

	// Prints 'foobar'
	fmt.Println(filename.Unix.Sanitize("foo\x00bar.txt"))
}

Creating custom rule sets

Sanity provides a clean and obvious way to create custom rule sets with Rule factory functions. For example, here's Linux default file name rule set.

package main

import (
	"github.com/dreamscached/sanity"
	"github.com/aquilax/truncate"
)

var Unix = sanity.New(
	sanity.Replace("/", " "),
	sanity.StripRune(0x0),
	sanity.Truncate(255, truncate.DEFAULT_OMISSION, truncate.PositionEnd),
)