{"version":3,"file":"filter.cjs","names":[],"sources":["../src/filter.ts"],"sourcesContent":["/**\n * Formats the value of a filter element to a stringified version accepted by the Prismic REST API.\n *\n * @param value - Value to format.\n * @returns `value` formatted for the Prismic REST API.\n */\nconst formatValue = (\n\tvalue: string | number | Date | unknown | (string | number | Date | unknown)[],\n): string => {\n\tif (Array.isArray(value)) {\n\t\treturn `[${value.map(formatValue).join(\", \")}]`\n\t}\n\n\tif (typeof value === \"string\") {\n\t\treturn `\"${value.replace(/\"/g, '\\\\\"')}\"`\n\t}\n\n\tif (value instanceof Date) {\n\t\treturn `${value.getTime()}`\n\t}\n\n\treturn `${value}`\n}\n\n/**\n * Creates a filter builder function for filters with a path and arguments.\n *\n * @typeParam Args - Arguments for the filter.\n * @param name - Name of the filter used in the resulting string.\n * @returns Filter builder function for the given name.\n */\nconst pathWithArgsFilter = <Args extends unknown[]>(name: string) => {\n\t/** @param path - Path to the value to be compared. */\n\tconst fn = (path: string, ...args: Args): string => {\n\t\tconst formattedArgs = args.map(formatValue).join(\", \")\n\t\tconst joiner = path && args.length ? \", \" : \"\"\n\n\t\treturn `[${name}(${path}${joiner}${formattedArgs})]`\n\t}\n\n\treturn fn\n}\n\n/**\n * Creates a filter builder function for filters with only a path.\n *\n * @param name - Name of the filter used in the resulting string.\n * @returns Filter builder function for the given name.\n */\nconst pathFilter = (name: string) => {\n\tconst filterFn = pathWithArgsFilter(name)\n\n\t/** @param path - Path for the filter. */\n\tconst fn = (path: string): string => {\n\t\treturn filterFn(path)\n\t}\n\n\treturn fn\n}\n\n/**\n * Creates a filter builder function for filters with only arguments and no path.\n *\n * @param name - Name of the filter used in the resulting string.\n * @returns Filter builder function for the given name.\n */\nconst argsFilter = <Args extends unknown[]>(name: string) => {\n\tconst filterFn = pathWithArgsFilter<Args>(name)\n\n\t/** @param args - Arguments for the filter. */\n\tconst fn = (...args: Args): string => {\n\t\treturn filterFn(\"\", ...args)\n\t}\n\n\treturn fn\n}\n\nexport const filter = {\n\t/**\n\t * The `at` filter checks that the path matches the described value exactly. It takes a single\n\t * value for a field or an array (only for tags).\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#at}\n\t */\n\tat: pathWithArgsFilter<[value: string | number | boolean | Date | string[]]>(\"at\"),\n\n\t/**\n\t * The `not` filter checks that the path doesn't match the provided value exactly. It takes a\n\t * single value for a field or an array (only for tags).\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#not}\n\t */\n\tnot: pathWithArgsFilter<[value: string | number | boolean | Date | string[]]>(\"not\"),\n\n\t/**\n\t * The `any` filter takes an array of values. It works exactly the same way as the `at` operator,\n\t * but checks whether the fragment matches any of the values in the array.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#any}\n\t */\n\tany: pathWithArgsFilter<[values: (string | number | boolean | Date)[]]>(\"any\"),\n\n\t/**\n\t * The `in` filter is used specifically to retrieve an array of documents by their IDs or UIDs.\n\t * This filter is much more efficient at this than the any filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#in}\n\t */\n\tin: pathWithArgsFilter<[values: string[]]>(\"in\"),\n\n\t/**\n\t * The `fulltext` filter provides two capabilities:\n\t *\n\t * 1. Checking if a certain string is anywhere inside a document (this is what you should use to make\n\t *    your project's search engine feature)\n\t * 2. Checking if the string is contained inside a specific custom type’s Rich Text or Key Text\n\t *    fragment.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#fulltext}\n\t */\n\tfulltext: pathWithArgsFilter<[searchTerms: string]>(\"fulltext\"),\n\n\t/**\n\t * The `has` filter checks whether a fragment has a value. It will return all the documents of the\n\t * specified type that contain a value for the specified field.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#has}\n\t */\n\thas: pathFilter(\"has\"),\n\n\t/**\n\t * The `missing` filter checks if a fragment doesn't have a value. It will return all the\n\t * documents of the specified type that do not contain a value for the specified field.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#missing}\n\t */\n\tmissing: pathFilter(\"missing\"),\n\n\t/**\n\t * The `similar` filter takes the ID of a document, and returns a list of documents with similar\n\t * content. This allows you to build an automated content discovery feature (for example, a\n\t * \"Related posts\" section).\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#similar}\n\t */\n\tsimilar: argsFilter<[id: string, value: number]>(\"similar\"),\n\n\t/**\n\t * The `geopoint.near` filter checks that the value in the path is within the radius of the given\n\t * coordinates.\n\t *\n\t * This filter will only work for a geopoint field.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#geopointnear}\n\t */\n\tgeopointNear:\n\t\tpathWithArgsFilter<[latitude: number, longitude: number, radius: number]>(\"geopoint.near\"),\n\n\t/**\n\t * The `number.lt` filter checks that the value in the number field is less than the value passed\n\t * into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#numberlessthan}\n\t */\n\tnumberLessThan: pathWithArgsFilter<[value: number]>(\"number.lt\"),\n\n\t/**\n\t * The `number.gt` filter checks that the value in the number field is greater than the value\n\t * passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#numbergreaterthan}\n\t */\n\tnumberGreaterThan: pathWithArgsFilter<[value: number]>(\"number.gt\"),\n\n\t/**\n\t * The `number.inRange` filter checks that the value in the path is within the two values passed\n\t * into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#numberinrange}\n\t */\n\tnumberInRange: pathWithArgsFilter<[lowerLimit: number, upperLimit: number]>(\"number.inRange\"),\n\n\t/**\n\t * The `date.after` filter checks that the value in the path is after the date value passed into\n\t * the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateAfter: pathWithArgsFilter<[date: string | number | Date]>(\"date.after\"),\n\n\t/**\n\t * The `date.before` filter checks that the value in the path is before the date value passed into\n\t * the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateBefore: pathWithArgsFilter<[date: string | number | Date]>(\"date.before\"),\n\n\t/**\n\t * The `date.between` filter checks that the value in the path is within the date values passed\n\t * into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateBetween:\n\t\tpathWithArgsFilter<[startDate: string | number | Date, endDate: string | number | Date]>(\n\t\t\t\"date.between\",\n\t\t),\n\n\t/**\n\t * The `date.day-of-month` filter checks that the value in the path is equal to the day of the\n\t * month passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateDayOfMonth: pathWithArgsFilter<[day: number]>(\"date.day-of-month\"),\n\n\t/**\n\t * The `date.day-of-month-after` filter checks that the value in the path is after the day of the\n\t * month passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateDayOfMonthAfter: pathWithArgsFilter<[day: number]>(\"date.day-of-month-after\"),\n\n\t/**\n\t * The `date.day-of-month-before` filter checks that the value in the path is before the day of\n\t * the month passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateDayOfMonthBefore: pathWithArgsFilter<[day: number]>(\"date.day-of-month-before\"),\n\n\t/**\n\t * The `date.day-of-week` filter checks that the value in the path is equal to the day of the week\n\t * passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateDayOfWeek: pathWithArgsFilter<[day: string | number]>(\"date.day-of-week\"),\n\n\t/**\n\t * The `date.day-of-week-after` filter checks that the value in the path is after the day of the\n\t * week passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateDayOfWeekAfter: pathWithArgsFilter<[day: string | number]>(\"date.day-of-week-after\"),\n\n\t/**\n\t * The date.day-of-week-before filter checks that the value in the path is before the day of the\n\t * week passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateDayOfWeekBefore: pathWithArgsFilter<[day: string | number]>(\"date.day-of-week-before\"),\n\n\t/**\n\t * The `date.month` filter checks that the value in the path occurs in the month value passed into\n\t * the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateMonth: pathWithArgsFilter<[month: string | number]>(\"date.month\"),\n\n\t/**\n\t * The `date.month-after` filter checks that the value in the path occurs in any month after the\n\t * value passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateMonthAfter: pathWithArgsFilter<[month: string | number]>(\"date.month-after\"),\n\n\t/**\n\t * The `date.month-before` filter checks that the value in the path occurs in any month before the\n\t * value passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateMonthBefore: pathWithArgsFilter<[month: string | number]>(\"date.month-before\"),\n\n\t/**\n\t * The `date.year` filter checks that the value in the path occurs in the year value passed into\n\t * the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateYear: pathWithArgsFilter<[year: number]>(\"date.year\"),\n\n\t/**\n\t * The `date.hour` filter checks that the value in the path occurs within the hour value passed\n\t * into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateHour: pathWithArgsFilter<[hour: number]>(\"date.hour\"),\n\n\t/**\n\t * The `date.hour-after` filter checks that the value in the path occurs after the hour value\n\t * passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateHourAfter: pathWithArgsFilter<[hour: number]>(\"date.hour-after\"),\n\n\t/**\n\t * The `date.hour-before` filter checks that the value in the path occurs before the hour value\n\t * passed into the filter.\n\t *\n\t * {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}\n\t */\n\tdateHourBefore: pathWithArgsFilter<[hour: number]>(\"date.hour-before\"),\n}\n"],"mappings":";;;;;;;AAMA,MAAM,eACL,UACY;AACZ,KAAI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