From fdafaa4681da85c416098256654fe96c171a850b Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Sat, 28 Oct 2023 22:06:33 +0200 Subject: infra(unicorn): filename-case (#2492) --- src/modules/lorem/index.ts | 2 +- src/modules/word/filter-word-list-by-length.ts | 101 +++++++++++++++++++++++++ src/modules/word/filterWordListByLength.ts | 101 ------------------------- src/modules/word/index.ts | 2 +- 4 files changed, 103 insertions(+), 103 deletions(-) create mode 100644 src/modules/word/filter-word-list-by-length.ts delete mode 100644 src/modules/word/filterWordListByLength.ts (limited to 'src/modules') diff --git a/src/modules/lorem/index.ts b/src/modules/lorem/index.ts index 0f3423bc..82e557e6 100644 --- a/src/modules/lorem/index.ts +++ b/src/modules/lorem/index.ts @@ -1,6 +1,6 @@ import type { Faker } from '../..'; import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; -import { filterWordListByLength } from '../word/filterWordListByLength'; +import { filterWordListByLength } from '../word/filter-word-list-by-length'; /** * Module to generate random texts and words. diff --git a/src/modules/word/filter-word-list-by-length.ts b/src/modules/word/filter-word-list-by-length.ts new file mode 100644 index 00000000..106e9858 --- /dev/null +++ b/src/modules/word/filter-word-list-by-length.ts @@ -0,0 +1,101 @@ +import { FakerError } from '../../errors/faker-error'; + +/** + * The error handling strategies for the `filterWordListByLength` function. + * + * Always returns a new array. + */ +const STRATEGIES = { + fail: () => { + throw new FakerError('No words found that match the given length.'); + }, + closest: ( + wordList: ReadonlyArray, + length: { min: number; max: number } + ): string[] => { + const wordsByLength = wordList.reduce( + (data, word) => { + (data[word.length] = data[word.length] ?? []).push(word); + return data; + }, + {} as Record + ); + + const lengths = Object.keys(wordsByLength).map(Number); + const min = Math.min(...lengths); + const max = Math.max(...lengths); + + const closestOffset = Math.min(length.min - min, max - length.max); + + return wordList.filter( + (word) => + word.length === length.min - closestOffset || + word.length === length.max + closestOffset + ); + }, + shortest: (wordList: ReadonlyArray): string[] => { + const minLength = Math.min(...wordList.map((word) => word.length)); + return wordList.filter((word) => word.length === minLength); + }, + longest: (wordList: ReadonlyArray): string[] => { + const maxLength = Math.max(...wordList.map((word) => word.length)); + return wordList.filter((word) => word.length === maxLength); + }, + 'any-length': (wordList: ReadonlyArray): string[] => { + return [...wordList]; + }, +} as const; /* +satisfies Record< +string, // Parameters[0]['strategy'] +(wordList: string[], length: { min: number; max: number }) => string[] +>; +*/ + +/** + * Filters a string array for values with a matching length. + * If length is not provided or no values with a matching length are found, + * then the result will be determined using the given error handling strategy. + * + * @param options The options to provide. + * @param options.wordList A list of words to filter. + * @param options.length The exact or the range of lengths the words should have. + * @param options.strategy The strategy to apply when no words with a matching length are found. Defaults to 'any-length'. + * + * Available error handling strategies: + * + * - `fail`: Throws an error if no words with the given length are found. + * - `shortest`: Returns any of the shortest words. + * - `closest`: Returns any of the words closest to the given length. + * - `longest`: Returns any of the longest words. + * - `any-length`: Returns a copy of the original word list. + */ +export function filterWordListByLength(options: { + wordList: ReadonlyArray; + length?: number | { min: number; max: number }; + strategy?: 'fail' | 'closest' | 'shortest' | 'longest' | 'any-length'; +}): string[] { + const { wordList, length, strategy = 'any-length' } = options; + + if (length) { + const filter: (word: string) => boolean = + typeof length === 'number' + ? (word) => word.length === length + : (word) => word.length >= length.min && word.length <= length.max; + + const wordListWithLengthFilter = wordList.filter(filter); + + if (wordListWithLengthFilter.length > 0) { + return wordListWithLengthFilter; + } + + if (typeof length === 'number') { + return STRATEGIES[strategy](wordList, { min: length, max: length }); + } + + return STRATEGIES[strategy](wordList, length); + } else if (strategy === 'shortest' || strategy === 'longest') { + return STRATEGIES[strategy](wordList); + } + + return [...wordList]; +} diff --git a/src/modules/word/filterWordListByLength.ts b/src/modules/word/filterWordListByLength.ts deleted file mode 100644 index 106e9858..00000000 --- a/src/modules/word/filterWordListByLength.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { FakerError } from '../../errors/faker-error'; - -/** - * The error handling strategies for the `filterWordListByLength` function. - * - * Always returns a new array. - */ -const STRATEGIES = { - fail: () => { - throw new FakerError('No words found that match the given length.'); - }, - closest: ( - wordList: ReadonlyArray, - length: { min: number; max: number } - ): string[] => { - const wordsByLength = wordList.reduce( - (data, word) => { - (data[word.length] = data[word.length] ?? []).push(word); - return data; - }, - {} as Record - ); - - const lengths = Object.keys(wordsByLength).map(Number); - const min = Math.min(...lengths); - const max = Math.max(...lengths); - - const closestOffset = Math.min(length.min - min, max - length.max); - - return wordList.filter( - (word) => - word.length === length.min - closestOffset || - word.length === length.max + closestOffset - ); - }, - shortest: (wordList: ReadonlyArray): string[] => { - const minLength = Math.min(...wordList.map((word) => word.length)); - return wordList.filter((word) => word.length === minLength); - }, - longest: (wordList: ReadonlyArray): string[] => { - const maxLength = Math.max(...wordList.map((word) => word.length)); - return wordList.filter((word) => word.length === maxLength); - }, - 'any-length': (wordList: ReadonlyArray): string[] => { - return [...wordList]; - }, -} as const; /* -satisfies Record< -string, // Parameters[0]['strategy'] -(wordList: string[], length: { min: number; max: number }) => string[] ->; -*/ - -/** - * Filters a string array for values with a matching length. - * If length is not provided or no values with a matching length are found, - * then the result will be determined using the given error handling strategy. - * - * @param options The options to provide. - * @param options.wordList A list of words to filter. - * @param options.length The exact or the range of lengths the words should have. - * @param options.strategy The strategy to apply when no words with a matching length are found. Defaults to 'any-length'. - * - * Available error handling strategies: - * - * - `fail`: Throws an error if no words with the given length are found. - * - `shortest`: Returns any of the shortest words. - * - `closest`: Returns any of the words closest to the given length. - * - `longest`: Returns any of the longest words. - * - `any-length`: Returns a copy of the original word list. - */ -export function filterWordListByLength(options: { - wordList: ReadonlyArray; - length?: number | { min: number; max: number }; - strategy?: 'fail' | 'closest' | 'shortest' | 'longest' | 'any-length'; -}): string[] { - const { wordList, length, strategy = 'any-length' } = options; - - if (length) { - const filter: (word: string) => boolean = - typeof length === 'number' - ? (word) => word.length === length - : (word) => word.length >= length.min && word.length <= length.max; - - const wordListWithLengthFilter = wordList.filter(filter); - - if (wordListWithLengthFilter.length > 0) { - return wordListWithLengthFilter; - } - - if (typeof length === 'number') { - return STRATEGIES[strategy](wordList, { min: length, max: length }); - } - - return STRATEGIES[strategy](wordList, length); - } else if (strategy === 'shortest' || strategy === 'longest') { - return STRATEGIES[strategy](wordList); - } - - return [...wordList]; -} diff --git a/src/modules/word/index.ts b/src/modules/word/index.ts index 7cc4729a..93c56829 100644 --- a/src/modules/word/index.ts +++ b/src/modules/word/index.ts @@ -1,7 +1,7 @@ import type { Faker } from '../..'; import { FakerError } from '../../errors/faker-error'; import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions'; -import { filterWordListByLength } from './filterWordListByLength'; +import { filterWordListByLength } from './filter-word-list-by-length'; /** * Module to return various types of words. -- cgit v1.2.3