actn-db 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,634 @@
1
+ /*
2
+ Copyright (c) 2010 Ryan Schuft (ryan.schuft@gmail.com)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+ */
22
+
23
+ /*
24
+ This code is based in part on the work done in Ruby to support
25
+ infection as part of Ruby on Rails in the ActiveSupport's Inflector
26
+ and Inflections classes. It was initally ported to Javascript by
27
+ Ryan Schuft (ryan.schuft@gmail.com) in 2007.
28
+
29
+ The code is available at http://code.google.com/p/inflection-js/
30
+
31
+ The basic usage is:
32
+ 1. Include this script on your web page.
33
+ 2. Call functions on any String object in Javascript
34
+
35
+ Currently implemented functions:
36
+
37
+ String.pluralize(plural) == String
38
+ renders a singular English language noun into its plural form
39
+ normal results can be overridden by passing in an alternative
40
+
41
+ String.singularize(singular) == String
42
+ renders a plural English language noun into its singular form
43
+ normal results can be overridden by passing in an alterative
44
+
45
+ String.camelize(lowFirstLetter) == String
46
+ renders a lower case underscored word into camel case
47
+ the first letter of the result will be upper case unless you pass true
48
+ also translates "/" into "::" (underscore does the opposite)
49
+
50
+ String.underscore() == String
51
+ renders a camel cased word into words seperated by underscores
52
+ also translates "::" back into "/" (camelize does the opposite)
53
+
54
+ String.humanize(lowFirstLetter) == String
55
+ renders a lower case and underscored word into human readable form
56
+ defaults to making the first letter capitalized unless you pass true
57
+
58
+ String.capitalize() == String
59
+ renders all characters to lower case and then makes the first upper
60
+
61
+ String.dasherize() == String
62
+ renders all underbars and spaces as dashes
63
+
64
+ String.titleize() == String
65
+ renders words into title casing (as for book titles)
66
+
67
+ String.demodulize() == String
68
+ renders class names that are prepended by modules into just the class
69
+
70
+ String.tableize() == String
71
+ renders camel cased singular words into their underscored plural form
72
+
73
+ String.classify() == String
74
+ renders an underscored plural word into its camel cased singular form
75
+
76
+ String.foreign_key(dropIdUbar) == String
77
+ renders a class name (camel cased singular noun) into a foreign key
78
+ defaults to seperating the class from the id with an underbar unless
79
+ you pass true
80
+
81
+ String.ordinalize() == String
82
+ renders all numbers found in the string into their sequence like "22nd"
83
+ */
84
+
85
+ /*
86
+ This sets up a container for some constants in its own namespace
87
+ We use the window (if available) to enable dynamic loading of this script
88
+ Window won't necessarily exist for non-browsers.
89
+ */
90
+
91
+
92
+ /*
93
+ This sets up some constants for later use
94
+ This should use the window namespace variable if available
95
+ */
96
+ InflectionJS =
97
+ {
98
+ /*
99
+ This is a list of nouns that use the same form for both singular and plural.
100
+ This list should remain entirely in lower case to correctly match Strings.
101
+ */
102
+ uncountable_words: [
103
+ 'equipment', 'information', 'rice', 'money', 'species', 'series',
104
+ 'fish', 'sheep', 'moose', 'deer', 'news'
105
+ ],
106
+
107
+ /*
108
+ These rules translate from the singular form of a noun to its plural form.
109
+ */
110
+ plural_rules: [
111
+ [new RegExp('(m)an$', 'gi'), '$1en'],
112
+ [new RegExp('(pe)rson$', 'gi'), '$1ople'],
113
+ [new RegExp('(child)$', 'gi'), '$1ren'],
114
+ [new RegExp('^(ox)$', 'gi'), '$1en'],
115
+ [new RegExp('(ax|test)is$', 'gi'), '$1es'],
116
+ [new RegExp('(octop|vir)us$', 'gi'), '$1i'],
117
+ [new RegExp('(alias|status)$', 'gi'), '$1es'],
118
+ [new RegExp('(bu)s$', 'gi'), '$1ses'],
119
+ [new RegExp('(buffal|tomat|potat)o$', 'gi'), '$1oes'],
120
+ [new RegExp('([ti])um$', 'gi'), '$1a'],
121
+ [new RegExp('sis$', 'gi'), 'ses'],
122
+ [new RegExp('(?:([^f])fe|([lr])f)$', 'gi'), '$1$2ves'],
123
+ [new RegExp('(hive)$', 'gi'), '$1s'],
124
+ [new RegExp('([^aeiouy]|qu)y$', 'gi'), '$1ies'],
125
+ [new RegExp('(x|ch|ss|sh)$', 'gi'), '$1es'],
126
+ [new RegExp('(matr|vert|ind)ix|ex$', 'gi'), '$1ices'],
127
+ [new RegExp('([m|l])ouse$', 'gi'), '$1ice'],
128
+ [new RegExp('(quiz)$', 'gi'), '$1zes'],
129
+ [new RegExp('s$', 'gi'), 's'],
130
+ [new RegExp('$', 'gi'), 's']
131
+ ],
132
+
133
+ /*
134
+ These rules translate from the plural form of a noun to its singular form.
135
+ */
136
+ singular_rules: [
137
+ [new RegExp('(m)en$', 'gi'), '$1an'],
138
+ [new RegExp('(pe)ople$', 'gi'), '$1rson'],
139
+ [new RegExp('(child)ren$', 'gi'), '$1'],
140
+ [new RegExp('([ti])a$', 'gi'), '$1um'],
141
+ [new RegExp('((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$','gi'), '$1$2sis'],
142
+ [new RegExp('(hive)s$', 'gi'), '$1'],
143
+ [new RegExp('(tive)s$', 'gi'), '$1'],
144
+ [new RegExp('(curve)s$', 'gi'), '$1'],
145
+ [new RegExp('([lr])ves$', 'gi'), '$1f'],
146
+ [new RegExp('([^fo])ves$', 'gi'), '$1fe'],
147
+ [new RegExp('([^aeiouy]|qu)ies$', 'gi'), '$1y'],
148
+ [new RegExp('(s)eries$', 'gi'), '$1eries'],
149
+ [new RegExp('(m)ovies$', 'gi'), '$1ovie'],
150
+ [new RegExp('(x|ch|ss|sh)es$', 'gi'), '$1'],
151
+ [new RegExp('([m|l])ice$', 'gi'), '$1ouse'],
152
+ [new RegExp('(bus)es$', 'gi'), '$1'],
153
+ [new RegExp('(o)es$', 'gi'), '$1'],
154
+ [new RegExp('(shoe)s$', 'gi'), '$1'],
155
+ [new RegExp('(cris|ax|test)es$', 'gi'), '$1is'],
156
+ [new RegExp('(octop|vir)i$', 'gi'), '$1us'],
157
+ [new RegExp('(alias|status)es$', 'gi'), '$1'],
158
+ [new RegExp('^(ox)en', 'gi'), '$1'],
159
+ [new RegExp('(vert|ind)ices$', 'gi'), '$1ex'],
160
+ [new RegExp('(matr)ices$', 'gi'), '$1ix'],
161
+ [new RegExp('(quiz)zes$', 'gi'), '$1'],
162
+ [new RegExp('s$', 'gi'), '']
163
+ ],
164
+
165
+ /*
166
+ This is a list of words that should not be capitalized for title case
167
+ */
168
+ non_titlecased_words: [
169
+ 'and', 'or', 'nor', 'a', 'an', 'the', 'so', 'but', 'to', 'of', 'at',
170
+ 'by', 'from', 'into', 'on', 'onto', 'off', 'out', 'in', 'over',
171
+ 'with', 'for'
172
+ ],
173
+
174
+ /*
175
+ These are regular expressions used for converting between String formats
176
+ */
177
+ id_suffix: new RegExp('(_ids|_id)$', 'g'),
178
+ underbar: new RegExp('_', 'g'),
179
+ space_or_underbar: new RegExp('[\ _]', 'g'),
180
+ uppercase: new RegExp('([A-Z])', 'g'),
181
+ underbar_prefix: new RegExp('^_'),
182
+
183
+ /*
184
+ This is a helper method that applies rules based replacement to a String
185
+ Signature:
186
+ InflectionJS.apply_rules(str, rules, skip, override) == String
187
+ Arguments:
188
+ str - String - String to modify and return based on the passed rules
189
+ rules - Array: [RegExp, String] - Regexp to match paired with String to use for replacement
190
+ skip - Array: [String] - Strings to skip if they match
191
+ override - String (optional) - String to return as though this method succeeded (used to conform to APIs)
192
+ Returns:
193
+ String - passed String modified by passed rules
194
+ Examples:
195
+ InflectionJS.apply_rules("cows", InflectionJs.singular_rules) === 'cow'
196
+ */
197
+ apply_rules: function(str, rules, skip, override)
198
+ {
199
+ if (override)
200
+ {
201
+ str = override;
202
+ }
203
+ else
204
+ {
205
+ var ignore = (skip.indexOf(str.toLowerCase()) > -1);
206
+ if (!ignore)
207
+ {
208
+ for (var x = 0; x < rules.length; x++)
209
+ {
210
+ if (str.match(rules[x][0]))
211
+ {
212
+ str = str.replace(rules[x][0], rules[x][1]);
213
+ break;
214
+ }
215
+ }
216
+ }
217
+ }
218
+ return str;
219
+ }
220
+ };
221
+
222
+ /*
223
+ This lets us detect if an Array contains a given element
224
+ Signature:
225
+ Array.indexOf(item, fromIndex, compareFunc) == Integer
226
+ Arguments:
227
+ item - Object - object to locate in the Array
228
+ fromIndex - Integer (optional) - starts checking from this position in the Array
229
+ compareFunc - Function (optional) - function used to compare Array item vs passed item
230
+ Returns:
231
+ Integer - index position in the Array of the passed item
232
+ Examples:
233
+ ['hi','there'].indexOf("guys") === -1
234
+ ['hi','there'].indexOf("hi") === 0
235
+ */
236
+ if (!Array.prototype.indexOf)
237
+ {
238
+ Array.prototype.indexOf = function(item, fromIndex, compareFunc)
239
+ {
240
+ if (!fromIndex)
241
+ {
242
+ fromIndex = -1;
243
+ }
244
+ var index = -1;
245
+ for (var i = fromIndex; i < this.length; i++)
246
+ {
247
+ if (this[i] === item || compareFunc && compareFunc(this[i], item))
248
+ {
249
+ index = i;
250
+ break;
251
+ }
252
+ }
253
+ return index;
254
+ };
255
+ }
256
+
257
+ /*
258
+ You can override this list for all Strings or just one depending on if you
259
+ set the new values on prototype or on a given String instance.
260
+ */
261
+
262
+ String.prototype._uncountable_words = InflectionJS.uncountable_words;
263
+
264
+
265
+ /*
266
+ You can override this list for all Strings or just one depending on if you
267
+ set the new values on prototype or on a given String instance.
268
+ */
269
+
270
+ String.prototype._plural_rules = InflectionJS.plural_rules;
271
+
272
+
273
+ /*
274
+ You can override this list for all Strings or just one depending on if you
275
+ set the new values on prototype or on a given String instance.
276
+ */
277
+
278
+ String.prototype._singular_rules = InflectionJS.singular_rules;
279
+
280
+
281
+ /*
282
+ You can override this list for all Strings or just one depending on if you
283
+ set the new values on prototype or on a given String instance.
284
+ */
285
+
286
+ String.prototype._non_titlecased_words = InflectionJS.non_titlecased_words;
287
+
288
+
289
+ /*
290
+ This function adds plurilization support to every String object
291
+ Signature:
292
+ String.pluralize(plural) == String
293
+ Arguments:
294
+ plural - String (optional) - overrides normal output with said String
295
+ Returns:
296
+ String - singular English language nouns are returned in plural form
297
+ Examples:
298
+ "person".pluralize() == "people"
299
+ "octopus".pluralize() == "octopi"
300
+ "Hat".pluralize() == "Hats"
301
+ "person".pluralize("guys") == "guys"
302
+ */
303
+
304
+ String.prototype.pluralize = function(plural)
305
+ {
306
+ return InflectionJS.apply_rules(
307
+ this,
308
+ this._plural_rules,
309
+ this._uncountable_words,
310
+ plural
311
+ );
312
+ };
313
+
314
+
315
+ /*
316
+ This function adds singularization support to every String object
317
+ Signature:
318
+ String.singularize(singular) == String
319
+ Arguments:
320
+ singular - String (optional) - overrides normal output with said String
321
+ Returns:
322
+ String - plural English language nouns are returned in singular form
323
+ Examples:
324
+ "people".singularize() == "person"
325
+ "octopi".singularize() == "octopus"
326
+ "Hats".singularize() == "Hat"
327
+ "guys".singularize("person") == "person"
328
+ */
329
+
330
+ String.prototype.singularize = function(singular)
331
+ {
332
+ return InflectionJS.apply_rules(
333
+ this,
334
+ this._singular_rules,
335
+ this._uncountable_words,
336
+ singular
337
+ );
338
+ };
339
+
340
+
341
+ /*
342
+ This function adds camelization support to every String object
343
+ Signature:
344
+ String.camelize(lowFirstLetter) == String
345
+ Arguments:
346
+ lowFirstLetter - boolean (optional) - default is to capitalize the first
347
+ letter of the results... passing true will lowercase it
348
+ Returns:
349
+ String - lower case underscored words will be returned in camel case
350
+ additionally '/' is translated to '::'
351
+ Examples:
352
+ "message_properties".camelize() == "MessageProperties"
353
+ "message_properties".camelize(true) == "messageProperties"
354
+ */
355
+
356
+ String.prototype.camelize = function(lowFirstLetter)
357
+ {
358
+ var str = this.underscore().toLowerCase();
359
+ var str_path = str.split('/');
360
+ for (var i = 0; i < str_path.length; i++)
361
+ {
362
+ var str_arr = str_path[i].split('_');
363
+ var initX = ((lowFirstLetter && i + 1 === str_path.length) ? (1) : (0));
364
+ for (var x = initX; x < str_arr.length; x++)
365
+ {
366
+ str_arr[x] = str_arr[x].charAt(0).toUpperCase() + str_arr[x].substring(1);
367
+ }
368
+ str_path[i] = str_arr.join('');
369
+ }
370
+ str = str_path.join('::').replace(/\s/ig,"");
371
+ return str;
372
+ };
373
+
374
+
375
+ /*
376
+ This function adds underscore support to every String object
377
+ Signature:
378
+ String.underscore() == String
379
+ Arguments:
380
+ N/A
381
+ Returns:
382
+ String - camel cased words are returned as lower cased and underscored
383
+ additionally '::' is translated to '/'
384
+ Examples:
385
+ "MessageProperties".camelize() == "message_properties"
386
+ "messageProperties".underscore() == "message_properties"
387
+ */
388
+
389
+ String.prototype.underscore = function()
390
+ {
391
+ var str = this;
392
+ var str_path = str.split('::');
393
+ for (var i = 0; i < str_path.length; i++)
394
+ {
395
+ str_path[i] = str_path[i].replace(InflectionJS.uppercase, '_$1');
396
+ str_path[i] = str_path[i].replace(InflectionJS.underbar_prefix, '');
397
+ }
398
+ str = str_path.join('/').toLowerCase();
399
+ str = str.replace(/\s/gi,"_");
400
+ return str;
401
+ };
402
+
403
+
404
+ /*
405
+ This function adds humanize support to every String object
406
+ Signature:
407
+ String.humanize(lowFirstLetter) == String
408
+ Arguments:
409
+ lowFirstLetter - boolean (optional) - default is to capitalize the first
410
+ letter of the results... passing true will lowercase it
411
+ Returns:
412
+ String - lower case underscored words will be returned in humanized form
413
+ Examples:
414
+ "message_properties".humanize() == "Message properties"
415
+ "message_properties".humanize(true) == "message properties"
416
+ */
417
+
418
+ String.prototype.humanize = function(lowFirstLetter)
419
+ {
420
+ var str = this.toLowerCase();
421
+ str = str.replace(InflectionJS.id_suffix, '');
422
+ str = str.replace(InflectionJS.underbar, ' ');
423
+ if (!lowFirstLetter)
424
+ {
425
+ str = str.replace(/_/ig," ").capitalize();
426
+ }
427
+ return str;
428
+ };
429
+
430
+
431
+ /*
432
+ This function adds capitalization support to every String object
433
+ Signature:
434
+ String.capitalize() == String
435
+ Arguments:
436
+ N/A
437
+ Returns:
438
+ String - all characters will be lower case and the first will be upper
439
+ Examples:
440
+ "message_properties".capitalize() == "Message_properties"
441
+ "message properties".capitalize() == "Message properties"
442
+ */
443
+
444
+ String.prototype.capitalize = function()
445
+ {
446
+ var str = this.toLowerCase();
447
+ str = str.substring(0, 1).toUpperCase() + str.substring(1);
448
+ return str;
449
+ };
450
+
451
+
452
+ /*
453
+ This function adds dasherization support to every String object
454
+ Signature:
455
+ String.dasherize() == String
456
+ Arguments:
457
+ N/A
458
+ Returns:
459
+ String - replaces all spaces or underbars with dashes
460
+ Examples:
461
+ "message_properties".capitalize() == "message-properties"
462
+ "Message Properties".capitalize() == "Message-Properties"
463
+ */
464
+
465
+ String.prototype.dasherize = function()
466
+ {
467
+ var str = this;
468
+ str = str.replace(InflectionJS.space_or_underbar, '-');
469
+ return str;
470
+ };
471
+
472
+
473
+ /*
474
+ This function adds titleize support to every String object
475
+ Signature:
476
+ String.titleize() == String
477
+ Arguments:
478
+ N/A
479
+ Returns:
480
+ String - capitalizes words as you would for a book title
481
+ Examples:
482
+ "message_properties".titleize() == "Message Properties"
483
+ "message properties to keep".titleize() == "Message Properties to Keep"
484
+ */
485
+
486
+ String.prototype.titleize = function()
487
+ {
488
+ var str = this.toLowerCase();
489
+ str = str.replace(InflectionJS.underbar, ' ');
490
+ var str_arr = str.split(' ');
491
+ for (var x = 0; x < str_arr.length; x++)
492
+ {
493
+ var d = str_arr[x].split('-');
494
+ for (var i = 0; i < d.length; i++)
495
+ {
496
+ if (this._non_titlecased_words.indexOf(d[i].toLowerCase()) < 0)
497
+ {
498
+ d[i] = d[i].capitalize();
499
+ }
500
+ }
501
+ str_arr[x] = d.join('-');
502
+ }
503
+ str = str_arr.join(' ');
504
+ str = str.substring(0, 1).toUpperCase() + str.substring(1);
505
+ return str;
506
+ };
507
+
508
+
509
+ /*
510
+ This function adds demodulize support to every String object
511
+ Signature:
512
+ String.demodulize() == String
513
+ Arguments:
514
+ N/A
515
+ Returns:
516
+ String - removes module names leaving only class names (Ruby style)
517
+ Examples:
518
+ "Message::Bus::Properties".demodulize() == "Properties"
519
+ */
520
+
521
+ String.prototype.demodulize = function()
522
+ {
523
+ var str = this;
524
+ var str_arr = str.split('::');
525
+ str = str_arr[str_arr.length - 1];
526
+ return str;
527
+ };
528
+
529
+ /*
530
+ This function adds tableize support to every String object
531
+ Signature:
532
+ String.tableize() == String
533
+ Arguments:
534
+ N/A
535
+ Returns:
536
+ String - renders camel cased words into their underscored plural form
537
+ Examples:
538
+ "MessageBusProperty".tableize() == "message_bus_properties"
539
+ */
540
+
541
+ String.prototype.tableize = function()
542
+ {
543
+ var str = this;
544
+ str = str.underscore().pluralize();
545
+ return str;
546
+ };
547
+
548
+
549
+ /*
550
+ This function adds classification support to every String object
551
+ Signature:
552
+ String.classify() == String
553
+ Arguments:
554
+ N/A
555
+ Returns:
556
+ String - underscored plural nouns become the camel cased singular form
557
+ Examples:
558
+ "message_bus_properties".classify() == "MessageBusProperty"
559
+ */
560
+
561
+ String.prototype.classify = function()
562
+ {
563
+ var str = this;
564
+ str = str.camelize().singularize();
565
+ return str;
566
+ };
567
+
568
+ /*
569
+ This function adds foreign key support to every String object
570
+ Signature:
571
+ String.foreign_key(dropIdUbar) == String
572
+ Arguments:
573
+ dropIdUbar - boolean (optional) - default is to seperate id with an
574
+ underbar at the end of the class name, you can pass true to skip it
575
+ Returns:
576
+ String - camel cased singular class names become underscored with id
577
+ Examples:
578
+ "MessageBusProperty".foreign_key() == "message_bus_property_id"
579
+ "MessageBusProperty".foreign_key(true) == "message_bus_propertyid"
580
+ */
581
+
582
+ String.prototype.foreign_key = function(dropIdUbar)
583
+ {
584
+ var str = this;
585
+ str = str.demodulize().underscore() + ((dropIdUbar) ? ('') : ('_')) + 'id';
586
+ return str;
587
+ };
588
+
589
+
590
+ /*
591
+ This function adds ordinalize support to every String object
592
+ Signature:
593
+ String.ordinalize() == String
594
+ Arguments:
595
+ N/A
596
+ Returns:
597
+ String - renders all found numbers their sequence like "22nd"
598
+ Examples:
599
+ "the 1 pitch".ordinalize() == "the 1st pitch"
600
+ */
601
+
602
+ String.prototype.ordinalize = function()
603
+ {
604
+ var str = this;
605
+ var str_arr = str.split(' ');
606
+ for (var x = 0; x < str_arr.length; x++)
607
+ {
608
+ var i = parseInt(str_arr[x]);
609
+ if (i === NaN)
610
+ {
611
+ var ltd = str_arr[x].substring(str_arr[x].length - 2);
612
+ var ld = str_arr[x].substring(str_arr[x].length - 1);
613
+ var suf = "th";
614
+ if (ltd != "11" && ltd != "12" && ltd != "13")
615
+ {
616
+ if (ld === "1")
617
+ {
618
+ suf = "st";
619
+ }
620
+ else if (ld === "2")
621
+ {
622
+ suf = "nd";
623
+ }
624
+ else if (ld === "3")
625
+ {
626
+ suf = "rd";
627
+ }
628
+ }
629
+ str_arr[x] += suf;
630
+ }
631
+ }
632
+ str = str_arr.join(' ');
633
+ return str;
634
+ };