@hmcts/opal-frontend-common 0.0.25 → 0.0.26

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.
@@ -0,0 +1,11 @@
1
+ const TRANSFORM_ITEM_DEFAULTS = {
2
+ dateConfig: null,
3
+ timeConfig: null,
4
+ };
5
+
6
+ /**
7
+ * Generated bundle index. Do not edit.
8
+ */
9
+
10
+ export { TRANSFORM_ITEM_DEFAULTS };
11
+ //# sourceMappingURL=hmcts-opal-frontend-common-services-transformation-service-constants.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hmcts-opal-frontend-common-services-transformation-service-constants.mjs","sources":["../../../projects/opal-frontend-common/services/transformation-service/constants/transform-item.constants.ts","../../../projects/opal-frontend-common/services/transformation-service/constants/hmcts-opal-frontend-common-services-transformation-service-constants.ts"],"sourcesContent":["import { ITransformItem } from '@hmcts/opal-frontend-common/services/transformation-service/interfaces';\n\nexport const TRANSFORM_ITEM_DEFAULTS: Pick<ITransformItem, 'dateConfig' | 'timeConfig'> = {\n dateConfig: null,\n timeConfig: null,\n};\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":"AAEO,MAAM,uBAAuB,GAAsD;AACxF,IAAA,UAAU,EAAE,IAAI;AAChB,IAAA,UAAU,EAAE,IAAI;;;ACJlB;;AAEG;;;;"}
@@ -16,15 +16,23 @@ class TransformationService {
16
16
  if (!value) {
17
17
  return value;
18
18
  }
19
- if (transformItem.transformType === 'date') {
20
- if (transformItem.dateInputFormat !== null && transformItem.dateOutputFormat !== null) {
21
- const parsedDate = this.dateService.getFromFormat(value, transformItem.dateInputFormat);
22
- if (this.dateService.isValidDate(parsedDate)) {
23
- return this.dateService.toFormat(parsedDate, transformItem.dateOutputFormat);
24
- }
19
+ if (transformItem.transformType === 'date' && transformItem.dateConfig) {
20
+ const parsedDate = this.dateService.getFromFormat(value, transformItem.dateConfig.inputFormat);
21
+ if (this.dateService.isValidDate(parsedDate)) {
22
+ return this.dateService.toFormat(parsedDate, transformItem.dateConfig.outputFormat);
25
23
  }
26
24
  return value;
27
25
  }
26
+ if (transformItem.transformType === 'time' && transformItem.timeConfig) {
27
+ if (transformItem.timeConfig.addOffset) {
28
+ // Add offset to the time value
29
+ return `${value}:00Z`;
30
+ }
31
+ else if (transformItem.timeConfig.removeOffset) {
32
+ // Remove offset from the time value
33
+ return value.replace(/:00Z$/, '');
34
+ }
35
+ }
28
36
  return value;
29
37
  }
30
38
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"hmcts-opal-frontend-common-services-transformation-service.mjs","sources":["../../../projects/opal-frontend-common/services/transformation-service/transformation.service.ts","../../../projects/opal-frontend-common/services/transformation-service/hmcts-opal-frontend-common-services-transformation-service.ts"],"sourcesContent":["import { inject, Injectable } from '@angular/core';\nimport { ITransformItem } from '@hmcts/opal-frontend-common/services/transformation-service/interfaces';\nimport { DateService } from '@hmcts/opal-frontend-common/services/date-service';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class TransformationService {\n private readonly dateService = inject(DateService);\n\n /**\n * Applies a transformation to the given value based on the specified transformation configuration.\n *\n * @param value - The value to be transformed.\n * @param transformItem - The configuration for the transformation, including the type of transformation and any necessary format details.\n * @returns The transformed value, or the original value if no transformation is applied.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private applyTransformation(value: any, transformItem: ITransformItem): any {\n if (!value) {\n return value;\n }\n\n if (transformItem.transformType === 'date') {\n if (transformItem.dateInputFormat !== null && transformItem.dateOutputFormat !== null) {\n const parsedDate = this.dateService.getFromFormat(value, transformItem.dateInputFormat);\n if (this.dateService.isValidDate(parsedDate)) {\n return this.dateService.toFormat(parsedDate, transformItem.dateOutputFormat);\n }\n }\n return value;\n }\n\n return value;\n }\n\n /**\n * Transforms the values of an object based on a given transformation configuration.\n *\n * @param obj - The object whose values need to be transformed. It should be a non-null object.\n * @param toTransform - An array of transformation configurations, where each configuration specifies\n * the key to transform and the transformation details.\n * @returns The transformed object with values modified according to the transformation configuration.\n *\n * @remarks\n * - If the input `obj` is not an object or is null, it returns the input as is.\n * - The function recursively processes nested objects.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public transformObjectValues(obj: { [key: string]: any }, toTransform: ITransformItem[]): any {\n if (typeof obj !== 'object' || obj === null) {\n return obj;\n }\n\n for (const [key, value] of Object.entries(obj)) {\n const transformItem = toTransform.find((item) => item.key === key);\n\n if (transformItem) {\n obj[key] = this.applyTransformation(value, transformItem);\n } else if (Array.isArray(value)) {\n obj[key] = value.map((item) =>\n typeof item === 'object' ? this.transformObjectValues(item, toTransform) : item,\n );\n } else if (typeof value === 'object') {\n obj[key] = this.transformObjectValues(value, toTransform); // Recursive call\n }\n }\n return obj;\n }\n\n /**\n * replaces the keys in the object provided\n * by replacing the current prefix with a new prefix.\n * Useful when mapping object keys to a different format or structure.\n * @param data - The data object containing key-value pairs.\n * @param currentPrefix - The prefix to be replaced in the keys.\n * @param replacementPrefix - The prefix to replace the current prefix with.\n * @returns A new object with the keys replaced.\n * @template T - The type of the form data object.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public replaceKeys<T extends object>(data: T, currentPrefix: string, replacementPrefix: string): Record<string, any> {\n if (typeof data !== 'object' || data === null) {\n return {};\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const result: Record<string, any> = {};\n\n for (const [key, value] of Object.entries(data)) {\n const newKey = key.replace(currentPrefix, replacementPrefix);\n result[newKey] = value;\n }\n\n return result;\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;MAOa,qBAAqB,CAAA;AACf,IAAA,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;AAElD;;;;;;AAMG;;IAEK,mBAAmB,CAAC,KAAU,EAAE,aAA6B,EAAA;QACnE,IAAI,CAAC,KAAK,EAAE;AACV,YAAA,OAAO,KAAK;QACd;AAEA,QAAA,IAAI,aAAa,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A,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AACpC,gBAAA,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,qBAAqB,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;YAC5D;QACF;AACA,QAAA,OAAO,GAAG;IACZ;AAEA;;;;;;;;;AASG;;AAEI,IAAA,WAAW,CAAmB,IAAO,EAAE,aAAqB,EAAE,iBAAyB,EAAA;QAC5F,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE;AAC7C,YAAA,OAAO,EAAE;QACX;;QAEA,MAAM,MAAM,GAAwB,EAAE;AAEtC,QAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YAC/C,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,EAAE,iBAAiB,CAAC;AAC5D,YAAA,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK;QACxB;AAEA,QAAA,OAAO,MAAM;IACf;wGAvFW,qBAAqB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;AAArB,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,SAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,qBAAqB,cAFpB,MAAM,EAAA,CAAA;;4FAEP,qBAAqB,EAAA,UAAA,EAAA,CAAA;kBAHjC,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;ACND;;AAEG;;;;"}
1
+ {"version":3,"file":"hmcts-opal-frontend-common-services-transformation-service.mjs","sources":["../../../projects/opal-frontend-common/services/transformation-service/transformation.service.ts","../../../projects/opal-frontend-common/services/transformation-service/hmcts-opal-frontend-common-services-transformation-service.ts"],"sourcesContent":["import { inject, Injectable } from '@angular/core';\nimport { ITransformItem } from '@hmcts/opal-frontend-common/services/transformation-service/interfaces';\nimport { DateService } from '@hmcts/opal-frontend-common/services/date-service';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class TransformationService {\n private readonly dateService = inject(DateService);\n\n /**\n * Applies a transformation to the given value based on the specified transformation configuration.\n *\n * @param value - The value to be transformed.\n * @param transformItem - The configuration for the transformation, including the type of transformation and any necessary format details.\n * @returns The transformed value, or the original value if no transformation is applied.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private applyTransformation(value: any, transformItem: ITransformItem): any {\n if (!value) {\n return value;\n }\n\n if (transformItem.transformType === 'date' && transformItem.dateConfig) {\n const parsedDate = this.dateService.getFromFormat(value, transformItem.dateConfig.inputFormat);\n if (this.dateService.isValidDate(parsedDate)) {\n return this.dateService.toFormat(parsedDate, transformItem.dateConfig.outputFormat);\n }\n return value;\n }\n\n if (transformItem.transformType === 'time' && transformItem.timeConfig) {\n if (transformItem.timeConfig.addOffset) {\n // Add offset to the time value\n return `${value}:00Z`;\n } else if (transformItem.timeConfig.removeOffset) {\n // Remove offset from the time value\n return value.replace(/:00Z$/, '');\n }\n }\n\n return value;\n }\n\n /**\n * Transforms the values of an object based on a given transformation configuration.\n *\n * @param obj - The object whose values need to be transformed. It should be a non-null object.\n * @param toTransform - An array of transformation configurations, where each configuration specifies\n * the key to transform and the transformation details.\n * @returns The transformed object with values modified according to the transformation configuration.\n *\n * @remarks\n * - If the input `obj` is not an object or is null, it returns the input as is.\n * - The function recursively processes nested objects.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public transformObjectValues(obj: { [key: string]: any }, toTransform: ITransformItem[]): any {\n if (typeof obj !== 'object' || obj === null) {\n return obj;\n }\n\n for (const [key, value] of Object.entries(obj)) {\n const transformItem = toTransform.find((item) => item.key === key);\n\n if (transformItem) {\n obj[key] = this.applyTransformation(value, transformItem);\n } else if (Array.isArray(value)) {\n obj[key] = value.map((item) =>\n typeof item === 'object' ? this.transformObjectValues(item, toTransform) : item,\n );\n } else if (typeof value === 'object') {\n obj[key] = this.transformObjectValues(value, toTransform); // Recursive call\n }\n }\n return obj;\n }\n\n /**\n * replaces the keys in the object provided\n * by replacing the current prefix with a new prefix.\n * Useful when mapping object keys to a different format or structure.\n * @param data - The data object containing key-value pairs.\n * @param currentPrefix - The prefix to be replaced in the keys.\n * @param replacementPrefix - The prefix to replace the current prefix with.\n * @returns A new object with the keys replaced.\n * @template T - The type of the form data object.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n public replaceKeys<T extends object>(data: T, currentPrefix: string, replacementPrefix: string): Record<string, any> {\n if (typeof data !== 'object' || data === null) {\n return {};\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const result: Record<string, any> = {};\n\n for (const [key, value] of Object.entries(data)) {\n const newKey = key.replace(currentPrefix, replacementPrefix);\n result[newKey] = value;\n }\n\n return result;\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;MAOa,qBAAqB,CAAA;AACf,IAAA,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;AAElD;;;;;;AAMG;;IAEK,mBAAmB,CAAC,KAAU,EAAE,aAA6B,EAAA;QACnE,IAAI,CAAC,KAAK,EAAE;AACV,YAAA,OAAO,KAAK;QACd;QAEA,IAAI,aAAa,CAAC,aAAa,KAAK,MAAM,IAAI,aAAa,CAAC,UAAU,EAAE;AACtE,YAAA,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,KAAK,EAAE,aAAa,CAAC,UAAU,CAAC,WAAW,CAAC;YAC9F,IAAI,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,UAAU,CAAC,EAAE;AAC5C,gBAAA,OAAO,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,UAAU,CAAC,YAAY,CAAC;YACr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,CAAC,qBAAqB,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;YAC5D;QACF;AACA,QAAA,OAAO,GAAG;IACZ;AAEA;;;;;;;;;AASG;;AAEI,IAAA,WAAW,CAAmB,IAAO,EAAE,aAAqB,EAAE,iBAAyB,EAAA;QAC5F,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE;AAC7C,YAAA,OAAO,EAAE;QACX;;QAEA,MAAM,MAAM,GAAwB,EAAE;AAEtC,QAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;YAC/C,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,EAAE,iBAAiB,CAAC;AAC5D,YAAA,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK;QACxB;AAEA,QAAA,OAAO,MAAM;IACf;wGA/FW,qBAAqB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;AAArB,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,SAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,qBAAqB,cAFpB,MAAM,EAAA,CAAA;;4FAEP,qBAAqB,EAAA,UAAA,EAAA,CAAA;kBAHjC,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;ACND;;AAEG;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmcts/opal-frontend-common",
3
- "version": "0.0.25",
3
+ "version": "0.0.26",
4
4
  "license": "MIT",
5
5
  "peerDependencies": {
6
6
  "@angular/common": "^18.2.0 || ^19.0.0",
@@ -706,6 +706,11 @@
706
706
  "./services/transformation-service/mocks": {
707
707
  "import": "./fesm2022/hmcts-opal-frontend-common-services-transformation-service-mocks.mjs"
708
708
  },
709
+ "./services/transformation-service/constants": {
710
+ "import": "./fesm2022/hmcts-opal-frontend-common-services-transformation-service-constants.mjs",
711
+ "types": "./services/transformation-service/constants/index.d.ts",
712
+ "default": "./fesm2022/hmcts-opal-frontend-common-services-transformation-service-constants.mjs"
713
+ },
709
714
  "./services/utils-service": {
710
715
  "import": "./fesm2022/hmcts-opal-frontend-common-services-utils-service.mjs",
711
716
  "types": "./services/utils-service/index.d.ts",
@@ -0,0 +1,5 @@
1
+ import { ITransformItem } from '@hmcts/opal-frontend-common/services/transformation-service/interfaces';
2
+
3
+ declare const TRANSFORM_ITEM_DEFAULTS: Pick<ITransformItem, 'dateConfig' | 'timeConfig'>;
4
+
5
+ export { TRANSFORM_ITEM_DEFAULTS };
@@ -1,8 +1,16 @@
1
+ interface ITransformItemDateConfig {
2
+ inputFormat: string;
3
+ outputFormat: string;
4
+ }
5
+ interface ITransformItemTimeConfig {
6
+ addOffset: boolean;
7
+ removeOffset: boolean;
8
+ }
1
9
  interface ITransformItem {
2
10
  key: string;
3
11
  transformType: string;
4
- dateInputFormat: string | null;
5
- dateOutputFormat: string | null;
12
+ dateConfig: ITransformItemDateConfig | null;
13
+ timeConfig: ITransformItemTimeConfig | null;
6
14
  }
7
15
 
8
- export type { ITransformItem };
16
+ export type { ITransformItem, ITransformItemDateConfig, ITransformItemTimeConfig };