sanity

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2021 License: MIT Imports: 3 Imported by: 0

README

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),
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rule

type Rule func(in string) (out string)

Rule represents a file sanitization rule, a function that turns (potentially) invalid input string into valid output.

func Replace

func Replace(substringsAndRepl ...string) Rule

Replace replaces met substrings (all varargs except the last one) with a replacement string (last vararg.) It panics if there were less than two arguments provided.

func ReplaceRegexp

func ReplaceRegexp(patternsAndRepl ...string) Rule

ReplaceRegexp replaces matched patterns (all varargs except the last one) with a replacement string (last vararg.) It panics if there were less than two arguments provided.

func Strip

func Strip(substrings ...string) Rule

Strip is a convenience call to Replace with last argument being an empty string. It panics if there was less than one argument provided.

func StripRange

func StripRange(ranges ...int32) Rule

StripRange removes runes in the provided ranges (defined as paired varargs min-max) from the input string. It panics if odd amount of arguments was provided.

func StripRegexp

func StripRegexp(patterns ...string) Rule

StripRegexp is a convenience call to ReplaceRegexp with last argument being an empty string.

func StripRune

func StripRune(runes ...rune) Rule

StripRune removes the provided runes from the input string.

func Truncate

func Truncate(length int, omission string, pos truncate.TruncatePosition) Rule

Truncate truncates string to the specified length.

type Ruleset

type Ruleset []Rule

Ruleset represents string sanitization rule set.

func New

func New(rules ...Rule) Ruleset

New constructs new Ruleset from the provided rules.

func (Ruleset) Copy

func (r Ruleset) Copy() Ruleset

Copy creates a copy of existing Ruleset.

func (Ruleset) Extend

func (r Ruleset) Extend(rules ...Rule) Ruleset

Extend adds more rules to the existing Ruleset

func (Ruleset) Sanitize

func (r Ruleset) Sanitize(in string) string

Sanitize applies rules in the Ruleset to the input string and returns sanitized string with all rules applied.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL