パッケージの詳細

inflection

dreamerslab13.6mMIT3.0.2

A port of inflection-js to node.js module

inflection, inflections, inflection-js, pluralize

readme

inflection

A package to transform english strings into other forms like the plural form, singular form, camelCase form, etc.

NPM Version

Description

This package was originally a port of inflection-js, which is a port of the functionality from Ruby on Rails' Active Support Inflection classes into Javascript.

Note: This library uses Wiktionary as its reference.

Requires

Checkout package.json for dependencies.

Angular Support

Checkout ngInflection from konsumer

Meteor Support

Checkout Meteor Inflector from Veaceslav Cotruta

Installation

Install inflection through npm

npm install inflection

API

  • inflection.pluralize( str, plural );
  • inflection.singularize( str, singular );
  • inflection.inflect( str, count, singular, plural );
  • inflection.camelize( str, low_first_letter );
  • inflection.underscore( str, all_upper_case );
  • inflection.humanize( str, low_first_letter );
  • inflection.capitalize( str );
  • inflection.dasherize( str );
  • inflection.titleize( str );
  • inflection.demodulize( str );
  • inflection.tableize( str );
  • inflection.classify( str );
  • inflection.foreign_key( str, drop_id_ubar );
  • inflection.ordinalize( str );
  • inflection.transform( str, arr );

Usage

Require the module before using

const inflection = require( 'inflection' );

inflection.pluralize( str, plural );

This function adds pluralization support to every String object.

Arguments

str

type: String
desc: The subject string.

plural

type: String
desc: Overrides normal output with said String.(optional)

Example code

var inflection = require( 'inflection' );

inflection.pluralize( 'person' ); // === 'people'
inflection.pluralize( 'octopus' ); // === "octopi"
inflection.pluralize( 'Hat' ); // === 'Hats'
inflection.pluralize( 'person', 'guys' ); // === 'guys'

inflection.singularize( str, singular );

This function adds singularization support to every String object.

Arguments

str

type: String
desc: The subject string.

singular

type: String
desc: Overrides normal output with said String.(optional)

Example code

var inflection = require( 'inflection' );

inflection.singularize( 'people' ); // === 'person'
inflection.singularize( 'octopi' ); // === "octopus"
inflection.singularize( 'Hats' ); // === 'Hat'
inflection.singularize( 'guys', 'person' ); // === 'person'

inflection.inflect( str, count, singular, plural );

This function will pluralize or singularlize a String appropriately based on an integer value.

Arguments

str

type: String
desc: The subject string.

count

type: Number
desc: The number to base pluralization off of.

singular

type: String
desc: Overrides normal output with said String.(optional)

plural

type: String
desc: Overrides normal output with said String.(optional)

Example code

    var inflection = require( 'inflection' );

    inflection.inflect( 'people', 1 ); // === 'person'
    inflection.inflect( 'octopi', 1 ); // === 'octopus'
    inflection.inflect( 'Hats', 1 ); // === 'Hat'
    inflection.inflect( 'guys', 1 , 'person' ); // === 'person'
    inflection.inflect( 'person', 2 ); // === 'people'
    inflection.inflect( 'octopus', 2 ); // === 'octopi'
    inflection.inflect( 'Hat', 2 ); // === 'Hats'
    inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'

inflection.camelize( str, low_first_letter );

This function transforms String object from underscore to camelcase.

Arguments

str

type: String
desc: The subject string.

low_first_letter

type: Boolean
desc: Default is to capitalize the first letter of the results. Passing true will lowercase it. (optional)

Example code

var inflection = require( 'inflection' );

inflection.camelize( 'message_properties' ); // === 'MessageProperties'
inflection.camelize( 'message_properties', true ); // === 'messageProperties'

inflection.underscore( str, all_upper_case );

This function transforms String object from camelcase to underscore.

Arguments

str

type: String
desc: The subject string.

all_upper_case

type: Boolean
desc: Default is to lowercase and add underscore prefix

Example code

var inflection = require( 'inflection' );

inflection.underscore( 'MessageProperties' ); // === 'message_properties'
inflection.underscore( 'messageProperties' ); // === 'message_properties'
inflection.underscore( 'MP' ); // === 'm_p'
inflection.underscore( 'MP', true ); // === 'MP'

inflection.humanize( str, low_first_letter );

This function adds humanize support to every String object.

Arguments

str

type: String
desc: The subject string.

low_first_letter

type: Boolean
desc: Default is to capitalize the first letter of the results. Passing true will lowercase it. (optional)

Example code

var inflection = require( 'inflection' );

inflection.humanize( 'message_properties' ); // === 'Message properties'
inflection.humanize( 'message_properties', true ); // === 'message properties'

inflection.capitalize( str );

This function adds capitalization support to every String object.

Arguments

str

type: String
desc: The subject string.

Example code

var inflection = require( 'inflection' );

inflection.capitalize( 'message_properties' ); // === 'Message_properties'
inflection.capitalize( 'message properties', true ); // === 'Message properties'

inflection.dasherize( str );

This function replaces underscores with dashes in the string.

Arguments

str

type: String
desc: The subject string.

Example code

var inflection = require( 'inflection' );

inflection.dasherize( 'message_properties' ); // === 'message-properties'
inflection.dasherize( 'Message Properties' ); // === 'Message-Properties'

inflection.titleize( str );

This function adds titleize support to every String object.

Arguments

str

type: String
desc: The subject string.

Example code

var inflection = require( 'inflection' );

inflection.titleize( 'message_properties' ); // === 'Message Properties'
inflection.titleize( 'message properties to keep' ); // === 'Message Properties to Keep'

inflection.demodulize( str );

This function adds demodulize support to every String object.

Arguments

str

type: String
desc: The subject string.

Example code

var inflection = require( 'inflection' );

inflection.demodulize( 'Message::Bus::Properties' ); // === 'Properties'

inflection.tableize( str );

This function adds tableize support to every String object.

Arguments

str

type: String
desc: The subject string.

Example code

var inflection = require( 'inflection' );

inflection.tableize( 'MessageBusProperty' ); // === 'message_bus_properties'

inflection.classify( str );

This function adds classification support to every String object.

Arguments

str

type: String
desc: The subject string.

Example code

var inflection = require( 'inflection' );

inflection.classify( 'message_bus_properties' ); // === 'MessageBusProperty'

inflection.foreign_key( str, drop_id_ubar );

This function adds foreign key support to every String object.

Arguments

str

type: String
desc: The subject string.

low_first_letter

type: Boolean
desc: Default is to seperate id with an underbar at the end of the class name, you can pass true to skip it.(optional)

Example code

var inflection = require( 'inflection' );

inflection.foreign_key( 'MessageBusProperty' ); // === 'message_bus_property_id'
inflection.foreign_key( 'MessageBusProperty', true ); // === 'message_bus_propertyid'

inflection.ordinalize( str );

This function adds ordinalize support to every String object.

Arguments

str

type: String
desc: The subject string.

Example code

var inflection = require( 'inflection' );

inflection.ordinalize( 'the 1 pitch' ); // === 'the 1st pitch'

inflection.transform( str, arr );

This function performs multiple inflection methods on a string.

Arguments

str

type: String
desc: The subject string.

arr

type: Array
desc: An array of inflection methods.

Example code

var inflection = require( 'inflection' );

inflection.transform( 'all job', [ 'pluralize', 'capitalize', 'dasherize' ]); // === 'All-jobs'

Credit

License

(The MIT License)

Copyright (c) 2011 dreamerslab <ben@dreamerslab.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

更新履歴

History

1.13.3 and later

Please see the GitHub Releases for release notes.

1.13.2 / 2022-01-29

  • [fix] drives singular form
  • [feat] allow inflect to use floats
  • [chore] upgrade packages

1.13.1 / 2021-05-21

  • [fix] use correct version for inflector.version
  • [build] reduce npm bundle size by excluding more files
  • [build] use terser to create a minified file

1.13.0 / 2021-05-01

  • [update packages] mocha->8.3.2, should->13.2.3
  • [bug fix] grammar plural form
  • [bug fix] bonus plural form
  • [bug fix] octopus plural form
  • [bug fix] virus plural form
  • [info] add LICENSE file
  • [info] additional maintainer @p-kuen. @ben-lin thanks for your trust!

1.12.0 / 2017-01-27

  • [update packages] mocha->3.2.0, should->11.2.0
  • [bug fix] minus plural & singular form
  • [bug fix] save plural & singular form

1.11.0 / 2016-04-20

  • [update packages] mocha->3.0.2, should->11.1.0
  • [bug fix] inflection.transform in ES6

1.10.0 / 2016-04-20

  • [update packages] should->8.3.1
  • [bug fix] campus plural & singular form

1.9.0 / 2016-04-06

  • [update packages] mocha->2.4.5, should->8.3.0
  • [bug fix] genus plural & singular form

1.8.0 / 2015-11-22

  • [update packages] mocha->2.3.4, should->7.1.1
  • [bug fix] criterion plural & singular form

1.7.2 / 2015-10-11

  • [update packages] mocha->2.3.3, should->7.1.0

1.7.1 / 2015-03-25

  • [bug fix] woman plural & singular form

1.7.0 / 2015-03-25

  • [bug fix] canvas plural & singular form
  • [update packages] mocha->2.2.1, should->5.2.0

1.6.0 / 2014-12-06

  • [bug fix] Special rules for index, vertex, and matrix masked by general rule x
  • [update packages] mocha->2.1.0, should->4.6.5

1.5.3 / 2014-12-06

  • [bug fix] Remove invalid uncountable words

1.5.2 / 2014-11-14

  • [bug fix] business & access plural form

1.5.1 / 2014-09-23

  • [bug fix] Fix whereas plural & singular form

1.5.0 / 2014-09-23

  • [refactor] Add more rules and uncountable nouns

1.4.2 / 2014-09-05

  • [bug fix] Fix wrong implementation of goose, tooth & foot

1.4.1 / 2014-08-31

  • [bug fix] Fix goose, tooth & foot plural & singular form

1.4.0 / 2014-08-23

  • [new feature] Adds support for an inflect method that will choose to pluralize or singularize a noun based on an integer value

1.3.8 / 2014-07-03

  • [others] Syntax tuning

1.3.7 / 2014-06-25

  • [refactor] Adopt UMD import to work in a variety of different situations
  • [update packages] should->4.0.4

1.3.6 / 2014-06-07

  • [bug fix] Rearrange rules. movies->movy

1.3.5 / 2014-02-12

  • Unable to publsih v1.3.4 therefore jump to v1.3.5

1.3.4 / 2014-02-12

  • [update packages] should->3.1.2
  • [refactor] Use mocha instead of hard coding tests

1.3.3 / 2014-01-22

  • [update packages] should->3.0.1
  • Added brower.json

1.3.2 / 2013-12-12

  • [update packages] node.flow->1.2.3

1.3.1 / 2013-12-12

  • [refactor] Support Requirejs

1.3.0 / 2013-12-11

  • [refactor] Move var out of loops
  • [refactor] Change the way camelize acts to mimic 100% Rails ActiveSupport Inflector camelize

1.2.7 / 2013-12-11

  • [new feature] Added transform, thnaks to luk3thomas
  • [update packages] should->v2.1.1

1.2.6 / 2013-05-24

  • [bug fix] Use instance instead of this

1.2.5 / 2013-01-09

  • [refactor] Allow all caps strings to be returned from underscore

1.2.4 / 2013-01-06

  • [bug fix] window obj does not have call method

1.2.3 / 2012-08-02

  • [bug fix] Singularize status produces statu
  • [update packages] should->v1.1.0

1.2.2 / 2012-07-23

  • [update packages] node.flow->v1.1.3 & should->v1.0.0

1.2.1 / 2012-06-22

  • [bug fix] Singularize address produces addres

1.2.0 / 2012-04-10

  • [new feature] Browser support
  • [update packages] node.flow->v1.1.1

1.1.1 / 2012-02-13

  • [update packages] node.flow->v1.1.0

1.1.0 / 2012-02-13

  • [update packages] node.flow->v1.0.0
  • [refactor] Read version number from package.json

1.0.0 / 2012-02-08

  • Remove make file
  • Add pluralize rules
  • Add pluralize tests
  • [refactor] Use object.jey instead of for in

0.0.1 / 2012-01-16

  • Initial release