Giter Club home page Giter Club logo

data-utilities's Introduction

DataUtilities

Latest Version codecov

A handful of useful helper functions to process data

data-utilities's People

Contributors

battis avatar scrutinizer-auto-fixer avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

hanleybrand

data-utilities's Issues

superscripts for 1st, 2nd, 3rd

// TODO superscripts for 1st, 2nd, 3rd

        /* add a space after each piece of punctuation */
        $title = preg_replace('/([^a-z0-9])\s*/i', '$1 ', strtolower($title));

        // TODO smart em- and en-dashes
        // TODO superscripts for 1st, 2nd, 3rd

        /* Split the string into separate words */
        $words = preg_split('/[' . implode('', $spaceEquivalents) . ']+/', $title, -1, PREG_SPLIT_NO_EMPTY);

dry out this code somehow

return "{$singular}ES"; // TODO dry out this code somehow

            case "S":
            case "X":
            case "Z":
                return "{$singular}ES"; // TODO dry out this code somehow
            case "y":
                if (self::isVowel(substr($singular, -2, 1))) {
                    return "{$singular}s";
                }
                return substr($singular, 0, strlen($singular) - 1) . "ies";
            case "Y":
                if (self::isVowel(substr($singular, -2, 1))) {
                    return "{$singular}S";
                }
                return substr($singular, 0, strlen($singular) - 1) . "IES";
            default:
                switch (substr($singular, -2)) {

document pluralize

@param string $singular

@return string

* TODO document pluralize

        return strtolower($snake_case);
    }

    /**
     * TODO document pluralize
     * @param string $singular
     * @return string
     */
    public static function pluralize(string $singular): string
    {
        switch (substr($singular, -1)) {

smart em- and en-dashes

// TODO smart em- and en-dashes

        /* add a space after each piece of punctuation */
        $title = preg_replace('/([^a-z0-9])\s*/i', '$1 ', strtolower($title));

        // TODO smart em- and en-dashes
        // TODO superscripts for 1st, 2nd, 3rd

        /* Split the string into separate words */
        $words = preg_split('/[' . implode('', $spaceEquivalents) . ']+/', $title, -1, PREG_SPLIT_NO_EMPTY);

smart em- and en-dashes

// TODO smart em- and en-dashes

<?php

namespace Battis\DataUtilities;

use Battis\Hydratable\Hydrate;

class Text
{
    /**
     * Converts $title to Title Case, and returns the result
     *
     * @param string $title
     * @param array $params An associative array of additional special cases,
     *                      e.g. (with any subset of the keys below)
     *                      ```
     * [
     *     'lowerCaseWords' => ['foo', 'bar'],
     *     'allCapsWords' => ['BAZ'],
     *     'camelCaseWords' => ['foobarbaz' => 'fooBarBaz'],
     *     'spaceEquivalents' => ['\t']
     * ]
     * ```
     *
     * @return string
     *
     * @see http://www.sitepoint.com/title-case-in-php/ SitePoint
     **/
    public static function titleCase($title, $params = [])
    {
        $hydrate = new Hydrate();
        /*
         * Our array of 'small words' which shouldn't be capitalized if they
         * aren't the first word.  Add your own words to taste.
         */
        $lowerCaseWords = $hydrate($params['lowerCaseWords'],[
            'of','a','the','and','an','or','nor','but','is','if','then','else',
            'when','at','from','by','on','off','for','in','out','over','to',
            'into','with'
        ]);

        $allCapsWords = $hydrate($params['allCapsWords'], [
            'i', 'ii', 'iii', 'iv', 'v', 'vi', 'sis', 'csv', 'php', 'html',
            'lti'
        ]);

        $camelCaseWords = $hydrate($params['camelCaseWords'], [
            'github' => 'GitHub'
        ]);

        $spaceEquivalents = $hydrate($params['spaceEquivalents'], [
            '\s', '_'
        ]);

        /* add a space after each piece of punctuation */
        $title = preg_replace('/([^a-z0-9])\s*/i', '$1 ', strtolower($title));

        // TODO smart em- and en-dashes
        // TODO superscripts for 1st, 2nd, 3rd

        /* Split the string into separate words */
        $words = preg_split('/[' . implode('', $spaceEquivalents) . ']+/', $title, -1, PREG_SPLIT_NO_EMPTY);

        foreach ($words as $key => $word) {
            if (in_array($word, $allCapsWords)) {
                $words[$key] = strtoupper($word);
            } elseif (array_key_exists($word, $camelCaseWords)) {
                $words[$key] = $camelCaseWords[$word];
            } elseif ($key == 0 or !in_array($word, $lowerCaseWords)) {
                $words[$key] = ucwords($word);
            }
        }

        /* Join the words back into a string */
        $newtitle = implode(' ', $words);

        return $newtitle;
    }

    /**
     * What portion of string `$a` and `$b` overlaps?
     *
     * For example if `$a` is `'abcdefg`` and `$b` is `'fgjkli'`, the overlapping
     * portion would be `'fg'`.
     *
     * @param string $a
     * @param string $b
     * @param boolean $swap Attempt to swap `$a` and `$b` to find overlap. (default: `true`)
     * @return string Overlapping portion of `$a` and `$b`, `''` if no overlap
     */
    public static function overlap($a, $b, $swap = true)
    {
        if (!is_string($a) || !is_string($b)) {
            return '';
        }

        for ($i = 0; $i < strlen($a); $i++) {
            $overlap = true;
            for ($j = 0; $j < strlen($b) && $i + $j < strlen($a); $j++) {
                if ($a[$i+$j] !== $b[$j]) {
                    $overlap = false;
                    break;
                }
            }
            if ($overlap) {
                return substr($a, $i, $j);
            }
        }
        if ($swap) {
            return static::overlap($b, $a, false);
        }
        return '';
    }

    public static function snake_case_to_PascalCase(string $snake_case): string
    {
        return join(
            array_map(
                fn($token) => strtoupper(substr($token, 0, 1)) .
                    substr($token, 1),
                explode("_", $snake_case)
            )
        );
    }

    public static function camelCase_to_snake_case(string $camelCase): string
    {
        $snake_case = $camelCase;
        foreach (
            [
                "/([^0-9])([0-9])/", // separate numeric phrases
                "/([A-Z])([A-Z][a-z])/", // separate trailing word from acronym
                "/([^A-Z])([A-Z])/", // separate end of word from trailing word,
                "/([^_])_+([^_])/", // minimize underscores
            ]
            as $regexp
        ) {
            $snake_case = preg_replace($regexp, "$1_$2", $snake_case);
        }
        return strtolower($snake_case);
    }

    public static function pluralize(string $singular): string
    {
        switch (substr($singular, -1)) {
            case "s":
            case "x":
            case "z":
                return "{$singular}es";
            case "S":
            case "X":
            case "Z":
                return "{$singular}ES";
            case "y":
                return substr($singular, 0, strlen($singular) - 1) . "ies";
            case "Y":
                return substr($singular, 0, strlen($singular) - 1) . "IES";
            default:
                switch (substr($singular, -2)) {
                    case "sh":
                    case "Sh":
                    case "ch":
                    case "Ch":
                        return "{$singular}es";
                    case "SH":
                    case "sH":
                    case "CH":
                    case "cH":
                        return "{$singular}ES";
                    default:
                        if (
                            substr($singular, -1) ===
                            strtolower(substr($singular, -1))
                        ) {
                            return "{$singular}s";
                        } else {
                            return "{$singular}S";
                        }
                }
        }
    }
}

superscripts for 1st, 2nd, 3rd

"/([A-Z])([A-Z][a-z])/", // separate trailing word from acronym

"/([^A-Z])([A-Z])/", // separate end of word from trailing word,

"/([^_])_+([^_])/", // minimize underscores

// TODO superscripts for 1st, 2nd, 3rd

<?php

namespace Battis\DataUtilities;

use Battis\Hydratable\Hydrate;

class Text
{
    /**
     * Converts $title to Title Case, and returns the result
     *
     * @param string $title
     * @param array $params An associative array of additional special cases,
     *                      e.g. (with any subset of the keys below)
     *                      ```
     * [
     *     'lowerCaseWords' => ['foo', 'bar'],
     *     'allCapsWords' => ['BAZ'],
     *     'camelCaseWords' => ['foobarbaz' => 'fooBarBaz'],
     *     'spaceEquivalents' => ['\t']
     * ]
     * ```
     *
     * @return string
     *
     * @see http://www.sitepoint.com/title-case-in-php/ SitePoint
     **/
    public static function titleCase($title, $params = [])
    {
        $hydrate = new Hydrate();
        /*
         * Our array of 'small words' which shouldn't be capitalized if they
         * aren't the first word.  Add your own words to taste.
         */
        $lowerCaseWords = $hydrate($params['lowerCaseWords'],[
            'of','a','the','and','an','or','nor','but','is','if','then','else',
            'when','at','from','by','on','off','for','in','out','over','to',
            'into','with'
        ]);

        $allCapsWords = $hydrate($params['allCapsWords'], [
            'i', 'ii', 'iii', 'iv', 'v', 'vi', 'sis', 'csv', 'php', 'html',
            'lti'
        ]);

        $camelCaseWords = $hydrate($params['camelCaseWords'], [
            'github' => 'GitHub'
        ]);

        $spaceEquivalents = $hydrate($params['spaceEquivalents'], [
            '\s', '_'
        ]);

        /* add a space after each piece of punctuation */
        $title = preg_replace('/([^a-z0-9])\s*/i', '$1 ', strtolower($title));

        // TODO smart em- and en-dashes
        // TODO superscripts for 1st, 2nd, 3rd

        /* Split the string into separate words */
        $words = preg_split('/[' . implode('', $spaceEquivalents) . ']+/', $title, -1, PREG_SPLIT_NO_EMPTY);

        foreach ($words as $key => $word) {
            if (in_array($word, $allCapsWords)) {
                $words[$key] = strtoupper($word);
            } elseif (array_key_exists($word, $camelCaseWords)) {
                $words[$key] = $camelCaseWords[$word];
            } elseif ($key == 0 or !in_array($word, $lowerCaseWords)) {
                $words[$key] = ucwords($word);
            }
        }

        /* Join the words back into a string */
        $newtitle = implode(' ', $words);

        return $newtitle;
    }

    /**
     * What portion of string `$a` and `$b` overlaps?
     *
     * For example if `$a` is `'abcdefg`` and `$b` is `'fgjkli'`, the overlapping
     * portion would be `'fg'`.
     *
     * @param string $a
     * @param string $b
     * @param boolean $swap Attempt to swap `$a` and `$b` to find overlap. (default: `true`)
     * @return string Overlapping portion of `$a` and `$b`, `''` if no overlap
     */
    public static function overlap($a, $b, $swap = true)
    {
        if (!is_string($a) || !is_string($b)) {
            return '';
        }

        for ($i = 0; $i < strlen($a); $i++) {
            $overlap = true;
            for ($j = 0; $j < strlen($b) && $i + $j < strlen($a); $j++) {
                if ($a[$i+$j] !== $b[$j]) {
                    $overlap = false;
                    break;
                }
            }
            if ($overlap) {
                return substr($a, $i, $j);
            }
        }
        if ($swap) {
            return static::overlap($b, $a, false);
        }
        return '';
    }

    public static function snake_case_to_PascalCase(string $snake_case): string
    {
        return join(
            array_map(
                fn($token) => strtoupper(substr($token, 0, 1)) .
                    substr($token, 1),
                explode("_", $snake_case)
            )
        );
    }

    public static function camelCase_to_snake_case(string $camelCase): string
    {
        $snake_case = $camelCase;
        foreach (
            [
                "/([^0-9])([0-9])/", // separate numeric phrases
                "/([A-Z])([A-Z][a-z])/", // separate trailing word from acronym
                "/([^A-Z])([A-Z])/", // separate end of word from trailing word,
                "/([^_])_+([^_])/", // minimize underscores
            ]
            as $regexp
        ) {
            $snake_case = preg_replace($regexp, "$1_$2", $snake_case);
        }
        return strtolower($snake_case);
    }

    public static function pluralize(string $singular): string
    {
        switch (substr($singular, -1)) {
            case "s":
            case "x":
            case "z":
                return "{$singular}es";
            case "S":
            case "X":
            case "Z":
                return "{$singular}ES";
            case "y":
                return substr($singular, 0, strlen($singular) - 1) . "ies";
            case "Y":
                return substr($singular, 0, strlen($singular) - 1) . "IES";
            default:
                switch (substr($singular, -2)) {
                    case "sh":
                    case "Sh":
                    case "ch":
                    case "Ch":
                        return "{$singular}es";
                    case "SH":
                    case "sH":
                    case "CH":
                    case "cH":
                        return "{$singular}ES";
                    default:
                        if (
                            substr($singular, -1) ===
                            strtolower(substr($singular, -1))
                        ) {
                            return "{$singular}s";
                        } else {
                            return "{$singular}S";
                        }
                }
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.