(obj: {
[key: string]:
| FilterValue
| { [key: string]: FilterValue | undefined }
| undefined;
})
| 105 | const identifierRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/; |
| 106 | |
| 107 | export function objectToQuery(obj: { |
| 108 | [key: string]: |
| 109 | | FilterValue |
| 110 | | { [key: string]: FilterValue | undefined } |
| 111 | | undefined; |
| 112 | }): string { |
| 113 | let filterList: Filter[] = []; |
| 114 | const entries = Object.entries(obj); |
| 115 | |
| 116 | const keyValuePairs: [string, string | number | boolean | null][] = []; |
| 117 | const keyValuePairsWithOperator: [string, OperatorFilterValue][] = []; |
| 118 | const indexedKeys: [string, Record<string, FilterValue | undefined>][] = []; |
| 119 | |
| 120 | entries.forEach(([key, value]) => { |
| 121 | if (!identifierRegex.test(key)) { |
| 122 | throw new Error("Key must only contain letters, numbers, _"); |
| 123 | } |
| 124 | |
| 125 | if (isSimpleValue(value)) { |
| 126 | keyValuePairs.push([key, value]); |
| 127 | } else if (isPlainObject(value)) { |
| 128 | if (isStartsWithOperator(value) || isNumberOperator(value)) { |
| 129 | keyValuePairsWithOperator.push([key, value]); |
| 130 | } else { |
| 131 | indexedKeys.push([key, value]); |
| 132 | } |
| 133 | } |
| 134 | }); |
| 135 | |
| 136 | filterList = [ |
| 137 | ...getFiltersFromKeyValuePairs(keyValuePairs), |
| 138 | ...getFiltersFromKeyValuePairsWithOperator(keyValuePairsWithOperator), |
| 139 | ]; |
| 140 | |
| 141 | indexedKeys.forEach(([key, value]) => { |
| 142 | const nestedEntries = Object.entries(value); |
| 143 | const nKeyValuePairs: [string, SimpleFilterValue][] = []; |
| 144 | const nKeyValuePairsWithOperator: [string, OperatorFilterValue][] = []; |
| 145 | nestedEntries.forEach(([nestedKey, nestedValue]) => { |
| 146 | if (isStringEmpty(nestedKey)) { |
| 147 | throw new Error("Key cannot be empty"); |
| 148 | } |
| 149 | |
| 150 | if (isSimpleValue(nestedValue)) { |
| 151 | nKeyValuePairs.push([formatFilterKey(key, nestedKey), nestedValue]); |
| 152 | } else if ( |
| 153 | isStartsWithOperator(nestedValue) || |
| 154 | isNumberOperator(nestedValue) |
| 155 | ) { |
| 156 | nKeyValuePairsWithOperator.push([ |
| 157 | formatFilterKey(key, nestedKey), |
| 158 | nestedValue, |
| 159 | ]); |
| 160 | } |
| 161 | }); |
| 162 | filterList = [ |
| 163 | ...filterList, |
| 164 | ...getFiltersFromKeyValuePairs(nKeyValuePairs), |
no test coverage detected