forked from dkeskar/airdb
/
Inflector.as
152 lines (140 loc) · 4.35 KB
/
Inflector.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.memamsa.airdb
{
/**
* Author: Jacob Wright http://jacwright.com
* Source: AIR ActiveRecord -
* http://code.google.com/p/air-activerecord/source/browse/trunk/src/flight/utils/Inflector.as
**/
public class Inflector
{
/**
* Returns the plural version of a word
*/
public static function pluralize(word:String):String
{
for each (var rule:Array in pluralRules)
{
var regex:RegExp = rule[0];
var replacement:String = rule[1];
if (regex.test(word))
return word.replace(regex, replacement);
}
return word;
}
/**
* Gives back the singular version of a plural word
*/
public static function singularize(word:String):String
{
for each (var rule:Array in singularRules)
{
var regex:RegExp = rule[0];
var replacement:String = rule[1];
if (regex.test(word))
return word.replace(regex, replacement);
}
return word;
}
/**
* Camelizes an underscored phrase
*/
public static function camelize(word:String):String
{
return word.replace(camelizeExp, function(match:String, underScore:String, char:String, index:int, word:String):String
{
return char.toUpperCase();
});
}
private static var camelizeExp:RegExp = /(^|_)(.)/g;
/**
* Returns the underscore version of a phrase
*/
public static function underscore(word:String):String
{
return word.replace(underscoreExp1, "$1_$2").replace(underscoreExp2, "$1_$2").toLowerCase();
}
private static var underscoreExp1:RegExp = /([A-Z]+)([A-Z])/g;
private static var underscoreExp2:RegExp = /([a-z])([A-Z])/g;
/**
* Returns a the phrase as it would be read with spaces, optionally capitalizing the first
* character in each word
*/
public static function humanize(word:String, capitalize:Boolean = false):String
{
return underscore(word).replace(camelizeExp, function(match:String, underScore:String, char:String, index:int, word:String):String
{
return (index ? " " : "") + (capitalize ? char.toUpperCase() : char); // if this is the start of the string don't add the space
});
}
/**
* Returns a phrase with all the words in it capitalized
*/
public static function upperWords(phrase:String):String
{
return phrase.replace(upperWordsExp, function(match:String, space:String, char:String, index:int, word:String):String
{
return space + char.toUpperCase();
});
}
private static var upperWordsExp:RegExp = /(^| )(\w)/g;
/**
* Returns a phrase with all the words in it not capitalized
*/
public static function lowerWords(phrase:String):String
{
return phrase.replace(upperWordsExp, function(match:String, space:String, char:String, index:int, word:String):String
{
return space + char.toLowerCase();
});
}
/**
* Returns a phrase with only the first character capitalized
*/
public static function upperFirst(phrase:String):String
{
return phrase.charAt(0).toUpperCase() + phrase.substr(1);
}
/**
* Returns a phrase with only the first character lowercased
*/
public static function lowerFirst(phrase:String):String
{
return phrase.charAt(0).toLowerCase() + phrase.substr(1);
}
protected static var pluralRules:Array = [
[/fish$/i, "fish"], // fish
[/(x|ch|ss|sh)$/i, "$1es"], // search, switch, fix, box, process, address
[/(series)$/i, "$1"],
[/([^aeiouy]|qu)ies$/i, "$1y"],
[/([^aeiouy]|qu)y$/i, "$1ies"], // query, ability, agency
[/(?:([^f])fe|([lr])f)$/i, "$1$2ves"], // half, safe, wife
[/sis$/i, "ses"], // basis, diagnosis
[/([ti])um$/i, "$1a"], // datum, medium
[/person$/i, "people"], // person, salesperson
[/man$/i, "men"], // man, woman, spokesman
[/child$/i, "children"], // child
[/s$/i, "s"], // no change (compatibility)
[/$/, "s"],
[/media$/i, "media"]
];
protected static var singularRules:Array = [
[/fish$/i, "fish"],
[/(x|ch|ss|sh)es$/i, "$1"],
[/movies$/i, "movie"],
[/series$/i, "series"],
[/([^aeiouy]|qu)ies$/i, "$1y"],
[/([lr])ves$/i, "$1f"],
[/(tive)s$/i, "$1"],
[/([^f])ves$/i, "$1fe"],
[/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, "$1$2sis"],
[/([ti])a$/i, "$1um"],
[/people$/i, "person"],
[/men$/i, "man"],
[/status$/i, "status"],
[/children$/i, "child"],
[/news$/i, "news"],
[/s$/i, ""],
[/media$/i, "media"]
];
}
}