react-querybuilder
Version:
React Query Builder component for constructing queries and filters, with utilities for executing them in various database and evaluation contexts
164 lines (161 loc) • 4.44 kB
JavaScript
//#region src/defaults.ts
/**
* @group Defaults
*/
const defaultPlaceholderName = "~";
/**
* Default `name` for placeholder option in the `fields` array.
*
* @group Defaults
*/
const defaultPlaceholderFieldName = defaultPlaceholderName;
/**
* Default `name` for placeholder option in the `operators` array.
*
* @group Defaults
*/
const defaultPlaceholderOperatorName = defaultPlaceholderName;
/**
* Default character used to `.join` and `.split` arrays.
*
* @group Defaults
*/
const defaultJoinChar = ",";
/**
* Map of default operators to their respective opposite/negating operators.
*
* @group Defaults
*/
const defaultOperatorNegationMap = {
"=": "!=",
"!=": "=",
"<": ">=",
"<=": ">",
">": "<=",
">=": "<",
beginsWith: "doesNotBeginWith",
doesNotBeginWith: "beginsWith",
endsWith: "doesNotEndWith",
doesNotEndWith: "endsWith",
contains: "doesNotContain",
doesNotContain: "contains",
between: "notBetween",
notBetween: "between",
in: "notIn",
notIn: "in",
notNull: "null",
null: "notNull"
};
/**
* Default combinator list.
*
* @group Defaults
*/
const defaultCombinators = [{
name: "and",
value: "and",
label: "AND"
}, {
name: "or",
value: "or",
label: "OR"
}];
/**
* Default combinator list, with `XOR` added.
*
* @group Defaults
*/
const defaultCombinatorsExtended = [...defaultCombinators, {
name: "xor",
value: "xor",
label: "XOR"
}];
//#endregion
//#region src/utils/arrayUtils.ts
/**
* Splits a string by a given character (see {@link defaultJoinChar}). Escaped characters
* (characters preceded by a backslash) will not apply to the split, and the backslash will
* be removed in the array element. Inverse of {@link joinWith}.
*
* @example
* splitBy('this\\,\\,that,,the other,,,\\,')
* // or
* splitBy('this\\,\\,that,,the other,,,\\,', ',')
* // would return
* ['this,,that', '', 'the other', '', '', ',']
*/
const splitBy = (str, splitChar = defaultJoinChar) => typeof str === "string" ? str.split(`\\${splitChar}`).map((c) => c.split(splitChar)).reduce((prev, curr, idx) => {
if (idx === 0) return curr;
return [
...prev.slice(0, -1),
`${prev.at(-1)}${splitChar}${curr[0]}`,
...curr.slice(1)
];
}, []) : [];
/**
* Joins an array of strings using the given character (see {@link defaultJoinChar}). When
* the given character appears in an array element, a backslash will be added just before it
* to distinguish it from the join character. Effectively the inverse of {@link splitBy}.
*
* TIP: The join character can actually be a string of any length. Only the first character
* will be searched for in the array elements and preceded by a backslash.
*
* @example
* joinWith(['this,,that', '', 'the other', '', '', ','], ', ')
* // would return
* 'this\\,\\,that, , the other, , , \\,'
*/
const joinWith = (strArr, joinChar = defaultJoinChar) => strArr.map((str) => `${str ?? ""}`.replaceAll(joinChar[0], `\\${joinChar[0]}`)).join(joinChar);
/**
* Trims the value if it is a string. Otherwise returns the value as is.
*/
const trimIfString = (val) => typeof val === "string" ? val.trim() : val;
/**
* Splits a string by comma then trims each element. Arrays are returned as is except
* any string elements are trimmed.
*/
const toArray = (v, { retainEmptyStrings } = {}) => Array.isArray(v) ? v.map((v$1) => trimIfString(v$1)) : typeof v === "string" ? splitBy(v, defaultJoinChar).filter(retainEmptyStrings ? () => true : (s) => !/^\s*$/.test(s)).map((s) => s.trim()) : typeof v === "number" ? [v] : [];
//#endregion
Object.defineProperty(exports, 'defaultOperatorNegationMap', {
enumerable: true,
get: function () {
return defaultOperatorNegationMap;
}
});
Object.defineProperty(exports, 'defaultPlaceholderFieldName', {
enumerable: true,
get: function () {
return defaultPlaceholderFieldName;
}
});
Object.defineProperty(exports, 'defaultPlaceholderOperatorName', {
enumerable: true,
get: function () {
return defaultPlaceholderOperatorName;
}
});
Object.defineProperty(exports, 'joinWith', {
enumerable: true,
get: function () {
return joinWith;
}
});
Object.defineProperty(exports, 'splitBy', {
enumerable: true,
get: function () {
return splitBy;
}
});
Object.defineProperty(exports, 'toArray', {
enumerable: true,
get: function () {
return toArray;
}
});
Object.defineProperty(exports, 'trimIfString', {
enumerable: true,
get: function () {
return trimIfString;
}
});
//# sourceMappingURL=arrayUtils-D5EoIsKP.js.map