@8ms/helpers 1.6.0 → 1.6.2

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,9 @@
1
+ type GetDecimal = {
2
+ dp?: number;
3
+ input: any;
4
+ };
5
+ /**
6
+ * Return a decimal number after rounding.
7
+ */
8
+ declare const getDecimal: (props: GetDecimal) => number;
9
+ export default getDecimal;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const getNumber_1 = __importDefault(require("./getNumber"));
7
+ /**
8
+ * Return a decimal number after rounding.
9
+ */
10
+ const getDecimal = (props) => {
11
+ const number = (0, getNumber_1.default)({
12
+ input: props.input,
13
+ });
14
+ return Number(number.toFixed(props?.dp || 2));
15
+ };
16
+ exports.default = getDecimal;
@@ -1,9 +1,11 @@
1
- /**
2
- * Shorthand function to divide number, but fallback if the divisor is 0.
3
- */
4
- declare const getDivide: ({ defaultValue, divisor, numerator }: {
1
+ type GetDivide = {
5
2
  defaultValue?: number;
3
+ dp?: number;
6
4
  divisor: any;
7
5
  numerator: any;
8
- }) => number;
6
+ };
7
+ /**
8
+ * Shorthand function to divide number, but fallback if the divisor is 0.
9
+ */
10
+ declare const getDivide: (props: GetDivide) => number;
9
11
  export default getDivide;
@@ -4,19 +4,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const getNumber_1 = __importDefault(require("./getNumber"));
7
+ const getDecimal_1 = __importDefault(require("./getDecimal"));
7
8
  /**
8
9
  * Shorthand function to divide number, but fallback if the divisor is 0.
9
10
  */
10
- const getDivide = ({ defaultValue = 0, divisor, numerator }) => {
11
+ const getDivide = (props) => {
11
12
  const numeratorInt = (0, getNumber_1.default)({
12
- input: numerator
13
+ input: props.numerator
13
14
  });
14
15
  const divisorInt = (0, getNumber_1.default)({
15
- input: divisor
16
+ input: props.divisor
16
17
  });
17
18
  if (0 === divisorInt) {
18
- return defaultValue;
19
+ return props.defaultValue || 0;
19
20
  }
20
- return numeratorInt / divisorInt;
21
+ return (0, getDecimal_1.default)({
22
+ dp: props?.dp,
23
+ input: numeratorInt / divisorInt
24
+ });
21
25
  };
22
26
  exports.default = getDivide;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.6.0",
4
+ "version": "1.6.2",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"
@@ -1,5 +1,6 @@
1
1
  type GetDecimal = {
2
2
  input: any;
3
+ dp?: number;
3
4
  };
4
5
  /**
5
6
  * Fetch the number from a prisma decimal (comes through as an object).
@@ -1,11 +1,18 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const getDecimal_1 = __importDefault(require("../number/getDecimal"));
3
7
  /**
4
8
  * Fetch the number from a prisma decimal (comes through as an object).
5
9
  */
6
10
  const getDecimal = (props) => {
7
11
  let whole = props?.input?.d?.[0] || 0;
8
12
  let decimal = props?.input?.d?.[1] || 0;
9
- return Number(`${whole}.${decimal}`);
13
+ return (0, getDecimal_1.default)({
14
+ input: `${whole}.${decimal}`,
15
+ dp: props?.dp
16
+ });
10
17
  };
11
18
  exports.default = getDecimal;