承接 dakujem/shorts 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dakujem/shorts

最新稳定版本:0.1.1

Composer 安装命令:

composer require dakujem/shorts

包简介

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

README 文档

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

统计信息

  • 总下载量: 61.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2019-10-10