@cirrobio/sdk 0.2.22 → 0.3.0
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/file/calculate-size.d.ts +7 -0
- package/dist/file/calculate-size.js +16 -0
- package/dist/file.d.ts +1 -0
- package/dist/file.js +3 -1
- package/dist/formatters/bytes-to-string.d.ts +10 -0
- package/dist/formatters/bytes-to-string.js +30 -0
- package/dist/formatters/json-pretty-print.d.ts +6 -0
- package/dist/formatters/json-pretty-print.js +12 -0
- package/dist/formatters/normalize-date.d.ts +6 -0
- package/dist/formatters/normalize-date.js +16 -0
- package/dist/formatters/normalize-string.d.ts +5 -0
- package/dist/formatters/normalize-string.js +13 -0
- package/dist/formatters/slash.d.ts +10 -0
- package/dist/formatters/slash.js +19 -0
- package/dist/formatters/to-date-format.d.ts +6 -0
- package/dist/formatters/to-date-format.js +16 -0
- package/dist/formatters/to-friendly-name.d.ts +6 -0
- package/dist/formatters/to-friendly-name.js +17 -0
- package/dist/formatters/to-money.d.ts +6 -0
- package/dist/formatters/to-money.js +16 -0
- package/dist/formatters/to-pascal-case.d.ts +6 -0
- package/dist/formatters/to-pascal-case.js +12 -0
- package/dist/formatters/to-title-case.d.ts +6 -0
- package/dist/formatters/to-title-case.js +14 -0
- package/dist/formatters.d.ts +10 -0
- package/dist/formatters.js +24 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/util/download.d.ts +8 -0
- package/dist/util/download.js +22 -0
- package/dist/util/extract-from-object.d.ts +6 -0
- package/dist/util/extract-from-object.js +15 -0
- package/dist/util/handle-promise.d.ts +5 -0
- package/dist/util/handle-promise.js +11 -0
- package/dist/util.d.ts +3 -0
- package/dist/util.js +10 -0
- package/package.json +3 -2
- package/src/file/calculate-size.ts +14 -0
- package/src/file.ts +1 -0
- package/src/formatters/__tests__/formatters.spec.ts +65 -0
- package/src/formatters/bytes-to-string.ts +32 -0
- package/src/formatters/json-pretty-print.ts +8 -0
- package/src/formatters/normalize-date.ts +10 -0
- package/src/formatters/normalize-string.ts +8 -0
- package/src/formatters/slash.ts +15 -0
- package/src/formatters/to-date-format.ts +12 -0
- package/src/formatters/to-friendly-name.ts +14 -0
- package/src/formatters/to-money.ts +13 -0
- package/src/formatters/to-pascal-case.ts +8 -0
- package/src/formatters/to-title-case.ts +9 -0
- package/src/formatters.ts +10 -0
- package/src/index.ts +2 -0
- package/src/util/download.ts +18 -0
- package/src/util/extract-from-object.ts +11 -0
- package/src/util/handle-promise.ts +7 -0
- package/src/util.ts +3 -0
- package/tsconfig.json +2 -1
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { FileSystemObject } from "./file-object.model";
|
|
2
|
+
/**
|
|
3
|
+
* Calculate the total size of all files in a directory.
|
|
4
|
+
* @param files - An array of FileSystemObject representing the files in the directory.
|
|
5
|
+
* @return The total size of all files in bytes.
|
|
6
|
+
*/
|
|
7
|
+
export declare function calculateTotalSize(files: Array<FileSystemObject>): number;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateTotalSize = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Calculate the total size of all files in a directory.
|
|
6
|
+
* @param files - An array of FileSystemObject representing the files in the directory.
|
|
7
|
+
* @return The total size of all files in bytes.
|
|
8
|
+
*/
|
|
9
|
+
function calculateTotalSize(files) {
|
|
10
|
+
let totalSize = 0;
|
|
11
|
+
for (const file of files) {
|
|
12
|
+
totalSize += file.size;
|
|
13
|
+
}
|
|
14
|
+
return totalSize;
|
|
15
|
+
}
|
|
16
|
+
exports.calculateTotalSize = calculateTotalSize;
|
package/dist/file.d.ts
CHANGED
|
@@ -9,4 +9,5 @@ export { s3UriToParams } from './file/util/s3-utils';
|
|
|
9
9
|
export { createS3Client } from './file/util/s3-client';
|
|
10
10
|
export { credentialsCache, credentialsMutex } from './file/util/credentials-mutex.so';
|
|
11
11
|
export { getProjectS3Bucket } from './file/shared';
|
|
12
|
+
export { calculateTotalSize } from './file/calculate-size';
|
|
12
13
|
export { Progress, Upload } from '@aws-sdk/lib-storage';
|
package/dist/file.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.Upload = exports.getProjectS3Bucket = exports.credentialsMutex = exports.credentialsCache = exports.createS3Client = exports.s3UriToParams = exports.FileSystemObjectType = exports.ProjectFileAccessContext = exports.uploadFile = exports.getSignedUrl = exports.deleteFile = exports.FileService = void 0;
|
|
17
|
+
exports.Upload = exports.calculateTotalSize = exports.getProjectS3Bucket = exports.credentialsMutex = exports.credentialsCache = exports.createS3Client = exports.s3UriToParams = exports.FileSystemObjectType = exports.ProjectFileAccessContext = exports.uploadFile = exports.getSignedUrl = exports.deleteFile = exports.FileService = void 0;
|
|
18
18
|
__exportStar(require("./file/extensions.fn"), exports);
|
|
19
19
|
var file_service_1 = require("./file/file.service");
|
|
20
20
|
Object.defineProperty(exports, "FileService", { enumerable: true, get: function () { return file_service_1.FileService; } });
|
|
@@ -37,5 +37,7 @@ Object.defineProperty(exports, "credentialsCache", { enumerable: true, get: func
|
|
|
37
37
|
Object.defineProperty(exports, "credentialsMutex", { enumerable: true, get: function () { return credentials_mutex_so_1.credentialsMutex; } });
|
|
38
38
|
var shared_1 = require("./file/shared");
|
|
39
39
|
Object.defineProperty(exports, "getProjectS3Bucket", { enumerable: true, get: function () { return shared_1.getProjectS3Bucket; } });
|
|
40
|
+
var calculate_size_1 = require("./file/calculate-size");
|
|
41
|
+
Object.defineProperty(exports, "calculateTotalSize", { enumerable: true, get: function () { return calculate_size_1.calculateTotalSize; } });
|
|
40
42
|
var lib_storage_1 = require("@aws-sdk/lib-storage");
|
|
41
43
|
Object.defineProperty(exports, "Upload", { enumerable: true, get: function () { return lib_storage_1.Upload; } });
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format bytes as human-readable text.
|
|
3
|
+
* @param bytes Number of bytes.
|
|
4
|
+
* @param si True to use metric(SI) units, aka powers of 1000. False to use
|
|
5
|
+
* binary(IEC), aka powers of 1024.
|
|
6
|
+
* @param dp Number of decimal places to display.
|
|
7
|
+
*
|
|
8
|
+
* @return Formatted string.
|
|
9
|
+
*/
|
|
10
|
+
export declare function bytesToString(bytes: any, si?: boolean, dp?: number): string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bytesToString = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Format bytes as human-readable text.
|
|
6
|
+
* @param bytes Number of bytes.
|
|
7
|
+
* @param si True to use metric(SI) units, aka powers of 1000. False to use
|
|
8
|
+
* binary(IEC), aka powers of 1024.
|
|
9
|
+
* @param dp Number of decimal places to display.
|
|
10
|
+
*
|
|
11
|
+
* @return Formatted string.
|
|
12
|
+
*/
|
|
13
|
+
function bytesToString(bytes, si = false, dp = 1) {
|
|
14
|
+
const thresh = si ? 1000 : 1024;
|
|
15
|
+
if (Math.abs(bytes) < thresh) {
|
|
16
|
+
return `${bytes} B`;
|
|
17
|
+
}
|
|
18
|
+
const units = si
|
|
19
|
+
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
20
|
+
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
21
|
+
let u = -1;
|
|
22
|
+
const r = 10 ** dp;
|
|
23
|
+
do {
|
|
24
|
+
bytes /= thresh;
|
|
25
|
+
++u;
|
|
26
|
+
} while (Math.round(Math.abs(bytes) * r) / r >= thresh
|
|
27
|
+
&& u < units.length - 1);
|
|
28
|
+
return `${bytes.toFixed(dp)} ${units[u]}`;
|
|
29
|
+
}
|
|
30
|
+
exports.bytesToString = bytesToString;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pretty prints a JSON object with the specified indentation level.
|
|
3
|
+
* @param obj JSON object to be pretty printed.
|
|
4
|
+
* @param indentLevel Number of spaces to use for indentation.
|
|
5
|
+
*/
|
|
6
|
+
export declare function jsonPrettyPrint(obj: object, indentLevel?: number): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.jsonPrettyPrint = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Pretty prints a JSON object with the specified indentation level.
|
|
6
|
+
* @param obj JSON object to be pretty printed.
|
|
7
|
+
* @param indentLevel Number of spaces to use for indentation.
|
|
8
|
+
*/
|
|
9
|
+
function jsonPrettyPrint(obj, indentLevel = 2) {
|
|
10
|
+
return JSON.stringify(obj, null, indentLevel);
|
|
11
|
+
}
|
|
12
|
+
exports.jsonPrettyPrint = jsonPrettyPrint;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeDate = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Normalizes a date to UTC by adjusting for the timezone offset.
|
|
6
|
+
* Useful when you are working with dates that may not include time information.
|
|
7
|
+
* @param date
|
|
8
|
+
*/
|
|
9
|
+
function normalizeDate(date) {
|
|
10
|
+
if (!date)
|
|
11
|
+
throw new Error("Attempt to normalize undefined");
|
|
12
|
+
if (!(date instanceof Date))
|
|
13
|
+
date = new Date(date);
|
|
14
|
+
return new Date(date.getTime() - date.getTimezoneOffset() * -60000);
|
|
15
|
+
}
|
|
16
|
+
exports.normalizeDate = normalizeDate;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeString = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Normalize a string by trimming it and returning null if it is empty.
|
|
6
|
+
* @param input The string to normalize.
|
|
7
|
+
*/
|
|
8
|
+
function normalizeString(input) {
|
|
9
|
+
if (!(input === null || input === void 0 ? void 0 : input.trim()))
|
|
10
|
+
return null;
|
|
11
|
+
return input.trim();
|
|
12
|
+
}
|
|
13
|
+
exports.normalizeString = normalizeString;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes the starting slash from a path.
|
|
3
|
+
* @param path Input path
|
|
4
|
+
*/
|
|
5
|
+
export declare function removeStartingSlash(path: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* Removes the ending slash from a path.
|
|
8
|
+
* @param path Input path
|
|
9
|
+
*/
|
|
10
|
+
export declare function removeEndingSlash(path: string): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.removeEndingSlash = exports.removeStartingSlash = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Removes the starting slash from a path.
|
|
6
|
+
* @param path Input path
|
|
7
|
+
*/
|
|
8
|
+
function removeStartingSlash(path) {
|
|
9
|
+
return (path === null || path === void 0 ? void 0 : path.startsWith('/')) ? path.substring(1) : path;
|
|
10
|
+
}
|
|
11
|
+
exports.removeStartingSlash = removeStartingSlash;
|
|
12
|
+
/**
|
|
13
|
+
* Removes the ending slash from a path.
|
|
14
|
+
* @param path Input path
|
|
15
|
+
*/
|
|
16
|
+
function removeEndingSlash(path) {
|
|
17
|
+
return (path === null || path === void 0 ? void 0 : path.endsWith('/')) ? path.slice(0, -1) : path;
|
|
18
|
+
}
|
|
19
|
+
exports.removeEndingSlash = removeEndingSlash;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toDateFormat = void 0;
|
|
4
|
+
const dateFormat = new Intl.DateTimeFormat('en-US', { month: '2-digit', day: '2-digit', year: '2-digit' });
|
|
5
|
+
/**
|
|
6
|
+
* Converts a date string or Date object to MM/DD/YY date string.
|
|
7
|
+
* Ex., "2023-10-01" -> "10/01/23"
|
|
8
|
+
* @param date Input date string or Date object.
|
|
9
|
+
*/
|
|
10
|
+
function toDateFormat(date) {
|
|
11
|
+
if (!date)
|
|
12
|
+
return null;
|
|
13
|
+
const d = (typeof date === 'string') ? new Date(date) : date;
|
|
14
|
+
return dateFormat.format(d);
|
|
15
|
+
}
|
|
16
|
+
exports.toDateFormat = toDateFormat;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toFriendlyName = void 0;
|
|
4
|
+
const to_title_case_1 = require("./to-title-case");
|
|
5
|
+
/**
|
|
6
|
+
* Converts a field name to a more human-readable format.
|
|
7
|
+
* Ex., "helloWorld" -> "Hello World"
|
|
8
|
+
* @param value The value to convert.
|
|
9
|
+
*/
|
|
10
|
+
function toFriendlyName(value) {
|
|
11
|
+
let _value = value.split(/(?=[A-Z])/).join(' ');
|
|
12
|
+
_value = _value.split('_').join(' ');
|
|
13
|
+
_value = _value.split('|').join(' ');
|
|
14
|
+
_value = _value.replace(/ +/g, ' ');
|
|
15
|
+
return (0, to_title_case_1.toTitleCase)(_value);
|
|
16
|
+
}
|
|
17
|
+
exports.toFriendlyName = toFriendlyName;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toMoney = void 0;
|
|
4
|
+
const money = new Intl.NumberFormat('en-US', {
|
|
5
|
+
style: 'currency',
|
|
6
|
+
currency: 'USD',
|
|
7
|
+
});
|
|
8
|
+
/**
|
|
9
|
+
* Converts a number or bigint to a formatted money string.
|
|
10
|
+
* Ex., 1234.56 -> $1,234.56
|
|
11
|
+
* @param value Input number or bigint value.
|
|
12
|
+
*/
|
|
13
|
+
function toMoney(value) {
|
|
14
|
+
return money.format(value);
|
|
15
|
+
}
|
|
16
|
+
exports.toMoney = toMoney;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toPascalCase = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Converts a string to PascalCase.
|
|
6
|
+
* Ex., "hello world" -> "HelloWorld"
|
|
7
|
+
* @param value The string to convert.
|
|
8
|
+
*/
|
|
9
|
+
function toPascalCase(value) {
|
|
10
|
+
return value.replace(/(\w)(\w*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase());
|
|
11
|
+
}
|
|
12
|
+
exports.toPascalCase = toPascalCase;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toTitleCase = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Converts a string to Title case.
|
|
6
|
+
* Ex., "hello world" -> "Hello world"
|
|
7
|
+
* @param value
|
|
8
|
+
*/
|
|
9
|
+
function toTitleCase(value) {
|
|
10
|
+
if (!value)
|
|
11
|
+
return null;
|
|
12
|
+
return value[0].toUpperCase() + value.slice(1).toLowerCase();
|
|
13
|
+
}
|
|
14
|
+
exports.toTitleCase = toTitleCase;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { jsonPrettyPrint } from './formatters/json-pretty-print';
|
|
2
|
+
export { bytesToString } from './formatters/bytes-to-string';
|
|
3
|
+
export { normalizeDate } from './formatters/normalize-date';
|
|
4
|
+
export { normalizeString } from './formatters/normalize-string';
|
|
5
|
+
export { removeStartingSlash, removeEndingSlash } from './formatters/slash';
|
|
6
|
+
export { toDateFormat } from './formatters/to-date-format';
|
|
7
|
+
export { toFriendlyName } from './formatters/to-friendly-name';
|
|
8
|
+
export { toMoney } from './formatters/to-money';
|
|
9
|
+
export { toPascalCase } from './formatters/to-pascal-case';
|
|
10
|
+
export { toTitleCase } from './formatters/to-title-case';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toTitleCase = exports.toPascalCase = exports.toMoney = exports.toFriendlyName = exports.toDateFormat = exports.removeEndingSlash = exports.removeStartingSlash = exports.normalizeString = exports.normalizeDate = exports.bytesToString = exports.jsonPrettyPrint = void 0;
|
|
4
|
+
var json_pretty_print_1 = require("./formatters/json-pretty-print");
|
|
5
|
+
Object.defineProperty(exports, "jsonPrettyPrint", { enumerable: true, get: function () { return json_pretty_print_1.jsonPrettyPrint; } });
|
|
6
|
+
var bytes_to_string_1 = require("./formatters/bytes-to-string");
|
|
7
|
+
Object.defineProperty(exports, "bytesToString", { enumerable: true, get: function () { return bytes_to_string_1.bytesToString; } });
|
|
8
|
+
var normalize_date_1 = require("./formatters/normalize-date");
|
|
9
|
+
Object.defineProperty(exports, "normalizeDate", { enumerable: true, get: function () { return normalize_date_1.normalizeDate; } });
|
|
10
|
+
var normalize_string_1 = require("./formatters/normalize-string");
|
|
11
|
+
Object.defineProperty(exports, "normalizeString", { enumerable: true, get: function () { return normalize_string_1.normalizeString; } });
|
|
12
|
+
var slash_1 = require("./formatters/slash");
|
|
13
|
+
Object.defineProperty(exports, "removeStartingSlash", { enumerable: true, get: function () { return slash_1.removeStartingSlash; } });
|
|
14
|
+
Object.defineProperty(exports, "removeEndingSlash", { enumerable: true, get: function () { return slash_1.removeEndingSlash; } });
|
|
15
|
+
var to_date_format_1 = require("./formatters/to-date-format");
|
|
16
|
+
Object.defineProperty(exports, "toDateFormat", { enumerable: true, get: function () { return to_date_format_1.toDateFormat; } });
|
|
17
|
+
var to_friendly_name_1 = require("./formatters/to-friendly-name");
|
|
18
|
+
Object.defineProperty(exports, "toFriendlyName", { enumerable: true, get: function () { return to_friendly_name_1.toFriendlyName; } });
|
|
19
|
+
var to_money_1 = require("./formatters/to-money");
|
|
20
|
+
Object.defineProperty(exports, "toMoney", { enumerable: true, get: function () { return to_money_1.toMoney; } });
|
|
21
|
+
var to_pascal_case_1 = require("./formatters/to-pascal-case");
|
|
22
|
+
Object.defineProperty(exports, "toPascalCase", { enumerable: true, get: function () { return to_pascal_case_1.toPascalCase; } });
|
|
23
|
+
var to_title_case_1 = require("./formatters/to-title-case");
|
|
24
|
+
Object.defineProperty(exports, "toTitleCase", { enumerable: true, get: function () { return to_title_case_1.toTitleCase; } });
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,3 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./file"), exports);
|
|
18
18
|
__exportStar(require("./api"), exports);
|
|
19
|
+
__exportStar(require("./formatters"), exports);
|
|
20
|
+
__exportStar(require("./util"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Download a blob as a file with the specified file name.
|
|
3
|
+
*/
|
|
4
|
+
export declare function downloadBlob(blob: Blob, fileName: string): void;
|
|
5
|
+
/**
|
|
6
|
+
* Downloads the specified content as a file with the specified file name and type.
|
|
7
|
+
*/
|
|
8
|
+
export declare function downloadContent(content: any, fileName: string, fileType?: string): void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.downloadContent = exports.downloadBlob = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Download a blob as a file with the specified file name.
|
|
6
|
+
*/
|
|
7
|
+
function downloadBlob(blob, fileName) {
|
|
8
|
+
const url = window.URL.createObjectURL(blob);
|
|
9
|
+
const a = document.createElement('a');
|
|
10
|
+
a.href = url;
|
|
11
|
+
a.download = fileName;
|
|
12
|
+
a.click();
|
|
13
|
+
}
|
|
14
|
+
exports.downloadBlob = downloadBlob;
|
|
15
|
+
/**
|
|
16
|
+
* Downloads the specified content as a file with the specified file name and type.
|
|
17
|
+
*/
|
|
18
|
+
function downloadContent(content, fileName, fileType = 'text/plain') {
|
|
19
|
+
const blob = new Blob([content], { type: fileType });
|
|
20
|
+
downloadBlob(blob, fileName);
|
|
21
|
+
}
|
|
22
|
+
exports.downloadContent = downloadContent;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractFromObject = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Extracts a value from an object using the json path notation, i.e. $.test.id
|
|
6
|
+
* @param path JSON path to the value
|
|
7
|
+
* @param obj Object to extract the value from
|
|
8
|
+
*/
|
|
9
|
+
function extractFromObject(path, obj) {
|
|
10
|
+
const pathParts = path.slice(2).split('.');
|
|
11
|
+
return pathParts.reduce(function (o, k) {
|
|
12
|
+
return o && o[k];
|
|
13
|
+
}, obj);
|
|
14
|
+
}
|
|
15
|
+
exports.extractFromObject = extractFromObject;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handlePromiseError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Ignores dangling promise rejections.
|
|
6
|
+
* @param err The error to ignore.
|
|
7
|
+
*/
|
|
8
|
+
function handlePromiseError(err) {
|
|
9
|
+
console.warn(err);
|
|
10
|
+
}
|
|
11
|
+
exports.handlePromiseError = handlePromiseError;
|
package/dist/util.d.ts
ADDED
package/dist/util.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.downloadContent = exports.downloadBlob = exports.handlePromiseError = exports.extractFromObject = void 0;
|
|
4
|
+
var extract_from_object_1 = require("./util/extract-from-object");
|
|
5
|
+
Object.defineProperty(exports, "extractFromObject", { enumerable: true, get: function () { return extract_from_object_1.extractFromObject; } });
|
|
6
|
+
var handle_promise_1 = require("./util/handle-promise");
|
|
7
|
+
Object.defineProperty(exports, "handlePromiseError", { enumerable: true, get: function () { return handle_promise_1.handlePromiseError; } });
|
|
8
|
+
var download_1 = require("./util/download");
|
|
9
|
+
Object.defineProperty(exports, "downloadBlob", { enumerable: true, get: function () { return download_1.downloadBlob; } });
|
|
10
|
+
Object.defineProperty(exports, "downloadContent", { enumerable: true, get: function () { return download_1.downloadContent; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cirrobio/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "SDK for Cirro",
|
|
5
5
|
"author": "CirroBio",
|
|
6
6
|
"repository": {
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"typings": "./dist/index.d.ts",
|
|
15
15
|
"scripts": {
|
|
16
16
|
"build": "tsc",
|
|
17
|
-
"prepare": "npm run build"
|
|
17
|
+
"prepare": "npm run build",
|
|
18
|
+
"test": "jest --coverage --silent"
|
|
18
19
|
},
|
|
19
20
|
"jest": {
|
|
20
21
|
"coverageReporters": [
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { FileSystemObject } from "./file-object.model";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Calculate the total size of all files in a directory.
|
|
5
|
+
* @param files - An array of FileSystemObject representing the files in the directory.
|
|
6
|
+
* @return The total size of all files in bytes.
|
|
7
|
+
*/
|
|
8
|
+
export function calculateTotalSize(files: Array<FileSystemObject>): number {
|
|
9
|
+
let totalSize = 0;
|
|
10
|
+
for (const file of files) {
|
|
11
|
+
totalSize += file.size;
|
|
12
|
+
}
|
|
13
|
+
return totalSize;
|
|
14
|
+
}
|
package/src/file.ts
CHANGED
|
@@ -9,4 +9,5 @@ export { s3UriToParams } from './file/util/s3-utils'
|
|
|
9
9
|
export { createS3Client } from './file/util/s3-client'
|
|
10
10
|
export { credentialsCache, credentialsMutex } from './file/util/credentials-mutex.so';
|
|
11
11
|
export { getProjectS3Bucket } from './file/shared'
|
|
12
|
+
export { calculateTotalSize } from './file/calculate-size'
|
|
12
13
|
export { Progress, Upload } from '@aws-sdk/lib-storage';
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { bytesToString } from "../bytes-to-string";
|
|
2
|
+
import { normalizeDate } from "../normalize-date";
|
|
3
|
+
import { removeEndingSlash, removeStartingSlash } from "../slash";
|
|
4
|
+
import { toFriendlyName } from "../to-friendly-name";
|
|
5
|
+
import { toMoney } from "../to-money";
|
|
6
|
+
import { toTitleCase } from "../to-title-case";
|
|
7
|
+
|
|
8
|
+
describe('bytesToString', () => {
|
|
9
|
+
it('should convert IEC bytes to string', () => {
|
|
10
|
+
expect(bytesToString(12521, false)).toBe('12.2 KiB');
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should convert SI bytes to string', () => {
|
|
14
|
+
expect(bytesToString(12521, true, 2)).toBe('12.52 kB');
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe('normalizeDate', () => {
|
|
19
|
+
it('should work on a string input', () => {
|
|
20
|
+
const dateString = '2022-12-01'
|
|
21
|
+
const output = normalizeDate(dateString).toISOString().split('T')[0]
|
|
22
|
+
expect(output).toEqual(dateString);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should work on a Date object input', () => {
|
|
26
|
+
const dateString = '2022-12-01'
|
|
27
|
+
const output = normalizeDate(new Date(dateString)).toISOString().split('T')[0]
|
|
28
|
+
expect(output).toEqual(dateString);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('removeStartingSlash', () => {
|
|
33
|
+
it('should remove the starting slash from a string', () => {
|
|
34
|
+
expect(removeStartingSlash('/example/path/')).toBe('example/path/');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('removeEndingSlash', () => {
|
|
39
|
+
it('should remove the ending slash from a string', () => {
|
|
40
|
+
expect(removeEndingSlash('/example/path/')).toBe('/example/path');
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('toFriendlyName', () => {
|
|
45
|
+
it('should add correct spaces to a string', () => {
|
|
46
|
+
expect(toFriendlyName('ExampleUnfriendly|Name_here')).toBe('Example unfriendly name here');
|
|
47
|
+
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('toMoney', () => {
|
|
52
|
+
it('convert an integer value to a money-formatted string', () => {
|
|
53
|
+
expect(toMoney(25)).toBe('$25.00');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('convert a float value to a money-formatted string', () => {
|
|
57
|
+
expect(toMoney(117.15)).toBe('$117.15');
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('toTitleCase', () => {
|
|
62
|
+
it('should convert a string to title case', () => {
|
|
63
|
+
expect(toTitleCase('convert To Title case')).toBe('Convert to title case');
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format bytes as human-readable text.
|
|
3
|
+
* @param bytes Number of bytes.
|
|
4
|
+
* @param si True to use metric(SI) units, aka powers of 1000. False to use
|
|
5
|
+
* binary(IEC), aka powers of 1024.
|
|
6
|
+
* @param dp Number of decimal places to display.
|
|
7
|
+
*
|
|
8
|
+
* @return Formatted string.
|
|
9
|
+
*/
|
|
10
|
+
export function bytesToString(bytes, si = false, dp = 1): string {
|
|
11
|
+
const thresh = si ? 1000 : 1024;
|
|
12
|
+
|
|
13
|
+
if (Math.abs(bytes) < thresh) {
|
|
14
|
+
return `${bytes} B`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const units = si
|
|
18
|
+
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
19
|
+
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
|
20
|
+
let u = -1;
|
|
21
|
+
const r = 10 ** dp;
|
|
22
|
+
|
|
23
|
+
do {
|
|
24
|
+
bytes /= thresh;
|
|
25
|
+
++u;
|
|
26
|
+
} while (
|
|
27
|
+
Math.round(Math.abs(bytes) * r) / r >= thresh
|
|
28
|
+
&& u < units.length - 1
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return `${bytes.toFixed(dp)} ${units[u]}`;
|
|
32
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pretty prints a JSON object with the specified indentation level.
|
|
3
|
+
* @param obj JSON object to be pretty printed.
|
|
4
|
+
* @param indentLevel Number of spaces to use for indentation.
|
|
5
|
+
*/
|
|
6
|
+
export function jsonPrettyPrint(obj: object, indentLevel=2): string {
|
|
7
|
+
return JSON.stringify(obj, null, indentLevel);
|
|
8
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizes a date to UTC by adjusting for the timezone offset.
|
|
3
|
+
* Useful when you are working with dates that may not include time information.
|
|
4
|
+
* @param date
|
|
5
|
+
*/
|
|
6
|
+
export function normalizeDate(date: Date | string | undefined | null): Date {
|
|
7
|
+
if (!date) throw new Error("Attempt to normalize undefined");
|
|
8
|
+
if (!(date instanceof Date)) date = new Date(date);
|
|
9
|
+
return new Date(date.getTime() - date.getTimezoneOffset() * -60000);
|
|
10
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes the starting slash from a path.
|
|
3
|
+
* @param path Input path
|
|
4
|
+
*/
|
|
5
|
+
export function removeStartingSlash(path: string): string {
|
|
6
|
+
return path?.startsWith('/') ? path.substring(1) : path;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Removes the ending slash from a path.
|
|
11
|
+
* @param path Input path
|
|
12
|
+
*/
|
|
13
|
+
export function removeEndingSlash(path: string): string {
|
|
14
|
+
return path?.endsWith('/') ? path.slice(0, -1) : path;
|
|
15
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const dateFormat = new Intl.DateTimeFormat('en-US', { month: '2-digit', day: '2-digit', year: '2-digit' });
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a date string or Date object to MM/DD/YY date string.
|
|
5
|
+
* Ex., "2023-10-01" -> "10/01/23"
|
|
6
|
+
* @param date Input date string or Date object.
|
|
7
|
+
*/
|
|
8
|
+
export function toDateFormat(date: string | Date): string {
|
|
9
|
+
if (!date) return null;
|
|
10
|
+
const d: Date = (typeof date === 'string') ? new Date(date) : date
|
|
11
|
+
return dateFormat.format(d);
|
|
12
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { toTitleCase } from './to-title-case';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a field name to a more human-readable format.
|
|
5
|
+
* Ex., "helloWorld" -> "Hello World"
|
|
6
|
+
* @param value The value to convert.
|
|
7
|
+
*/
|
|
8
|
+
export function toFriendlyName(value: string): string {
|
|
9
|
+
let _value = value.split(/(?=[A-Z])/).join(' ');
|
|
10
|
+
_value = _value.split('_').join(' ');
|
|
11
|
+
_value = _value.split('|').join(' ');
|
|
12
|
+
_value = _value.replace(/ +/g, ' ');
|
|
13
|
+
return toTitleCase(_value);
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const money = new Intl.NumberFormat('en-US', {
|
|
2
|
+
style: 'currency',
|
|
3
|
+
currency: 'USD',
|
|
4
|
+
});
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Converts a number or bigint to a formatted money string.
|
|
8
|
+
* Ex., 1234.56 -> $1,234.56
|
|
9
|
+
* @param value Input number or bigint value.
|
|
10
|
+
*/
|
|
11
|
+
export function toMoney(value: number | bigint): string {
|
|
12
|
+
return money.format(value);
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a string to PascalCase.
|
|
3
|
+
* Ex., "hello world" -> "HelloWorld"
|
|
4
|
+
* @param value The string to convert.
|
|
5
|
+
*/
|
|
6
|
+
export function toPascalCase(value: string): string {
|
|
7
|
+
return value.replace(/(\w)(\w*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase());
|
|
8
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { jsonPrettyPrint } from './formatters/json-pretty-print';
|
|
2
|
+
export { bytesToString } from './formatters/bytes-to-string';
|
|
3
|
+
export { normalizeDate } from './formatters/normalize-date';
|
|
4
|
+
export { normalizeString } from './formatters/normalize-string';
|
|
5
|
+
export { removeStartingSlash, removeEndingSlash } from './formatters/slash';
|
|
6
|
+
export { toDateFormat } from './formatters/to-date-format';
|
|
7
|
+
export { toFriendlyName } from './formatters/to-friendly-name';
|
|
8
|
+
export { toMoney } from './formatters/to-money';
|
|
9
|
+
export { toPascalCase } from './formatters/to-pascal-case';
|
|
10
|
+
export { toTitleCase } from './formatters/to-title-case';
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Download a blob as a file with the specified file name.
|
|
3
|
+
*/
|
|
4
|
+
export function downloadBlob(blob: Blob, fileName: string): void {
|
|
5
|
+
const url = window.URL.createObjectURL(blob);
|
|
6
|
+
const a = document.createElement('a');
|
|
7
|
+
a.href = url;
|
|
8
|
+
a.download = fileName;
|
|
9
|
+
a.click();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Downloads the specified content as a file with the specified file name and type.
|
|
14
|
+
*/
|
|
15
|
+
export function downloadContent(content: any, fileName: string, fileType = 'text/plain'): void {
|
|
16
|
+
const blob = new Blob([content], { type: fileType });
|
|
17
|
+
downloadBlob(blob, fileName);
|
|
18
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts a value from an object using the json path notation, i.e. $.test.id
|
|
3
|
+
* @param path JSON path to the value
|
|
4
|
+
* @param obj Object to extract the value from
|
|
5
|
+
*/
|
|
6
|
+
export function extractFromObject(path: string, obj: object): any {
|
|
7
|
+
const pathParts = path.slice(2).split('.')
|
|
8
|
+
return pathParts.reduce(function(o, k) {
|
|
9
|
+
return o && o[k];
|
|
10
|
+
}, obj);
|
|
11
|
+
}
|
package/src/util.ts
ADDED