-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add normalize action to normalize strings #691
- Loading branch information
1 parent
8dcf6c0
commit 555ae78
Showing
9 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './normalize.ts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { describe, expectTypeOf, test } from 'vitest'; | ||
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts'; | ||
import { normalize, type NormalizeAction } from './normalize.ts'; | ||
|
||
describe('normalize', () => { | ||
describe('should return action object', () => { | ||
test('with undefined form', () => { | ||
expectTypeOf(normalize()).toEqualTypeOf<NormalizeAction<undefined>>(); | ||
}); | ||
|
||
test('with defined form', () => { | ||
expectTypeOf(normalize('NFKC')).toEqualTypeOf<NormalizeAction<'NFKC'>>(); | ||
}); | ||
}); | ||
|
||
describe('should infer correct types', () => { | ||
type Action = NormalizeAction<undefined>; | ||
|
||
test('of input', () => { | ||
expectTypeOf<InferInput<Action>>().toEqualTypeOf<string>(); | ||
}); | ||
|
||
test('of output', () => { | ||
expectTypeOf<InferOutput<Action>>().toEqualTypeOf<string>(); | ||
}); | ||
|
||
test('of issue', () => { | ||
expectTypeOf<InferIssue<Action>>().toEqualTypeOf<never>(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { normalize, type NormalizeAction } from './normalize.ts'; | ||
|
||
describe('normalize', () => { | ||
describe('should return action object', () => { | ||
const baseAction: Omit<NormalizeAction<never>, 'form'> = { | ||
kind: 'transformation', | ||
type: 'normalize', | ||
reference: normalize, | ||
async: false, | ||
_run: expect.any(Function), | ||
}; | ||
|
||
test('with undefined form', () => { | ||
expect(normalize()).toStrictEqual({ | ||
...baseAction, | ||
form: undefined, | ||
} satisfies NormalizeAction<undefined>); | ||
}); | ||
|
||
test('with defined form', () => { | ||
expect(normalize('NFKD')).toStrictEqual({ | ||
...baseAction, | ||
form: 'NFKD', | ||
} satisfies NormalizeAction<'NFKD'>); | ||
}); | ||
}); | ||
|
||
describe('should normalize string', () => { | ||
test('with undefined form', () => { | ||
expect( | ||
normalize()._run({ typed: true, value: '\u00F1' }, {}) | ||
).toStrictEqual({ | ||
typed: true, | ||
value: 'ñ', | ||
}); | ||
expect( | ||
normalize()._run({ typed: true, value: '\u006E\u0303' }, {}) | ||
).toStrictEqual({ | ||
typed: true, | ||
value: 'ñ', | ||
}); | ||
}); | ||
|
||
test('with defined form', () => { | ||
expect( | ||
normalize('NFKD')._run({ typed: true, value: '\uFB00' }, {}) | ||
).toStrictEqual({ | ||
typed: true, | ||
value: 'ff', | ||
}); | ||
expect( | ||
normalize('NFKD')._run({ typed: true, value: '\u0066\u0066' }, {}) | ||
).toStrictEqual({ | ||
typed: true, | ||
value: 'ff', | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { BaseTransformation } from '../../types/index.ts'; | ||
|
||
/** | ||
* Normalize form type. | ||
*/ | ||
export type NormalizeForm = 'NFC' | 'NFD' | 'NFKC' | 'NFKD'; | ||
|
||
/** | ||
* Normalize action type. | ||
*/ | ||
export interface NormalizeAction<TForm extends NormalizeForm | undefined> | ||
extends BaseTransformation<string, string, never> { | ||
/** | ||
* The action type. | ||
*/ | ||
readonly type: 'normalize'; | ||
/** | ||
* The action reference. | ||
*/ | ||
readonly reference: typeof normalize; | ||
/** | ||
* The normalization form. | ||
*/ | ||
readonly form: TForm; | ||
} | ||
|
||
/** | ||
* Creates a normalize transformation action. | ||
* | ||
* @returns A normalize action. | ||
*/ | ||
export function normalize(): NormalizeAction<undefined>; | ||
|
||
/** | ||
* Creates a normalize transformation action. | ||
* | ||
* @param form The normalization form. | ||
* | ||
* @returns A normalize action. | ||
*/ | ||
export function normalize<TForm extends NormalizeForm | undefined>( | ||
form: TForm | ||
): NormalizeAction<TForm>; | ||
|
||
export function normalize( | ||
form?: NormalizeForm | ||
): NormalizeAction<NormalizeForm | undefined> { | ||
return { | ||
kind: 'transformation', | ||
type: 'normalize', | ||
reference: normalize, | ||
async: false, | ||
form, | ||
_run(dataset) { | ||
dataset.value = dataset.value.normalize(this.form); | ||
return dataset; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
title: normalize | ||
description: Creates a normalize transformation action. | ||
source: /actions/normalize/normalize.ts | ||
contributors: | ||
- fabian-hiller | ||
--- | ||
|
||
import { ApiList, Property } from '~/components'; | ||
import { properties } from './properties'; | ||
|
||
# normalize | ||
|
||
Creates a normalize transformation action. | ||
|
||
```ts | ||
const Action = v.normalize<TForm>(form); | ||
``` | ||
|
||
## Generics | ||
|
||
- `TForm` <Property {...properties.TForm} /> | ||
|
||
## Parameters | ||
|
||
- `form` <Property {...properties.form} /> | ||
|
||
## Returns | ||
|
||
- `Action` <Property {...properties.Action} /> | ||
|
||
## Examples | ||
|
||
The following examples show how `normalize` can be used. | ||
|
||
### Normalized string | ||
|
||
Schema to normalize a string. | ||
|
||
```ts | ||
const StringSchema = v.pipe(v.string(), v.normalize()); | ||
``` | ||
|
||
## Related | ||
|
||
The following APIs can be combined with `normalize`. | ||
|
||
### Schemas | ||
|
||
<ApiList items={['any', 'special', 'string', 'unknown']} /> | ||
|
||
### Methods | ||
|
||
<ApiList items={['pipe']} /> | ||
|
||
### Utils | ||
|
||
<ApiList items={['isOfKind', 'isOfType']} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { PropertyProps } from '~/components'; | ||
|
||
export const properties: Record<string, PropertyProps> = { | ||
TForm: { | ||
modifier: 'extends', | ||
type: { | ||
type: 'union', | ||
options: [ | ||
{ | ||
type: 'custom', | ||
name: 'NormalizeForm', | ||
href: '../NormalizeForm/', | ||
}, | ||
'undefined', | ||
], | ||
}, | ||
}, | ||
form: { | ||
type: { | ||
type: 'custom', | ||
name: 'TForm', | ||
}, | ||
}, | ||
Action: { | ||
type: { | ||
type: 'custom', | ||
name: 'NormalizeAction', | ||
href: '../NormalizeAction/', | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters