@qrvey/utils 1.1.2 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/README.md +196 -143
  2. package/dist/cjs/constants/COLUMN_PROPERTY_VALUE.js +1 -1
  3. package/dist/cjs/date/constants/DATE_VALUES.js +1 -1
  4. package/dist/cjs/filters/adapters/FDToFlatUI.js +2 -1
  5. package/dist/cjs/filters/adapters/FDToUI.js +2 -1
  6. package/dist/cjs/filters/adapters/UIToFlatUI.js +2 -1
  7. package/dist/cjs/filters/adapters/UIToOldLogic.js +2 -1
  8. package/dist/cjs/filters/adapters/adaptFilterValues.d.ts +8 -0
  9. package/dist/cjs/filters/adapters/adaptFilterValues.js +18 -0
  10. package/dist/cjs/filters/adapters/flatUIToFD.js +2 -1
  11. package/dist/cjs/filters/adapters/flatUIToUI.js +2 -1
  12. package/dist/cjs/filters/adapters/logicToFlatUI.js +0 -3
  13. package/dist/cjs/filters/helpers/common/getFilterColumnLabel.js +1 -1
  14. package/dist/cjs/filters/helpers/common/getFiltersByParams.js +1 -1
  15. package/dist/cjs/filters/helpers/ui/getFilterPropertyLabel.d.ts +7 -0
  16. package/dist/cjs/filters/helpers/ui/getFilterPropertyLabel.js +13 -0
  17. package/dist/cjs/filters/helpers/ui/index.d.ts +1 -0
  18. package/dist/cjs/filters/helpers/ui/index.js +1 -0
  19. package/dist/cjs/filters/interfaces/common/IFSValueRelativeDate.d.ts +1 -1
  20. package/dist/cjs/general/array/filterNestedTree.d.ts +5 -2
  21. package/dist/cjs/general/array/filterNestedTree.js +13 -5
  22. package/dist/cjs/general/mix/isEmpty.js +3 -1
  23. package/dist/cjs/general/mix/isNull.js +1 -1
  24. package/dist/cjs/general/mix/randomId.d.ts +6 -3
  25. package/dist/cjs/general/mix/randomId.js +24 -7
  26. package/dist/cjs/general/mix/size.d.ts +5 -5
  27. package/dist/cjs/general/mix/size.js +9 -6
  28. package/dist/cjs/general/object/get.d.ts +1 -1
  29. package/dist/cjs/general/object/getAttribute.d.ts +9 -2
  30. package/dist/cjs/general/object/getAttribute.js +39 -8
  31. package/dist/cjs/general/object/hasProperty.d.ts +1 -1
  32. package/dist/cjs/general/object/hasProperty.js +10 -2
  33. package/dist/cjs/general/object/index.d.ts +1 -0
  34. package/dist/cjs/general/object/index.js +1 -0
  35. package/dist/cjs/general/object/isObject.js +1 -1
  36. package/dist/cjs/general/object/objectCopy.d.ts +7 -0
  37. package/dist/cjs/general/object/objectCopy.js +32 -0
  38. package/dist/cjs/general/string/capitalize.d.ts +3 -3
  39. package/dist/cjs/general/string/capitalize.js +6 -3
  40. package/dist/cjs/qrvey/getPropertyLabel.d.ts +2 -0
  41. package/dist/cjs/qrvey/getPropertyLabel.js +14 -0
  42. package/dist/cjs/qrvey/index.d.ts +1 -0
  43. package/dist/cjs/qrvey/index.js +1 -0
  44. package/dist/constants/COLUMN_PROPERTY_VALUE.js +2 -2
  45. package/dist/date/constants/DATE_VALUES.js +1 -1
  46. package/dist/filters/adapters/FDToFlatUI.js +2 -1
  47. package/dist/filters/adapters/FDToUI.js +2 -1
  48. package/dist/filters/adapters/UIToFlatUI.js +2 -1
  49. package/dist/filters/adapters/UIToOldLogic.js +2 -1
  50. package/dist/filters/adapters/adaptFilterValues.d.ts +8 -0
  51. package/dist/filters/adapters/adaptFilterValues.js +14 -0
  52. package/dist/filters/adapters/flatUIToFD.js +2 -1
  53. package/dist/filters/adapters/flatUIToUI.js +2 -1
  54. package/dist/filters/adapters/logicToFlatUI.js +0 -3
  55. package/dist/filters/helpers/common/getFilterColumnLabel.js +2 -2
  56. package/dist/filters/helpers/common/getFiltersByParams.js +1 -1
  57. package/dist/filters/helpers/ui/getFilterPropertyLabel.d.ts +7 -0
  58. package/dist/filters/helpers/ui/getFilterPropertyLabel.js +9 -0
  59. package/dist/filters/helpers/ui/index.d.ts +1 -0
  60. package/dist/filters/helpers/ui/index.js +1 -0
  61. package/dist/filters/interfaces/common/IFSValueRelativeDate.d.ts +1 -1
  62. package/dist/general/array/filterNestedTree.d.ts +5 -2
  63. package/dist/general/array/filterNestedTree.js +13 -5
  64. package/dist/general/mix/isEmpty.js +3 -1
  65. package/dist/general/mix/isNull.js +1 -1
  66. package/dist/general/mix/randomId.d.ts +6 -3
  67. package/dist/general/mix/randomId.js +24 -7
  68. package/dist/general/mix/size.d.ts +5 -5
  69. package/dist/general/mix/size.js +9 -6
  70. package/dist/general/object/get.d.ts +1 -1
  71. package/dist/general/object/getAttribute.d.ts +9 -2
  72. package/dist/general/object/getAttribute.js +39 -8
  73. package/dist/general/object/hasProperty.d.ts +1 -1
  74. package/dist/general/object/hasProperty.js +8 -0
  75. package/dist/general/object/index.d.ts +1 -0
  76. package/dist/general/object/index.js +1 -0
  77. package/dist/general/object/isObject.js +1 -1
  78. package/dist/general/object/objectCopy.d.ts +7 -0
  79. package/dist/general/object/objectCopy.js +28 -0
  80. package/dist/general/string/capitalize.d.ts +3 -3
  81. package/dist/general/string/capitalize.js +6 -3
  82. package/dist/qrvey/getPropertyLabel.d.ts +2 -0
  83. package/dist/qrvey/getPropertyLabel.js +10 -0
  84. package/dist/qrvey/index.d.ts +1 -0
  85. package/dist/qrvey/index.js +1 -0
  86. package/package.json +1 -1
  87. package/src/constants/COLUMN_PROPERTY_VALUE.ts +2 -2
  88. package/src/date/constants/DATE_VALUES.ts +1 -1
  89. package/src/filters/adapters/FDToFlatUI.ts +2 -1
  90. package/src/filters/adapters/FDToUI.ts +2 -0
  91. package/src/filters/adapters/UIToFlatUI.ts +2 -1
  92. package/src/filters/adapters/UIToOldLogic.ts +2 -1
  93. package/src/filters/adapters/adaptFilterValues.ts +17 -0
  94. package/src/filters/adapters/flatUIToFD.ts +2 -1
  95. package/src/filters/adapters/flatUIToUI.ts +2 -1
  96. package/src/filters/adapters/logicToFlatUI.ts +0 -3
  97. package/src/filters/helpers/backend/getAggFiltersBySummaryIndex.ts +1 -1
  98. package/src/filters/helpers/common/getFilterColumnLabel.ts +2 -2
  99. package/src/filters/helpers/common/getFiltersByParams.ts +1 -1
  100. package/src/filters/helpers/ui/getFilterPropertyLabel.ts +11 -0
  101. package/src/filters/helpers/ui/index.ts +1 -0
  102. package/src/filters/interfaces/common/IFSValueRelativeDate.ts +1 -1
  103. package/src/general/array/filterNestedTree.ts +13 -4
  104. package/src/general/mix/getTag.ts +1 -1
  105. package/src/general/mix/isEmpty.ts +3 -1
  106. package/src/general/mix/isNull.ts +1 -1
  107. package/src/general/mix/randomId.ts +24 -7
  108. package/src/general/mix/size.ts +10 -6
  109. package/src/general/object/get.ts +1 -1
  110. package/src/general/object/getAttribute.ts +47 -9
  111. package/src/general/object/hasProperty.ts +10 -1
  112. package/src/general/object/index.ts +1 -0
  113. package/src/general/object/isObject.ts +1 -1
  114. package/src/general/object/objectCopy.ts +35 -0
  115. package/src/general/string/capitalize.ts +7 -3
  116. package/src/qrvey/getPropertyLabel.ts +9 -0
  117. package/src/qrvey/index.ts +1 -0
  118. package/test/general/array/filterNestedTree.test.js +115 -0
  119. package/test/general/mix/getTag.test.js +101 -0
  120. package/test/general/mix/isEmpty.test.js +24 -24
  121. package/test/general/mix/isNull.test.js +72 -0
  122. package/test/general/mix/randomId.test.js +134 -0
  123. package/test/general/mix/size.test.js +87 -0
  124. package/test/general/object/cloneDeep.test.js +8 -16
  125. package/test/general/object/getAttribute.test.js +316 -0
  126. package/test/general/object/hasProperty.test.js +103 -0
  127. package/test/general/object/objectCopy.test.js +105 -0
  128. package/test/general/string/capitalize.test.js +87 -0
@@ -1,3 +1,4 @@
1
1
  export * from './getColumnsLabel';
2
+ export * from './getPropertyLabel';
2
3
  export * from './isDateColumn';
3
4
  export * from './isComplexColumn';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qrvey/utils",
3
- "version": "1.1.2",
3
+ "version": "1.1.6",
4
4
  "description": "Helper, Utils for all Qrvey Projects",
5
5
  "homepage": "https://bitbucket.org/qrvey/qrvey_utils/wiki/Home",
6
6
  "main": "dist/index.js",
@@ -1,4 +1,4 @@
1
- import { DATE_P_VALUES, DATE_TIME_P_VALUES } from "../date";
1
+ import { DATE_P_VALUES, DATE_TIME_VALUES, DATE_TIME_P_VALUES } from "../date";
2
2
  import { DATE_VALUES } from "../date/constants/DATE_VALUES";
3
3
  import { COLUMN } from "./COLUMN";
4
4
  import { COLUMN_COMPLEX_VALUE } from "./COLUMN_COMPLEX_VALUE";
@@ -20,6 +20,6 @@ const C = {
20
20
  [COLUMN.IMAGEUPLOAD]: COLUMN_COMPLEX_VALUE.IMAGEUPLOAD.map(setTabStructure),
21
21
  };
22
22
 
23
- C[COLUMN.DATE + '-complete'] = C[COLUMN.DATE].concat(DATE_P_VALUES).concat(DATE_TIME_P_VALUES);
23
+ C[COLUMN.DATE + '-complete'] = C[COLUMN.DATE].concat(DATE_TIME_VALUES).concat(DATE_P_VALUES).concat(DATE_TIME_P_VALUES);
24
24
 
25
25
  export const COLUMN_PROPERTY_VALUE = C;
@@ -5,5 +5,5 @@ export const DATE_VALUES = [
5
5
  { value: COLUMN_PROPERTY.DATE_YEAR, label: 'Year', display: true },
6
6
  { value: COLUMN_PROPERTY.DATE_QUARTER, label: 'Quarter, Year', display: true },
7
7
  { value: COLUMN_PROPERTY.DATE_MONTH, label: 'Month, Year', display: true },
8
- { value: COLUMN_PROPERTY.DATE_WEEK, label: 'Weeks, Year', display: true }
8
+ { value: COLUMN_PROPERTY.DATE_WEEK, label: 'Week, Year', display: true }
9
9
  ];
@@ -3,6 +3,7 @@ import { IQrveyColumn, IQrveyDataset } from "../../interfaces/Qrvey.Interface";
3
3
  import { getUIValues } from "../helpers";
4
4
  import { IFSData, IFSScope, IFUFlattenedFilter } from "../interfaces";
5
5
  import { adaptDateGroupingProperty } from "./adaptDateGroupingProperty";
6
+ import { adaptFilterValues } from "./adaptFilterValues";
6
7
 
7
8
  /**
8
9
  * Generates a Flattened UI filter structure from Filter Data structure.
@@ -93,7 +94,7 @@ function FD21ToFlatUI(scopes: IFSScope[] = [], datasetsInfo: IQrveyDataset[] = [
93
94
  property: property,
94
95
  uiValues: getUIValues(filter),
95
96
  validator: filter.validator,
96
- values: filter.values,
97
+ values: adaptFilterValues(filter),
97
98
  };
98
99
  uFilters.push(uFilter);
99
100
  });
@@ -4,6 +4,7 @@ import { IQrveyColumn, IQrveyDataset } from "../../interfaces";
4
4
  import { FILTER_SECTION, FILTER_STRUCTURE_VERSION } from "../constants";
5
5
  import { getUIValues } from "../helpers";
6
6
  import { IFSData, IFSection, IFSScope, IFUData, IFUScope } from "../interfaces";
7
+ import { adaptFilterValues } from "./adaptFilterValues";
7
8
 
8
9
  /**
9
10
  * Generates a UI filter structure from Filter Data structure.
@@ -70,6 +71,7 @@ import { IFSData, IFSection, IFSScope, IFUData, IFUScope } from "../interfaces";
70
71
  },
71
72
  property,
72
73
  uiValues: getUIValues(filter),
74
+ values: adaptFilterValues(filter),
73
75
  };
74
76
  })
75
77
  };
@@ -3,6 +3,7 @@ import { cloneDeep, isEmpty } from "../../general";
3
3
  import { IQrveyColumn, IQrveyDataset } from "../../interfaces/Qrvey.Interface";
4
4
  import { getUIValues } from "../helpers";
5
5
  import { IFUData, IFUFlattenedFilter, IFUScope } from "../interfaces";
6
+ import { adaptFilterValues } from "./adaptFilterValues";
6
7
 
7
8
  /**
8
9
  * Generates a Flattened UI filter structure from UI Filter Data structure.
@@ -92,7 +93,7 @@ function UI21ToFlatUI(scopes: IFUScope[] = [], datasetsInfo: IQrveyDataset[] = [
92
93
  property,
93
94
  uiValues: getUIValues(filter),
94
95
  validator: filter.validator,
95
- values: filter.values,
96
+ values: adaptFilterValues(filter),
96
97
  };
97
98
  uFilters.push(uFilter);
98
99
  });
@@ -3,6 +3,7 @@ import { cloneDeep, isEmpty } from "../../general";
3
3
  import { FILTER_OPERATOR } from "../constants";
4
4
  import { getBackendGroupValue, getBackendProperty, getBackendValidator, getBackendValues, getUIValues } from "../helpers";
5
5
  import { IFSection, IFUData, IFUDataset, IFUFilter, IFUScope, OLD_IFilterData, OLD_IFilterDetail, OLD_IFilterExpression, OLD_IFilterFilters, OLD_IFilterLogic } from "../interfaces";
6
+ import { adaptFilterValues } from "./adaptFilterValues";
6
7
  import { flatUIToOldLogic } from "./flatUIToOldLogic";
7
8
  import { UIToFlatUI } from "./UIToFlatUI";
8
9
 
@@ -146,6 +147,6 @@ const getFilterDetail = (filter: IFUFilter, scope: IFUScope, dataset: IFUDataset
146
147
  section: filter.extras.section,
147
148
  uiValue: getUIValues(filter, true),
148
149
  validator: filter.validator,
149
- values: filter.values
150
+ values: adaptFilterValues(filter)
150
151
  };
151
152
  };
@@ -0,0 +1,17 @@
1
+ import { IFSFilter, IFSValue, IFSValueRange, IFSValueRanking, IFSValueRelativeDate, IFValue } from "..";
2
+ import {COLUMN, isEmpty } from "../..";
3
+
4
+ /**
5
+ * [TODO: For 2022, eliminate this adapter]
6
+ * Gets an adapted filter value array. Validates the enabled property and sets
7
+ * @param filter The filter
8
+ * @returns A new value array with the filled properties.
9
+ */
10
+ export function adaptFilterValues(filter: IFSFilter): IFValue[] {
11
+ const setValue = value => ({ ...value, enabled: isEmpty(value.enabled) ? true : value.enabled });
12
+
13
+ if (filter.column.type === COLUMN.RANKING && isEmpty(filter.column.aggregate)) {
14
+ return (filter.values as IFSValueRanking[][]).map(rValues => rValues.map(setValue));
15
+ }
16
+ return (filter.values as IFSValue[] | IFSValueRange[] | IFSValueRelativeDate[]).map(setValue);
17
+ }
@@ -2,6 +2,7 @@ import { adaptDateGroupingProperty } from ".";
2
2
  import { isEmpty } from "../../general";
3
3
  import { FILTER_SECTION, FILTER_STRUCTURE_VERSION } from "../constants";
4
4
  import { IFSData, IFSDataset, IFSection, IFSFilter, IFSScope, IFUFlattenedFilter } from "../interfaces";
5
+ import { adaptFilterValues } from "./adaptFilterValues";
5
6
 
6
7
  /**
7
8
  * Generates a filter data structure from the flatttened UI filters.
@@ -114,6 +115,6 @@ function buildFilter(uFilter: IFUFlattenedFilter): IFSFilter {
114
115
  operator: uFilter.operator,
115
116
  property,
116
117
  validator: uFilter.validator,
117
- values: uFilter.values,
118
+ values: adaptFilterValues(uFilter),
118
119
  };
119
120
  }
@@ -3,6 +3,7 @@ import { isEmpty, getLastIndexFromArray } from "../../general";
3
3
  import { FILTER_SECTION, FILTER_STRUCTURE_VERSION } from "../constants";
4
4
  import { getUIValues } from "../helpers";
5
5
  import { IFSection, IFUData, IFUDataset, IFUFilter, IFUFlattenedFilter, IFUScope } from "../interfaces";
6
+ import { adaptFilterValues } from "./adaptFilterValues";
6
7
 
7
8
  /**
8
9
  * Generates a UI filter data structure from the flatttened UI filters.
@@ -128,6 +129,6 @@ function buildFilter(uFilter: IFUFlattenedFilter): IFUFilter {
128
129
  property,
129
130
  uiValues: getUIValues(uFilter),
130
131
  validator: uFilter.validator,
131
- values: uFilter.values,
132
+ values: adaptFilterValues(uFilter),
132
133
  };
133
134
  }
@@ -25,9 +25,6 @@ export function logicToFlatUI(logics: OLD_IFilterLogic[] = []): IFUFlattenedFilt
25
25
  buildFilterByExpression(expression2, scopeItem, uFilters);
26
26
  }) : buildFilterByExpression(expression1, scopeItem, uFilters);
27
27
  });
28
- // (filterItem.expressions[0].expressions as OLD_IFilterExpression[]).forEach((expression: OLD_IFilterExpression) => {
29
- // buildFilterByExpression(expression, scopeItem, uFilters);
30
- // });
31
28
  });
32
29
  }
33
30
  });
@@ -9,7 +9,7 @@ import { IFBFilterAggregate } from "../../interfaces";
9
9
  */
10
10
  export function getAggFiltersBySummaryIndex(aggFilters: IFBFilterAggregate, summaryIndex: number): IFBFilterAggregate {
11
11
  if (!aggFilters) return undefined;
12
- const expressions = filterNestedTree(aggFilters.expressions, 'expressions', expression => {
12
+ const expressions = filterNestedTree(aggFilters.expressions as any, 'expressions', expression => {
13
13
  const isSameSummary = expression.summaryIndex === summaryIndex;
14
14
  if (isSameSummary) expression.summaryIndex = 0;
15
15
  return isSameSummary;
@@ -1,6 +1,6 @@
1
1
  import { isComplexColumn } from "../../../qrvey/isComplexColumn";
2
2
  import { IFUColumn } from "../../interfaces";
3
- import { COLUMN_COMPLEX_VALUE } from "../../../constants";
3
+ import { AGGREGATE_ABBREVIATION, COLUMN_COMPLEX_VALUE } from "../../../constants";
4
4
 
5
5
  /**
6
6
  * Get an string of the properties of the given filter column.
@@ -10,7 +10,7 @@ import { COLUMN_COMPLEX_VALUE } from "../../../constants";
10
10
  export function getFilterColumnLabel(column: IFUColumn): string {
11
11
  const labels: string[] = [];
12
12
  if (isComplexColumn(column as any) && column.property != null) labels.push(COLUMN_COMPLEX_VALUE[column.property].shortLabel);
13
- if (column.aggregate != null) labels.push(column.aggregate);
13
+ if (column.aggregate != null) labels.push(AGGREGATE_ABBREVIATION[column.aggregate]);
14
14
  if (column.calculation != null) labels.push(column.calculation);
15
15
 
16
16
  return labels.length > 0 ? `(${labels.join(' - ')})` : '';
@@ -42,7 +42,7 @@ export function getFiltersByParams(filterData: IFSData | IFUData, params: IFSPar
42
42
  values: filter.values.map((value: IFValue) => {
43
43
  if (Array.isArray(value) && value.length > 0) {
44
44
  const rankingValues = value as IFSValueRanking[];
45
- rankingValues.map(rValue => {
45
+ return rankingValues.map(rValue => {
46
46
  if (!_hasProperty(params, 'enableds') || !_hasProperty(params.enableds, 'values') || params.enableds.values === rValue.enabled) return rValue;
47
47
  }).filter(Boolean);
48
48
  } else {
@@ -0,0 +1,11 @@
1
+ import { IFSFilter, IFUFilter } from "../..";
2
+ import { getPropertyLabel } from "../../..";
3
+
4
+ /**
5
+ * Gets the label of the filter property
6
+ * @param filter The UI Filter
7
+ * @returns a string of the filter property label
8
+ */
9
+ export function getFilterPropertyLabel(filter: IFUFilter | IFSFilter): string {
10
+ return getPropertyLabel(filter.column as any, filter.property);
11
+ }
@@ -1,5 +1,6 @@
1
1
  export * from './excludeUIFiltersByAggregate';
2
2
  export * from './excludeUIFlatFiltersByScopes';
3
+ export * from './getFilterPropertyLabel';
3
4
  export * from './getOutputFormatByColumn';
4
5
  export * from './getUIFlatFilterByParams';
5
6
  export * from './getUIFlatFiltersByParams';
@@ -1,3 +1,3 @@
1
1
  import { RelativeStatement } from "../../../date";
2
2
 
3
- export type IFSValueRelativeDate = { enabled: boolean; } & RelativeStatement;
3
+ export type IFSValueRelativeDate = { enabled?: boolean; } & RelativeStatement;
@@ -2,18 +2,27 @@ import { isEmpty } from "../mix";
2
2
  import { cloneDeep } from "../object/cloneDeep";
3
3
 
4
4
  /**
5
- * Filters a nested tree array by a custom condition on the las child node
5
+ * Filters a nested tree array by a custom condition on the last child node
6
+ * - If the given arguments are not valid, the function returns the first argument.
7
+ * - If the childArrKey is not matched in the object, the condition tries to resolve the filter anyway and returns an empty array.
8
+ * - If the condition is not fulfilled, the function returns a filtered array, probably a empty array inside of the child array
6
9
  * @param arr nested tree array
7
10
  * @param childArrKey property representing the children array on the nested tree
8
11
  * @param condition function callback that determines if the filter is applied on the last child node of the nested tree
9
12
  * @returns array filtered
10
13
  */
11
- export function filterNestedTree(arr: any[], childArrKey: string, condition: any): any[] {
12
- if (isEmpty(arr)) return [];
14
+ export function filterNestedTree<T = any>(arr: T[] = [], childArrKey = '', condition: any = undefined): T[] {
15
+ if (!isValid(arr, childArrKey, condition)) return arr;
13
16
 
14
17
  return cloneDeep(arr).filter(obj => {
15
- const hasChildArr = Array.isArray(obj[childArrKey]) && obj[childArrKey].length;
18
+ const hasChildArr = Array.isArray(obj[childArrKey]) && obj[childArrKey].length > 0;
16
19
  if (hasChildArr) obj[childArrKey] = filterNestedTree(obj[childArrKey], childArrKey, condition);
17
20
  return hasChildArr || condition(obj);
18
21
  });
19
22
  }
23
+
24
+ function isValid<T = any>(arr: T[] = [], childArrKey = '', condition: any = undefined): boolean {
25
+ return (!isEmpty(arr) && Array.isArray(arr))
26
+ && (!isEmpty(childArrKey) && typeof childArrKey === 'string')
27
+ && (!isEmpty(condition) && typeof condition === "function");
28
+ }
@@ -4,7 +4,7 @@
4
4
  * @param {*} value The value to query.
5
5
  * @returns {string} Returns the `toStringTag`.
6
6
  */
7
- export function getTag(value): string {
7
+ export function getTag(value: any): string {
8
8
  if (value == null) {
9
9
  return value === undefined ? '[object Undefined]' : '[object Null]';
10
10
  }
@@ -14,5 +14,7 @@ export function isEmpty<T = any>(variable: T, includeFalsy = false): boolean {
14
14
  || (typeof variable == 'boolean' && includeFalsy && variable === false)
15
15
  || (Array.isArray(variable) && variable.length === 0)
16
16
  || ((getTag(variable) === '[object Map]' || getTag(variable) === '[object Set]') && (variable as any | Set<T>).size === 0)
17
- || (isObject(variable) && Object.entries(variable).length === 0);
17
+ || (variable instanceof Date && isNaN(variable.valueOf()))
18
+ || (typeof variable === 'function' && Object.prototype.toString.call(variable).indexOf("Function") === 0)
19
+ || ((!(variable instanceof Date) && typeof variable !== 'function') && isObject(variable) && Object.entries(variable).length === 0);
18
20
  }
@@ -5,5 +5,5 @@
5
5
  * @returns {Boolean}
6
6
  */
7
7
  export function isNull(arg: any): boolean {
8
- return arg === null || arg === undefined;
8
+ return arg == null;
9
9
  }
@@ -1,14 +1,31 @@
1
+ import { isEmpty, isNull } from ".";
2
+
1
3
  /**
2
- * create random unique string
4
+ * Creates a random string
5
+ * - If the first given argument is different than a length number, the variable is replaced by a default number
6
+ * - If the optional second given argument is passed the random string is permutated.
3
7
  * @param {Number} length size of the generated string. Default 8
4
- * @return {String}
8
+ * @param {Array} exclude collection of strings that is going to be excluded of the random string.
9
+ * @return {String} Random string
5
10
  */
6
- export function randomId (_length = 8): string {
11
+ export function randomId(length = 8, exclude: string[] = []): string {
12
+ if (typeof length !== 'number') length = 8;
13
+ if (isNull(exclude) || !Array.isArray(exclude)) exclude = [];
14
+ exclude = exclude.filter(e => typeof e === 'string');
15
+
7
16
  const charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_';
17
+ const blackList = [];
8
18
  let id = '';
9
- for (let i = 0; i < 8; i++) {
10
- const randPos = Math.floor(Math.random() * charSet.length);
11
- id += charSet[randPos];
12
- }
19
+
20
+ do {
21
+ if (!isEmpty(id) && !blackList.includes(id)) blackList.push(id);
22
+ id = '';
23
+ for (let i = 0; i < length; i++) {
24
+ const randPos = Math.floor(Math.random() * charSet.length);
25
+ id += charSet[randPos];
26
+ }
27
+ } while (exclude.length > 0 && exclude.includes(id) && blackList.length < exclude.length);
28
+
29
+ if (exclude.length > 0 && blackList.length >= exclude.length) id = '';
13
30
  return id;
14
31
  }
@@ -1,10 +1,14 @@
1
+ import { isEmpty } from ".";
2
+
1
3
  /**
2
- * get the length of an Array or String and also the number of first level attributes
3
- * in an Object.
4
- * For `null` or `undefined` argument the returned value will be 0.
5
- * @param {Any} obj
6
- * @returns {Number}
4
+ * Gets the length of the given array.
5
+ * - Useful for Object, Array and string type.
6
+ * - For `null` or `undefined` or else argument the returned value will be 0.
7
+ * @param {Any} obj Any object-type variable
8
+ * @returns {Number} the size of the given variable
7
9
  */
8
10
  export function size(obj: any): number {
9
- return Object.keys(obj || '').length;
11
+ if (isEmpty(obj)) return 0;
12
+
13
+ return Object.keys(obj).length;
10
14
  }
@@ -26,7 +26,7 @@
26
26
  * _get({ item1: { item11: 'Hello Again' }, item2: {} }, ['item1', 'item11'])
27
27
  * @returns the resolved value.
28
28
  */
29
- export function _get(baseObject: any, path: string | string[], defaultValue?: any): any {
29
+ export function _get<T = any>(baseObject: T, path: string | string[], defaultValue?: any): any {
30
30
  if (validateToReturnDefaultValue(baseObject, path)) return defaultValue;
31
31
 
32
32
  const keys = Array.isArray(path) ? path : path.replace(/(\[(\d)\])/g, '.$2').split('.').filter(k => k.length);
@@ -1,6 +1,13 @@
1
1
  /**
2
- * search for properties in different case styles such as: lower, upper, camel and pascal
3
- * But for this, is required ask for a property in snake_case style
2
+ * Searchs for properties in different case styles such as: lower, upper, camel and pascal
3
+ * - To optimize the searching, it is required a key in a snake_case style
4
+ * - List of cases that do not match
5
+ * -- From lower to snake case
6
+ * -- From upper to snake case
7
+ * -- From lower to camel case
8
+ * -- From upper to camel case
9
+ * -- From lower to pascal case
10
+ * -- From upper to pascal case
4
11
  * @param {object} obj object to look for
5
12
  * @param {string} key String attribute in snake_case style
6
13
  *
@@ -8,11 +15,42 @@
8
15
  * getAttribute(obj, 'snake_case') //it search for: obj.snake_case || obj.snakeCase || obj.SnakeCase || obj.snakecase || obj.SNAKECASE
9
16
  */
10
17
 
11
- export function getAttribute(obj: any, key: string) {
12
- // getAttr: key should be lower_underscore_case
13
- const camel = key.replace(/_(.)/g, (_, a) => a.toUpperCase());
14
- const pascal = camel.replace(/(.)/, (_, a) => a.toUpperCase());
15
- const lower = key.replace(/_/g, '');
16
- const upper = lower.toUpperCase();
17
- return obj[key] || obj[camel] || obj[pascal] || obj[lower] || obj[upper];
18
+ import { isObject } from ".";
19
+ import { isEmpty } from "..";
20
+
21
+ export function getAttribute(obj: any, key: string): any {
22
+ if (!isValid(obj, key)) return;
23
+
24
+ const snake = getSnakeCase(key);
25
+ const camel = getCamelCase(snake);
26
+ const pascal = getPascalCase(camel);
27
+ const lower = getLowerCase(pascal);
28
+ const upper = getUpperCase(lower);
29
+
30
+ return obj[key] ?? obj[snake] ?? obj[camel] ?? obj[pascal] ?? obj[lower] ?? obj[upper];
31
+ }
32
+
33
+ function getSnakeCase(key: string): string {
34
+ return key.replace(/[A-Z]/g, (a, i) => i == 0 ? a.toLowerCase() : '_'+ a.toLowerCase());
35
+ }
36
+
37
+ function getCamelCase(key: string): string {
38
+ return key.replace(/(.)/, (_, a) => a.toLowerCase()).replace(/_(.)/g, (_, a) => a.toUpperCase());
39
+ }
40
+
41
+ function getPascalCase(key: string): string {
42
+ return key.replace(/(.)/, (_, a) => a.toUpperCase());
43
+ }
44
+
45
+ function getLowerCase(key: string): string {
46
+ return key.replace(/_/g, '').toLowerCase();
47
+ }
48
+
49
+ function getUpperCase(key: string): string {
50
+ return key.toUpperCase();
51
+ }
52
+
53
+ function isValid(obj: any, key: string): boolean {
54
+ return (!isEmpty(obj) && isObject(obj))
55
+ && (!isEmpty(key) && typeof key === 'string');
18
56
  }
@@ -1,3 +1,6 @@
1
+ import { isObject } from ".";
2
+ import { isEmpty } from "..";
3
+
1
4
  /**
2
5
  * Use the hasOwnProperty in order to verify if the given property exists in the object.
3
6
  *
@@ -14,7 +17,13 @@
14
17
  * const obj2 = { prop1: 'hello world' }
15
18
  * _hasProperty(ob1, prop2) // false
16
19
  */
20
+ export function _hasProperty(obj = {}, property: string | number = ''): boolean {
21
+ if (!isValid(obj, property)) return false;
17
22
 
18
- export function _hasProperty(obj = {}, property = ''): boolean {
19
23
  return Object.prototype.hasOwnProperty.call(obj, property);
20
24
  }
25
+
26
+ function isValid(obj, property): boolean {
27
+ return !isEmpty(obj) && (isObject(obj) || Array.isArray(obj))
28
+ && !isEmpty(property) && (typeof property === 'string' || typeof property === 'number');
29
+ }
@@ -3,6 +3,7 @@ export * from './get';
3
3
  export * from './getAttribute';
4
4
  export * from './hasProperty';
5
5
  export * from './isObject';
6
+ export * from './objectCopy';
6
7
  export * from './omit';
7
8
  export * from './pick';
8
9
  export * from './mapValues';
@@ -6,5 +6,5 @@ import { getTag } from "..";
6
6
  * @returns True: It is an object; False: It is not.
7
7
  */
8
8
  export function isObject(obj: any): boolean {
9
- return getTag(obj) === '[object Object]';
9
+ return obj === Object(obj) || getTag(obj) === '[object Object]';
10
10
  }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Created a new reference of the given argument
3
+ * @param entity The variable to be copied
4
+ * @param cache
5
+ * @returns A new reference of the given argument
6
+ */
7
+ export function objectCopy(entity, cache = new WeakMap()): any {
8
+ if (!isClonable(entity)) return entity;
9
+
10
+ if (cache.has(entity)) return cache.get(entity);
11
+
12
+ const c = new (entity as any).constructor;
13
+
14
+ if (c.name === 'Date') return new Date(entity as any) as any;
15
+
16
+ if (entity instanceof Map) {
17
+ entity.forEach((value, key) => c.set(objectCopy(key), objectCopy(value)));
18
+ }
19
+
20
+ if (entity instanceof Set) {
21
+ entity.forEach((value) => c.add(objectCopy(value)));
22
+ }
23
+
24
+ cache.set(entity, c);
25
+
26
+ return Object.assign(c,
27
+ ...Object.keys(entity).map((prop) => ({ [prop]: objectCopy(entity[prop], cache) }))
28
+ );
29
+ }
30
+
31
+ function isClonable(entity) {
32
+ const referenceTypes = ['Array', 'Object', 'Map', 'Set', 'Date'];
33
+ const entityName = entity && entity.constructor.name;
34
+ return referenceTypes.includes(entityName);
35
+ }
@@ -1,8 +1,12 @@
1
+ import { isEmpty } from "..";
2
+
1
3
  /**
2
- * Upper case teh first letter of a given text
4
+ * Upper case the first letter of a given text
3
5
  * @param {String} text
4
- * @returns {String}
6
+ * @returns {String} a capitalized text
5
7
  */
6
- export function capitalize(text: string): string {
8
+ export function capitalize(text = ''): string {
9
+ if (isEmpty(text) || typeof text !== 'string') return text;
10
+
7
11
  return text.toLowerCase().replace(/^\w/, c => c.toUpperCase());
8
12
  }
@@ -0,0 +1,9 @@
1
+ import { isDateColumn } from ".";
2
+ import { COLUMN_PROPERTY, COLUMN_PROPERTY_VALUE, IQrveyColumn, isEmpty } from "..";
3
+
4
+ export function getPropertyLabel(column: IQrveyColumn, property: COLUMN_PROPERTY): string {
5
+ let propertyLabel;
6
+ const propertyValues = COLUMN_PROPERTY_VALUE[isDateColumn(column) ? 'DATE-complete' : column.type];
7
+ if (!isEmpty(propertyValues)) propertyLabel = propertyValues.find(pValue => pValue.value === property)?.label;
8
+ return propertyLabel;
9
+ }
@@ -1,3 +1,4 @@
1
1
  export * from './getColumnsLabel';
2
+ export * from './getPropertyLabel';
2
3
  export * from './isDateColumn';
3
4
  export * from './isComplexColumn';