All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.chilldev.commons.text.slugifier.SimpleSlugifier Maven / Gradle / Ivy

There is a newer version: 0.4.4
Show newest version
// Generated by delombok at Wed Jun 06 18:27:19 UTC 2018
/**
 * This file is part of the ChillDev-Commons.
 *
 * @license http://mit-license.org/ The MIT license
 * @copyright 2015 - 2016 © by Rafał Wrzeszcz - Wrzasq.pl.
 */
package pl.chilldev.commons.text.slugifier;

import java.text.Normalizer;
import java.util.Locale;

/**
 * Simple implementation of slugifier.
 */
public class SimpleSlugifier implements Slugifier {
    /**
     * Default separator.
     */
    public static final String DEFAULT_DELIMITER = "-";
    /**
     * Separator for URL parts.
     */
    private String delimiter = SimpleSlugifier.DEFAULT_DELIMITER;

    /**
     * {@inheritDoc}
     */
    @Override
    public String slugify(String text) {
        return Normalizer.normalize(text, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "").replaceAll("\\W+", this.delimiter).replaceAll(this.delimiter + "+", this.delimiter).replaceAll("^" + this.delimiter, "").replaceAll(this.delimiter + "$", "").toLowerCase(Locale.ROOT);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String slugify(String... texts) {
        return this.slugify(String.join(this.delimiter, texts));
    }

    /**
     * Separator for URL parts.
     */
    @SuppressWarnings("all")
    public void setDelimiter(final String delimiter) {
        this.delimiter = delimiter;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy