From 117e752ddb85671327038ff4641daa4c293581c7 Mon Sep 17 00:00:00 2001 From: RobsonTrasel Date: Sat, 1 Mar 2025 22:00:07 -0300 Subject: [PATCH 1/3] feat(String): add includesSome function - Creates the static includesSome method in String - Checks if at least one of the given terms is included in the base string --- src/helpers/String.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/helpers/String.ts b/src/helpers/String.ts index c02f276..99b556b 100644 --- a/src/helpers/String.ts +++ b/src/helpers/String.ts @@ -230,4 +230,24 @@ export class String extends Macroable { return `${value}th` } } + + /** + * Check if at least one of the provided search strings + * is included in the given value. + * + * @example + * ```ts + * String.includesSome('Hello model.id', 'models.id', 'models.provider') // false + * String.includesSome('Hello models.id', ['models.id', 'provider']) // true (models.id is found) + * ``` + */ + public static includesSome( + value: string, + ...searches: (string | string[])[] + ): boolean { + const terms = Array.isArray(searches[0]) ? (searches[0] as string[]) : (searches as string[]) + return terms.some(term => value.includes(term)) + } + + } From c47ed9e23d0065db91f88b7f08c7c5aba569b14f Mon Sep 17 00:00:00 2001 From: RobsonTrasel Date: Sat, 1 Mar 2025 22:03:34 -0300 Subject: [PATCH 2/3] feat(String): add includesEvery function - Creates the static includesEvery method in String - Checks if all of the given terms are included in the base string --- src/helpers/String.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/helpers/String.ts b/src/helpers/String.ts index 99b556b..d265da0 100644 --- a/src/helpers/String.ts +++ b/src/helpers/String.ts @@ -249,5 +249,22 @@ export class String extends Macroable { return terms.some(term => value.includes(term)) } + /** + * Check if every provided search string is included + * in the given value. + * + * @example + * ```ts + * String.includesEvery('Hello model.id', 'models.id', 'models.provider') // false + * String.includesEvery('Hello model.id', ['model.id', 'Hello']) // true (both are found) + * ``` + */ + public static includesEvery( + value: string, + ...searches: (string | string[])[] + ): boolean { + const terms = Array.isArray(searches[0]) ? (searches[0] as string[]) : (searches as string[]) + return terms.every(term => value.includes(term)) + } } From 4e2c96e4efa2fd21e37541c1138551af997e6834 Mon Sep 17 00:00:00 2001 From: RobsonTrasel Date: Sat, 1 Mar 2025 22:18:18 -0300 Subject: [PATCH 3/3] test(String): add test coverage for includesSome and includesEvery - Adds tests for includesSome with multiple params and with an array: * shouldReturnTrueIfAtLeastOneTermMatchesUsingMultipleParams * shouldReturnTrueIfAtLeastOneTermMatchesUsingAnArray * shouldReturnFalseForIncludesSomeWithEmptyTerms - Adds tests for includesEvery with multiple params and with an array: * shouldReturnTrueOnlyIfAllTermsMatchUsingMultipleParams * shouldReturnTrueOnlyIfAllTermsMatchUsingAnArray * shouldReturnTrueForIncludesEveryWithEmptyTerms Ensures maximum coverage and verifies behavior under various conditions (e.g., no matches, partial matches, empty arrays). --- tests/unit/helpers/StringTest.ts | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/unit/helpers/StringTest.ts b/tests/unit/helpers/StringTest.ts index 954375b..9f50da7 100644 --- a/tests/unit/helpers/StringTest.ts +++ b/tests/unit/helpers/StringTest.ts @@ -122,4 +122,50 @@ export default class StringTest { assert.throws(useCase, OrdinalNanException) } + + @Test() + public async shouldReturnTrueIfAtLeastOneTermMatchesUsingMultipleParams({ assert }: Context) { + const base = 'Hello model.id and some other text' + + assert.isTrue(String.includesSome(base, 'model.id', 'nope')) + assert.isTrue(String.includesSome(base, 'some', 'anything')) + assert.isFalse(String.includesSome(base, 'not-found', 'nope')) + } + + @Test() + public async shouldReturnTrueIfAtLeastOneTermMatchesUsingAnArray({ assert }: Context) { + const base = 'Hello model.id and some other text' + + assert.isTrue(String.includesSome(base, ['model.id', 'random'])) + assert.isFalse(String.includesSome(base, ['aaa', 'bbb'])) + } + + @Test() + public async shouldReturnFalseForIncludesSomeWithEmptyTerms({ assert }: Context) { + assert.isFalse(String.includesSome('anything')) + assert.isFalse(String.includesSome('anything', [])) + } + + @Test() + public async shouldReturnTrueOnlyIfAllTermsMatchUsingMultipleParams({ assert }: Context) { + const base = 'Hello model.id and some text' + + assert.isFalse(String.includesEvery(base, 'Hello', 'somethingElse')) + assert.isTrue(String.includesEvery(base, 'Hello', 'model.id')) + assert.isFalse(String.includesEvery(base, 'model.id', 'not-found')) + } + + @Test() + public async shouldReturnTrueOnlyIfAllTermsMatchUsingAnArray({ assert }: Context) { + const base = 'Hello model.id and some text' + + assert.isTrue(String.includesEvery(base, ['Hello', 'model.id'])) + assert.isFalse(String.includesEvery(base, ['Hello', 'random'])) + } + + @Test() + public async shouldReturnTrueForIncludesEveryWithEmptyTerms({ assert }: Context) { + assert.isTrue(String.includesEvery('anything')) + assert.isTrue(String.includesEvery('anything', [])) + } }