@posx/core 5.5.339 → 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 CHANGED
@@ -3035,24 +3035,53 @@ const inArray = function(value, ...args) {
3035
3035
  const options = args.pop();
3036
3036
  return getValue(args.includes(value), this, options);
3037
3037
  };
3038
+ const getByPath = (obj, path) => {
3039
+ if (!obj || !path) return void 0;
3040
+ return path.split(".").reduce((o, k) => o?.[k], obj);
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
+ };
3038
3059
  /**
3039
- * Find first object in array by property, returns the object for chaining
3040
- * {{findBy modifiers "category_uid" "imc_xxx"}} - returns first matching object
3041
- * {{lookup (findBy modifiers "category_uid" "imc_xxx") "name"}} - get name of found object
3060
+ * Find first object matching ANY condition (OR logic)
3061
+ * {{findOr categories "name" "Topping" "name" "Topping one"}}
3042
3062
  */
3043
- const findBy = function(collection, prop, value) {
3063
+ const findOr = function(collection, ...args) {
3044
3064
  if (!Array.isArray(collection)) return null;
3045
- return collection.find((item) => item && item[prop] === value) || null;
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));
3046
3076
  };
3047
3077
  /**
3048
- * Filter array by property value, returns array for indexing
3049
- * {{filterBy modifiers "category_uid" "imc_xxx"}} - returns matching array
3050
- * {{lookup (filterBy modifiers "category_uid" "imc_xxx") 0}} - get first match
3051
- * {{lookup (lookup (filterBy modifiers "category_uid" "imc_xxx") 0) "name"}} - get name of first match
3078
+ * Filter array matching ANY condition (OR logic)
3079
+ * {{filterOr modifiers "name" "Topping" "name" "Topping one"}}
3052
3080
  */
3053
- const filterBy = function(collection, prop, value) {
3081
+ const filterOr = function(collection, ...args) {
3054
3082
  if (!Array.isArray(collection)) return [];
3055
- return collection.filter((item) => item && item[prop] === value);
3083
+ const conditions = parseConditions(args);
3084
+ return collection.filter((item) => conditions.some((c) => getByPath(item, c.prop) === c.value));
3056
3085
  };
3057
3086
  /**
3058
3087
  * Returns the first truthy value, or default
@@ -3106,8 +3135,10 @@ const comparisonHelpers = {
3106
3135
  contains,
3107
3136
  includes,
3108
3137
  in: inArray,
3109
- findBy,
3110
- filterBy,
3138
+ findAnd,
3139
+ findOr,
3140
+ filterAnd,
3141
+ filterOr,
3111
3142
  default: defaultValue,
3112
3143
  neither,
3113
3144
  ifEven,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posx/core",
3
- "version": "5.5.339",
3
+ "version": "5.5.350",
4
4
  "description": "POSX core libraries",
5
5
  "main": "./build/index.js",
6
6
  "author": "Steven Lee",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posx/core",
3
- "version": "5.5.339",
3
+ "version": "5.5.350",
4
4
  "description": "POSX core libraries",
5
5
  "type": "module",
6
6
  "main": "./build/index.js",