@rudderstack/workflow-engine
Version:
A generic workflow execution engine
90 lines • 3.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.containsAll = exports.SHA256 = exports.toSeconds = exports.toMilliseconds = exports.doThrow = exports.assertThrow = exports.doReturn = exports.getOneByPaths = exports.toArray = exports.getByPaths = exports.values = exports.sum = exports.chunk = exports.warn = exports.info = exports.error = exports.debug = void 0;
const sha256_js_1 = require("@aws-crypto/sha256-js");
const lodash_1 = require("lodash");
const errors_1 = require("../errors");
var logger_1 = require("../common/logger");
Object.defineProperty(exports, "debug", { enumerable: true, get: function () { return logger_1.debug; } });
Object.defineProperty(exports, "error", { enumerable: true, get: function () { return logger_1.error; } });
Object.defineProperty(exports, "info", { enumerable: true, get: function () { return logger_1.info; } });
Object.defineProperty(exports, "warn", { enumerable: true, get: function () { return logger_1.warn; } });
var lodash_2 = require("lodash");
Object.defineProperty(exports, "chunk", { enumerable: true, get: function () { return lodash_2.chunk; } });
Object.defineProperty(exports, "sum", { enumerable: true, get: function () { return lodash_2.sum; } });
function values(obj) {
return Object.values(obj);
}
exports.values = values;
function getByPaths(obj, paths) {
if (!obj || !paths) {
return undefined;
}
const result = (0, lodash_1.at)(obj, paths).filter(lodash_1.identity);
return Array.isArray(paths) ? result : result[0];
}
exports.getByPaths = getByPaths;
function toArray(obj) {
if (obj === undefined) {
return obj;
}
return Array.isArray(obj) ? obj : [obj];
}
exports.toArray = toArray;
function getOneByPaths(obj, paths) {
const newPaths = toArray(paths);
if (!obj || !newPaths?.length) {
return undefined;
}
return (0, lodash_1.at)(obj, newPaths.shift())[0] ?? getOneByPaths(obj, newPaths);
}
exports.getOneByPaths = getOneByPaths;
function doReturn(obj) {
throw new errors_1.ReturnResultError(obj);
}
exports.doReturn = doReturn;
function assertThrow(val, error) {
if (!val) {
if (typeof error === 'string') {
throw new Error(error);
}
throw error;
}
}
exports.assertThrow = assertThrow;
function doThrow(message, status = 500) {
throw new errors_1.StatusError(message, Number(status));
}
exports.doThrow = doThrow;
function toMilliseconds(timestamp) {
const time = new Date(timestamp).getTime();
if (!time) {
return undefined;
}
return time;
}
exports.toMilliseconds = toMilliseconds;
function toSeconds(timestamp) {
const timeInMillis = toMilliseconds(timestamp);
if (!timeInMillis) {
return undefined;
}
return Math.floor(timeInMillis / 1000);
}
exports.toSeconds = toSeconds;
function SHA256(text) {
if (!text) {
return undefined;
}
const hash = new sha256_js_1.Sha256();
hash.update(`${text}`);
const digest = hash.digestSync();
return Buffer.from(digest).toString('hex');
}
exports.SHA256 = SHA256;
// Check if arr1 is subset of arr2
function containsAll(arr1, arr2) {
return arr1.every((element) => arr2.includes(element));
}
exports.containsAll = containsAll;
//# sourceMappingURL=common.js.map