dakujem/shorts

A flexible tool to shorten or limit person names in length or create initials.

0.1.1 2019-10-13 16:58 UTC

This package is auto-updated.

Last update: 2024-12-06 23:20:02 UTC


README

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