MCPcopy Create free account
hub / github.com/liveblocks/liveblocks / objectToQuery

Function objectToQuery

packages/liveblocks-core/src/lib/objectToQuery.ts:107–172  ·  view source on GitHub ↗
(obj: {
  [key: string]:
    | FilterValue
    | { [key: string]: FilterValue | undefined }
    | undefined;
})

Source from the content-addressed store, hash-verified

105const identifierRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
106
107export 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),

Callers 10

getThreadsFunction · 0.90
searchCommentsFunction · 0.90
getInboxNotificationsFunction · 0.90
getRoomsMethod · 0.90
getThreadsMethod · 0.90
getInboxNotificationsMethod · 0.90

Calls 13

isPlainObjectFunction · 0.90
isStartsWithOperatorFunction · 0.90
isNumberOperatorFunction · 0.90
isSimpleValueFunction · 0.85
isStringEmptyFunction · 0.85
formatFilterKeyFunction · 0.85
entriesMethod · 0.80
mapMethod · 0.80
quoteFunction · 0.70
forEachMethod · 0.45

Tested by

no test coverage detected