@darkpos/utils 1.0.7 → 1.0.9

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.
@@ -44,5 +44,13 @@ module.exports = (settings = {}) => {
44
44
  dateFormat,
45
45
  hour12
46
46
  ),
47
+ getFormat: monthAndDateFormat => {
48
+ const date = monthAndDateFormat
49
+ ? utils.monthAndDateFormatPattern(locale)
50
+ : utils.dateFormatPattern(locale);
51
+ let format = `${date}, HH:mm`;
52
+ if (hour12 === 'true') format += ' A';
53
+ return format;
54
+ },
47
55
  };
48
56
  };
package/lib/math.js CHANGED
@@ -1,17 +1,21 @@
1
1
  const Decimal = require('decimal.js');
2
2
 
3
- //
3
+ const getDefaultNumbers = numbers => {
4
+ if (!Array.isArray(numbers)) return [];
5
+ return numbers.map(num => num || 0);
6
+ };
7
+
4
8
  const add = (...args) => {
5
9
  if (!args || !Array.isArray(args) || !args.length)
6
10
  throw new Error('Invalid argument');
7
11
 
8
- const sum = args.reduce(
12
+ const numbers = getDefaultNumbers(args);
13
+ const sum = numbers.reduce(
9
14
  (acc, current) => acc.add(Decimal(current)),
10
15
  Decimal(0)
11
16
  );
12
17
 
13
- // console.log('add =>', args, ' : ', sum);
14
- return Decimal(sum.toFixed(2, 1)).toNumber();
18
+ return Decimal(sum).toNumber();
15
19
  };
16
20
 
17
21
  //
@@ -19,18 +23,16 @@ const sub = (...args) => {
19
23
  if (!args || !Array.isArray(args) || !args.length)
20
24
  throw new Error('Invalid argument');
21
25
 
22
- const numbers = [...args];
26
+ const numbers = getDefaultNumbers(args);
23
27
  const first = numbers.shift();
24
28
  const sum = numbers.reduce(
25
29
  (acc, current) => acc.sub(Decimal(current)),
26
30
  Decimal(first)
27
31
  );
28
32
 
29
- // console.log('sub =>', args, ' : ', sum);
30
- return Decimal(sum.toFixed(2, 1)).toNumber();
33
+ return Decimal(sum).toNumber();
31
34
  };
32
35
 
33
- //
34
36
  const mul = (...args) => {
35
37
  if (!args || !Array.isArray(args) || !args.length)
36
38
  throw new Error('Invalid argument');
@@ -40,11 +42,9 @@ const mul = (...args) => {
40
42
  Decimal(1)
41
43
  );
42
44
 
43
- // console.log('mul =>', args, ' : ', sum);
44
- return Decimal(sum.toFixed(2, 1)).toNumber();
45
+ return Decimal(sum).toNumber();
45
46
  };
46
47
 
47
- //
48
48
  const div = (...args) => {
49
49
  if (!args || !Array.isArray(args) || !args.length)
50
50
  throw new Error('Invalid argument');
@@ -56,8 +56,7 @@ const div = (...args) => {
56
56
  Decimal(first)
57
57
  );
58
58
 
59
- // console.log('div =>', args, ' : ', sum);
60
- return Decimal(sum.toFixed(2, 1)).toNumber();
59
+ return Decimal(sum).toNumber();
61
60
  };
62
61
 
63
62
  //
@@ -84,6 +83,13 @@ const abs = x => Decimal(x).abs().toNumber();
84
83
  //
85
84
  const toFixed = x => Decimal(x).toFixed(2, 1);
86
85
 
86
+ //
87
+ const toDecimalPlaces = x => Decimal(x).toDecimalPlaces(2).toNumber();
88
+
89
+ const max = (...args) => Decimal.max(...args).toNumber();
90
+
91
+ const min = (...args) => Decimal.min(...args).toNumber();
92
+
87
93
  module.exports = {
88
94
  mul,
89
95
  div,
@@ -97,4 +103,7 @@ module.exports = {
97
103
  eq,
98
104
  isZero,
99
105
  toFixed,
106
+ toDecimalPlaces,
107
+ max,
108
+ min,
100
109
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darkpos/utils",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "General purpose utilities",
5
5
  "author": "Dark POS",
6
6
  "homepage": "https://gitlab.com/darkpos/packages/dark#readme",
@@ -39,5 +39,5 @@
39
39
  "eslint-plugin-prettier": "^4.0.0",
40
40
  "prettier": "^2.7.0"
41
41
  },
42
- "gitHead": "61d62f77f8bbbff2bfe0230bdb803d87516db17b"
42
+ "gitHead": "6648705882ec8351463ba823c213cf7c198129ce"
43
43
  }