UNPKG

@vivianhealth/braze-segment-debounce

Version:

Segment utilities to debounce data before it reaches the Braze destination

136 lines (107 loc) 3.74 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.combinePreviousPayloads = exports.getPayloadKey = exports.transform = exports.merge = exports.isEqual = exports.isObject = void 0; var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); /** * Checks if value is an Object * @param {*} value The object to check * @return {boolean} Whether the value is an object or not */ var isObject = function isObject(value) { var type = (0, _typeof2.default)(value); return value != null && (type === 'object' || type === 'function'); }; /** * Checks if two values are deeply equal * @param {*} a The first value to check * @param {*} b The second value to check * @return {boolean} Whether the two values are deeply equal */ exports.isObject = isObject; var isEqual = function isEqual(a, b) { if (a === b) { return true; } else if (isObject(a) && isObject(b)) { if (Object.keys(a).length !== Object.keys(b).length) { return false; } for (var prop in a) { if (Object.prototype.hasOwnProperty.call(b, prop)) { if (!isEqual(a[prop], b[prop])) { return false; } } else { return false; } } return true; } return false; }; /** * Deeply merges a source object into a target object * @param {object} target The target object * @param {object} source The source object * @return {object} The deeply merged objects */ exports.isEqual = isEqual; var merge = function merge(target, source) { for (var prop in source) { var targetValue = target[prop]; var sourceValue = source[prop]; if (isObject(targetValue) && isObject(sourceValue)) { target[prop] = merge(targetValue, sourceValue); continue; } target[prop] = source[prop]; } return target; }; /** * Like reduce but works on objects and arrays, guessing the type of the * accumulator if not specified. * @param {object} target The object to transform * @param {function} fn The function to apply to the object * @param {object} accumulator The optional initial accumulator * @return {object} The transformed object */ exports.merge = merge; var transform = function transform(object, fn) { var accumulator = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; if (accumulator == null) { accumulator = Array.isArray(object) ? [] : {}; } for (var prop in object) { fn(accumulator, object[prop], prop); } return accumulator; }; /** * Creates the cache/storage key from the payload using one of `userId`, * `anonymousId` or the literal `no-id`, in that order. * @param {Object} payload The object from which to derive the key * @return {string} The payload key */ exports.transform = transform; var getPayloadKey = function getPayloadKey(payload, getPayloadProperty) { var userId = getPayloadProperty(payload, 'userId'); var anonymousId = getPayloadProperty(payload, 'anonymousId'); return "".concat(userId || anonymousId || 'no-id', "-previousSegmentPayload"); }; /** * Merges an Array of payloads into a single object containing * all of the data. * @param {Arrray} previousPayloads The payloads to combine into one * @return {Object} The combined payloads */ exports.getPayloadKey = getPayloadKey; var combinePreviousPayloads = function combinePreviousPayloads(previousPayloads) { return previousPayloads.reduce(function (combinedPayload, previousPayload) { combinedPayload = merge(combinedPayload, previousPayload); return combinedPayload; }, {}); }; exports.combinePreviousPayloads = combinePreviousPayloads;