Skip to content

Commit 76eb88f

Browse files
committed
format utils, merge stringUtils into single utils
1 parent b932d3f commit 76eb88f

File tree

2 files changed

+62
-44
lines changed

2 files changed

+62
-44
lines changed

src/utils/stringUtils.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/utils/utils.ts

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
// External Dependencies
22
import { isEmpty } from 'lodash-es';
33

4+
//--------------------------------------------------------------
5+
// public constants
6+
//--------------------------------------------------------------
7+
8+
export const FIELD_SET = 'fieldset';
9+
export const HELP_VALUE = 'helpvalue';
10+
export const REQUIRED_PROPERTY = 'required';
11+
export const CHECKBOXES = 'checkboxes';
12+
export const INACTIVE_ENUM = 'inactive_enum';
13+
export const DISABLED_ENUM = 'inactive_titleMap';
14+
export const STRING_TYPE = 'string';
15+
export const ARRAY_TYPE = 'array';
16+
export const ENUM = 'enum';
17+
18+
//--------------------------------------------------------------
19+
// public enums
20+
//--------------------------------------------------------------
21+
422
export enum DateTimeFormat {
523
DateTime = 'date-time',
624
Date = 'date',
@@ -17,39 +35,9 @@ export enum ElementDisplay {
1735
Header = 'header',
1836
}
1937

20-
export const FIELD_SET = 'fieldset';
21-
export const HELP_VALUE = 'helpvalue';
22-
export const REQUIRED_PROPERTY = 'required';
23-
export const CHECKBOXES = 'checkboxes';
24-
export const INACTIVE_ENUM = 'inactive_enum';
25-
export const DISABLED_ENUM = 'inactive_titleMap';
26-
export const STRING_TYPE = 'string';
27-
export const ARRAY_TYPE = 'array';
28-
export const ENUM = 'enum';
29-
30-
export const isObject = (item: any) => item instanceof Object;
31-
32-
export const isString = (item: any) => typeof item === STRING_TYPE;
33-
export const isSchemaFieldSet = (definition: any[]) => {
34-
if (definition.length === 0) {
35-
return false;
36-
}
37-
const fieldSet = definition.find((item: any) => isObject(item)
38-
&& item && (item.type || '') === FIELD_SET);
39-
return fieldSet !== undefined;
40-
};
41-
42-
export const isFieldSetTitle = (item: any) => isObject(item) && item.type === FIELD_SET
43-
&& !isEmpty(item.title);
44-
45-
export const isFieldSetTitleWithoutItems = (item: any) => isObject(item) && item.type === FIELD_SET
46-
&& item.items.length === 0 && !isEmpty(item.title);
47-
48-
export const isFieldSet = (item: any) => isObject(item) && item.type === FIELD_SET && item.items.length > 0;
49-
50-
export const isCheckbox = (item: any) => isObject(item) && item.type === CHECKBOXES;
51-
52-
export const isPropertyKey = (item: any) => !isEmpty(item.key);
38+
//--------------------------------------------------------------
39+
// public functions
40+
//--------------------------------------------------------------
5341

5442
export const getFieldSetTitleKey = (title: string) => {
5543
const key = title.toLowerCase().replace(/[\s\\/\\%]/gi, '_');
@@ -63,23 +51,56 @@ export const getSchemaValidations = (stringSchema: string) => ({
6351
hasEnums: hasEnums(stringSchema),
6452
});
6553

54+
export const hasDuplicatedItems = (items: string[]) => (new Set(items).size !== items.length);
55+
6656
export const isArrayProperty = (property: any) => property.type === ARRAY_TYPE && !property.items?.enum
6757
&& !property.items?.enumNames;
6858

69-
export const isRequiredProperty = (property: any) => property.required === 'true' || property.required > 0;
59+
export const isCheckbox = (item: any) => isObject(item) && item.type === CHECKBOXES;
7060

71-
const hasCheckboxes = (stringSchema: string) => stringSchema.includes(CHECKBOXES);
61+
export const isDisabledChoice = (item: any) => isObject(item) && item.type === CHECKBOXES && item.inactive_titleMap?.length > 0
62+
&& item.titleMap?.length > 0;
7263

73-
const hasInactiveChoices = (stringSchema: string) => stringSchema.includes(INACTIVE_ENUM);
64+
export const isEmptyString = (value: string | undefined) => (value === undefined
65+
|| value === null
66+
|| value.trim().length === 0);
7467

75-
const hasDisabledChoices = (stringSchema: string) => stringSchema.includes(DISABLED_ENUM);
68+
export const isFieldSetTitle = (item: any) => isObject(item) && item.type === FIELD_SET
69+
&& !isEmpty(item.title);
7670

77-
const hasEnums = (stringSchema: string) => stringSchema.includes(ENUM);
71+
export const isFieldSetTitleWithoutItems = (item: any) => isObject(item) && item.type === FIELD_SET
72+
&& item.items.length === 0 && !isEmpty(item.title);
73+
74+
export const isFieldSet = (item: any) => isObject(item) && item.type === FIELD_SET && item.items.length > 0;
7875

7976
export const isInactiveChoice = (item: any) => item.type === STRING_TYPE
8077
&& item.enum?.length > 0 && item.inactive_enum?.length > 0;
8178

82-
export const isDisabledChoice = (item: any) => isObject(item) && item.type === CHECKBOXES && item.inactive_titleMap?.length > 0
83-
&& item.titleMap?.length > 0;
79+
export const isObject = (item: any) => item instanceof Object;
8480

85-
export const hasDuplicatedItems = (items: string[]) => (new Set(items).size !== items.length);
81+
export const isPropertyKey = (item: any) => !isEmpty(item.key);
82+
83+
export const isRequiredProperty = (property: any) => property.required === 'true' || property.required > 0;
84+
85+
export const isSchemaFieldSet = (definition: any[]) => {
86+
if (definition.length === 0) {
87+
return false;
88+
}
89+
const fieldSet = definition.find((item: any) => isObject(item)
90+
&& item && (item.type || '') === FIELD_SET);
91+
return fieldSet !== undefined;
92+
};
93+
94+
export const isString = (item: any) => typeof item === STRING_TYPE;
95+
96+
//--------------------------------------------------------------
97+
// private functions
98+
//--------------------------------------------------------------
99+
100+
const hasCheckboxes = (stringSchema: string) => stringSchema.includes(CHECKBOXES);
101+
102+
const hasInactiveChoices = (stringSchema: string) => stringSchema.includes(INACTIVE_ENUM);
103+
104+
const hasDisabledChoices = (stringSchema: string) => stringSchema.includes(DISABLED_ENUM);
105+
106+
const hasEnums = (stringSchema: string) => stringSchema.includes(ENUM);

0 commit comments

Comments
 (0)