Skip to content

Commit

Permalink
Add case manipulation (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
mishankov authored Jun 9, 2024
1 parent 8f72443 commit ca1d564
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Available manipulations:
- Slice
- Split-compose
- Trim
- Case
- Split-join
5 changes: 5 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ it('Test trim manipulation', () => {
expect(doManipulation(' input ', { type: 'trim', trimType: 'trailing' })).eq(' input');
expect(doManipulation(' input ', { type: 'trim', trimType: 'all' })).eq('input');
});

it('Test case manipulation', () => {
expect(doManipulation('gOOd iNPuT', { type: 'case', caseType: 'lower' })).eq('good input');
expect(doManipulation('gOOd iNPuT', { type: 'case', caseType: 'upper' })).eq('GOOD INPUT');
});
8 changes: 8 additions & 0 deletions src/lib/manipulations/components/AddManipulation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@
id: randomId()
});
break;
case 'case':
manipulations.push({
type: 'case',
caseType: 'lower',
id: randomId()
});
break;
}
manipulations = manipulations;
Expand All @@ -98,5 +105,6 @@
<option value="slice">Slice</option>
<option value="splitCompose">Split-compose</option>
<option value="trim">Trim</option>
<option value="case">Case</option>
<option value="splitJoin">Split-join</option>
</select>
37 changes: 37 additions & 0 deletions src/lib/manipulations/components/Case.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script lang="ts" context="module">
import type { ManipulationBase } from '..';
import RadioButton from '$lib/components/RadioButton.svelte';
export interface CaseManipulation extends ManipulationBase {
type: 'case';
caseType: string;
}
const options = [
{
value: 'upper',
label: 'Upper'
},
{
value: 'lower',
label: 'Lower'
}
];
</script>

<script lang="ts">
export let { caseType }: CaseManipulation = { caseType: 'lower', type: 'case' };
</script>

<div>
<span>Case </span>
<RadioButton bind:selected={caseType} {options} />
</div>

<style>
div {
display: flex;
flex-direction: column;
gap: 5px;
}
</style>
3 changes: 3 additions & 0 deletions src/lib/manipulations/components/Manipulation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Slice,
SplitCompose,
Trim,
Case,
SplitJoin
} from '..';
Expand Down Expand Up @@ -49,6 +50,8 @@
/>
{:else if manipulation.type == 'trim'}
<Trim bind:trimType={manipulation.trimType} />
{:else if manipulation.type == 'case'}
<Case bind:caseType={manipulation.caseType} />
{:else if manipulation.type == 'splitJoin'}
<SplitJoin
bind:splitString={manipulation.splitString}
Expand Down
18 changes: 17 additions & 1 deletion src/lib/manipulations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SplitGetFromIndex, {
} from './components/SplitGetFromIndex.svelte';
import SplitJoin, { type SplitJoinManipulation } from './components/SplitJoin.svelte';
import Trim, { type TrimManipulation } from './components/Trim.svelte';
import Case, { type CaseManipulation } from './components/Case.svelte';

import AddManipulation from './components/AddManipulation.svelte';
import Manipulation from './components/Manipulation.svelte';
Expand All @@ -25,6 +26,7 @@ export {
SplitGetFromIndex,
SplitJoin,
Trim,
Case,
Manipulation,
AddManipulation,
ManipulationsList
Expand All @@ -44,7 +46,8 @@ export type TManipulation =
| SplitComposeManipulation
| SplitGetFromIndexManipulation
| SplitJoinManipulation
| TrimManipulation;
| TrimManipulation
| CaseManipulation;

// Supported manupulations to use in splitJoin
export type InnerSplitJoinManipulation = TManipulation;
Expand Down Expand Up @@ -151,6 +154,19 @@ function doManipulationInner(input: string, manipulation: TManipulation): string
}
}
}
case 'case': {
switch (manipulation.caseType) {
case 'lower': {
return input.toLowerCase();
}
case 'upper': {
return input.toUpperCase();
}
default: {
return input;
}
}
}
}
} catch (exception) {
if (exception instanceof Error) {
Expand Down
7 changes: 7 additions & 0 deletions src/routes/docs/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
source: ' Some good input ',
manipulation: { id: 'trim', type: 'trim', trimType: 'all' }
},
{
name: 'Case',
description: 'Transform input to lower or upper case',
source: 'Some gOOd iNPuT',
manipulation: { id: 'case', type: 'case', caseType: 'lower' }
},
{
name: 'Split-join',
description: 'Split input by delimeter, apply manipulations and join',
Expand All @@ -81,6 +87,7 @@
joinString: '\\n',
innerManipulations: [
{ id: 'splitJoinTrim', type: 'trim', trimType: 'leading' },
{ id: 'splitJoinCase', type: 'case', caseType: 'lower' },
{ id: 'splitJoinPrepend', type: 'prepend', prefix: 'Number: ' },
{ id: 'splitJoinAppend', type: 'append', suffix: '!' }
]
Expand Down

0 comments on commit ca1d564

Please sign in to comment.