@posx/core 5.5.337 → 5.5.339
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 +278 -6
- package/package.json +1 -1
- package/package.publish.json +1 -1
package/build/index.js
CHANGED
|
@@ -2850,16 +2850,282 @@ const stringHelpers = {
|
|
|
2850
2850
|
*/
|
|
2851
2851
|
var string_helpers_default = stringHelpers;
|
|
2852
2852
|
|
|
2853
|
+
//#endregion
|
|
2854
|
+
//#region src/helpers/comparison.helpers.ts
|
|
2855
|
+
/**
|
|
2856
|
+
* Comparison Helpers for Handlebars
|
|
2857
|
+
* Browser-compatible comparison and logical utilities
|
|
2858
|
+
*/
|
|
2859
|
+
const isOptions = (val) => val && typeof val === "object" && typeof val.fn === "function";
|
|
2860
|
+
const getValue = (val, context, options) => {
|
|
2861
|
+
if (isOptions(options)) {
|
|
2862
|
+
if (val) return options.fn ? options.fn(context) : true;
|
|
2863
|
+
return options.inverse ? options.inverse(context) : false;
|
|
2864
|
+
}
|
|
2865
|
+
return val;
|
|
2866
|
+
};
|
|
2867
|
+
/**
|
|
2868
|
+
* Block helper that renders if ALL given values are truthy
|
|
2869
|
+
* {{#and a b c}}...{{else}}...{{/and}}
|
|
2870
|
+
*/
|
|
2871
|
+
const and = function(...args) {
|
|
2872
|
+
const options = args.pop();
|
|
2873
|
+
return getValue(args.every((arg) => !!arg), this, options);
|
|
2874
|
+
};
|
|
2875
|
+
/**
|
|
2876
|
+
* Block helper that renders if ANY given value is truthy
|
|
2877
|
+
* {{#or a b c}}...{{else}}...{{/or}}
|
|
2878
|
+
*/
|
|
2879
|
+
const or = function(...args) {
|
|
2880
|
+
const options = args.pop();
|
|
2881
|
+
return getValue(args.some((arg) => !!arg), this, options);
|
|
2882
|
+
};
|
|
2883
|
+
/**
|
|
2884
|
+
* Returns true if value is falsey
|
|
2885
|
+
* {{#not val}}...{{/not}}
|
|
2886
|
+
*/
|
|
2887
|
+
const not = function(val, options) {
|
|
2888
|
+
return getValue(!val, this, options);
|
|
2889
|
+
};
|
|
2890
|
+
/**
|
|
2891
|
+
* Block helper that renders if a === b (strict equality)
|
|
2892
|
+
* {{#eq a b}}...{{else}}...{{/eq}}
|
|
2893
|
+
*/
|
|
2894
|
+
const eq = function(a, b, options) {
|
|
2895
|
+
if (arguments.length === 2) {
|
|
2896
|
+
options = b;
|
|
2897
|
+
b = options?.hash?.compare;
|
|
2898
|
+
}
|
|
2899
|
+
return getValue(a === b, this, options);
|
|
2900
|
+
};
|
|
2901
|
+
/**
|
|
2902
|
+
* Block helper that renders if a == b (loose equality)
|
|
2903
|
+
* {{#is a b}}...{{else}}...{{/is}}
|
|
2904
|
+
*/
|
|
2905
|
+
const is = function(a, b, options) {
|
|
2906
|
+
if (arguments.length === 2) {
|
|
2907
|
+
options = b;
|
|
2908
|
+
b = options?.hash?.compare;
|
|
2909
|
+
}
|
|
2910
|
+
return getValue(a == b, this, options);
|
|
2911
|
+
};
|
|
2912
|
+
/**
|
|
2913
|
+
* Block helper that renders if a !== b
|
|
2914
|
+
* {{#neq a b}}...{{else}}...{{/neq}}
|
|
2915
|
+
*/
|
|
2916
|
+
const neq = function(a, b, options) {
|
|
2917
|
+
if (arguments.length === 2) {
|
|
2918
|
+
options = b;
|
|
2919
|
+
b = options?.hash?.compare;
|
|
2920
|
+
}
|
|
2921
|
+
return getValue(a !== b, this, options);
|
|
2922
|
+
};
|
|
2923
|
+
/**
|
|
2924
|
+
* Block helper that renders if a != b (loose inequality)
|
|
2925
|
+
* {{#isnt a b}}...{{else}}...{{/isnt}}
|
|
2926
|
+
*/
|
|
2927
|
+
const isnt = function(a, b, options) {
|
|
2928
|
+
if (arguments.length === 2) {
|
|
2929
|
+
options = b;
|
|
2930
|
+
b = options?.hash?.compare;
|
|
2931
|
+
}
|
|
2932
|
+
return getValue(a != b, this, options);
|
|
2933
|
+
};
|
|
2934
|
+
/**
|
|
2935
|
+
* Block helper that renders if a > b
|
|
2936
|
+
* {{#gt a b}}...{{else}}...{{/gt}}
|
|
2937
|
+
*/
|
|
2938
|
+
const gt = function(a, b, options) {
|
|
2939
|
+
if (arguments.length === 2) {
|
|
2940
|
+
options = b;
|
|
2941
|
+
b = options?.hash?.compare;
|
|
2942
|
+
}
|
|
2943
|
+
return getValue(a > b, this, options);
|
|
2944
|
+
};
|
|
2945
|
+
/**
|
|
2946
|
+
* Block helper that renders if a >= b
|
|
2947
|
+
* {{#gte a b}}...{{else}}...{{/gte}}
|
|
2948
|
+
*/
|
|
2949
|
+
const gte = function(a, b, options) {
|
|
2950
|
+
if (arguments.length === 2) {
|
|
2951
|
+
options = b;
|
|
2952
|
+
b = options?.hash?.compare;
|
|
2953
|
+
}
|
|
2954
|
+
return getValue(a >= b, this, options);
|
|
2955
|
+
};
|
|
2956
|
+
/**
|
|
2957
|
+
* Block helper that renders if a < b
|
|
2958
|
+
* {{#lt a b}}...{{else}}...{{/lt}}
|
|
2959
|
+
*/
|
|
2960
|
+
const lt = function(a, b, options) {
|
|
2961
|
+
if (arguments.length === 2) {
|
|
2962
|
+
options = b;
|
|
2963
|
+
b = options?.hash?.compare;
|
|
2964
|
+
}
|
|
2965
|
+
return getValue(a < b, this, options);
|
|
2966
|
+
};
|
|
2967
|
+
/**
|
|
2968
|
+
* Block helper that renders if a <= b
|
|
2969
|
+
* {{#lte a b}}...{{else}}...{{/lte}}
|
|
2970
|
+
*/
|
|
2971
|
+
const lte = function(a, b, options) {
|
|
2972
|
+
if (arguments.length === 2) {
|
|
2973
|
+
options = b;
|
|
2974
|
+
b = options?.hash?.compare;
|
|
2975
|
+
}
|
|
2976
|
+
return getValue(a <= b, this, options);
|
|
2977
|
+
};
|
|
2978
|
+
/**
|
|
2979
|
+
* Generic compare helper with operator
|
|
2980
|
+
* {{#compare a "===" b}}...{{/compare}}
|
|
2981
|
+
*/
|
|
2982
|
+
const compare = function(a, operator, b, options) {
|
|
2983
|
+
if (arguments.length < 4) throw new Error("Helper {{compare}} expects 4 arguments");
|
|
2984
|
+
let result;
|
|
2985
|
+
switch (operator) {
|
|
2986
|
+
case "==":
|
|
2987
|
+
result = a == b;
|
|
2988
|
+
break;
|
|
2989
|
+
case "===":
|
|
2990
|
+
result = a === b;
|
|
2991
|
+
break;
|
|
2992
|
+
case "!=":
|
|
2993
|
+
result = a != b;
|
|
2994
|
+
break;
|
|
2995
|
+
case "!==":
|
|
2996
|
+
result = a !== b;
|
|
2997
|
+
break;
|
|
2998
|
+
case "<":
|
|
2999
|
+
result = a < b;
|
|
3000
|
+
break;
|
|
3001
|
+
case ">":
|
|
3002
|
+
result = a > b;
|
|
3003
|
+
break;
|
|
3004
|
+
case "<=":
|
|
3005
|
+
result = a <= b;
|
|
3006
|
+
break;
|
|
3007
|
+
case ">=":
|
|
3008
|
+
result = a >= b;
|
|
3009
|
+
break;
|
|
3010
|
+
case "typeof":
|
|
3011
|
+
result = typeof a === b;
|
|
3012
|
+
break;
|
|
3013
|
+
default: throw new Error(`Helper {{compare}}: invalid operator: \`${operator}\``);
|
|
3014
|
+
}
|
|
3015
|
+
return getValue(result, this, options);
|
|
3016
|
+
};
|
|
3017
|
+
/**
|
|
3018
|
+
* Block helper that renders if collection contains value
|
|
3019
|
+
* {{#contains array "value"}}...{{else}}...{{/contains}}
|
|
3020
|
+
*/
|
|
3021
|
+
const contains = function(collection, value, options) {
|
|
3022
|
+
if (typeof options === "number") options = arguments[3];
|
|
3023
|
+
if (collection == null) return getValue(false, this, options);
|
|
3024
|
+
if (Array.isArray(collection)) return getValue(collection.includes(value), this, options);
|
|
3025
|
+
if (typeof collection === "string") return getValue(collection.includes(value), this, options);
|
|
3026
|
+
if (typeof collection === "object") return getValue(value in collection, this, options);
|
|
3027
|
+
return getValue(false, this, options);
|
|
3028
|
+
};
|
|
3029
|
+
const includes = contains;
|
|
3030
|
+
/**
|
|
3031
|
+
* Block helper that renders if value is in the list
|
|
3032
|
+
* {{#in value "a" "b" "c"}}...{{else}}...{{/in}}
|
|
3033
|
+
*/
|
|
3034
|
+
const inArray = function(value, ...args) {
|
|
3035
|
+
const options = args.pop();
|
|
3036
|
+
return getValue(args.includes(value), this, options);
|
|
3037
|
+
};
|
|
3038
|
+
/**
|
|
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
|
|
3042
|
+
*/
|
|
3043
|
+
const findBy = function(collection, prop, value) {
|
|
3044
|
+
if (!Array.isArray(collection)) return null;
|
|
3045
|
+
return collection.find((item) => item && item[prop] === value) || null;
|
|
3046
|
+
};
|
|
3047
|
+
/**
|
|
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
|
|
3052
|
+
*/
|
|
3053
|
+
const filterBy = function(collection, prop, value) {
|
|
3054
|
+
if (!Array.isArray(collection)) return [];
|
|
3055
|
+
return collection.filter((item) => item && item[prop] === value);
|
|
3056
|
+
};
|
|
3057
|
+
/**
|
|
3058
|
+
* Returns the first truthy value, or default
|
|
3059
|
+
* {{default value "fallback"}}
|
|
3060
|
+
*/
|
|
3061
|
+
const defaultValue = function(...args) {
|
|
3062
|
+
for (let i = 0; i < args.length - 1; i++) if (args[i] != null) return args[i];
|
|
3063
|
+
return "";
|
|
3064
|
+
};
|
|
3065
|
+
/**
|
|
3066
|
+
* Block helper that renders if neither a nor b is truthy
|
|
3067
|
+
* {{#neither a b}}...{{else}}...{{/neither}}
|
|
3068
|
+
*/
|
|
3069
|
+
const neither = function(a, b, options) {
|
|
3070
|
+
return getValue(!a && !b, this, options);
|
|
3071
|
+
};
|
|
3072
|
+
/**
|
|
3073
|
+
* Block helper that renders if value is even
|
|
3074
|
+
* {{#ifEven value}}...{{else}}...{{/ifEven}}
|
|
3075
|
+
*/
|
|
3076
|
+
const ifEven = function(num, options) {
|
|
3077
|
+
return getValue(num % 2 === 0, this, options);
|
|
3078
|
+
};
|
|
3079
|
+
/**
|
|
3080
|
+
* Block helper that renders if value is odd
|
|
3081
|
+
* {{#ifOdd value}}...{{else}}...{{/ifOdd}}
|
|
3082
|
+
*/
|
|
3083
|
+
const ifOdd = function(num, options) {
|
|
3084
|
+
return getValue(num % 2 !== 0, this, options);
|
|
3085
|
+
};
|
|
3086
|
+
/**
|
|
3087
|
+
* Block helper that renders if b is divisible by a
|
|
3088
|
+
* {{#ifNth 3 value}}...{{else}}...{{/ifNth}}
|
|
3089
|
+
*/
|
|
3090
|
+
const ifNth = function(a, b, options) {
|
|
3091
|
+
return getValue(typeof a === "number" && typeof b === "number" && b % a === 0, this, options);
|
|
3092
|
+
};
|
|
3093
|
+
const comparisonHelpers = {
|
|
3094
|
+
and,
|
|
3095
|
+
or,
|
|
3096
|
+
not,
|
|
3097
|
+
eq,
|
|
3098
|
+
is,
|
|
3099
|
+
neq,
|
|
3100
|
+
isnt,
|
|
3101
|
+
gt,
|
|
3102
|
+
gte,
|
|
3103
|
+
lt,
|
|
3104
|
+
lte,
|
|
3105
|
+
compare,
|
|
3106
|
+
contains,
|
|
3107
|
+
includes,
|
|
3108
|
+
in: inArray,
|
|
3109
|
+
findBy,
|
|
3110
|
+
filterBy,
|
|
3111
|
+
default: defaultValue,
|
|
3112
|
+
neither,
|
|
3113
|
+
ifEven,
|
|
3114
|
+
ifOdd,
|
|
3115
|
+
ifNth
|
|
3116
|
+
};
|
|
3117
|
+
var comparison_helpers_default = comparisonHelpers;
|
|
3118
|
+
|
|
2853
3119
|
//#endregion
|
|
2854
3120
|
//#region src/helpers/helpers.register.ts
|
|
2855
3121
|
/**
|
|
2856
|
-
*
|
|
3122
|
+
* Helpers Registration - Following handlebars-helpers API pattern
|
|
2857
3123
|
*
|
|
2858
3124
|
* This module follows the same API as the original handlebars-helpers library:
|
|
2859
3125
|
* helpers({ handlebars: Handlebars })
|
|
2860
3126
|
*/
|
|
2861
3127
|
/**
|
|
2862
|
-
* Register
|
|
3128
|
+
* Register all helpers with Handlebars
|
|
2863
3129
|
*
|
|
2864
3130
|
* @param options - Options object with handlebars instance
|
|
2865
3131
|
* @returns The registered helpers object
|
|
@@ -2867,19 +3133,25 @@ var string_helpers_default = stringHelpers;
|
|
|
2867
3133
|
* @example
|
|
2868
3134
|
* ```typescript
|
|
2869
3135
|
* import Handlebars from 'handlebars';
|
|
2870
|
-
* import helpers from './
|
|
3136
|
+
* import helpers from './helpers.register';
|
|
2871
3137
|
*
|
|
2872
|
-
* // Register all
|
|
3138
|
+
* // Register all helpers
|
|
2873
3139
|
* helpers({ handlebars: Handlebars });
|
|
2874
3140
|
*
|
|
2875
3141
|
* const template = Handlebars.compile('{{camelcase "foo bar baz"}}');
|
|
2876
3142
|
* console.log(template({})); // 'fooBarBaz'
|
|
3143
|
+
*
|
|
3144
|
+
* const template2 = Handlebars.compile('{{#eq a b}}equal{{else}}not equal{{/eq}}');
|
|
3145
|
+
* console.log(template2({ a: 1, b: 1 })); // 'equal'
|
|
2877
3146
|
* ```
|
|
2878
3147
|
*/
|
|
2879
3148
|
function helpers(options) {
|
|
2880
3149
|
const hbs = options?.handlebars || options?.hbs || __require("handlebars");
|
|
2881
|
-
Object.keys(string_helpers_default).forEach((
|
|
2882
|
-
hbs.registerHelper(
|
|
3150
|
+
Object.keys(string_helpers_default).forEach((name) => {
|
|
3151
|
+
hbs.registerHelper(name, string_helpers_default[name]);
|
|
3152
|
+
});
|
|
3153
|
+
Object.keys(comparison_helpers_default).forEach((name) => {
|
|
3154
|
+
hbs.registerHelper(name, comparison_helpers_default[name]);
|
|
2883
3155
|
});
|
|
2884
3156
|
return hbs.helpers;
|
|
2885
3157
|
}
|
package/package.json
CHANGED