@sapphire/shapeshift
Advanced tools
Comparing version 1.1.0-next.8727427.0 to 1.1.0-next.89a8ddd.0
@@ -59,2 +59,4 @@ declare class ConstraintError<T = unknown> extends Error { | ||
or<O>(...predicates: readonly BaseValidator<O>[]): UnionValidator<T | O>; | ||
transform(cb: (value: T) => T): this; | ||
transform<O>(cb: (value: T) => O): BaseValidator<O>; | ||
run(value: unknown): Result<T, Error>; | ||
@@ -156,2 +158,10 @@ parse(value: unknown): T; | ||
get negative(): this; | ||
divisibleBy(divider: number): this; | ||
get abs(): this; | ||
get sign(): this; | ||
get trunc(): this; | ||
get floor(): this; | ||
get fround(): this; | ||
get round(): this; | ||
get ceil(): this; | ||
protected handle(value: unknown): Result<T, ValidationError>; | ||
@@ -158,0 +168,0 @@ } |
@@ -39,2 +39,32 @@ "use strict"; | ||
// src/lib/Result.ts | ||
var Result = class { | ||
constructor(success, value, error) { | ||
this.success = success; | ||
if (success) { | ||
this.value = value; | ||
} else { | ||
this.error = error; | ||
} | ||
} | ||
isOk() { | ||
return this.success; | ||
} | ||
isErr() { | ||
return !this.success; | ||
} | ||
unwrap() { | ||
if (this.isOk()) | ||
return this.value; | ||
throw this.error; | ||
} | ||
static ok(value) { | ||
return new Result(true, value); | ||
} | ||
static err(error) { | ||
return new Result(false, void 0, error); | ||
} | ||
}; | ||
__name(Result, "Result"); | ||
// src/validators/BaseValidator.ts | ||
@@ -64,2 +94,5 @@ var BaseValidator = class { | ||
} | ||
transform(cb) { | ||
return this.addConstraint({ run: (input) => Result.ok(cb(input)) }); | ||
} | ||
run(value) { | ||
@@ -107,32 +140,2 @@ let result = this.handle(value); | ||
// src/lib/Result.ts | ||
var Result = class { | ||
constructor(success, value, error) { | ||
this.success = success; | ||
if (success) { | ||
this.value = value; | ||
} else { | ||
this.error = error; | ||
} | ||
} | ||
isOk() { | ||
return this.success; | ||
} | ||
isErr() { | ||
return !this.success; | ||
} | ||
unwrap() { | ||
if (this.isOk()) | ||
return this.value; | ||
throw this.error; | ||
} | ||
static ok(value) { | ||
return new Result(true, value); | ||
} | ||
static err(error) { | ||
return new Result(false, void 0, error); | ||
} | ||
}; | ||
__name(Result, "Result"); | ||
// src/validators/ArrayValidator.ts | ||
@@ -198,3 +201,3 @@ var ArrayValidator = class extends BaseValidator { | ||
function ge(a, b) { | ||
return a > b; | ||
return a >= b; | ||
} | ||
@@ -448,2 +451,11 @@ __name(ge, "ge"); | ||
}; | ||
function numberDivisibleBy(divider) { | ||
const expected = `% ${divider}`; | ||
return { | ||
run(input) { | ||
return input % divider === 0 ? Result.ok(input) : Result.err(new ConstraintError("numberDivisibleBy", `Expected number to be divisible by ${divider}, but received ${input}`, input, expected)); | ||
} | ||
}; | ||
} | ||
__name(numberDivisibleBy, "numberDivisibleBy"); | ||
@@ -485,2 +497,26 @@ // src/validators/NumberValidator.ts | ||
} | ||
divisibleBy(divider) { | ||
return this.addConstraint(numberDivisibleBy(divider)); | ||
} | ||
get abs() { | ||
return this.transform(Math.abs); | ||
} | ||
get sign() { | ||
return this.transform(Math.sign); | ||
} | ||
get trunc() { | ||
return this.transform(Math.trunc); | ||
} | ||
get floor() { | ||
return this.transform(Math.floor); | ||
} | ||
get fround() { | ||
return this.transform(Math.fround); | ||
} | ||
get round() { | ||
return this.transform(Math.round); | ||
} | ||
get ceil() { | ||
return this.transform(Math.ceil); | ||
} | ||
handle(value) { | ||
@@ -677,3 +713,3 @@ return typeof value === "number" ? Result.ok(value) : Result.err(new ValidationError("NumberValidator", "Expected a number primitive", value)); | ||
if (!(values instanceof Set)) { | ||
return Result.err(new ValidationError("ArrayValidator", "Expected an array", values)); | ||
return Result.err(new ValidationError("SetValidator", "Expected a set", values)); | ||
} | ||
@@ -680,0 +716,0 @@ const errors = []; |
@@ -38,2 +38,32 @@ "use strict"; | ||
// src/lib/Result.ts | ||
var Result = class { | ||
constructor(success, value, error) { | ||
this.success = success; | ||
if (success) { | ||
this.value = value; | ||
} else { | ||
this.error = error; | ||
} | ||
} | ||
isOk() { | ||
return this.success; | ||
} | ||
isErr() { | ||
return !this.success; | ||
} | ||
unwrap() { | ||
if (this.isOk()) | ||
return this.value; | ||
throw this.error; | ||
} | ||
static ok(value) { | ||
return new Result(true, value); | ||
} | ||
static err(error) { | ||
return new Result(false, void 0, error); | ||
} | ||
}; | ||
__name(Result, "Result"); | ||
// src/validators/BaseValidator.ts | ||
@@ -63,2 +93,5 @@ var BaseValidator = class { | ||
} | ||
transform(cb) { | ||
return this.addConstraint({ run: (input) => Result.ok(cb(input)) }); | ||
} | ||
run(value) { | ||
@@ -106,32 +139,2 @@ let result = this.handle(value); | ||
// src/lib/Result.ts | ||
var Result = class { | ||
constructor(success, value, error) { | ||
this.success = success; | ||
if (success) { | ||
this.value = value; | ||
} else { | ||
this.error = error; | ||
} | ||
} | ||
isOk() { | ||
return this.success; | ||
} | ||
isErr() { | ||
return !this.success; | ||
} | ||
unwrap() { | ||
if (this.isOk()) | ||
return this.value; | ||
throw this.error; | ||
} | ||
static ok(value) { | ||
return new Result(true, value); | ||
} | ||
static err(error) { | ||
return new Result(false, void 0, error); | ||
} | ||
}; | ||
__name(Result, "Result"); | ||
// src/validators/ArrayValidator.ts | ||
@@ -197,3 +200,3 @@ var ArrayValidator = class extends BaseValidator { | ||
function ge(a, b) { | ||
return a > b; | ||
return a >= b; | ||
} | ||
@@ -447,2 +450,11 @@ __name(ge, "ge"); | ||
}; | ||
function numberDivisibleBy(divider) { | ||
const expected = `% ${divider}`; | ||
return { | ||
run(input) { | ||
return input % divider === 0 ? Result.ok(input) : Result.err(new ConstraintError("numberDivisibleBy", `Expected number to be divisible by ${divider}, but received ${input}`, input, expected)); | ||
} | ||
}; | ||
} | ||
__name(numberDivisibleBy, "numberDivisibleBy"); | ||
@@ -484,2 +496,26 @@ // src/validators/NumberValidator.ts | ||
} | ||
divisibleBy(divider) { | ||
return this.addConstraint(numberDivisibleBy(divider)); | ||
} | ||
get abs() { | ||
return this.transform(Math.abs); | ||
} | ||
get sign() { | ||
return this.transform(Math.sign); | ||
} | ||
get trunc() { | ||
return this.transform(Math.trunc); | ||
} | ||
get floor() { | ||
return this.transform(Math.floor); | ||
} | ||
get fround() { | ||
return this.transform(Math.fround); | ||
} | ||
get round() { | ||
return this.transform(Math.round); | ||
} | ||
get ceil() { | ||
return this.transform(Math.ceil); | ||
} | ||
handle(value) { | ||
@@ -676,3 +712,3 @@ return typeof value === "number" ? Result.ok(value) : Result.err(new ValidationError("NumberValidator", "Expected a number primitive", value)); | ||
if (!(values instanceof Set)) { | ||
return Result.err(new ValidationError("ArrayValidator", "Expected an array", values)); | ||
return Result.err(new ValidationError("SetValidator", "Expected a set", values)); | ||
} | ||
@@ -679,0 +715,0 @@ const errors = []; |
{ | ||
"name": "@sapphire/shapeshift", | ||
"version": "1.1.0-next.8727427.0", | ||
"version": "1.1.0-next.89a8ddd.0", | ||
"description": "Blazing fast input validation and transformation ⚡", | ||
@@ -5,0 +5,0 @@ "author": "@sapphire", |
@@ -135,3 +135,3 @@ <div align="center"> | ||
s.number.divisibleBy(5); // TODO | Divisible by 5 | ||
s.number.divisibleBy(5); // Divisible by 5 | ||
``` | ||
@@ -142,10 +142,10 @@ | ||
```typescript | ||
s.number.abs; // TODO | Transforms the number to an absolute number | ||
s.number.sign; // TODO | Gets the number's sign | ||
s.number.abs; // Transforms the number to an absolute number | ||
s.number.sign; // Gets the number's sign | ||
s.number.trunc; // TODO | Transforms the number to the result of Math.trunc` | ||
s.number.floor; // TODO | Transforms the number to the result of Math.floor` | ||
s.number.fround; // TODO | Transforms the number to the result of Math.fround` | ||
s.number.round; // TODO | Transforms the number to the result of Math.round` | ||
s.number.ceil; // TODO | Transforms the number to the result of Math.ceil` | ||
s.number.trunc; // Transforms the number to the result of `Math.trunc` | ||
s.number.floor; // Transforms the number to the result of `Math.floor` | ||
s.number.fround; // Transforms the number to the result of `Math.fround` | ||
s.number.round; // Transforms the number to the result of `Math.round` | ||
s.number.ceil; // Transforms the number to the result of `Math.ceil` | ||
``` | ||
@@ -424,3 +424,3 @@ | ||
const getLength = s.string.transform((value) => value.length); // TODO | ||
const getLength = s.string.transform((value) => value.length); | ||
getLength.parse('Hello There'); // => 11 | ||
@@ -520,2 +520,3 @@ ``` | ||
<td align="center"><a href="https://renovate.whitesourcesoftware.com/"><img src="https://avatars.githubusercontent.com/u/25180681?v=4?s=100" width="100px;" alt=""/><br /><sub><b>WhiteSource Renovate</b></sub></a><br /><a href="#maintenance-renovate-bot" title="Maintenance">🚧</a></td> | ||
<td align="center"><a href="https://github.com/Khasms"><img src="https://avatars.githubusercontent.com/u/36800359?v=4?s=100" width="100px;" alt=""/><br /><sub><b>John</b></sub></a><br /><a href="https://github.com/sapphiredev/shapeshift/commits?author=Khasms" title="Code">💻</a></td> | ||
</tr> | ||
@@ -522,0 +523,0 @@ </table> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
318614
2874
530