Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve(#41): Added convertCase #59

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ containsAll('This is my name', ['my', 'name']);

// true
```

### convertCase

The `convertCase` function converts the given string to given mode:

```js
convertCase('HeLLo WoRLD', MB_CASE_LOWER);

// 'hello world'
```

### endsWith
The `endsWith` function determines if the given string ends with the given value:
```js
Expand Down
11 changes: 11 additions & 0 deletions docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ Stringable.of('This is my name').containsAll(['my', 'name']);

// true
```

### convertCase

The `convertCase` method converts the given string to given mode:

```js
Stringable.of('HeLLo WoRLD').convertCase(MB_CASE_LOWER);

// 'hello world'
```

### dirname
The `dirname` method returns the parent directory portion of the given string:
```js
Expand Down
11 changes: 11 additions & 0 deletions docs/statics.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ Str.containsAll('This is my name', ['my', 'name']);

// true
```

### convertCase

The `convertCase` function converts the given string to given mode:

```js
Str.convertCase('HeLLo WoRLD', MB_CASE_LOWER);

// 'hello world'
```

### endsWith
The `endsWith` function determines if the given string ends with the given value:
```js
Expand Down
2 changes: 2 additions & 0 deletions src/Str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
charAt,
contains,
containsAll,
convertCase,
endsWith,
excerpt,
explode,
Expand Down Expand Up @@ -107,6 +108,7 @@ export const Str = {
},
contains,
containsAll,
convertCase,
endsWith,
excerpt,
explode,
Expand Down
7 changes: 7 additions & 0 deletions src/Stringable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Str, {ExcerptOptions} from './Str';
import {MarkdownConfiguration, defaultConfiguration} from './types/markdown';
import {CASE_MODE, MB_CASE_FOLD} from './types/case';

type Closure = Function | null | string | { (callback: Stringable, value: string): Stringable };

Expand Down Expand Up @@ -117,6 +118,12 @@ export class Stringable {
return Str.containsAll(this._value, needles, ignoreCase);
}

public convertCase = (mode: CASE_MODE = MB_CASE_FOLD): this => {
this._value = Str.convertCase(this._value, mode);

return this;
}

public dirname = (levels: number = 1): this => {
const components = this.explode('/', levels * -1);

Expand Down
33 changes: 31 additions & 2 deletions src/methods.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import {
CASE_MODE,
MB_CASE_FOLD, MB_CASE_FOLD_SIMPLE,
MB_CASE_LOWER,
MB_CASE_LOWER_SIMPLE,
MB_CASE_TITLE, MB_CASE_TITLE_SIMPLE,
MB_CASE_UPPER,
MB_CASE_UPPER_SIMPLE
} from './types/case';

export type ExcerptOptions = {
radius?: number,
omission?: string,
Expand Down Expand Up @@ -89,6 +99,22 @@ export const containsAll = (haystack: string, needles: string[], ignoreCase: boo
return needles.every(needle => haystack.includes(needle));
}

export const convertCase = (string: string, mode: CASE_MODE = MB_CASE_FOLD): string => {
if ([MB_CASE_UPPER, MB_CASE_UPPER_SIMPLE].includes(mode)) {
return upper(string);
}

if ([MB_CASE_FOLD, MB_CASE_LOWER, MB_CASE_LOWER_SIMPLE, MB_CASE_FOLD_SIMPLE].includes(mode)) {
return lower(string);
}

if ([MB_CASE_TITLE, MB_CASE_TITLE_SIMPLE].includes(mode)) {
return title(string);
}

throw new Error(`Unsupported mode.`);
}

export const endsWith = (haystack: string, needles: null | string | number | string[]): boolean => {
if (needles === null || needles === '') {
return false;
Expand All @@ -99,7 +125,10 @@ export const endsWith = (haystack: string, needles: null | string | number | str
return values.some(needle => haystack.endsWith(String(needle)));
}

export const excerpt = (text: string, phrase: string = '', {radius = 100, omission = '...'}: ExcerptOptions = {radius: 100, omission: '...'}): string => {
export const excerpt = (text: string, phrase: string = '', {
radius = 100,
omission = '...'
}: ExcerptOptions = {radius: 100, omission: '...'}): string => {
if (text === phrase) {
return text;
}
Expand Down Expand Up @@ -394,7 +423,7 @@ export const parseCallback = (callback: string, method: string | null = null): A
: [callback, method];
}

export const position = (haystack: string, needle: string, offset: number = 0): number|boolean => {
export const position = (haystack: string, needle: string, offset: number = 0): number | boolean => {
const index = haystack.indexOf(needle, offset);

return index < 0 ? false : index;
Expand Down
10 changes: 10 additions & 0 deletions src/types/case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type CASE_MODE = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;

export const MB_CASE_UPPER = 0;
export const MB_CASE_LOWER = 1;
export const MB_CASE_TITLE = 2;
export const MB_CASE_FOLD = 3;
export const MB_CASE_UPPER_SIMPLE = 4;
export const MB_CASE_LOWER_SIMPLE = 5;
export const MB_CASE_TITLE_SIMPLE = 6;
export const MB_CASE_FOLD_SIMPLE = 7;
46 changes: 46 additions & 0 deletions tests/convertCase.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const {Stringable} = require('../src/Stringable');
const {Str} = require('../src/Str');
const {convertCase} = require('../src/methods');
const {MB_CASE_UPPER, MB_CASE_LOWER, MB_CASE_FOLD, MB_CASE_TITLE} = require('../src/types/case');

it('converts the case of a string', () => {
expect(Stringable.of('hello').convertCase(MB_CASE_UPPER).toString())
.toBe('HELLO');

expect(Stringable.of('WORLD').convertCase(MB_CASE_UPPER).toString())
.toBe('WORLD');

expect(Stringable.of('HeLLo').convertCase(MB_CASE_LOWER).toString())
.toBe('hello');

expect(Stringable.of('WoRLD').convertCase(MB_CASE_LOWER).toString())
.toBe('world');

expect(Stringable.of('HeLLo WoRLD').convertCase(MB_CASE_TITLE).toString())
.toBe('Hello World');

expect(Stringable.of('HeLLo').convertCase(MB_CASE_FOLD).toString())
.toBe('hello');

expect(Stringable.of('WoRLD').convertCase(MB_CASE_FOLD).toString())
.toBe('world');

expect(Stringable.of('üöä').convertCase(MB_CASE_UPPER).toString())
.toBe('ÜÖÄ');

expect(Stringable.of('ÜÖÄ').convertCase(MB_CASE_LOWER).toString())
.toBe('üöä');

expect(Str.convertCase('hello', MB_CASE_UPPER).toString())
.toBe('HELLO');

expect(convertCase('hello', MB_CASE_UPPER).toString())
.toBe('HELLO');
});

it('returns error on unsupported mode', () => {
expect(() => Str.convertCase('Hello', -1).toString())
.toThrowError('Unsupported mode.');
});
Loading