summaryrefslogtreecommitdiffstats
path: root/js/vendor/angular-ui/modules/filters/inflector
diff options
context:
space:
mode:
Diffstat (limited to 'js/vendor/angular-ui/modules/filters/inflector')
-rw-r--r--js/vendor/angular-ui/modules/filters/inflector/inflector.js44
-rw-r--r--js/vendor/angular-ui/modules/filters/inflector/test/inflectorSpec.js39
2 files changed, 83 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;
+ }
+ };
+});
diff --git a/js/vendor/angular-ui/modules/filters/inflector/test/inflectorSpec.js b/js/vendor/angular-ui/modules/filters/inflector/test/inflectorSpec.js
new file mode 100644
index 000000000..1dcb236c6
--- /dev/null
+++ b/js/vendor/angular-ui/modules/filters/inflector/test/inflectorSpec.js
@@ -0,0 +1,39 @@
+describe('inflector', function () {
+ var inflectorFilter, testPhrase = 'here isMy_phone_number';
+
+ beforeEach(module('ui.filters'));
+ beforeEach(inject(function ($filter) {
+ inflectorFilter = $filter('inflector');
+ }));
+
+ describe('default', function () {
+ it('should default to humanize', function () {
+ expect(inflectorFilter(testPhrase)).toEqual('Here Is My Phone Number');
+ });
+ it('should fail gracefully for invalid input', function () {
+ expect(inflectorFilter(undefined)).toBeUndefined();
+ });
+ it('should do nothing for empty input', function () {
+ expect(inflectorFilter('')).toEqual('');
+ });
+ });
+
+ describe('humanize', function () {
+ it('should uppercase first letter and separate words with a space', function () {
+ expect(inflectorFilter(testPhrase, 'humanize')).toEqual('Here Is My Phone Number');
+ });
+ });
+ describe('underscore', function () {
+ it('should lowercase everything and separate words with an underscore', function () {
+ expect(inflectorFilter(testPhrase, 'underscore')).toEqual('here_is_my_phone_number');
+ });
+ });
+ describe('variable', function () {
+ it('should remove all separators and camelHump the phrase', function () {
+ expect(inflectorFilter(testPhrase, 'variable')).toEqual('hereIsMyPhoneNumber');
+ });
+ it('should do nothing if already formatted properly', function () {
+ expect(inflectorFilter("hereIsMyPhoneNumber", 'variable')).toEqual('hereIsMyPhoneNumber');
+ });
+ });
+}); \ No newline at end of file