Skip to content

Commit 0fb33af

Browse files
committed
feat(globals): add String.athenna for includesSome and includesEvery
1 parent ca8b83d commit 0fb33af

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

src/globals/String.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @athenna/common
3+
*
4+
* (c) Robson Trasel <robson@athenna.io>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { String } from '#src/helpers/String'
11+
12+
export class AthennaString {
13+
public constructor(private value: string) {}
14+
15+
/**
16+
* Check if at least one of the provided search strings
17+
* is included in the given string value.
18+
*
19+
* @example
20+
* ```ts
21+
* 'Hello model.id'.athenna.includesSome('models.id', 'models.provider') // false
22+
* 'Hello models.id'.athenna.includesSome(['models.id', 'provider']) // true
23+
* ```
24+
*/
25+
public includesSome(...searches: (string | string[])[]): boolean {
26+
return String.includesSome(this.value, ...searches)
27+
}
28+
29+
/**
30+
* Check if every provided search string is included
31+
* in the given string value.
32+
*
33+
* @example
34+
* ```ts
35+
* 'Hello model.id'.athenna.includesEvery('models.id', 'models.provider') // false
36+
* 'Hello model.id'.athenna.includesEvery(['model.id', 'Hello']) // true
37+
* ```
38+
*/
39+
public includesEvery(...searches: (string | string[])[]): boolean {
40+
return String.includesEvery(this.value, ...searches)
41+
}
42+
}
43+
44+
declare global {
45+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
46+
interface String {
47+
athenna: AthennaString
48+
}
49+
}
50+
51+
if (!String.prototype.athenna) {
52+
// eslint-disable-next-line no-extend-native
53+
Object.defineProperty(String.prototype, 'athenna', {
54+
get: function () {
55+
return new AthennaString(this)
56+
}
57+
})
58+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export * from '#src/constants/alphabet'
1414
export * from '#src/globals/Enum'
1515
export * from '#src/globals/Error'
1616
export * from '#src/globals/Array'
17+
export * from '#src/globals/String'
1718

1819
export * from '#src/helpers/Exception'
1920
export * from '#src/helpers/Clean'

tests/unit/globals/StringTest.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @athenna/common
3+
*
4+
* (c) Robson Trasel <robson@athenna.io>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { Test, type Context } from '@athenna/test'
11+
12+
export default class GlobalStringTest {
13+
@Test()
14+
public async shouldReturnTrueIfAtLeastOneTermMatchesUsingMultipleParams({ assert }: Context) {
15+
const value = 'Hello model.id and some other text'
16+
17+
assert.isTrue(value.athenna.includesSome('model.id', 'nope'))
18+
assert.isTrue(value.athenna.includesSome('some', 'anything'))
19+
assert.isFalse(value.athenna.includesSome('not-found', 'nope'))
20+
}
21+
22+
@Test()
23+
public async shouldReturnTrueIfAtLeastOneTermMatchesUsingAnArray({ assert }: Context) {
24+
const value = 'Hello model.id and some other text'
25+
26+
assert.isTrue(value.athenna.includesSome(['model.id', 'random']))
27+
assert.isFalse(value.athenna.includesSome(['aaa', 'bbb']))
28+
}
29+
30+
@Test()
31+
public async shouldReturnFalseForIncludesSomeWithEmptyTerms({ assert }: Context) {
32+
const value = 'anything'
33+
34+
assert.isFalse(value.athenna.includesSome())
35+
assert.isFalse(value.athenna.includesSome([]))
36+
}
37+
38+
@Test()
39+
public async shouldReturnTrueOnlyIfAllTermsMatchUsingMultipleParams({ assert }: Context) {
40+
const value = 'Hello model.id and some text'
41+
42+
assert.isFalse(value.athenna.includesEvery('Hello', 'somethingElse'))
43+
assert.isTrue(value.athenna.includesEvery('Hello', 'model.id'))
44+
assert.isFalse(value.athenna.includesEvery('model.id', 'not-found'))
45+
}
46+
47+
@Test()
48+
public async shouldReturnTrueOnlyIfAllTermsMatchUsingAnArray({ assert }: Context) {
49+
const value = 'Hello model.id and some text'
50+
51+
assert.isTrue(value.athenna.includesEvery(['Hello', 'model.id']))
52+
assert.isFalse(value.athenna.includesEvery(['Hello', 'random']))
53+
}
54+
55+
@Test()
56+
public async shouldReturnTrueForIncludesEveryWithEmptyTerms({ assert }: Context) {
57+
const value = 'anything'
58+
59+
assert.isTrue(value.athenna.includesEvery())
60+
assert.isTrue(value.athenna.includesEvery([]))
61+
}
62+
}

0 commit comments

Comments
 (0)