-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rename Either to Result * Update the readme to fully reflect the changes * Add cast * Fix linter
- Loading branch information
Showing
30 changed files
with
213 additions
and
166 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@restless/sanitizers", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Piotr Szlachciak <[email protected]>", | ||
"description": "Data sanitization in a functional way", | ||
"license": "Unlicense", | ||
|
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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import { Either, Sanitizer, SanitizerFailure } from './model' | ||
import { Result, Sanitizer, SanitizerFailure } from './model' | ||
|
||
export const asArray = <T> (sanitizer: Sanitizer<T>): Sanitizer<T[]> => | ||
(value, path) => { | ||
if (!Array.isArray(value)) { | ||
return Either.left([{ path, expected: 'array' }]) | ||
return Result.error([{ path, expected: 'array' }]) | ||
} | ||
const results: T[] = [] | ||
const errors: SanitizerFailure[] = [] | ||
for (let i = 0; i < value.length; i++) { | ||
const result = sanitizer(value[i], `${path}[${i}]`) | ||
if (Either.isRight(result)) { | ||
results.push(result.right) | ||
if (Result.isOk(result)) { | ||
results.push(result.ok) | ||
} else { | ||
errors.push(...result.left) | ||
errors.push(...result.error) | ||
} | ||
} | ||
return errors.length > 0 | ||
? Either.left(errors) | ||
: Either.right(results) | ||
? Result.error(errors) | ||
: Result.ok(results) | ||
} |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { Either, Sanitizer } from './model' | ||
import { Result, Sanitizer } from './model' | ||
|
||
export const asChecked = <T> ( | ||
sanitizer: Sanitizer<T>, | ||
predicate: (value: T) => boolean, | ||
expected: string = 'custom logic' | ||
): Sanitizer<T> => (value, path) => { | ||
const result = sanitizer(value, path) | ||
if (Either.isRight(result) && !predicate(result.right)) { | ||
return Either.left([{ path, expected }]) | ||
if (Result.isOk(result) && !predicate(result.ok)) { | ||
return Result.error([{ path, expected }]) | ||
} | ||
return result | ||
} |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { Either, Sanitizer, SanitizerFailure } from './model' | ||
import { Result, Sanitizer, SanitizerFailure } from './model' | ||
|
||
export const asFlatMapped = <T, U> ( | ||
sanitizer: Sanitizer<T>, | ||
flatMapFn: (value: T, path: string) => Either<SanitizerFailure[], U> | ||
flatMapFn: (value: T, path: string) => Result<SanitizerFailure[], U> | ||
): Sanitizer<U> => (value, path) => { | ||
const result = sanitizer(value, path) | ||
if (Either.isRight(result)) { | ||
return flatMapFn(result.right, path) | ||
if (Result.isOk(result)) { | ||
return flatMapFn(result.ok, path) | ||
} | ||
return result | ||
} |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { Either, Sanitizer } from './model' | ||
import { Result, Sanitizer } from './model' | ||
|
||
export const asMapped = <T, U> ( | ||
sanitizer: Sanitizer<T>, | ||
mapFn: (value: T) => U | ||
): Sanitizer<U> => (value, path) => { | ||
const result = sanitizer(value, path) | ||
if (Either.isRight(result)) { | ||
return Either.right(mapFn(result.right)) | ||
if (Result.isOk(result)) { | ||
return Result.ok(mapFn(result.ok)) | ||
} | ||
return result | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Either, Sanitizer } from './model' | ||
import { Result, Sanitizer } from './model' | ||
|
||
export const asMatching = (re: RegExp, message?: string): Sanitizer<string> => | ||
(value, path) => typeof value === 'string' && re.test(value) | ||
? Either.right(value) | ||
: Either.left([{ path, expected: message || `string matching ${re}` }]) | ||
? Result.ok(value) | ||
: Result.error([{ path, expected: message || `string matching ${re}` }]) |
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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import { Either, Sanitizer, SanitizerFailure, Schema } from './model' | ||
import { Result, Sanitizer, SanitizerFailure, Schema } from './model' | ||
|
||
export const asObject = <T extends object> (schema: Schema<T>): Sanitizer<T> => | ||
(value, path) => { | ||
if (typeof value !== 'object' || value === null) { | ||
return Either.left([{ path, expected: 'object' }]) | ||
return Result.error([{ path, expected: 'object' }]) | ||
} | ||
const results: T = {} as any | ||
const errors: SanitizerFailure[] = [] | ||
for (const key in schema) { | ||
if (Object.hasOwnProperty.call(schema, key)) { | ||
const result = schema[key]((value as T)[key], `${path}.${key}`) | ||
if (Either.isRight(result)) { | ||
results[key] = result.right | ||
if (Result.isOk(result)) { | ||
results[key] = result.ok | ||
} else { | ||
errors.push(...result.left) | ||
errors.push(...result.error) | ||
} | ||
} | ||
} | ||
return errors.length > 0 | ||
? Either.left(errors) | ||
: Either.right(results) | ||
? Result.error(errors) | ||
: Result.ok(results) | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { Either, Sanitizer } from './model' | ||
import { Result, Sanitizer } from './model' | ||
|
||
export const asOptional = <T> (sanitizer: Sanitizer<T>): Sanitizer<T | undefined> => | ||
(value, path) => value == null | ||
? Either.right(undefined) | ||
? Result.ok(undefined) | ||
: sanitizer(value, path) |
Oops, something went wrong.