walrus-rb 0.10.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ (function() {
2
+ var Walrus, locale, separate;
3
+
4
+ Walrus = (typeof exports !== "undefined" && exports !== null ? require('./walrus') : this).Walrus;
5
+
6
+ separate = function(value, thousands, decimal) {
7
+ var fraction, whole, _ref;
8
+ _ref = value.split(decimal), whole = _ref[0], fraction = _ref[1];
9
+ whole = whole.replace(/(\d)(?=(\d{3})+$)/g, "$1" + thousands);
10
+ if (fraction) {
11
+ return [whole, fraction].join(decimal);
12
+ } else {
13
+ return whole;
14
+ }
15
+ };
16
+
17
+ /**
18
+ * *:currency*
19
+ * Returns a string formatted in the current locale's format.
20
+ * Delegates to [accounting.js](http://josscrowcroft.github.com/accounting.js/) if present.
21
+ *
22
+ * Parameters:
23
+ * precision - the decimal place level to show cents, if applicable
24
+ *
25
+ * Usage:
26
+ *
27
+ * {{ 36000 | :currency( '$', 2 ) }} // => $36,000.00
28
+ * {{ 36000 | :currency }} // => $36,000
29
+ */
30
+
31
+ locale = Walrus.i18n.t('currencies');
32
+
33
+ if (typeof accounting !== "undefined" && accounting !== null) {
34
+ accounting.settings.currency.symbol = locale.symbol;
35
+ accounting.settings.currency.decimal = locale.decimal;
36
+ accounting.settings.currency.precision = locale.precision;
37
+ accounting.settings.currency.thousand = locale.thousand;
38
+ Walrus.addFilter('currency', function() {
39
+ return accounting.formatMoney.apply(accounting, arguments);
40
+ });
41
+ Walrus.addFilter('formatMoney', function() {
42
+ return accounting.formatMoney.apply(accounting, arguments);
43
+ });
44
+ } else {
45
+ Walrus.addFilter('currency', function(value, symbol, precision, decimal, thousand) {
46
+ var amount, moneys;
47
+ if (symbol == null) symbol = locale.symbol;
48
+ if (precision == null) precision = locale.precision;
49
+ if (decimal == null) decimal = locale.decimal;
50
+ if (thousand == null) thousand = locale.thousand;
51
+ moneys = value.toFixed(precision);
52
+ amount = separate(moneys, thousand, decimal);
53
+ return "" + symbol + amount;
54
+ });
55
+ }
56
+
57
+ }).call(this);
@@ -0,0 +1,296 @@
1
+ (function() {
2
+ var Walrus;
3
+
4
+ Walrus = (typeof exports !== "undefined" && exports !== null ? require('./walrus') : this).Walrus;
5
+
6
+ /**
7
+ * *:strftime*
8
+ * Formats a date into the string given by `format`. Accepts any value
9
+ * that can be passed to `new Date( )`.
10
+ *
11
+ * Parameters:
12
+ * format - The format string, according to these tokens, taken directly
13
+ * from `man 3 strftime` (with some omissions):
14
+ *
15
+ * %A is replaced by national representation of the full weekday name.
16
+ *
17
+ * %a is replaced by national representation of the abbreviated weekday name.
18
+ *
19
+ * %B is replaced by national representation of the full month name.
20
+ *
21
+ * %b is replaced by national representation of the abbreviated month name.
22
+ *
23
+ * %D is equivalent to ``%m/%d/%y''.
24
+ *
25
+ * %d is replaced by the day of the month as a decimal number (01-31).
26
+ *
27
+ * %e is replaced by the day of month as a decimal number (1-31); single digits are
28
+ * preceded by a blank.
29
+ *
30
+ * %F is equivalent to ``%Y-%m-%d''.
31
+ *
32
+ * %H is replaced by the hour (24-hour clock) as a decimal number (00-23).
33
+ *
34
+ * %I is replaced by the hour (12-hour clock) as a decimal number (01-12).
35
+ *
36
+ * %k is replaced by the hour (24-hour clock) as a decimal number (0-23); single dig-
37
+ * its are preceded by a blank.
38
+ *
39
+ * %l is replaced by the hour (12-hour clock) as a decimal number (1-12); single dig-
40
+ * its are preceded by a blank.
41
+ *
42
+ * %M is replaced by the minute as a decimal number (00-59).
43
+ *
44
+ * %m is replaced by the month as a decimal number (01-12).
45
+ *
46
+ * %n is replaced by a newline.
47
+ *
48
+ * %p is replaced by national representation of either "ante meridiem" or "post meri-
49
+ * diem" as appropriate.
50
+ *
51
+ * %R is equivalent to ``%H:%M''.
52
+ *
53
+ * %r is equivalent to ``%I:%M:%S %p''.
54
+ *
55
+ * %S is replaced by the second as a decimal number (00-60).
56
+ *
57
+ * %T is equivalent to ``%H:%M:%S''.
58
+ *
59
+ * %t is replaced by a tab.
60
+ *
61
+ * %U is replaced by the week number of the year (Sunday as the first day of the
62
+ * week) as a decimal number (00-53).
63
+ *
64
+ * %u is replaced by the weekday (Monday as the first day of the week) as a decimal
65
+ * number (1-7).
66
+ *
67
+ * %v is equivalent to ``%e-%b-%Y''.
68
+ *
69
+ * %w is replaced by the weekday (Sunday as the first day of the week) as a decimal
70
+ *
71
+ * %X is replaced by national representation of the time.
72
+ *
73
+ * %x is replaced by national representation of the date.
74
+ *
75
+ * %Y is replaced by the year with century as a decimal number.
76
+ *
77
+ * %y is replaced by the year without century as a decimal number (00-99).
78
+ *
79
+ * %Z is replaced by the time zone name.
80
+ */
81
+
82
+ Walrus.addFilter('strftime', function(dateish, format) {
83
+ var date, pad,
84
+ _this = this;
85
+ date = new Date(dateish);
86
+ pad = function(value, to, padding) {
87
+ if (to == null) to = 2;
88
+ if (padding == null) padding = '0';
89
+ if (("" + value).length < to) {
90
+ return pad("" + padding + value, to, padding);
91
+ } else {
92
+ return value;
93
+ }
94
+ };
95
+ return format.replace(/%(.)/g, function(input) {
96
+ switch (input) {
97
+ case '%a':
98
+ return Walrus.i18n.t('dates.abbr_daynames')[date.getDay()];
99
+ case '%A':
100
+ return Walrus.i18n.t('dates.full_daynames')[date.getDay()];
101
+ case '%b':
102
+ return Walrus.i18n.t('dates.abbr_monthnames')[date.getMonth()];
103
+ case '%B':
104
+ return Walrus.i18n.t('dates.full_monthnames')[date.getMonth()];
105
+ case '%D':
106
+ return _this.strftime(date, '%m/%d/%y');
107
+ case '%d':
108
+ return pad(date.getDate());
109
+ case '%e':
110
+ return date.getDate();
111
+ case '%F':
112
+ return _this.strftime(date, '%Y-%m-%d');
113
+ case '%H':
114
+ return pad(date.getHours());
115
+ case '%I':
116
+ if (date.getHours() > 12) {
117
+ return pad(date.getHours() - 12);
118
+ } else {
119
+ return pad(date.getHours());
120
+ }
121
+ break;
122
+ case '%k':
123
+ return date.getHours();
124
+ case '%l':
125
+ if (date.getHours() > 12) {
126
+ return date.getHours() - 12;
127
+ } else {
128
+ return date.getHours();
129
+ }
130
+ break;
131
+ case '%M':
132
+ return pad(date.getMinutes());
133
+ case '%m':
134
+ return pad(date.getMonth() + 1);
135
+ case '%n':
136
+ return "\n";
137
+ case '%p':
138
+ if (date.getHours() > 12) {
139
+ return Walrus.i18n.t('dates.pm');
140
+ } else {
141
+ return Walrus.i18n.t('dates.am');
142
+ }
143
+ break;
144
+ case '%R':
145
+ return _this.strftime(date, '%H:%M');
146
+ case '%r':
147
+ return _this.strftime(date, '%I:%M:%S %p');
148
+ case '%S':
149
+ return pad(date.getSeconds());
150
+ case '%T':
151
+ return _this.strftime(date, '%H:%M:%S');
152
+ case '%t':
153
+ return "\t";
154
+ case '%u':
155
+ return date.getDay() || 7;
156
+ case '%v':
157
+ return _this.strftime(date, '%e-%b-%Y');
158
+ case '%w':
159
+ return date.getDay();
160
+ case '%X':
161
+ return date.toTimeString();
162
+ case '%x':
163
+ return date.toDateString();
164
+ case '%Y':
165
+ return date.getFullYear();
166
+ case '%y':
167
+ return date.getFullYear().toString().slice(-2);
168
+ case '%Z':
169
+ return date.toString().match(/\((\w+)\)/)[1] || '';
170
+ }
171
+ });
172
+ });
173
+
174
+ /**
175
+ * returns whether or not the given year is a leap year
176
+ */
177
+
178
+ Walrus.Utils.isLeapYear = function(year) {
179
+ return new Date(year, 1, 29).getDate() === 29;
180
+ };
181
+
182
+ /**
183
+ * returns the number of leap years between the two given years
184
+ */
185
+
186
+ Walrus.Utils.leapYearsBetween = function(from, to) {
187
+ var count, year;
188
+ if (from > to) return 0;
189
+ count = 0;
190
+ for (year = from; from <= to ? year <= to : year >= to; from <= to ? year++ : year--) {
191
+ if (this.isLeapYear(year)) count++;
192
+ }
193
+ return count;
194
+ };
195
+
196
+ /**
197
+ * returns the distance between two times in words
198
+ */
199
+
200
+ Walrus.Utils.distanceOfTimeInWords = function(ftime, ttime, includeSeconds) {
201
+ var d, diff, distanceInMinutes, distanceInSeconds, distanceInYears, fdate, fyear, leapYears, minuteOffsetForLeapYear, minutesWithOffset, remainder, t, tdate, tyear;
202
+ if (ttime == null) ttime = 0;
203
+ if (includeSeconds == null) includeSeconds = false;
204
+ t = function(keypath, count) {
205
+ var amount;
206
+ if (count == null) count = 1;
207
+ amount = count === 1 ? 'one' : 'other';
208
+ return Walrus.i18n.t("dates.distance_in_words." + keypath + "." + amount, {
209
+ count: count
210
+ });
211
+ };
212
+ fdate = new Date(ftime);
213
+ tdate = new Date(ttime);
214
+ diff = (tdate - fdate) / 1000;
215
+ distanceInMinutes = Math.round(Math.abs(diff) / 60);
216
+ distanceInSeconds = Math.round(Math.abs(diff));
217
+ d = function(divisor) {
218
+ if (divisor == null) divisor = 1;
219
+ return Math.round(distanceInMinutes / divisor);
220
+ };
221
+ switch (false) {
222
+ case !((0 <= distanceInMinutes && distanceInMinutes <= 1)):
223
+ if (!includeSeconds) {
224
+ if (distanceInMinutes === 0) {
225
+ return t('less_than_x_minutes');
226
+ } else {
227
+ return t('x_minutes');
228
+ }
229
+ } else {
230
+ switch (false) {
231
+ case !((0 <= distanceInSeconds && distanceInSeconds <= 4)):
232
+ return t('less_than_x_seconds', 5);
233
+ case !((5 <= distanceInSeconds && distanceInSeconds <= 9)):
234
+ return t('less_than_x_seconds', 10);
235
+ case !((10 <= distanceInSeconds && distanceInSeconds <= 19)):
236
+ return t('less_than_x_seconds', 20);
237
+ case !((20 <= distanceInSeconds && distanceInSeconds <= 39)):
238
+ return t('half_a_minute');
239
+ case !((40 <= distanceInSeconds && distanceInSeconds <= 59)):
240
+ return t('less_than_x_minutes');
241
+ default:
242
+ return "1 minute";
243
+ }
244
+ }
245
+ break;
246
+ case !((2 <= distanceInMinutes && distanceInMinutes <= 44)):
247
+ return t('x_minutes', d());
248
+ case !((45 <= distanceInMinutes && distanceInMinutes <= 89)):
249
+ return t('about_x_hours');
250
+ case !((90 <= distanceInMinutes && distanceInMinutes <= 1439)):
251
+ return t('about_x_hours', d(60));
252
+ case !((1440 <= distanceInMinutes && distanceInMinutes <= 2519)):
253
+ return t('x_days');
254
+ case !((2520 <= distanceInMinutes && distanceInMinutes <= 43199)):
255
+ return t('x_days', d(1440));
256
+ case !((43200 <= distanceInMinutes && distanceInMinutes <= 86399)):
257
+ return t('about_x_months');
258
+ case !((86400 <= distanceInMinutes && distanceInMinutes <= 525599)):
259
+ return t('x_months', d(43200));
260
+ default:
261
+ fyear = fdate.getFullYear();
262
+ if (fdate.getMonth() >= 2) fyear += 1;
263
+ tyear = tdate.getFullYear();
264
+ if (tdate.getMonth() < 2) tyear -= 1;
265
+ leapYears = Walrus.Utils.leapYearsBetween(fyear, tyear);
266
+ minuteOffsetForLeapYear = leapYears * 1440;
267
+ minutesWithOffset = distanceInMinutes - minuteOffsetForLeapYear;
268
+ remainder = minutesWithOffset % 525600;
269
+ distanceInYears = Math.floor(minutesWithOffset / 525600);
270
+ if (remainder < 131400) {
271
+ return t('about_x_years', distanceInYears);
272
+ } else if (remainder < 394200) {
273
+ return t('over_x_years', distanceInYears);
274
+ } else {
275
+ return t('almost_x_years', distanceInYears + 1);
276
+ }
277
+ }
278
+ };
279
+
280
+ /**
281
+ * *:time_ago_in_words*
282
+ * Returns a human-readable relative time phrase from the given date.
283
+ *
284
+ * Parameters:
285
+ * includeSeconds - (Optional) whether or not to include results for less than one minute
286
+ *
287
+ * Usage:
288
+ *
289
+ * {{ created_at | :time_ago_in_words( true ) }} // => "less than a minute"
290
+ */
291
+
292
+ Walrus.addFilter('time_ago_in_words', function(dateish, includeSeconds) {
293
+ return Walrus.Utils.distanceOfTimeInWords(dateish, new Date(), includeSeconds);
294
+ });
295
+
296
+ }).call(this);
@@ -0,0 +1,120 @@
1
+ (function() {
2
+ var Walrus, gsub, inflect, pluralize, plurals, singularize, singulars, uncountables,
3
+ __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
4
+
5
+ Walrus = (typeof exports !== "undefined" && exports !== null ? require('./walrus') : this).Walrus;
6
+
7
+ /**
8
+ * Plenty of the methods in `walrus.inflections` are borrowed from or inspired by ActiveSupport:
9
+ * https://github.com/rails/rails/tree/master/activesupport
10
+ * JavaScript inflector implementation ported from underscore.inflection:
11
+ * https://github.com/jeremyruppel/underscore.inflection
12
+ * (Which was also inspired by and borrows from ActiveSupport)
13
+ */
14
+
15
+ plurals = [[/$/, 's'], [/s$/, 's'], [/(ax|test)is$/, '$1es'], [/(octop|vir)us$/, '$1i'], [/(octop|vir)i$/, '$1i'], [/(alias|status)$/, '$1es'], [/(bu)s$/, '$1ses'], [/(buffal|tomat)o$/, '$1oes'], [/([ti])um$/, '$1a'], [/([ti])a$/, '$1a'], [/sis$/, 'ses'], [/(?:([^f])fe|([lr])f)$/, '$1$2ves'], [/(hive)$/, '$1s'], [/([^aeiouy]|qu)y$/, '$1ies'], [/(x|ch|ss|sh)$/, '$1es'], [/(matr|vert|ind)(?:ix|ex)$/, '$1ices'], [/([m|l])ouse$/, '$1ice'], [/([m|l])ice$/, '$1ice'], [/^(ox)$/, '$1en'], [/^(oxen)$/, '$1'], [/(quiz)$/, '$1zes'], ['person', 'people'], ['man', 'men'], ['child', 'children'], ['sex', 'sexes'], ['move', 'moves'], ['cow', 'kine']].reverse();
16
+
17
+ singulars = [[/s$/, ''], [/(n)ews$/, '$1ews'], [/([ti])a$/, '$1um'], [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/, '$1$2sis'], [/(^analy)ses$/, '$1sis'], [/([^f])ves$/, '$1fe'], [/(hive)s$/, '$1'], [/(tive)s$/, '$1'], [/([lr])ves$/, '$1f'], [/([^aeiouy]|qu)ies$/, '$1y'], [/(s)eries$/, '$1eries'], [/(m)ovies$/, '$1ovie'], [/(x|ch|ss|sh)es$/, '$1'], [/([m|l])ice$/, '$1ouse'], [/(bus)es$/, '$1'], [/(o)es$/, '$1'], [/(shoe)s$/, '$1'], [/(cris|ax|test)es$/, '$1is'], [/(octop|vir)i$/, '$1us'], [/(alias|status)es$/, '$1'], [/^(ox)en/, '$1'], [/(vert|ind)ices$/, '$1ex'], [/(matr)ices$/, '$1ix'], [/(quiz)zes$/, '$1'], [/(database)s$/, '$1'], ['cow', 'kine'], ['move', 'moves'], ['sex', 'sexes'], ['child', 'children'], ['man', 'men'], ['person', 'people']].reverse();
18
+
19
+ uncountables = ['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep', 'jeans'];
20
+
21
+ gsub = function(word, rule, replacement) {
22
+ var pattern;
23
+ pattern = new RegExp(rule.source || rule, 'gi');
24
+ if (pattern.test(word)) return word.replace(pattern, replacement);
25
+ };
26
+
27
+ inflect = function(word, rules) {
28
+ var result, rule, sub, _i, _len;
29
+ result = word;
30
+ for (_i = 0, _len = rules.length; _i < _len; _i++) {
31
+ rule = rules[_i];
32
+ sub = gsub(word, rule[0], rule[1]);
33
+ if (sub) result = sub;
34
+ if (sub) break;
35
+ }
36
+ return result;
37
+ };
38
+
39
+ singularize = function(word) {
40
+ if (__indexOf.call(uncountables, word) >= 0) return word;
41
+ return inflect(word, singulars);
42
+ };
43
+
44
+ pluralize = function(word, count, includeCount) {
45
+ var result;
46
+ if (includeCount == null) includeCount = false;
47
+ if (count != null) {
48
+ result = count === 1 ? singularize(word) : pluralize(word);
49
+ return result = includeCount ? "" + count + " " + result : result;
50
+ } else {
51
+ if (__indexOf.call(uncountables, word) >= 0) return word;
52
+ return inflect(word, plurals);
53
+ }
54
+ };
55
+
56
+ /**
57
+ * *:pluralize*
58
+ * Pluralizes the given word, optionally based on a _count_, and also optionally
59
+ * including the count in the result.
60
+ *
61
+ * Parameters:
62
+ * word - the word to be pluralized
63
+ * count - Optional: count to base pluralization on
64
+ * includeCount - Optional: whether or not to include the count in the result
65
+ *
66
+ * Usage:
67
+ *
68
+ * {{ "book" | :pluralize }} // => "books"
69
+ *
70
+ * {{ "book" | :pluralize( 1 ) }} // => "book"
71
+ *
72
+ * {{ "book" | :pluralize( 5, true ) }} // => "5 books"
73
+ */
74
+
75
+ Walrus.addFilter('pluralize', pluralize);
76
+
77
+ /**
78
+ * *:singularize*
79
+ * Singularizes the given word. You're probably looking for `pluralize`.
80
+ *
81
+ * Parameters: none
82
+ *
83
+ * Usage:
84
+ *
85
+ * {{ "books" | :singularize }} // => "book"
86
+ */
87
+
88
+ Walrus.addFilter('singularize', singularize);
89
+
90
+ /**
91
+ * *:ordinalize*
92
+ * Turns a number into an ordinal string, like 1st, 2nd, 3rd, etc...
93
+ *
94
+ * Parameters: none
95
+ *
96
+ * Usage:
97
+ *
98
+ * {{ 5 | :ordinalize }} // => "5th"
99
+ */
100
+
101
+ Walrus.addFilter('ordinalize', function(value) {
102
+ var normal, _ref;
103
+ normal = Math.abs(Math.round(value));
104
+ if (_ref = normal % 100, __indexOf.call([11, 12, 13], _ref) >= 0) {
105
+ return "" + value + "th";
106
+ } else {
107
+ switch (normal % 10) {
108
+ case 1:
109
+ return "" + value + "st";
110
+ case 2:
111
+ return "" + value + "nd";
112
+ case 3:
113
+ return "" + value + "rd";
114
+ default:
115
+ return "" + value + "th";
116
+ }
117
+ }
118
+ });
119
+
120
+ }).call(this);