1 | # inflection
|
2 |
|
3 | A port of inflection-js to node.js module
|
4 |
|
5 |
|
6 |
|
7 | ## Description
|
8 | [inflection-js](http://code.google.com/p/inflection-js/) is a port of the functionality from Ruby on Rails' Active Support Inflection classes into Javascript. `inflection` is a port of `inflection-js` to node.js npm package. Instead of [extending JavaScript native](http://wonko.com/post/extending-javascript-natives) String object like `inflection-js` does, `inflection` separate the methods to a independent package to avoid unexpected behaviors.
|
9 |
|
10 |
|
11 |
|
12 | ## Requires
|
13 |
|
14 | Checkout `package.json` for dependencies.
|
15 |
|
16 |
|
17 |
|
18 | ## Angular Support
|
19 |
|
20 | Checkout [ngInflection](https://github.com/konsumer/ngInflection) from [konsumer](https://github.com/konsumer)
|
21 |
|
22 |
|
23 |
|
24 | ## Installation
|
25 |
|
26 | Install inflection through npm
|
27 |
|
28 | npm install inflection
|
29 |
|
30 |
|
31 |
|
32 | ## API
|
33 |
|
34 | - inflection.indexOf( arr, item, from_index, compare_func );
|
35 | - inflection.pluralize( str, plural );
|
36 | - inflection.singularize( str, singular );
|
37 | - inflection.inflect( str, count, singular, plural );
|
38 | - inflection.camelize( str, low_first_letter );
|
39 | - inflection.underscore( str, all_upper_case );
|
40 | - inflection.humanize( str, low_first_letter );
|
41 | - inflection.capitalize( str );
|
42 | - inflection.dasherize( str );
|
43 | - inflection.titleize( str );
|
44 | - inflection.demodulize( str );
|
45 | - inflection.tableize( str );
|
46 | - inflection.classify( str );
|
47 | - inflection.foreign_key( str, drop_id_ubar );
|
48 | - inflection.ordinalize( str );
|
49 | - inflection.transform( str, arr );
|
50 |
|
51 |
|
52 |
|
53 | ## Usage
|
54 |
|
55 | > Require the module before using
|
56 |
|
57 | var inflection = require( 'inflection' );
|
58 |
|
59 |
|
60 |
|
61 | ### inflection.indexOf( arr, item, from_index, compare_func );
|
62 |
|
63 | This lets us detect if an Array contains a given element.
|
64 |
|
65 | #### Arguments
|
66 |
|
67 | > arr
|
68 |
|
69 | type: Array
|
70 | desc: The subject array.
|
71 |
|
72 | > item
|
73 |
|
74 | type: Object
|
75 | desc: Object to locate in the Array.
|
76 |
|
77 | > from_index
|
78 |
|
79 | type: Number
|
80 | desc: Starts checking from this position in the Array.(optional)
|
81 |
|
82 | > compare_func
|
83 |
|
84 | type: Function
|
85 | desc: Function used to compare Array item vs passed item.(optional)
|
86 |
|
87 | #### Example code
|
88 |
|
89 | var inflection = require( 'inflection' );
|
90 |
|
91 | inflection.indexOf([ 'hi','there' ], 'guys' ); // === -1
|
92 | inflection.indexOf([ 'hi','there' ], 'hi' ); // === 0
|
93 |
|
94 |
|
95 |
|
96 | ### inflection.pluralize( str, plural );
|
97 |
|
98 | This function adds pluralization support to every String object.
|
99 |
|
100 | #### Arguments
|
101 |
|
102 | > str
|
103 |
|
104 | type: String
|
105 | desc: The subject string.
|
106 |
|
107 | > plural
|
108 |
|
109 | type: String
|
110 | desc: Overrides normal output with said String.(optional)
|
111 |
|
112 | #### Example code
|
113 |
|
114 | var inflection = require( 'inflection' );
|
115 |
|
116 | inflection.pluralize( 'person' ); // === 'people'
|
117 | inflection.pluralize( 'octopus' ); // === "octopi"
|
118 | inflection.pluralize( 'Hat' ); // === 'Hats'
|
119 | inflection.pluralize( 'person', 'guys' ); // === 'guys'
|
120 |
|
121 |
|
122 |
|
123 | ### inflection.singularize( str, singular );
|
124 |
|
125 | This function adds singularization support to every String object.
|
126 |
|
127 | #### Arguments
|
128 |
|
129 | > str
|
130 |
|
131 | type: String
|
132 | desc: The subject string.
|
133 |
|
134 | > singular
|
135 |
|
136 | type: String
|
137 | desc: Overrides normal output with said String.(optional)
|
138 |
|
139 | #### Example code
|
140 |
|
141 | var inflection = require( 'inflection' );
|
142 |
|
143 | inflection.singularize( 'people' ); // === 'person'
|
144 | inflection.singularize( 'octopi' ); // === "octopus"
|
145 | inflection.singularize( 'Hats' ); // === 'Hat'
|
146 | inflection.singularize( 'guys', 'person' ); // === 'person'
|
147 |
|
148 |
|
149 |
|
150 | ### inflection.inflect( str, count, singular, plural );
|
151 |
|
152 | This function will pluralize or singularlize a String appropriately based on an integer value.
|
153 |
|
154 | #### Arguments
|
155 |
|
156 | > str
|
157 |
|
158 | type: String
|
159 | desc: The subject string.
|
160 |
|
161 | > count
|
162 | type: Number
|
163 | desc: The number to base pluralization off of.
|
164 |
|
165 | > singular
|
166 |
|
167 | type: String
|
168 | desc: Overrides normal output with said String.(optional)
|
169 |
|
170 | > plural
|
171 |
|
172 | type: String
|
173 | desc: Overrides normal output with said String.(optional)
|
174 |
|
175 | #### Example code
|
176 |
|
177 | var inflection = require( 'inflection' );
|
178 |
|
179 | inflection.inflect( 'people' 1 ); // === 'person'
|
180 | inflection.inflect( 'octopi' 1 ); // === 'octopus'
|
181 | inflection.inflect( 'Hats' 1 ); // === 'Hat'
|
182 | inflection.inflect( 'guys', 1 , 'person' ); // === 'person'
|
183 | inflection.inflect( 'person', 2 ); // === 'people'
|
184 | inflection.inflect( 'octopus', 2 ); // === 'octopi'
|
185 | inflection.inflect( 'Hat', 2 ); // === 'Hats'
|
186 | inflection.inflect( 'person', 2, null, 'guys' ); // === 'guys'
|
187 |
|
188 |
|
189 |
|
190 | ### inflection.camelize( str, low_first_letter );
|
191 |
|
192 | This function transforms String object from underscore to camelcase.
|
193 |
|
194 | #### Arguments
|
195 |
|
196 | > str
|
197 |
|
198 | type: String
|
199 | desc: The subject string.
|
200 |
|
201 | > low_first_letter
|
202 |
|
203 | type: Boolean
|
204 | desc: Default is to capitalize the first letter of the results. Passing true will lowercase it. (optional)
|
205 |
|
206 | #### Example code
|
207 |
|
208 | var inflection = require( 'inflection' );
|
209 |
|
210 | inflection.camelize( 'message_properties' ); // === 'MessageProperties'
|
211 | inflection.camelize( 'message_properties', true ); // === 'messageProperties'
|
212 |
|
213 |
|
214 |
|
215 | ### inflection.underscore( str, all_upper_case );
|
216 |
|
217 | This function transforms String object from camelcase to underscore.
|
218 |
|
219 | #### Arguments
|
220 |
|
221 | > str
|
222 |
|
223 | type: String
|
224 | desc: The subject string.
|
225 |
|
226 | > all_upper_case
|
227 |
|
228 | type: Boolean
|
229 | desc: Default is to lowercase and add underscore prefix
|
230 |
|
231 |
|
232 |
|
233 | #### Example code
|
234 |
|
235 | var inflection = require( 'inflection' );
|
236 |
|
237 | inflection.underscore( 'MessageProperties' ); // === 'message_properties'
|
238 | inflection.underscore( 'messageProperties' ); // === 'message_properties'
|
239 | inflection.underscore( 'MP' ); // === 'm_p'
|
240 | inflection.underscore( 'MP', true ); // === 'MP'
|
241 |
|
242 |
|
243 |
|
244 | ### inflection.humanize( str, low_first_letter );
|
245 |
|
246 | This function adds humanize support to every String object.
|
247 |
|
248 | #### Arguments
|
249 |
|
250 | > str
|
251 |
|
252 | type: String
|
253 | desc: The subject string.
|
254 |
|
255 | > low_first_letter
|
256 |
|
257 | type: Boolean
|
258 | desc: Default is to capitalize the first letter of the results. Passing true will lowercase it. (optional)
|
259 |
|
260 | #### Example code
|
261 |
|
262 | var inflection = require( 'inflection' );
|
263 |
|
264 | inflection.humanize( 'message_properties' ); // === 'Message properties'
|
265 | inflection.humanize( 'message_properties', true ); // === 'message properties'
|
266 |
|
267 |
|
268 |
|
269 | ### inflection.capitalize( str );
|
270 |
|
271 | This function adds capitalization support to every String object.
|
272 |
|
273 | #### Arguments
|
274 |
|
275 | > str
|
276 |
|
277 | type: String
|
278 | desc: The subject string.
|
279 |
|
280 | #### Example code
|
281 |
|
282 | var inflection = require( 'inflection' );
|
283 |
|
284 | inflection.capitalize( 'message_properties' ); // === 'Message_properties'
|
285 | inflection.capitalize( 'message properties', true ); // === 'Message properties'
|
286 |
|
287 |
|
288 |
|
289 | ### inflection.dasherize( str );
|
290 |
|
291 | This function replaces underscores with dashes in the string.
|
292 |
|
293 | #### Arguments
|
294 |
|
295 | > str
|
296 |
|
297 | type: String
|
298 | desc: The subject string.
|
299 |
|
300 | #### Example code
|
301 |
|
302 | var inflection = require( 'inflection' );
|
303 |
|
304 | inflection.dasherize( 'message_properties' ); // === 'message-properties'
|
305 | inflection.dasherize( 'Message Properties' ); // === 'Message-Properties'
|
306 |
|
307 |
|
308 |
|
309 | ### inflection.titleize( str );
|
310 |
|
311 | This function adds titleize support to every String object.
|
312 |
|
313 | #### Arguments
|
314 |
|
315 | > str
|
316 |
|
317 | type: String
|
318 | desc: The subject string.
|
319 |
|
320 | #### Example code
|
321 |
|
322 | var inflection = require( 'inflection' );
|
323 |
|
324 | inflection.titleize( 'message_properties' ); // === 'Message Properties'
|
325 | inflection.titleize( 'message properties to keep' ); // === 'Message Properties to Keep'
|
326 |
|
327 |
|
328 |
|
329 | ### inflection.demodulize( str );
|
330 |
|
331 | This function adds demodulize support to every String object.
|
332 |
|
333 | #### Arguments
|
334 |
|
335 | > str
|
336 |
|
337 | type: String
|
338 | desc: The subject string.
|
339 |
|
340 | #### Example code
|
341 |
|
342 | var inflection = require( 'inflection' );
|
343 |
|
344 | inflection.demodulize( 'Message::Bus::Properties' ); // === 'Properties'
|
345 |
|
346 |
|
347 |
|
348 | ### inflection.tableize( str );
|
349 |
|
350 | This function adds tableize support to every String object.
|
351 |
|
352 | #### Arguments
|
353 |
|
354 | > str
|
355 |
|
356 | type: String
|
357 | desc: The subject string.
|
358 |
|
359 | #### Example code
|
360 |
|
361 | var inflection = require( 'inflection' );
|
362 |
|
363 | inflection.tableize( 'MessageBusProperty' ); // === 'message_bus_properties'
|
364 |
|
365 |
|
366 |
|
367 | ### inflection.classify( str );
|
368 |
|
369 | This function adds classification support to every String object.
|
370 |
|
371 | #### Arguments
|
372 |
|
373 | > str
|
374 |
|
375 | type: String
|
376 | desc: The subject string.
|
377 |
|
378 | #### Example code
|
379 |
|
380 | var inflection = require( 'inflection' );
|
381 |
|
382 | inflection.classify( 'message_bus_properties' ); // === 'MessageBusProperty'
|
383 |
|
384 |
|
385 |
|
386 | ### inflection.foreign_key( str, drop_id_ubar );
|
387 |
|
388 | This function adds foreign key support to every String object.
|
389 |
|
390 | #### Arguments
|
391 |
|
392 | > str
|
393 |
|
394 | type: String
|
395 | desc: The subject string.
|
396 |
|
397 | > low_first_letter
|
398 |
|
399 | type: Boolean
|
400 | desc: Default is to seperate id with an underbar at the end of the class name, you can pass true to skip it.(optional)
|
401 |
|
402 | #### Example code
|
403 |
|
404 | var inflection = require( 'inflection' );
|
405 |
|
406 | inflection.foreign_key( 'MessageBusProperty' ); // === 'message_bus_property_id'
|
407 | inflection.foreign_key( 'MessageBusProperty', true ); // === 'message_bus_propertyid'
|
408 |
|
409 |
|
410 |
|
411 | ### inflection.ordinalize( str );
|
412 |
|
413 | This function adds ordinalize support to every String object.
|
414 |
|
415 | #### Arguments
|
416 |
|
417 | > str
|
418 |
|
419 | type: String
|
420 | desc: The subject string.
|
421 |
|
422 | #### Example code
|
423 |
|
424 | var inflection = require( 'inflection' );
|
425 |
|
426 | inflection.ordinalize( 'the 1 pitch' ); // === 'the 1st pitch'
|
427 |
|
428 |
|
429 |
|
430 | ### inflection.transform( str, arr );
|
431 |
|
432 | This function performs multiple inflection methods on a string.
|
433 |
|
434 | #### Arguments
|
435 |
|
436 | > str
|
437 |
|
438 | type: String
|
439 | desc: The subject string.
|
440 |
|
441 | > arr
|
442 |
|
443 | type: Array
|
444 | desc: An array of inflection methods.
|
445 |
|
446 | #### Example code
|
447 |
|
448 | var inflection = require( 'inflection' );
|
449 |
|
450 | inflection.transform( 'all job', [ 'pluralize', 'capitalize', 'dasherize' ]); // === 'All-jobs'
|
451 |
|
452 |
|
453 |
|
454 | ## Credit
|
455 |
|
456 | - Ryan Schuft <ryan.schuft@gmail.com>
|
457 | - Lance Pollard <lancejpollard@gmail.com> (Browser support)
|
458 | - Dane O'Connor <dane.oconnor@gmail.com>
|
459 | - brandondewitt
|
460 | - luk3thomas
|
461 | - Marcel Klehr
|
462 | - Raymond Feng
|
463 | - Kane Cohen <kanecohen@gmail.com>
|
464 | - Gianni Chiappetta <gianni@runlevel6.org>
|
465 | - Eric Brody
|
466 |
|
467 |
|
468 |
|
469 | ## License
|
470 |
|
471 | (The MIT License)
|
472 |
|
473 | Copyright (c) 2011 dreamerslab <ben@dreamerslab.com>
|
474 |
|
475 | Permission is hereby granted, free of charge, to any person obtaining
|
476 | a copy of this software and associated documentation files (the
|
477 | 'Software'), to deal in the Software without restriction, including
|
478 | without limitation the rights to use, copy, modify, merge, publish,
|
479 | distribute, sublicense, and/or sell copies of the Software, and to
|
480 | permit persons to whom the Software is furnished to do so, subject to
|
481 | the following conditions:
|
482 |
|
483 | The above copyright notice and this permission notice shall be
|
484 | included in all copies or substantial portions of the Software.
|
485 |
|
486 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
487 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
488 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
489 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
490 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
491 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
492 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|