UNPKG

9.89 kBMarkdownView Raw
1# inflected
2
3[![Travis build status](https://img.shields.io/travis/martinandert/inflected/master.svg)](https://travis-ci.org/martinandert/inflected)
4[![npm downloads](https://img.shields.io/npm/dm/inflected.svg)](https://npmjs.com/package/inflected)
5[![no dependencies](https://img.shields.io/badge/dependencies-none-brightgreen.svg)](https://npmjs.com/package/inflected)
6[![license](https://img.shields.io/github/license/martinandert/inflected.svg)](https://github.com/martinandert/inflected/blob/master/LICENSE.txt)
7
8A port of ActiveSupport's inflector to Node.js. Also usable in the browser.
9
10
11## Installation
12
13Install via npm:
14
15```bash
16% npm install inflected
17```
18
19Or via yarn:
20
21```bash
22% yarn add inflected
23```
24
25The UMD build is also available on [unpkg](https://unpkg.com/), adding a `Inflector` object to the global scope.
26
27```html
28<script src="https://unpkg.com/inflected/dist/umd/inflected.min.js"></script>
29```
30
31
32## Usage
33
34The module exports an object with several utility functions.
35
36```js
37var Inflector = require('inflected');
38
39Inflector.pluralize('Category') // => 'Categories'
40```
41
42If using ES modules, you can cherry-pick only the functions you're interested in:
43
44```js
45import { pluralize } from 'inflected';
46
47pluralize('Category') // => 'Categories'
48```
49
50Here is the complete API reference:
51
52
53### Inflector.pluralize
54
55```js
56string pluralize(string word[, string locale])
57```
58
59Returns the plural form of the word in the string.
60
61If passed an optional `locale` parameter, the word will be pluralized using rules defined for that language. By default, this parameter is set to "en".
62
63```js
64Inflector.pluralize('post') // => 'posts'
65Inflector.pluralize('octopus') // => 'octopi'
66Inflector.pluralize('sheep') // => 'sheep'
67Inflector.pluralize('words') // => 'words'
68Inflector.pluralize('CamelOctopus') // => 'CamelOctopi'
69Inflector.pluralize('ley', 'es') // => 'leyes'
70```
71
72
73### Inflector.singularize
74
75```js
76string singularize(string word[, string locale])
77```
78
79The reverse of `pluralize`, returns the singular form of a word in a string.
80
81If passed an optional `locale` parameter, the word will be singularized using rules defined for that language. By default, this parameter is set to "en".
82
83```js
84Inflector.singularize('posts') // => 'post'
85Inflector.singularize('octopi') // => 'octopus'
86Inflector.singularize('sheep') // => 'sheep'
87Inflector.singularize('word') // => 'word'
88Inflector.singularize('CamelOctopi') // => 'CamelOctopus'
89Inflector.singularize('leyes', 'es') // => 'ley'
90```
91
92
93### Inflector.camelize
94
95```js
96string camelize(string term[, boolean uppercaseFirstLetter])
97```
98
99By default, `camelize` converts strings to UpperCamelCase. If the second argument is set to `false` then `camelize` produces lowerCamelCase.
100
101```js
102Inflector.camelize('foo_bar') // => 'FooBar'
103Inflector.camelize('foo_bar', false) // => 'fooBar'
104```
105
106As a rule of thumb you can think of `camelize` as the inverse of `underscore`, though there are cases where that does not hold:
107
108```js
109Inflector.camelize(Inflector.underscore('SSLError')) // => 'SslError'
110```
111
112### Inflector.underscore
113
114```js
115string underscore(string camelCasedWord)
116```
117
118Makes an underscored, lowercase form from the expression in the string.
119
120```js
121Inflector.underscore('FooBar') // => 'foo_bar'
122```
123
124As a rule of thumb you can think of `underscore` as the inverse of `camelize`, though there are cases where that does not hold:
125
126```js
127Inflector.camelize(Inflector.underscore('SSLError')) // => 'SslError'
128```
129
130
131### Inflector.humanize
132
133```js
134string humanize(string lowerCaseAndUnderscoredWord[, object options])
135```
136
137Capitalizes the first word, turns underscores into spaces, and strips a trailing "_id" if present.
138
139Like `titleize`, this is meant for creating pretty output.
140
141The capitalization of the first word can be turned off by setting the `capitalize` option key to `false`. By default, this option is `true`.
142
143```js
144Inflector.humanize('employee_salary') // => 'Employee salary'
145Inflector.humanize('author_id') // => 'Author'
146Inflector.humanize('author_id', { capitalize: false }) // => 'author'
147```
148
149
150### Inflector.titleize
151
152```js
153string titleize(string sentence)
154```
155
156Capitalizes all the words and replaces some characters in the string to create a nicer looking title. `titleize` is meant for creating pretty output.
157
158```js
159Inflector.titleize('man from the boondocks') // => 'Man From The Boondocks'
160Inflector.titleize('x-men: the last stand') // => 'X Men: The Last Stand'
161Inflector.titleize('TheManWithoutAPast') // => 'The Man Without A Past'
162Inflector.titleize('raiders_of_the_lost_ark') // => 'Raiders Of The Lost Ark'
163```
164
165
166### Inflector.tableize
167
168```js
169string tableize(string className)
170```
171
172Create the name of a table like Rails does for models to table names. This method uses the `pluralize` method on the last word in the string.
173
174```js
175Inflector.tableize('RawScaledScorer') // => 'raw_scaled_scorers'
176Inflector.tableize('egg_and_ham') // => 'egg_and_hams'
177Inflector.tableize('fancyCategory') // => 'fancy_categories'
178```
179
180
181### Inflector.classify
182
183```js
184string classify(string tableName)
185```
186
187Create a class name from a plural table name like Rails does for table names to models.
188
189```js
190Inflector.classify('egg_and_hams') // => 'EggAndHam'
191Inflector.classify('posts') // => 'Post'
192```
193
194Singular names are not handled correctly:
195
196```js
197Inflector.classify('business') // => 'Busines'
198```
199
200
201### Inflector.dasherize
202
203```js
204string dasherize(string underscoredWord)
205```
206
207Replaces underscores with dashes in the string.
208
209```js
210Inflector.dasherize('puni_puni') // => 'puni-puni'
211```
212
213
214### Inflector.foreignKey
215
216```js
217string foreignKey(string className[, boolean separateClassNameAndIdWithUnderscore])
218```
219
220Creates a foreign key name from a class name. `separateClassNameAndIdWithUnderscore` sets whether the method should put "_" between the name and "id" (default: `true`).
221
222```js
223Inflector.foreignKey('Message') // => 'message_id'
224Inflector.foreignKey('Message', false) // => 'messageid'
225```
226
227
228### Inflector.ordinal
229
230```js
231string ordinal(object number)
232```
233
234Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
235
236```js
237Inflector.ordinal(1) // => 'st'
238Inflector.ordinal(2) // => 'nd'
239Inflector.ordinal(1002) // => 'nd'
240Inflector.ordinal(1003) // => 'rd'
241Inflector.ordinal(-11) // => 'th'
242Inflector.ordinal(-1021) // => 'st'
243```
244
245
246### Inflector.ordinalize
247
248```js
249string ordinalize(object number)
250```
251
252Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
253
254```js
255Inflector.ordinalize(1) // => '1st'
256Inflector.ordinalize(2) // => '2nd'
257Inflector.ordinalize(1002) // => '1002nd'
258Inflector.ordinalize(1003) // => '1003rd'
259Inflector.ordinalize(-11) // => '-11th'
260Inflector.ordinalize(-1021) // => '-1021st'
261```
262
263
264### Inflector.inflections
265
266```js
267Inflections inflections([string locale])
268inflections([string locale], [function(Inflections) fn])
269```
270
271A singleton instance of the internal Inflections class is yielded by this function, which can then be used to specify additional inflection rules. If passed an optional locale, rules for other languages can be specified. The default locale is "en". Only rules for English are provided by this library.
272
273```js
274Inflector.inflections('en', function(inflect) {
275 inflect.plural(/^(ox)$/i, '$1$2en');
276 inflect.singular /^(ox)en/i, '$1');
277
278 inflect.irregular('octopus', 'octopi');
279
280 inflect.uncountable('equipment', 'snow');
281});
282```
283
284New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the pluralization and singularization rules that is run. This guarantees that your rules run before any of the rules that may already have been loaded.
285
286
287### Inflector.transliterate
288
289```js
290string transliterate(string sentence[, object options])
291```
292
293Replaces non-ASCII characters with an ASCII approximation, or if none exists, a replacement character which defaults to "?".
294
295```js
296Inflector.transliterate('Ærøskøbing') // => 'AEroskobing'
297```
298
299Default approximations are provided for Western/Latin characters, e.g, "ø", "ñ", "é", "ß", etc.
300
301This method is I18n-aware, so you can set up custom approximations for a locale. This can be useful, for example, to transliterate German's "ü" and "ö" to "ue" and "oe", or to add support for transliterating Russian to ASCII.
302
303In order to make your custom transliterations available, you must set them using the `approximate` helper function:
304
305```js
306Inflector.transliterations('de', function(t) {
307 t.approximate('ü', 'ue');
308 t.approximate('ö', 'oe');
309});
310```
311
312Now you can have different transliterations for each locale:
313
314```js
315Inflector.transliterate('Jürgen') // => 'Jurgen'
316Inflector.transliterate('Jürgen', { locale: 'de' }) // => 'Juergen'
317```
318
319
320### Inflector.parameterize
321
322```js
323string parameterize(string sentence[, object options])
324```
325
326Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
327
328```js
329Inflector.parameterize('Donald E. Knuth') // => 'donald-e-knuth'
330Inflector.parameterize('Donald E. Knuth', { separator: '+' }) // => 'donald+e+knuth'
331```
332
333
334## Contributing
335
336Here's a quick guide:
337
3381. Fork the repo and `make install`.
3392. Run the tests. We only take pull requests with passing tests, and it's great to know that you have a clean slate: `make test`.
3403. Add a test for your change. Only refactoring and documentation changes require no new tests. If you are adding functionality or are fixing a bug, we need a test!
3414. Make the test pass.
3425. Push to your fork and submit a pull request.
343
344
345## Licence
346
347Released under The MIT License.