Skip to content

Commit

Permalink
docs: describe internal predicate functions [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed May 6, 2021
1 parent c38e067 commit e4fed22
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/predicate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Formats the value of a predicate element to a stringified version accepted by the Prismic REST API.
*
* @params value Value to format.
*
* @returns `value` formatted for the Prismic REST API.
*/
const formatValue = (
value: string | number | Date | (string | number | Date)[],
): string =>
Expand All @@ -9,6 +16,13 @@ const formatValue = (
? `${value.getTime()}`
: `${value}`

/**
* Creates a predicate builder function for predicates with a path and arguments.
*
* @params name Name of the predicate used in the resulting string.
*
* @returns Predicate builder function for the given name.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pathWithArgsPredicate = <Args extends any[]>(name: string) =>
/**
Expand All @@ -19,14 +33,31 @@ const pathWithArgsPredicate = <Args extends any[]>(name: string) =>
.map(formatValue)
.join(', ')})]`

/**
* Creates a predicate builder function for predicates with only a path.
*
* @params name Name of the predicate used in the resulting string.
*
* @returns Predicate builder function for the given name.
*/
const pathPredicate = (name: string) => (path: string): string =>
pathWithArgsPredicate(name)(path)

/**
* Creates a predicate builder function for predicates with only arguments and no path.
*
* @params name Name of the predicate used in the resulting string.
*
* @returns Predicate builder function for the given name.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const argsPredicate = <Args extends any[]>(name: string) => (
...args: Args
): string => pathWithArgsPredicate<Args>(name)('', ...args)

/**
* The default arguments allowed by predicates.
*/
type DefaultPredicateArgs = [value: string | number | (string | number)[]]

/**
Expand Down

0 comments on commit e4fed22

Please sign in to comment.