* update to eslint 6.8.0 and related packages * Fixes for no-prototype-builtins * Updates for explicit-function-return-type * Add ignores for no-explicit-any * update to eslint 6.8.0 and related packages * Fixes for no-prototype-builtins * Updates for explicit-function-return-type * Add ignores for no-explicit-any * package: activate eslint-config-standard * apply updated eslint configuration * lint: apply eslint feedback after rebase * Update lockfile * Update lockfile * Restore missing deps * Update lockfile * Re-add eslint-plugin-node * Add eslint-plugin-standard and eslint-plugin-react-hooks * Clean lint Co-authored-by: Paul Melnikow <github@paulmelnikow.com>
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import escapeStringRegexp from 'escape-string-regexp'
|
|
import { Example, ServiceDefinition } from '.'
|
|
|
|
export function exampleMatchesRegex(example: Example, regex: RegExp): boolean {
|
|
const { title, keywords } = example
|
|
const haystack = [title].concat(keywords).join(' ')
|
|
return regex.test(haystack)
|
|
}
|
|
|
|
export function predicateFromQuery(
|
|
query: string
|
|
): ({ examples }: { examples: Example[] }) => boolean {
|
|
const escaped = escapeStringRegexp(query)
|
|
const regex = new RegExp(escaped, 'i') // Case-insensitive.
|
|
return ({ examples }: { examples: Example[] }) =>
|
|
examples.some(example => exampleMatchesRegex(example, regex))
|
|
}
|
|
|
|
export default class ServiceDefinitionSetHelper {
|
|
private readonly definitionData: ServiceDefinition[]
|
|
|
|
public constructor(definitionData: ServiceDefinition[]) {
|
|
this.definitionData = definitionData
|
|
}
|
|
|
|
public static create(
|
|
definitionData: ServiceDefinition[]
|
|
): ServiceDefinitionSetHelper {
|
|
return new ServiceDefinitionSetHelper(definitionData)
|
|
}
|
|
|
|
public getCategory(wantedCategory: string): ServiceDefinitionSetHelper {
|
|
return ServiceDefinitionSetHelper.create(
|
|
this.definitionData.filter(({ category }) => category === wantedCategory)
|
|
)
|
|
}
|
|
|
|
public search(query: string): ServiceDefinitionSetHelper {
|
|
const predicate = predicateFromQuery(query)
|
|
return ServiceDefinitionSetHelper.create(
|
|
this.definitionData.filter(predicate)
|
|
)
|
|
}
|
|
|
|
public notDeprecated(): ServiceDefinitionSetHelper {
|
|
return ServiceDefinitionSetHelper.create(
|
|
this.definitionData.filter(({ isDeprecated }) => !isDeprecated)
|
|
)
|
|
}
|
|
|
|
public toArray(): ServiceDefinition[] {
|
|
return this.definitionData
|
|
}
|
|
}
|