Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(123)

Side by Side Diff: app/views/utils.js

Issue 8488045: Adds template helpers e.g. for pluralization.
Patch Set: Adds template helpers e.g. for pluralization. Created 11 years, 12 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 'use strict'; 1 'use strict';
2 2
3 /** 3 /**
4 * The view utils. 4 * The view utils.
5 * 5 *
6 * @module views 6 * @module views
7 * @submodule views.utils 7 * @submodule views.utils
8 */ 8 */
9 9
10 YUI.add('juju-view-utils', function(Y) { 10 YUI.add('juju-view-utils', function(Y) {
(...skipping 1148 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 Y.Array.each(Y.Object.keys(object), function(key) { 1159 Y.Array.each(Y.Object.keys(object), function(key) {
1160 res = res + options.fn({ 1160 res = res + options.fn({
1161 key: key, 1161 key: key,
1162 value: object[key] 1162 value: object[key]
1163 }); 1163 });
1164 }); 1164 });
1165 } 1165 }
1166 return res; 1166 return res;
1167 }); 1167 });
1168 1168
1169 /**
1170 * pluralize
1171 *
1172 * pluralize is a handlebar helper that handles pluralization of strings.
1173 * The requirement for pluralization is based on the passed in object,
1174 * which can be number, array, or object. If a number, it is directly
1175 * checked to see if pluralization is needed. Arrays and objects are
1176 * checked for length or size attributes, which are then used.
1177 *
1178 * By default, if pluralization is needed, an 's' is appended to the
1179 * string. This handles the regular case (e.g. cat => cats). Irregular
1180 * cases are handled by passing in a plural form (e.g. octopus => ocotopi).
jeff.pihach 2013/04/08 21:49:30 Good thinking, plurals are very hard to do automat
1181 */
1182 Y.Handlebars.registerHelper('pluralize',
1183 function(word, object, plural_word, options) {
1184 var plural = false;
1185 if (typeof(object) === 'number') {
1186 plural = (object !== 1);
1187 }
1188 if (object) {
1189 if (object.size) {
1190 plural = (object.size() !== 1);
1191 } else if (object.length) {
1192 plural = (object.length !== 1);
1193 }
1194 }
1195 if (plural) {
1196 if (typeof(plural_word) === 'string') {
1197 return plural_word;
1198 } else {
1199 return word + 's';
1200 }
1201 } else {
1202 return word;
1203 }
1204 });
1205
1169 }, '0.1.0', { 1206 }, '0.1.0', {
1170 requires: ['base-build', 1207 requires: ['base-build',
1171 'handlebars', 1208 'handlebars',
1172 'node', 1209 'node',
1173 'view', 1210 'view',
1174 'panel', 1211 'panel',
1175 'json-stringify', 1212 'json-stringify',
1176 'gallery-markdown', 1213 'gallery-markdown',
1177 'datatype-date-format'] 1214 'datatype-date-format']
1178 }); 1215 });
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b