Skip to content

Commit

Permalink
Merge pull request #19 from mishankov/trim
Browse files Browse the repository at this point in the history
Trim manipulation
  • Loading branch information
mishankov authored Mar 12, 2024
2 parents 2c3fb55 + 6ace3d9 commit a547faa
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Available manipulations:
- Compose
- Slice
- Split-compose
- Trim
- Split-join
6 changes: 6 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ it('Test replace manipulation', () => {
expect(doManipulation('\\3', { type: 'replace', from: '\\\\3', to: 'result' })).eq('result');
expect(doManipulation('\\', { type: 'replace', from: '\\\\', to: 'result' })).eq('result');
});

it('Test trim manipulation', () => {
expect(doManipulation(' input ', { type: 'trim', trimType: 'leading' })).eq('input ');
expect(doManipulation(' input ', { type: 'trim', trimType: 'trailing' })).eq(' input');
expect(doManipulation(' input ', { type: 'trim', trimType: 'all' })).eq('input');
});
36 changes: 36 additions & 0 deletions src/lib/components/RadioButton.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<script lang="ts">
interface Option {
value: string;
label: string;
}
export let options: Option[];
export let selected: string;
</script>

<div>
{#each options as option}
<label>
<input type="radio" bind:group={selected} value={option.value} />
{option.label}
</label>
{/each}
</div>

<style>
div {
display: flex;
flex-direction: column;
gap: 10px;
}
label {
display: flex;
gap: 5px;
align-items: center;
}
input {
margin: 0;
}
</style>
8 changes: 8 additions & 0 deletions src/lib/manipulations/components/AddManipulation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@
id: randomId()
});
break;
case 'trim':
manipulations.push({
type: 'trim',
trimType: 'all',
id: randomId()
});
break;
}
manipulations = manipulations;
Expand All @@ -90,5 +97,6 @@
<option value="compose">Compose</option>
<option value="slice">Slice</option>
<option value="splitCompose">Split-compose</option>
<option value="trim">Trim</option>
<option value="splitJoin">Split-join</option>
</select>
5 changes: 4 additions & 1 deletion src/lib/manipulations/components/Manipulation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Compose,
Slice,
SplitCompose,
Trim,
SplitJoin
} from '..';
Expand Down Expand Up @@ -46,6 +47,8 @@
bind:pattern={manipulation.pattern}
bind:placeholder={manipulation.placeholder}
/>
{:else if manipulation.type == 'trim'}
<Trim bind:trimType={manipulation.trimType} />
{:else if manipulation.type == 'splitJoin'}
<SplitJoin
bind:splitString={manipulation.splitString}
Expand Down Expand Up @@ -84,7 +87,7 @@
.delete:hover {
outline: none;
border: 2px solid var(--border-color-100);
background-color: var(--bg-color-150);
}
.delete:active {
Expand Down
41 changes: 41 additions & 0 deletions src/lib/manipulations/components/Trim.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<script lang="ts" context="module">
import type { ManipulationBase } from '..';
import RadioButton from '$lib/components/RadioButton.svelte';
export interface TrimManipulation extends ManipulationBase {
type: 'trim';
trimType: string;
}
const options = [
{
value: 'leading',
label: 'Leading'
},
{
value: 'trailing',
label: 'Trailing'
},
{
value: 'all',
label: 'All'
}
];
</script>

<script lang="ts">
export let { trimType }: TrimManipulation = { trimType: 'all', type: 'trim' };
</script>

<div>
<span>Trim </span>
<RadioButton bind:selected={trimType} {options} />
</div>

<style>
div {
display: flex;
flex-direction: column;
gap: 5px;
}
</style>
21 changes: 20 additions & 1 deletion src/lib/manipulations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SplitGetFromIndex, {
type SplitGetFromIndexManipulation
} from './components/SplitGetFromIndex.svelte';
import SplitJoin, { type SplitJoinManipulation } from './components/SplitJoin.svelte';
import Trim, { type TrimManipulation } from './components/Trim.svelte';

import AddManipulation from './components/AddManipulation.svelte';
import Manipulation from './components/Manipulation.svelte';
Expand All @@ -23,6 +24,7 @@ export {
SplitCompose,
SplitGetFromIndex,
SplitJoin,
Trim,
Manipulation,
AddManipulation,
ManipulationsList
Expand All @@ -41,7 +43,8 @@ export type TManipulation =
| SliceManipulation
| SplitComposeManipulation
| SplitGetFromIndexManipulation
| SplitJoinManipulation;
| SplitJoinManipulation
| TrimManipulation;

// Supported manupulations to use in splitJoin
export type InnerSplitJoinManipulation = TManipulation;
Expand Down Expand Up @@ -132,6 +135,22 @@ function doManipulationInner(input: string, manipulation: TManipulation): string
})
.join(prepareInput(manipulation.joinString));
}
case 'trim': {
switch (manipulation.trimType) {
case 'leading': {
return input.trimStart();
}
case 'trailing': {
return input.trimEnd();
}
case 'all': {
return input.trim();
}
default: {
return input;
}
}
}
}
} catch (exception) {
if (exception instanceof Error) {
Expand Down
13 changes: 10 additions & 3 deletions src/routes/docs/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,25 @@
pattern: 'Reordered {1}, {0}, {2}!'
}
},
{
name: 'Trim',
description: 'Trim all, leading or triling spaces from input',
source: ' Some good input ',
manipulation: { id: 'trim', type: 'trim', trimType: 'all' }
},
{
name: 'Split-join',
description: 'Split input by delimeter, apply manipulations and join',
source: 'Zero, one, two, three',
manipulation: {
id: 'splitJoin',
type: 'splitJoin',
splitString: ', ',
splitString: ',',
joinString: '\\n',
innerManipulations: [
{ id: 'splitJoinprepend', type: 'prepend', prefix: 'Number: ' },
{ id: 'splitJoinappend', type: 'append', suffix: '!' }
{ id: 'splitJoinTrim', type: 'trim', trimType: 'leading' },
{ id: 'splitJoinPrepend', type: 'prepend', prefix: 'Number: ' },
{ id: 'splitJoinAppend', type: 'append', suffix: '!' }
]
}
}
Expand Down
1 change: 1 addition & 0 deletions static/app.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
html {
--bg-color-100: hsl(157, 100%, 90%);
--bg-color-150: hsl(157, 100%, 95%);
--border-color-100: hsl(157, 100%, 10%);

--text-color: hsl(157, 100%, 10%);
Expand Down

0 comments on commit a547faa

Please sign in to comment.