diff options
Diffstat (limited to 'js/vendor/angular-ui/modules/filters/inflector/inflector.js')
-rw-r--r-- | js/vendor/angular-ui/modules/filters/inflector/inflector.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/js/vendor/angular-ui/modules/filters/inflector/inflector.js b/js/vendor/angular-ui/modules/filters/inflector/inflector.js new file mode 100644 index 000000000..4ab74093a --- /dev/null +++ b/js/vendor/angular-ui/modules/filters/inflector/inflector.js @@ -0,0 +1,44 @@ +/** + * Converts variable-esque naming conventions to something presentational, capitalized words separated by space. + * @param {String} value The value to be parsed and prettified. + * @param {String} [inflector] The inflector to use. Default: humanize. + * @return {String} + * @example {{ 'Here Is my_phoneNumber' | inflector:'humanize' }} => Here Is My Phone Number + * {{ 'Here Is my_phoneNumber' | inflector:'underscore' }} => here_is_my_phone_number + * {{ 'Here Is my_phoneNumber' | inflector:'variable' }} => hereIsMyPhoneNumber + */ +angular.module('ui.filters').filter('inflector', function () { + function ucwords(text) { + return text.replace(/^([a-z])|\s+([a-z])/g, function ($1) { + return $1.toUpperCase(); + }); + } + + function breakup(text, separator) { + return text.replace(/[A-Z]/g, function (match) { + return separator + match; + }); + } + + var inflectors = { + humanize: function (value) { + return ucwords(breakup(value, ' ').split('_').join(' ')); + }, + underscore: function (value) { + return value.substr(0, 1).toLowerCase() + breakup(value.substr(1), '_').toLowerCase().split(' ').join('_'); + }, + variable: function (value) { + value = value.substr(0, 1).toLowerCase() + ucwords(value.split('_').join(' ')).substr(1).split(' ').join(''); + return value; + } + }; + + return function (text, inflector, separator) { + if (inflector !== false && angular.isString(text)) { + inflector = inflector || 'humanize'; + return inflectors[inflector](text); + } else { + return text; + } + }; +}); |