@ngxs/store
Version:
335 lines (324 loc) • 10.2 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @template T
* @param {?} items - Specific items to append to the end of an array
* @return {?}
*/
function append(items) {
return (/**
* @param {?} existing
* @return {?}
*/
function appendOperator(existing) {
// If `items` is `undefined` or `null` or `[]` but `existing` is provided
// just return `existing`
/** @type {?} */
const itemsNotProvidedButExistingIs = (!items || !items.length) && existing;
if (itemsNotProvidedButExistingIs) {
return (/** @type {?} */ (existing));
}
if (Array.isArray(existing)) {
return existing.concat((/** @type {?} */ (items)));
}
// For example if some property is added dynamically
// and didn't exist before thus it's not `ArrayLike`
return (/** @type {?} */ (items));
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @template T
* @param {...?} operators
* @return {?}
*/
function compose(...operators) {
return (/**
* @param {?} existing
* @return {?}
*/
function composeOperator(existing) {
return operators.reduce((/**
* @param {?} accumulator
* @param {?} operator
* @return {?}
*/
(accumulator, operator) => operator(accumulator)), existing);
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @template T
* @param {?} value
* @return {?}
*/
function isStateOperator(value) {
return typeof value === 'function';
}
/**
* @param {?} value
* @return {?}
*/
function isUndefined(value) {
return typeof value === 'undefined';
}
/**
* @template T
* @param {?} value
* @return {?}
*/
function isPredicate(value) {
return typeof value === 'function';
}
/**
* @param {?} value
* @return {?}
*/
function isNumber(value) {
return typeof value === 'number';
}
/**
* @param {?} index
* @return {?}
*/
function invalidIndex(index) {
return Number.isNaN(index) || index === -1;
}
/**
* @template T
* @param {?} value
* @return {?}
*/
function isNil(value) {
return value === null || isUndefined(value);
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @template T
* @param {?} operatorOrValue
* @param {?=} existing
* @return {?}
*/
function retrieveValue(operatorOrValue, existing) {
// If state operator is a function
// then call it with an original value
if (isStateOperator(operatorOrValue)) {
/** @type {?} */
const value = operatorOrValue((/** @type {?} */ ((/** @type {?} */ (existing)))));
return (/** @type {?} */ (value));
}
// If operator or value was not provided
// e.g. `elseOperatorOrValue` is `undefined`
// then we just return an original value
if (isUndefined(operatorOrValue)) {
return (/** @type {?} */ ((/** @type {?} */ (((/** @type {?} */ (existing)))))));
}
return (/** @type {?} */ (operatorOrValue));
}
/**
* @template T
* @param {?} condition - Condition can be a plain boolean value or a function,
* that returns boolean, also this function can take a value as an argument
* to which this state operator applies
* @param {?} trueOperatorOrValue - Any value or a state operator
* @param {?=} elseOperatorOrValue - Any value or a state operator
* @return {?}
*/
function iif(condition, trueOperatorOrValue, elseOperatorOrValue) {
return (/**
* @param {?} existing
* @return {?}
*/
function iifOperator(existing) {
// Convert the value to a boolean
/** @type {?} */
let result = !!condition;
// but if it is a function then run it to get the result
if (isPredicate(condition)) {
result = condition(existing);
}
if (result) {
return retrieveValue(trueOperatorOrValue, (/** @type {?} */ (existing)));
}
return retrieveValue((/** @type {?} */ (elseOperatorOrValue)), (/** @type {?} */ (existing)));
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @template T
* @param {?} value - Value to insert
* @param {?=} beforePosition
* @return {?}
*/
function insertItem(value, beforePosition) {
return (/**
* @param {?} existing
* @return {?}
*/
function insertItemOperator(existing) {
// Have to check explicitly for `null` and `undefined`
// because `value` can be `0`, thus `!value` will return `true`
if (isNil(value) && existing) {
return (/** @type {?} */ (existing));
}
// Property may be dynamic and might not existed before
if (!Array.isArray(existing)) {
return [(/** @type {?} */ (value))];
}
/** @type {?} */
const clone = existing.slice();
/** @type {?} */
let index = 0;
// No need to call `isNumber`
// as we are checking `> 0` not `>= 0`
// everything except number will return false here
if ((/** @type {?} */ (beforePosition)) > 0) {
index = (/** @type {?} */ (beforePosition));
}
clone.splice(index, 0, (/** @type {?} */ (value)));
return clone;
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @template T
* @param {?} patchObject
* @return {?}
*/
function patch(patchObject) {
return (/**
* @template U
* @param {?} existing
* @return {?}
*/
function patchStateOperator(existing) {
/** @type {?} */
let clone = null;
for (const k in patchObject) {
/** @type {?} */
const newValue = patchObject[k];
/** @type {?} */
const existingPropValue = existing[k];
/** @type {?} */
const newPropValue = isStateOperator(newValue)
? newValue((/** @type {?} */ (existingPropValue)))
: newValue;
if (newPropValue !== existingPropValue) {
if (!clone) {
clone = Object.assign({}, ((/** @type {?} */ (existing))));
}
clone[k] = newPropValue;
}
}
return clone || existing;
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @template T
* @param {?} selector - Index of item in the array or a predicate function
* that can be provided in `Array.prototype.findIndex`
* @param {?} operatorOrValue - New value under the `selector` index or a
* function that can be applied to an existing value
* @return {?}
*/
function updateItem(selector, operatorOrValue) {
return (/**
* @param {?} existing
* @return {?}
*/
function updateItemOperator(existing) {
/** @type {?} */
let index = -1;
if (isPredicate(selector)) {
index = existing.findIndex(selector);
}
else if (isNumber(selector)) {
index = selector;
}
if (invalidIndex(index)) {
return (/** @type {?} */ (existing));
}
/** @type {?} */
let value = (/** @type {?} */ (null));
// Need to check if the new item value will change the existing item value
// then, only if it will change it then clone the array and set the item
if (isStateOperator(operatorOrValue)) {
value = operatorOrValue((/** @type {?} */ (existing[index])));
}
else {
value = operatorOrValue;
}
// If the value hasn't been mutated
// then we just return `existing` array
if (value === existing[index]) {
return (/** @type {?} */ (existing));
}
/** @type {?} */
const clone = existing.slice();
clone[index] = (/** @type {?} */ (value));
return clone;
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @template T
* @param {?} selector - index or predicate to remove an item from an array by
* @return {?}
*/
function removeItem(selector) {
return (/**
* @param {?} existing
* @return {?}
*/
function removeItemOperator(existing) {
/** @type {?} */
let index = -1;
if (isPredicate(selector)) {
index = existing.findIndex(selector);
}
else if (isNumber(selector)) {
index = selector;
}
if (invalidIndex(index)) {
return (/** @type {?} */ (existing));
}
/** @type {?} */
const clone = existing.slice();
clone.splice(index, 1);
return clone;
});
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { append, compose, iif, insertItem, isStateOperator, patch, removeItem, updateItem };
//# sourceMappingURL=ngxs-store-operators.js.map