Git Product home page Git Product logo

shorts's Introduction

๐Ÿฉณ shorts

PHP from Packagist Build Status Nature Friendly

Use this tool to shorten or limit personal names to desired length, or to use initials instead of a full name.
Supports Unicode / UTF-8 names.

  • limit / cap name length (with the aim to keep the name as legible as possible):
    | John Roland Reuel Tolkien |
    | --> John R. Reuel Tolkien |   # last name priority
    | -----> John R. R. Tolkien |
    | -------> J. R. R. Tolkien |
    | ---------> J.R.R. Tolkien |
    | -----------> John Tolkien |
    | ---------------> J.R.R.T. |
    | -------------------> JRRT |
    | ---------------------> JT |
    |                           | 
    | John Roland Reuel Tolkien |
    | John R. R. Tolkien <----- |   # first name priority
    | John R. R. T. <---------- |
    | John R.R.T. <------------ |
    | J.R.R.T. <--------------- |
    | JRRT <------------------- |
    | JT <--------------------- |
    
  • shorten a name using initials for part of the name (no length constraint):
    John Roland Reuel Tolkien:
    
    - J. R. R. Tolkien
    - John R. R. T.
    
  • create initials (configurable separator):
    - JRRT
    - J. R. R. T.
    

well i could not google out anything reasonable in well over 60 minutes so i decided to code it in less than that. how silly i was...

TODO / in progress

The features and the public interface are in place. The code is still ugly, don't fret. ๐Ÿ™Š

I'm also deciding how to make it possible for a custom name parser to be provided on the fly/by configuration, so that special cases (like compound names) can easily be supported. Bear with me. ๐Ÿป

Supported:

  • unicode names
  • arbitrary length

What is not (yet) supported:

  • compound surnames (sorry folks, this may come later)
  • non-word characters will be lost
    • Bull, John
  • academic and other titles
    • Bc. Foo Bar, Dr.Sc.
    • John Bull, Sr.
  • other writing systems than latin (may the will work, i'm just not testing them)

You will need to handle these yourself before/after passing them through the shortener.

Usage

Limit (cap) names to desired lengths:

Shorts::cap('Pablo Escobar', 10); // "P. Escobar"
Shorts::cap('Pablo Escobar', 2); // "PE"

Shorts::cap('John Ronald Reuel Tolkien', 20); // "John R. R. Tolkien"
Shorts::cap('John Ronald Reuel Tolkien', 16); // "J. R. R. Tolkien"
Shorts::cap('John Ronald Reuel Tolkien', 15); // "J.R.R. Tolkien"
Shorts::cap('John Ronald Reuel Tolkien', 8);  // "J.R.R.T."
Shorts::cap('John Ronald Reuel Tolkien', 4);  // "JRRT"

The above will try to keep the last name legible, unless the limit is too strict.
Inverse version that will try to keep the first name legible is also available:

Shorts::cap('Pablo Escobar', 10, Shorts::FIRST_NAME); // "Pablo E."

Shrink names using initials except for the last name:

Shorts::shrink('John Ronald Reuel Tolkien'); // "J. R. R. Tolkien"
Shorts::shrink('Hugo Ventil');               // "H. Ventil"

Shrink names using initials except for the first name:

Shorts::shrink('John Ronald Reuel Tolkien', Shorts::FIRST_NAME); // "John R. R. T."
Shorts::shrink('Hugo Ventil', Shorts::FIRST_NAME);               // "Hugo V."

Create "short" initials:

Shorts::initials('John Ronald Reuel Tolkien'); // "JRRT"
Shorts::initials('Hugo Ventil');               // "HV"

... or "longer" version:

Shorts::initials('John Ronald Reuel Tolkien', '.', ' '); // "J. R. R. T."
Shorts::initials('Hugo Ventil', '.', ' ');               // "H. V."

Each of the static methods has a non-static counterpart:

Shorts::i()->limit( ... );      // Shorts::cap( ... )
Shorts::i()->reduce( ... );     // Shorts::shrink( ... )
Shorts::i()->toInitials( ... ); // Shorts::initials( ... )

Shorts also provides the ability co create a preconfigured formatter callable for each of the methods:

Shorts::i()->limiter( ... )
Shorts::i()->reducer( ... )
Shorts::i()->initialsFormatter( ... )

These can be used as follows:

$fmt = Shorts::i()->limiter(20); // will limit any input to 20 chars
$fmt('Foo Bar'); // this is equivalent to  Shorts::cap('Foo Bar', 20)

Note the formatters can come handy when defining filters for templating languages, like Twig or Latte.

Testing

$ composer test

Contributions

... are always welcome. Many times it is useful to just point out a use case the author have not thought about or come across.

Possible future stuff

shorts's People

Contributors

dakujem avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.