All Projects → ICanBoogie → Inflector

ICanBoogie / Inflector

Licence: other
A multilingual inflector that transforms words from singular to plural, underscore to camel case, and formats strings in various ways.

Projects that are alternatives of or similar to Inflector

rudash
Rudash - Lodash for Ruby Apps
Stars: ✭ 27 (-86.76%)
Mutual labels:  underscore
Yalinqo
Yet Another LINQ to Objects for PHP [Simplified BSD]
Stars: ✭ 400 (+96.08%)
Mutual labels:  underscore
Underscore Analysis
underscore.js源码分析一点点进行中,欢迎watch
Stars: ✭ 57 (-72.06%)
Mutual labels:  underscore
eslint-plugin-lodash-template
ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.
Stars: ✭ 15 (-92.65%)
Mutual labels:  underscore
Underscore Java
java port of Underscore.js
Stars: ✭ 327 (+60.29%)
Mutual labels:  underscore
Lodash Id
Makes it easy to manipulate id-based resources with lodash or lowdb
Stars: ✭ 434 (+112.75%)
Mutual labels:  underscore
backbone-tutorial-series
Source code generated in the Backbone.js tutorial series
Stars: ✭ 71 (-65.2%)
Mutual labels:  underscore
Param.macro
Partial application syntax and lambda parameters for JavaScript, inspired by Scala's `_` & Kotlin's `it`
Stars: ✭ 170 (-16.67%)
Mutual labels:  underscore
Nspl
Non-Standard PHP Library - functional primitives toolbox and more
Stars: ✭ 365 (+78.92%)
Mutual labels:  underscore
Javascript ninja
javascript-ninja 😆
Stars: ✭ 11 (-94.61%)
Mutual labels:  underscore
js-terminal
The copy of Terminal developed in JS
Stars: ✭ 21 (-89.71%)
Mutual labels:  underscore
Gulp Template
Render/precompile Lodash templates
Stars: ✭ 276 (+35.29%)
Mutual labels:  underscore
Moses
Utility library for functional programming in Lua
Stars: ✭ 541 (+165.2%)
Mutual labels:  underscore
underwater
~2kb - ES6 Collection of helper functions. Lodash like
Stars: ✭ 18 (-91.18%)
Mutual labels:  underscore
Babel Plugin Partial Application
[DEPRECATED] Please use https://github.com/citycide/param.macro
Stars: ✭ 60 (-70.59%)
Mutual labels:  underscore
underscore-analysis
underscore 源码分析
Stars: ✭ 20 (-90.2%)
Mutual labels:  underscore
Underscore Analysis
【NO LONGER UPDATE】underscore-1.8.3.js 源码解读 & 系列文章(完)
Stars: ✭ 3,940 (+1831.37%)
Mutual labels:  underscore
You Dont Need Lodash Underscore
List of JavaScript methods which you can use natively + ESLint Plugin
Stars: ✭ 13,915 (+6721.08%)
Mutual labels:  underscore
Dash
Functional programming library for PHP. Inspired by Underscore, Lodash, and Ramda.
Stars: ✭ 84 (-58.82%)
Mutual labels:  underscore
Sensei Grid
Simple and lightweight data grid in JS/HTML
Stars: ✭ 808 (+296.08%)
Mutual labels:  underscore

Inflector

Release Code Quality Code Coverage Packagist

A multilingual inflector that transforms words from singular to plural, underscore to camel case, and formats strings in various ways. Inflections are localized, the default english inflections for pluralization, singularization, and uncountable words are kept in lib/inflections/en.php.

Inflections are currently available for the following languages:

  • English (en)
  • French (fr)
  • Norwegian Bokmal (nb)
  • Portuguese (pt)
  • Spanish (es)
  • Turkish (tr)

Usage

These are some examples of the inflector with the en locale (default).

<?php

use ICanBoogie\Inflector;

$inflector = Inflector::get(Inflector::DEFAULT_LOCALE);
# or
$inflector = Inflector::get('en');
# or
$inflector = Inflector::get();

# pluralize

$inflector->pluralize('post');                       // "posts"
$inflector->pluralize('child');                      // "children"
$inflector->pluralize('sheep');                      // "sheep"
$inflector->pluralize('words');                      // "words"
$inflector->pluralize('CamelChild');                 // "CamelChildren"

# singularize

$inflector->singularize('posts');                    // "post"
$inflector->singularize('children');                 // "child"
$inflector->singularize('sheep');                    // "sheep"
$inflector->singularize('word');                     // "word"
$inflector->singularize('CamelChildren');            // "CamelChild"

# camelize

$inflector->camelize('active_model', Inflector::UPCASE_FIRST_LETTER);
# or
$inflector->camelize('active_model');
// 'ActiveModel'

$inflector->camelize('active_model', Inflector::DOWNCASE_FIRST_LETTER);
// 'activeModel'

$inflector->camelize('active_model/errors');
// 'ActiveModel\Errors'

$inflector->camelize('active_model/errors', Inflector::DOWNCASE_FIRST_LETTER);
// 'activeModel\Errors'

# underscore

$inflector->underscore('ActiveModel');               // 'active_model'
$inflector->underscore('ActiveModel\Errors');        // 'active_model/errors'
$inflector->underscore('Less Active Phrase');        // 'less_active_phrase'
$inflector->underscore('Number 1 Test');             // 'number_1_test'
$inflector->underscore('Johnny5 Still Alive');       // 'johnny5_still_alive'
$inflector->underscore('Lots   of   Spaces');        // 'lots_of_spaces'

# humanize

$inflector->humanize('employee_salary');             // "Employee salary"
$inflector->humanize('author_id');                   // "Author"

# titleize

$inflector->titleize('man from the boondocks');      // "Man From The Boondocks"
$inflector->titleize('x-men: the last stand');       // "X Men: The Last Stand"
$inflector->titleize('TheManWithoutAPast');          // "The Man Without A Past"
$inflector->titleize('raiders_of_the_lost_ark');     // "Raiders Of The Lost Ark"

# ordinal

$inflector->ordinal(1);                              // "st"
$inflector->ordinal(2);                              // "nd"
$inflector->ordinal(1002);                           // "nd"
$inflector->ordinal(1003);                           // "rd"
$inflector->ordinal(-11);                            // "th"
$inflector->ordinal(-1021);                          // "st"

# ordinalize

$inflector->ordinalize(1);                           // "1st"
$inflector->ordinalize(2);                           // "2nd"
$inflector->ordinalize(1002);                        // "1002nd"
$inflector->ordinalize(1003);                        // "1003rd"
$inflector->ordinalize(-11);                         // "-11th"
$inflector->ordinalize(-1021);                       // "-1021st"

# uncountable

$inflector->is_uncountable("advice");                // true
$inflector->is_uncountable("weather");               // true
$inflector->is_uncountable("cat");                   // false

Helpers makes it easy to use default locale inflections.

<?php

namespace ICanBoogie;

echo pluralize('child');                             // "children"
echo pluralize('genou', 'fr');                       // "genoux"
echo singularize('lærere', 'nb');                    // "lærer"
echo pluralize('üçgen', 'tr');                       // "üçgenler"

Acknowledgements

Most of the code and documentation was adapted from Ruby On Rails's Inflector and David Celis' inflections.

Significant differences:

  • Better support of accentuated characters.
  • The Ruby module separator :: as been replaced by the PHP namespace separator \.
  • The plural of "octopus" is "octopuses" (not "octopi"), the plural of "virus" is "viruses" (not viri) and the pural of "cow" is "cows" (not "kine").
  • The following methods have been removed: tableize, classify, demodulize, constantize, deconstantize and foreign_key. They can be easily implemented in specific inflectors.
  • Added the hyphenate method, which is a combination of underscore and dasherize.
  • One specifies true rather than false to camelize() to downcase the first letter of the camel cased string.

Getting started

Inflector expects to work in UTF-8, which is the default encoding character set starting PHP 5.6, for older versions please use mb_internal_encoding() as follows:

<?php

namespace ICanBoogie;

// …

mb_internal_encoding('UTF-8');

titleize("été_aux_âmes_inouïes"); // Été Aux Âmes Inouïes

Requirement

The package requires PHP 7.1 or later.

Installation

The recommended way to install this package is through Composer:

$ composer require icanboogie/inflector

Documentation

The package is documented as part of the ICanBoogie framework documentation. The documentation for the package is generated with the make doc command. The documentation is generated in the build/docs directory using ApiGen. The package directory can later by cleaned with the make clean command.

Testing

The test suite is run with the make test command. Composer is automatically installed as well as all the dependencies required to run the suite. It is recommended to run make test in the test container created with make test-container.

The package is continuously tested with GitHub Actions.

License

icanboogie/inflector is licensed under the New BSD License - See the LICENSE file for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].