Repository URL to install this package:
|
Version:
1.13.4 ▾
|
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/**
* Informs if DNT (Do Not Track) is enabled (FF returns 'unspecified' when disabled)
*
* Note: FF does not support JS retrieving of per-site DNT disabling (shield icon),
* unless by checking for a honeypot. See https://stackoverflow.com/a/35070265/462849
*
* @return {boolean}
*/
var hasDntEnabled = function hasDntEnabled() {
var dnt = navigator.doNotTrack || window.doNotTrack;
return Boolean(dnt && !['0', 'unspecified'].includes(dnt));
};
/**
* Checks if an object's type is undefined
*
* @param {*} obj - The object reference to be checked
* @return {boolean}
*/
var isUndefined = function isUndefined(obj) {
return typeof obj === 'undefined';
};
/**
* Checks if an object exists and is empty
*
* @param {Object} obj - The object reference to be checked
* @return {boolean}
*/
var isEmptyObject = function isEmptyObject(obj) {
return Object.keys(obj).length === 0;
};
/**
* Checks if a value is numeric
*
* @param {Object} value - The value to be checked
* @return {boolean}
*/
var isNumeric = function isNumeric(value) {
return !Number.isNaN(parseFloat(value)) && !Number.isNaN(value - 0);
};
/**
* Helper to pluralize words in error messages
*
* @param {string} word - A string that will be suffixed with an 's' if collection has more than 1 member
* @param {Array} arr - The group of items
* @return {string} Either the word sufixed with a simple plural, 's', or the provided original
*/
var pluralize = function pluralize(word, arr) {
return arr.length > 1 ? "".concat(word, "s") : word;
};
/**
* Computes a flag based on given probability thresholds and a sampling rate
*
* @param {TrackingApiServices} services An HTML element to inspect
* @param {Number|Boolean} samplingRate A number from 0 to 1 (inclusive), or a boolean
* @return {TrackingApiServices}
*/
var sampleServices = function sampleServices(services, samplingRate) {
return Object.keys(services).reduce(function (acc, cur) {
var serviceThreshold = services[cur];
acc[cur] = serviceThreshold >= samplingRate;
return acc;
}, {});
};
/**
* Provides a basic polyfill for Promise.allSettled
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
* @see https://caniuse.com/#feat=mdn-javascript_builtins_promise_allsettled
* @param {Promise[]} arr An array of Promises
* @return {Promise<Array<{status: string, value: any}|{status: string, reason: Error}>>}
*/
var allSettledPolyfill = function allSettledPolyfill(arr) {
return Promise.all(arr.map(function (promise) {
return promise.then(function (value) {
return {
status: 'fulfilled',
value: value
};
}).catch(function (error) {
return {
status: 'rejected',
reason: error
};
});
}));
};
exports.allSettledPolyfill = allSettledPolyfill;
exports.hasDntEnabled = hasDntEnabled;
exports.isEmptyObject = isEmptyObject;
exports.isNumeric = isNumeric;
exports.isUndefined = isUndefined;
exports.pluralize = pluralize;
exports.sampleServices = sampleServices;
//# sourceMappingURL=utils.js.map