├── .babelrc
├── .gitignore
├── .npmignore
├── LICENSE
├── README.md
├── package-lock.json
├── package.json
└── src
├── Pluralize.js
├── index.js
└── utils
├── index.js
├── utils.js
└── utils.spec.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["env", "react"],
3 | "plugins": [
4 | "transform-object-rest-spread"
5 | ]
6 | }
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | dist
2 | node_modules
3 |
4 | npm-debug.log
5 | webpack.config.js
6 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | src
3 | .babelrc
4 | .gitignore
5 | webpack.config.js
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Tom Smith
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React Pluralize
2 | A tiny pluralization component. Also check out [react-since](https://www.npmjs.com/package/react-since), another tiny component that presents time passed in a humanistic format.
3 |
4 | ## Install
5 | npm install react-pluralize --save
6 |
7 | ## Import
8 | import Pluralize from 'react-pluralize'
9 |
10 | ## Use
11 | => 3 views
12 | => 3 people
13 | => like
14 | => 0 clicks
15 | => Nothing to show
16 |
17 | ## Props
18 |
19 | **singular** (required)(String)
20 | The singular form of the noun
21 |
22 | **plural** (optional)(default: singular + 's')(String)
23 | The plural form of the noun if required (i.e. when the plural form isn't just 's' added to the end)
24 |
25 | **count** (optional)(default: 1)(Number)
26 | The count value used to determine whether the singular or plural form should be used.
27 |
28 | **showCount** (optional)(default: true)(Boolean)
29 | If you would prefer not to see the count in the output then set this prop to false.
30 |
31 | **zero** (optional)(default: null)(String)
32 | If you would like to show a different message when the count is 0 you can provide this prop.
33 |
34 | **className** (optional)(default: null)(String)
35 |
36 | **style** (optional)(default: {})(Object)
37 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-pluralize",
3 | "version": "1.6.3",
4 | "description": "Render nouns in singular or plural form",
5 | "main": "dist/index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1",
8 | "build": "webpack --config ./webpack.config.js",
9 | "prepublishOnly": "rm -rf ./dist && npm run build"
10 | },
11 | "author": "Tom Smith",
12 | "license": "MIT",
13 | "repository": {
14 | "type": "git",
15 | "url": "https://github.com/tsmith123/react-pluralize"
16 | },
17 | "peerDependencies": {
18 | "prop-types": "^15.6.2",
19 | "react": "^16.3.0"
20 | },
21 | "devDependencies": {
22 | "babel-cli": "^6.26.0",
23 | "babel-core": "^6.26.0",
24 | "babel-eslint": "^10.0.1",
25 | "babel-loader": "^7.1.4",
26 | "babel-plugin-transform-object-rest-spread": "^6.26.0",
27 | "babel-preset-env": "^1.6.1",
28 | "babel-preset-react": "^6.24.1",
29 | "jest": "^23.6.0",
30 | "prop-types": "^15.6.2",
31 | "react": "^16.3.0",
32 | "standard": "^12.0.1",
33 | "webpack": "^4.5.0",
34 | "webpack-cli": "^3.3.2"
35 | },
36 | "standard": {
37 | "parser": "babel-eslint",
38 | "globals": [
39 | "jest",
40 | "describe",
41 | "test",
42 | "expect"
43 | ]
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/Pluralize.js:
--------------------------------------------------------------------------------
1 | /*
2 | * React Pluralize (MIT)
3 | * Tom Smith (https://github.com/tsmith123)
4 | */
5 |
6 | import React from 'react'
7 | import PropTypes from 'prop-types'
8 | import { pluralize } from './utils'
9 |
10 | const Plural = ({ className, style, ...props }) => (
11 |
12 | {pluralize(props)}
13 |
14 | )
15 |
16 | Plural.propTypes = {
17 | singular: PropTypes.string.isRequired,
18 | plural: PropTypes.string,
19 | count: PropTypes.number,
20 | showCount: PropTypes.bool,
21 | className: PropTypes.string,
22 | style: PropTypes.object,
23 | zero: PropTypes.string
24 | }
25 |
26 | Plural.defaultProps = {
27 | count: 1,
28 | showCount: true,
29 | className: null,
30 | style: {},
31 | zero: null
32 | }
33 |
34 | export default Plural
35 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | export { default } from './Pluralize'
2 |
--------------------------------------------------------------------------------
/src/utils/index.js:
--------------------------------------------------------------------------------
1 | import { pluralize } from './utils'
2 |
3 | export {
4 | pluralize
5 | }
6 |
--------------------------------------------------------------------------------
/src/utils/utils.js:
--------------------------------------------------------------------------------
1 | export const pluralize = ({ singular, plural, count, showCount, zero }) => {
2 | if (count === 0 && zero) return zero
3 |
4 | let output = singular
5 | if (count !== 1) {
6 | output = plural || `${singular}s`
7 | }
8 |
9 | return showCount ? `${count} ${output}` : output
10 | }
11 |
--------------------------------------------------------------------------------
/src/utils/utils.spec.js:
--------------------------------------------------------------------------------
1 | import { pluralize } from './utils'
2 |
3 | describe('pluralize function correctly pluralizes strings', () => {
4 | test('a regular verb is pluralized', () => {
5 | const singular = 'hit'
6 | const result = pluralize({ singular })
7 | expect(result).toBe('hits')
8 | })
9 |
10 | test('an irregular verb is pluralized', () => {
11 | const singular = 'person'
12 | const plural = 'people'
13 | const result = pluralize({ singular, plural })
14 | expect(result).toBe('people')
15 | })
16 |
17 | test('count is visible when showCount is provided', () => {
18 | const singular = 'cat'
19 | const count = 3
20 | const showCount = true
21 | const result = pluralize({ singular, count, showCount })
22 | expect(result).toBe(`${count} cats`)
23 | })
24 | })
25 |
--------------------------------------------------------------------------------