UNPKG

jsm-utilities

Version:
159 lines (158 loc) 6.14 kB
"use strict"; 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __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.isProductionDev = exports.isDevelopmentDev = exports.getItemIdsFromOldAndNewData = exports.defaults = void 0; exports.sanitizeObject = sanitizeObject; exports.removeUndefinedAttributes = removeUndefinedAttributes; const deepmerge_1 = __importDefault(require("deepmerge")); const jsm_logger_1 = __importStar(require("jsm-logger")); const logger = (0, jsm_logger_1.default)(jsm_logger_1.LoggerContext.UTILITY, 'helpers'); const isDate = (value) => value instanceof Date && !Number.isNaN(value.getTime()); // Utility to remove circular references // eslint-disable-next-line unicorn/prevent-abbreviations function sanitizeObject(obj, cache = new WeakMap()) { // Check if the object is a function or primitive type if ((obj === null || typeof obj !== 'object') && typeof obj !== 'function') { return obj; } // Handle functions by returning them as is if (typeof obj === 'function') { return obj; } // Handle dates by returning them as is if (isDate(obj)) { return obj; } // Check cache to see if object was already visited if (cache.has(obj)) { // Circular reference found, return placeholder return '[Circular]'; } // Add the object to cache cache.set(obj, true); // The obj is an array if (Array.isArray(obj)) { return obj.map(item => sanitizeObject(item, cache)); } // The obj is an object, recurse its properties const sanitizedObject = {}; for (const key of Object.keys(obj)) { sanitizedObject[key] = sanitizeObject(obj[key], cache); } return sanitizedObject; } function removeUndefinedAttributes(object) { // Check if the object is a function or primitive type if ((object === null || typeof object !== 'object') && typeof object !== 'function') { return object; } // Handle functions by returning them as is if (typeof object === 'function') { return object; } // Handle dates by returning them as is if (isDate(object)) { return object; } // The obj is an array if (Array.isArray(object)) { return object.map(item => removeUndefinedAttributes(item)); } // The obj is an object, recurse its properties const sanitizedObject = {}; for (const key of Object.keys(object)) { // eslint-disable-next-line unicorn/no-typeof-undefined if (typeof object[key] !== 'undefined') { sanitizedObject[key] = removeUndefinedAttributes(object[key]); } } return sanitizedObject; } /** * Merges two objects, with properties from the second object taking precedence over the first object. * If a property is found on both objects, the property from the first object is returned. * @param x - The first object to merge. * @param y - The second object to merge. * @param options - The options for merging. * @returns The merged object. */ const defaults = (x, y, options) => { let result; try { result = (0, deepmerge_1.default)(removeUndefinedAttributes(sanitizeObject(y || {})), removeUndefinedAttributes(sanitizeObject(x || {})) || {}, Object.assign(Object.assign({}, options), { arrayMerge: (target, source) => source })); } catch (error) { logger.error('defaults function error', error); result = Object.assign(Object.assign({}, y), x); } return result; }; exports.defaults = defaults; /** * Gets the item IDs from the new and old data. * @param new_value - The new value. * @param old_value - The old value. * @returns The item IDs. */ const getItemIdsFromOldAndNewData = (new_value, old_value) => { if (Array.isArray(new_value) || Array.isArray(old_value)) { if (Array.isArray(new_value)) return new_value; if (old_value) return old_value; return []; } if (typeof new_value !== "undefined") return new_value; if (typeof old_value !== "undefined") return old_value; return null; }; exports.getItemIdsFromOldAndNewData = getItemIdsFromOldAndNewData; /** * Checks if the current environment is development. * @returns A boolean indicating if the current environment is development. */ const isDevelopmentDev = () => process.env.NODE_ENV !== "production"; exports.isDevelopmentDev = isDevelopmentDev; /** * Checks if the current environment is production. * @returns A boolean indicating if the current environment is production. */ const isProductionDev = () => process.env.NODE_ENV === "production"; exports.isProductionDev = isProductionDev;