@etainabl/nodejs-sdk 1.1.3 → 1.1.5

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.
package/dist/units.d.ts CHANGED
@@ -7,14 +7,18 @@ interface Item {
7
7
  export declare type AccountType = 'electricity' | 'gas' | 'water' | 'waste' | 'solar' | 'heating' | 'flow' | 'cooling' | 'temperature';
8
8
  export declare type ETNUnit = 'kwh' | 'kg' | 'm3' | 'lbs' | 'tonnes' | 'wh' | 'mwh' | 'ft3' | 'hcf' | 'm3/h' | 'wh' | 'qty' | 'l' | 'C';
9
9
  export declare type BaseUnit = 'kwh' | 'm3' | 'C' | 'kg' | 'm3/h';
10
- export declare const convertItems: (items: Item[], type: AccountType) => {
10
+ export declare const convertItems: (items: Item[], type: AccountType, defaultUnits: ETNUnit | undefined) => {
11
11
  value: number;
12
12
  units: BaseUnit;
13
13
  unit: string | null;
14
14
  factor: number | null;
15
15
  }[];
16
+ export declare const checkAccountTypeVsUnits: (type: AccountType, unit: ETNUnit) => {
17
+ type: string;
18
+ unit: ETNUnit;
19
+ };
16
20
  declare const _default: {
17
- convertItems: (items: Item[], type: AccountType) => {
21
+ convertItems: (items: Item[], type: AccountType, defaultUnits: ETNUnit | undefined) => {
18
22
  value: number;
19
23
  units: BaseUnit;
20
24
  unit: string | null;
package/dist/units.js CHANGED
@@ -34,8 +34,19 @@ const unitConversionFactors = {
34
34
  'm3/h': 1
35
35
  }
36
36
  };
37
+ const accountTypeUnitMap = {
38
+ electricity: ['kwh', 'mwh', 'wh'],
39
+ gas: ['kwh', 'm3', 'ft3', 'hcf'],
40
+ water: ['m3', 'l'],
41
+ waste: ['kg', 'lbs', 'tonnes'],
42
+ solar: ['kwh', 'mwh', 'wh'],
43
+ heating: ['kwh', 'mwh', 'wh'],
44
+ flow: ['m3/h'],
45
+ cooling: ['kwh', 'mwh', 'wh'],
46
+ temperature: ['C']
47
+ };
37
48
  // Convert units to base format
38
- export const convertItems = (items, type) => {
49
+ export const convertItems = (items, type, defaultUnits) => {
39
50
  if (!type)
40
51
  throw new Error('Account type is required');
41
52
  const baseUnit = accountTypeMap[type];
@@ -43,7 +54,7 @@ export const convertItems = (items, type) => {
43
54
  throw new Error(`Account type ${type} is not supported`);
44
55
  const convertedItems = items.map(item => {
45
56
  const factor = item.factor || 1;
46
- const units = item.units || item.unit || baseUnit;
57
+ const units = item.units || item.unit || defaultUnits || baseUnit;
47
58
  const convertedValue = item.value * _getConversionFactor(units, baseUnit) * factor;
48
59
  return { ...item, value: convertedValue, units: baseUnit };
49
60
  });
@@ -59,6 +70,21 @@ const _getConversionFactor = (fromUnit = 'kwh', toUnit) => {
59
70
  }
60
71
  return conversionFactors[fromUnit];
61
72
  };
73
+ export const checkAccountTypeVsUnits = (type, unit) => {
74
+ if (!type)
75
+ throw new Error('Account type is required');
76
+ if (!unit)
77
+ throw new Error('Unit is required');
78
+ const parsedType = type.toLowerCase().trim();
79
+ const accountTypeUnits = accountTypeUnitMap[parsedType];
80
+ if (!accountTypeUnits)
81
+ throw new Error(`Account type "${parsedType}" is not supported`);
82
+ const parsedUnit = unit.toLowerCase().trim();
83
+ if (!accountTypeUnits.includes(parsedUnit)) {
84
+ throw new Error(`Account type "${parsedType}" does not support unit "${parsedUnit}"`);
85
+ }
86
+ return { type: parsedType, unit: parsedUnit };
87
+ };
62
88
  export default {
63
89
  convertItems
64
90
  };
package/dist/units.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"units.js","sourceRoot":"","sources":["../src/units.ts"],"names":[],"mappings":"AAUA,MAAM,cAAc,GAAgC;IAClD,WAAW,EAAE,KAAK;IAClB,GAAG,EAAE,KAAK;IACV,KAAK,EAAE,IAAI;IACX,KAAK,EAAE,IAAI;IACX,KAAK,EAAE,KAAK;IACZ,OAAO,EAAE,KAAK;IACd,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,KAAK;IACd,WAAW,EAAE,GAAG;CACjB,CAAC;AAEF,MAAM,qBAAq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UAAU,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,IAAI,EAAa,CAAC;IAExD,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE;QAC1C,MAAM,IAAI,KAAK,CAAC,iBAAiB,UAAU,4BAA4B,UAAU,GAAG,CAAC,CAAC;KACvF;IAED,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAC,CAAC;AAC/C,CAAC,CAAC;AAEF,eAAe;IACb,YAAY;CACb,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etainabl/nodejs-sdk",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "main": "dist/index.js",
5
5
  "author": "Jonathan Lambert <jonathan@etainabl.com>",
6
6
  "type": "module",
package/src/units.ts CHANGED
@@ -46,8 +46,20 @@ const unitConversionFactors: { [key: string]: any } = {
46
46
  }
47
47
  };
48
48
 
49
+ const accountTypeUnitMap: { [key: string]: ETNUnit[] } = {
50
+ electricity: ['kwh', 'mwh', 'wh'],
51
+ gas: ['kwh', 'm3', 'ft3', 'hcf'],
52
+ water: ['m3', 'l'],
53
+ waste: ['kg', 'lbs', 'tonnes'],
54
+ solar: ['kwh', 'mwh', 'wh'],
55
+ heating: ['kwh', 'mwh', 'wh'],
56
+ flow: ['m3/h'],
57
+ cooling: ['kwh', 'mwh', 'wh'],
58
+ temperature: ['C']
59
+ };
60
+
49
61
  // Convert units to base format
50
- export const convertItems = (items: Item[], type: AccountType) => {
62
+ export const convertItems = (items: Item[], type: AccountType, defaultUnits: ETNUnit | undefined) => {
51
63
  if (!type) throw new Error('Account type is required');
52
64
 
53
65
  const baseUnit = accountTypeMap[type];
@@ -55,7 +67,7 @@ export const convertItems = (items: Item[], type: AccountType) => {
55
67
 
56
68
  const convertedItems = items.map(item => {
57
69
  const factor = item.factor || 1;
58
- const units = item.units || item.unit || baseUnit;
70
+ const units = item.units || item.unit || defaultUnits || baseUnit;
59
71
  const convertedValue = item.value * _getConversionFactor(units, baseUnit) * factor;
60
72
  return { ...item, value: convertedValue, units: baseUnit };
61
73
  });
@@ -77,6 +89,24 @@ const _getConversionFactor = (fromUnit: string = 'kwh', toUnit: string) => {
77
89
  return conversionFactors[fromUnit];
78
90
  };
79
91
 
92
+ export const checkAccountTypeVsUnits = (type: AccountType, unit: ETNUnit) => {
93
+ if (!type) throw new Error('Account type is required');
94
+ if (!unit) throw new Error('Unit is required');
95
+
96
+ const parsedType = type.toLowerCase().trim();
97
+
98
+ const accountTypeUnits = accountTypeUnitMap[parsedType];
99
+ if (!accountTypeUnits) throw new Error(`Account type "${parsedType}" is not supported`);
100
+
101
+ const parsedUnit = unit.toLowerCase().trim() as ETNUnit;
102
+
103
+ if (!accountTypeUnits.includes(parsedUnit)) {
104
+ throw new Error(`Account type "${parsedType}" does not support unit "${parsedUnit}"`);
105
+ }
106
+
107
+ return { type: parsedType, unit: parsedUnit};
108
+ };
109
+
80
110
  export default {
81
111
  convertItems
82
112
  };