Formatting labels
Change-Id: I131f27f2f6fcd5cd76f4fbc13c632f7cd1aa17d0
diff --git a/src/app/core/services/helpers/config.helpers.ts b/src/app/core/services/helpers/config.helpers.ts
new file mode 100644
index 0000000..44d3cdc
--- /dev/null
+++ b/src/app/core/services/helpers/config.helpers.ts
@@ -0,0 +1,107 @@
+import * as _ from 'lodash';
+import * as pluralize from 'pluralize';
+import {IXosTableColumn} from '../../table/table';
+
+export interface IXosModelDefsField {
+ name: string;
+ type: string;
+}
+
+export interface IXosConfigHelpersService {
+ modeldefToTableCfg(fields: IXosModelDefsField[]): any[]; // TODO use a proper interface
+ pluralize(string: string, quantity?: number, count?: boolean): string;
+ toLabel(string: string, pluralize?: boolean): string;
+}
+
+export class ConfigHelpers {
+
+ constructor() {
+ pluralize.addIrregularRule('xos', 'xosses');
+ pluralize.addPluralRule(/slice$/i, 'slices');
+ }
+
+ pluralize(string: string, quantity?: number, count?: boolean): string {
+ return pluralize(string, quantity, count);
+ }
+
+ toLabel(string: string, pluralize?: boolean): string {
+
+ if (angular.isArray(string)) {
+ return _.map(string, s => {
+ return this.toLabel(s, pluralize);
+ });
+ }
+
+ if (pluralize) {
+ string = this.pluralize(string);
+ }
+
+ string = this.fromCamelCase(string);
+ string = this.fromSnakeCase(string);
+ string = this.fromKebabCase(string);
+
+ return this.capitalizeFirst(string);
+ }
+
+ modeldefToTableCfg(fields: IXosModelDefsField[]): IXosTableColumn[] {
+ const excluded_fields = [
+ 'created',
+ 'updated',
+ 'enacted',
+ 'policed',
+ 'backend_register',
+ 'deleted',
+ 'write_protect',
+ 'lazy_blocked',
+ 'no_sync',
+ 'no_policy',
+ 'omf_friendly',
+ 'enabled'
+ ];
+ const cfg = _.map(fields, (f) => {
+ if (excluded_fields.indexOf(f.name) > -1) {
+ return;
+ }
+ const col: IXosTableColumn = {
+ label: this.toLabel(f.name),
+ prop: f.name
+ };
+
+ if (f.name === 'backend_status') {
+ col.type = 'icon';
+ col.formatter = (item) => {
+ if (item.backend_status.indexOf('1') > -1) {
+ return 'check';
+ }
+ if (item.backend_status.indexOf('2') > -1) {
+ return 'exclamation-circle';
+ }
+ if (item.backend_status.indexOf('0') > -1) {
+ return 'clock-o';
+ }
+ };
+ }
+ return col;
+ })
+ .filter(v => angular.isDefined(v));
+
+ return cfg;
+ };
+
+ private fromCamelCase(string: string): string {
+ return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
+ }
+
+ private fromSnakeCase(string: string): string {
+ return string.split('_').join(' ').trim();
+ }
+
+ private fromKebabCase(string: string): string {
+ return string.split('-').join(' ').trim();
+ }
+
+ private capitalizeFirst(string: string): string {
+ return string.slice(0, 1).toUpperCase() + string.slice(1);
+ }
+}
+