@cumulus/common
Version:
Common utilities used across tasks
110 lines • 4.24 kB
JavaScript
;
/**
* Simple utility functions
*
* @module util
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.omitDeepBy = exports.returnNullOrUndefinedOrDate = exports.isOneOf = exports.removeNilProperties = exports.deprecate = void 0;
const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
const curry_1 = __importDefault(require("lodash/curry"));
const isNil_1 = __importDefault(require("lodash/isNil"));
const omitBy_1 = __importDefault(require("lodash/omitBy"));
const log = __importStar(require("./log"));
/**
* Mark a piece of code as deprecated.
*
* Each deprecation notice for a given name and version combination will
* only be printed once.
*
* @param {string} name - the name of the function / method / class to deprecate
* @param {string} version - the version after which the code will be marked
* as deprecated
* @param {string} [alternative] - the function / method / class to use instead
* of this deprecated code
*
* @alias module:util
*/
exports.deprecate = (() => {
const warned = new Set();
return (name, version, alternative) => {
const key = `${name}-${version}`;
if (warned.has(key))
return;
warned.add(key);
let message = `${name} is deprecated after version ${version} and will be removed in a future release.`;
if (alternative)
message += ` Use ${alternative} instead.`;
log.warn(message);
};
})();
/**
* Remove properties whose values are `null` or `undefined`
*
* @param {Object} obj - object to update
* @returns {Object} a shallow clone of the object with `null` and `undefined`
* properties removed
*
* @alias module:util
*/
const removeNilProperties = (obj) => (0, omitBy_1.default)(obj, isNil_1.default);
exports.removeNilProperties = removeNilProperties;
/**
* Test if a value is included in a list of items
*
* This is a curried function - https://lodash.com/docs/4.17.11#curry
*
* @param {Array} collection - the list of items to check against
* @param {Object} val - the item to check for in the collection
* @returns {boolean}
*
* @alias module:util
* @kind function
*/
exports.isOneOf = (0, curry_1.default)((collection, val) => collection.includes(val), 2);
const returnNullOrUndefinedOrDate = (dateVal) => ((0, isNil_1.default)(dateVal) ? dateVal : new Date(dateVal));
exports.returnNullOrUndefinedOrDate = returnNullOrUndefinedOrDate;
const omitDeepBy = (object, cb) => {
function omitByDeepByOnOwnProps(obj) {
if (!Array.isArray(obj) && !(0, isPlainObject_1.default)(obj)) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map((element) => (0, exports.omitDeepBy)(element, cb));
}
const temp = {};
for (const [key, value] of Object.entries(obj)) {
temp[key] = (0, exports.omitDeepBy)(value, cb);
}
return (0, omitBy_1.default)(temp, cb);
}
return omitByDeepByOnOwnProps(object);
};
exports.omitDeepBy = omitDeepBy;
//# sourceMappingURL=util.js.map