@posx/core 5.5.340 → 5.5.350
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.js +40 -12
- package/package.json +1 -1
- package/package.publish.json +1 -1
package/build/index.js
CHANGED
|
@@ -3039,23 +3039,49 @@ const getByPath = (obj, path) => {
|
|
|
3039
3039
|
if (!obj || !path) return void 0;
|
|
3040
3040
|
return path.split(".").reduce((o, k) => o?.[k], obj);
|
|
3041
3041
|
};
|
|
3042
|
+
const parseConditions = (args) => {
|
|
3043
|
+
const conditions = [];
|
|
3044
|
+
for (let i = 0; i < args.length - 1; i += 2) if (typeof args[i] === "string" && !isOptions(args[i + 1])) conditions.push({
|
|
3045
|
+
prop: args[i],
|
|
3046
|
+
value: args[i + 1]
|
|
3047
|
+
});
|
|
3048
|
+
return conditions;
|
|
3049
|
+
};
|
|
3050
|
+
/**
|
|
3051
|
+
* Find first object matching ALL conditions (AND logic)
|
|
3052
|
+
* {{findAnd categories "name" "Topping" "active" true}}
|
|
3053
|
+
*/
|
|
3054
|
+
const findAnd = function(collection, ...args) {
|
|
3055
|
+
if (!Array.isArray(collection)) return null;
|
|
3056
|
+
const conditions = parseConditions(args);
|
|
3057
|
+
return collection.find((item) => conditions.every((c) => getByPath(item, c.prop) === c.value)) || null;
|
|
3058
|
+
};
|
|
3042
3059
|
/**
|
|
3043
|
-
* Find first object
|
|
3044
|
-
* {{
|
|
3045
|
-
* {{lookup (findBy modifiers "item.category_uid" "imc_xxx") "item.name"}} - get item.name
|
|
3060
|
+
* Find first object matching ANY condition (OR logic)
|
|
3061
|
+
* {{findOr categories "name" "Topping" "name" "Topping one"}}
|
|
3046
3062
|
*/
|
|
3047
|
-
const
|
|
3063
|
+
const findOr = function(collection, ...args) {
|
|
3048
3064
|
if (!Array.isArray(collection)) return null;
|
|
3049
|
-
|
|
3065
|
+
const conditions = parseConditions(args);
|
|
3066
|
+
return collection.find((item) => conditions.some((c) => getByPath(item, c.prop) === c.value)) || null;
|
|
3067
|
+
};
|
|
3068
|
+
/**
|
|
3069
|
+
* Filter array matching ALL conditions (AND logic)
|
|
3070
|
+
* {{filterAnd modifiers "item.category_uid" "imc_xxx" "price" 0}}
|
|
3071
|
+
*/
|
|
3072
|
+
const filterAnd = function(collection, ...args) {
|
|
3073
|
+
if (!Array.isArray(collection)) return [];
|
|
3074
|
+
const conditions = parseConditions(args);
|
|
3075
|
+
return collection.filter((item) => conditions.every((c) => getByPath(item, c.prop) === c.value));
|
|
3050
3076
|
};
|
|
3051
3077
|
/**
|
|
3052
|
-
* Filter array
|
|
3053
|
-
* {{
|
|
3054
|
-
* {{lookup (filterBy modifiers "item.category_uid" "imc_xxx") 0}} - get first match
|
|
3078
|
+
* Filter array matching ANY condition (OR logic)
|
|
3079
|
+
* {{filterOr modifiers "name" "Topping" "name" "Topping one"}}
|
|
3055
3080
|
*/
|
|
3056
|
-
const
|
|
3081
|
+
const filterOr = function(collection, ...args) {
|
|
3057
3082
|
if (!Array.isArray(collection)) return [];
|
|
3058
|
-
|
|
3083
|
+
const conditions = parseConditions(args);
|
|
3084
|
+
return collection.filter((item) => conditions.some((c) => getByPath(item, c.prop) === c.value));
|
|
3059
3085
|
};
|
|
3060
3086
|
/**
|
|
3061
3087
|
* Returns the first truthy value, or default
|
|
@@ -3109,8 +3135,10 @@ const comparisonHelpers = {
|
|
|
3109
3135
|
contains,
|
|
3110
3136
|
includes,
|
|
3111
3137
|
in: inArray,
|
|
3112
|
-
|
|
3113
|
-
|
|
3138
|
+
findAnd,
|
|
3139
|
+
findOr,
|
|
3140
|
+
filterAnd,
|
|
3141
|
+
filterOr,
|
|
3114
3142
|
default: defaultValue,
|
|
3115
3143
|
neither,
|
|
3116
3144
|
ifEven,
|
package/package.json
CHANGED