JavaScript pluralize an english string

前端 未结 11 1005
南旧
南旧 2020-12-23 16:24

In PHP, I use Kuwamoto\'s class to pluralize nouns in my strings. I didn\'t find something as good as this script in javascript except for some plugins. So, it would be grea

相关标签:
11条回答
  • 2020-12-23 17:03

    The new intl API spec from ECMA will provide the plural rules function, https://github.com/tc39/proposal-intl-plural-rules

    Here's the polyfill that can be used today https://github.com/eemeli/IntlPluralRules

    0 讨论(0)
  • 2020-12-23 17:04

    Use Pluralize

    There's a great little library called Pluralize that's packaged in npm and bower.

    This is what it looks like to use:

    import Pluralize from 'pluralize';
    
    Pluralize( 'Towel', 42 );       // "Towels"
    
    Pluralize( 'Towel', 42, true ); // "42 Towels"
    

    And you can get it here:

    https://github.com/blakeembrey/pluralize

    0 讨论(0)
  • 2020-12-23 17:05

    Taken from my blog: https://sergiotapia.me/pluralizing-strings-in-javascript-es6-b5d4d651d403


    You can use the pluralize library for this.

    NPM:
    npm install pluralize --save
    
    Yarn:
    yarn add pluralize
    

    Wherever you want to use the lib, you can require it easily.

    var pluralize = require('pluralize')
    

    I like to add it to the window object so I can just invoke pluralize() wherever I need it. Within my application.js root file:

    window.pluralize = require('pluralize')
    

    Then you can just use it anywhere, React components, or just plain Javascript:

    <span className="pull-left">
      {`${item.score} ${pluralize('point', item.score)}`}
    </span>
    
    console.log(pluralize('point', item.score))
    
    0 讨论(0)
  • 2020-12-23 17:05
    function pluralize( /* n, [ n2, n3, ... ] str */ ) {
        var n = Array.prototype.slice.call( arguments ) ;
        var str = n.pop(), iMax = n.length - 1, i = -1, j ;
        str = str.replace( /\$\$|\$(\d+)/g,
            function( m, p1 ) { return m == '$$' ? '$' : n[+p1-1] }
        ) ;
        return str.replace( /[(](.*?)([+-])(\d*)(?:,([^,)]*))?(?:,([^)]*))?[)]/g,
            function( match, one, sign, abs, not1, zero ) {
                // if abs, use indicated element in the array of numbers
                // instead of using the next element in sequence
                abs ? ( j = +abs - 1 ) : ( i < iMax && i++, j = i ) ;
                if ( zero != undefined && n[j] == 0 ) return zero ;
                return ( n[j] != 1 ) == ( sign == '+' ) ? ( not1 || 's' ) : one ;
            }
        ) ;  
    }
    
    console.log( pluralize( 1, 'the cat(+) live(-) outside' ) ) ;
    // the cat lives outside
    console.log( pluralize( 2, 'the child(+,ren) (is+,are) inside' ) ) ;
    // the children are inside
    console.log( pluralize( 0, '$1 dog(+), ($1+,$1,no) dog(+), ($1+,$1,no) dog(+,,)' ) ) ;
    // 0 dogs, no dogs, no dog
    console.log( pluralize( 100, 1, '$1 penn(y+,ies) make(-1) $$$2' ) ) ;
    // 100 pennies make $1
    console.log( pluralize( 1, 0.01, '$1 penn(y+,ies) make(-1) $$$2' ) ) ;
    // 1 penny makes $0.01
    
    0 讨论(0)
  • 2020-12-23 17:09

    Simple version (ES6):

    const maybePluralize = (count, noun, suffix = 's') =>
      `${count} ${noun}${count !== 1 ? suffix : ''}`;
    

    Usage:

    maybePluralize(0, 'turtle'); // 0 turtles
    maybePluralize(1, 'turtle'); // 1 turtle
    maybePluralize(2, 'turtle'); // 2 turtles
    maybePluralize(3, 'fox', 'es'); // 3 foxes
    

    This obviously doesn't support all english edge-cases, but it's suitable for most purposes

    0 讨论(0)
提交回复
热议问题