@qaflag/core
Version:
Base requirements for the QA Flag library
66 lines • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleep = exports.ucfirst = exports.ordinal = exports.shallowMerge = exports.deepMerge = exports.humanReadableList = void 0;
const is_1 = require("@sindresorhus/is");
const humanReadableList = (array, join = ',', finalJoin = 'and') => {
if (!Array.isArray(array) || array.length == 0)
return '';
if (array.length == 1)
return String(array[0]);
const arr = array.slice(0), last = arr.pop();
return array.length > 2
? arr.join(`${join} `) + `${join} ${finalJoin} ${last}`
: `${array[0]} ${finalJoin} ${last}`;
};
exports.humanReadableList = humanReadableList;
const deepMerge = (target, ...sources) => {
const merge = (target, source) => {
Object.keys(source).forEach((key) => {
const targetValue = target[key];
const sourceValue = source[key];
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
target[key] = targetValue.concat(sourceValue);
}
else if (is_1.default.object(targetValue) && is_1.default.object(sourceValue)) {
target[key] = merge(Object.assign({}, targetValue), sourceValue);
}
else {
target[key] = sourceValue;
}
});
return target;
};
return sources.reduce((prev, cur) => merge(prev, cur), target);
};
exports.deepMerge = deepMerge;
const shallowMerge = (target, ...sources) => {
const merge = (target, source) => {
Object.keys(source).forEach((key) => {
const targetValue = target[key];
const sourceValue = source[key];
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
target[key] = targetValue.concat(sourceValue);
}
else {
target[key] = sourceValue;
}
});
return target;
};
return sources.reduce((prev, cur) => merge(prev, cur), target);
};
exports.shallowMerge = shallowMerge;
const ordinal = (n) => {
const s = ['th', 'st', 'nd', 'rd'];
const v = n % 100;
return n + (s[(v - 20) % 10] || s[v] || s[0]);
};
exports.ordinal = ordinal;
const ucfirst = (str) => str
.split(' ')
.map(word => word.slice(0, 1).toUpperCase() + word.slice(1))
.join(' ');
exports.ucfirst = ucfirst;
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
exports.sleep = sleep;
//# sourceMappingURL=helpers.js.map