@8ms/helpers 1.5.10 → 1.6.1

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.
@@ -1,4 +1,4 @@
1
- type WriteBrotli = {
1
+ type WriteGzip = {
2
2
  bucket: string;
3
3
  data: any;
4
4
  key: string;
@@ -8,5 +8,5 @@ type WriteBrotli = {
8
8
  /**
9
9
  * Write a gzip file to S3.
10
10
  */
11
- declare const writeBrotli: ({ bucket, data, key, isJson, ...options }: WriteBrotli) => Promise<any>;
12
- export default writeBrotli;
11
+ declare const writeGzip: ({ bucket, data, key, isJson, ...options }: WriteGzip) => Promise<any>;
12
+ export default writeGzip;
@@ -8,7 +8,7 @@ const writeFile_1 = __importDefault(require("./writeFile"));
8
8
  /**
9
9
  * Write a gzip file to S3.
10
10
  */
11
- const writeBrotli = async ({ bucket, data, key, isJson, ...options }) => {
11
+ const writeGzip = async ({ bucket, data, key, isJson, ...options }) => {
12
12
  let finalData = data;
13
13
  if (false !== isJson) {
14
14
  finalData = JSON.stringify(data);
@@ -25,4 +25,4 @@ const writeBrotli = async ({ bucket, data, key, isJson, ...options }) => {
25
25
  });
26
26
  return apiResponse;
27
27
  };
28
- exports.default = writeBrotli;
28
+ exports.default = writeGzip;
@@ -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;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.5.10",
4
+ "version": "1.6.1",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"
@@ -39,6 +39,8 @@
39
39
  "babel-jest": "29.7.0",
40
40
  "jest": "29.7.0",
41
41
  "node-fetch": "3.3.2",
42
+ "stream-chain": "2.2.5",
43
+ "stream-json": "1.8.0",
42
44
  "timezone-mock": "1.3.6",
43
45
  "ts-node": "10.9.1",
44
46
  "tslib": "2.6.2",
@@ -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;
@@ -0,0 +1,13 @@
1
+ declare const Transform: any;
2
+ /**
3
+ * Batch sort stream without overloading the memory limit.
4
+ * https://www.npmjs.com/package/fast-stream-sort
5
+ */
6
+ declare class SortStreamer extends Transform {
7
+ constructor(options: any, comp: any);
8
+ _transform(data: any, _: any, callback: any): void;
9
+ _flush(): void;
10
+ fullflush(): void;
11
+ }
12
+ declare const sort: (comp: any, options: any) => SortStreamer;
13
+ export default sort;
package/stream/sort.js ADDED
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const Transform = require('stream').Transform;
4
+ const batch = 15000;
5
+ /**
6
+ * Batch sort stream without overloading the memory limit.
7
+ * https://www.npmjs.com/package/fast-stream-sort
8
+ */
9
+ class SortStreamer extends Transform {
10
+ constructor(options, comp) {
11
+ super(options);
12
+ this.a = new Array(batch);
13
+ this.i = 0;
14
+ this.k = 0;
15
+ this.currentSize = batch;
16
+ this.isDone = false;
17
+ this.sortedArray = undefined;
18
+ this.comp = comp;
19
+ }
20
+ _transform(data, _, callback) {
21
+ if (data) {
22
+ this.a[this.i++] = data;
23
+ if (this.i == this.currentSize) {
24
+ this.currentSize = this.currentSize * 2;
25
+ var tmp = new Array(this.currentSize);
26
+ for (var j = 0; j < this.i; j++) {
27
+ tmp[j] = this.a[j];
28
+ }
29
+ this.a = tmp;
30
+ }
31
+ }
32
+ callback();
33
+ }
34
+ _flush() {
35
+ this.sortedArray = this.a.slice(0, this.i)
36
+ .sort(this.comp);
37
+ this.fullflush();
38
+ }
39
+ fullflush() {
40
+ while (this.k < this.i) {
41
+ this.push(this.sortedArray[this.k++]);
42
+ }
43
+ this.push(null);
44
+ }
45
+ }
46
+ const sort = (comp, options) => {
47
+ options = options || { objectMode: true };
48
+ return new SortStreamer(options, comp);
49
+ };
50
+ exports.default = sort;