Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "safunc", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Create runtime-validated functions with ease, supporting optional parameters and overloaded signatures with smart type inference in TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,5 +7,17 @@ <h1 align="center">Safunc</h1> | ||
<p align="center"> | ||
<a href="https://www.npmjs.com/package/safunc"> | ||
<img src="https://img.shields.io/npm/v/safunc.svg" alt="npm version" height="18"> | ||
</a> | ||
<a href="https://bundlephobia.com/package/safunc"> | ||
<img src="https://img.shields.io/bundlephobia/minzip/safunc.svg" alt="minzipped size" height="18"> | ||
</a> | ||
<a href="https://github.com/gvergnaud/safunc"> | ||
<img src="https://img.shields.io/npm/l/safunc.svg" alt="MIT license" height="18"> | ||
</a> | ||
</p> | ||
 | ||
Have a try on [TS Playground](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgEwKYDMA0cDOwDm2MAnmKnAMoFwC+c6UEIcARDgIboCuAdgMYsA3AChhfCDxzwo7HvnIBeFBgAUwuHAD0mjQD0A-OtwEVLYDxip5UFthYKAfLdbnL1gNoBdFgEo47HEoCAB4VKQgwAC44Hi4QACNUKD9HGLjEqC8HTCM8fFNXKyTnMwsimzt9QusHAAYSxxLqpK9ff0CqfGCjDTCYdlho2ISk7HCotJGoMcswfSH0pJSHSYyvI2yjbn4YYAk4GTlUPoGYAHkoChgImYj9GdQwOCUARj8EHq0dDT1DH7hxJJ4FJTs9cNcngooXBeGh0OZUMg4Po4LU4NEQbALlcIiJ-uNkfolJjzpcIXiflBUDAuFAeHAAIJQGTEAB0DCYKiQABtUHIYAALaIAWXYgtZfFQwG5Kj6ETgAFpwac-DopI8-DRsCoAPrYYDLZWwOAAajgwDgACpwRqKVrhD4RMJDvJTOgIBBfCIXccACwAJjs8QGXuEQA). | ||
Have a try on [StackBlitz](https://stackblitz.com/edit/safunc-minimal-example?file=main.ts&view=editor)! | ||
@@ -133,3 +145,3 @@ ## About | ||
### Overload Signatures | ||
### Overloaded Signatures | ||
@@ -244,3 +256,11 @@ Safunc supports defining functions with overloaded signatures, which is useful when you want to provide multiple ways to call a function with different sets of parameters. | ||
); | ||
const isoDateString = morph("Date", (x) => x.toISOString().slice(0, 10)); | ||
const isoDateString = morph( | ||
"Date", | ||
(x) => | ||
x.getFullYear() + | ||
"-" + | ||
String(x.getMonth() + 1).padStart(2, "0") + | ||
"-" + | ||
String(x.getDate()).padStart(2, "0"), | ||
); | ||
@@ -372,25 +392,25 @@ const addYears = def( | ||
```html | ||
<!DOCTYPE html> | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Safunc in Browser</title> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"safunc": "https://cdn.jsdelivr.net/npm/safunc/+esm" | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Safunc in Browser</title> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"safunc": "https://cdn.jsdelivr.net/npm/safunc@latest/+esm" | ||
} | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body> | ||
<script type="module"> | ||
import { def, sig } from "safunc"; | ||
</script> | ||
</head> | ||
<body> | ||
<script type="module"> | ||
import { def, sig } from "safunc"; | ||
const add = def(sig("number", "number", "=>", "number"), (n, m) => n + m); | ||
console.log(add(1, 2)); // 3 | ||
add(1, "2"); // !TypeError | ||
</script> | ||
</body> | ||
const add = def(sig("number", "number", "=>", "number"), (n, m) => n + m); | ||
console.log(add(1, 2)); // 3 | ||
add(1, "2"); // !TypeError | ||
</script> | ||
</body> | ||
</html> | ||
@@ -397,0 +417,0 @@ ``` |
@@ -77,5 +77,7 @@ import type { Eq, Fn } from "./common"; | ||
type Conditions = [...[Condition<any, any>, _[], _[]][], Otherwise<any>]; | ||
type CountEq<X, TS extends unknown[], Acc extends number = 0> = | ||
type CountEq<X, TS extends unknown[]> = | ||
_CountEq<X, TS> extends infer R extends number ? R : never; | ||
type _CountEq<X, TS extends unknown[], Acc extends number = 0> = | ||
TS extends [] ? Acc | ||
: CountEq< | ||
: _CountEq< | ||
X, | ||
@@ -85,3 +87,5 @@ TailPart<TS>, | ||
>; | ||
type CountEqOneOf< | ||
type CountEqOneOf<XS extends unknown[], TS extends unknown[]> = | ||
_CountEqOneOf<XS, TS> extends infer R extends number ? R : never; | ||
type _CountEqOneOf< | ||
XS extends unknown[], | ||
@@ -92,3 +96,3 @@ TS extends unknown[], | ||
TS extends [] ? Acc | ||
: CountEqOneOf< | ||
: _CountEqOneOf< | ||
XS, | ||
@@ -104,5 +108,7 @@ TailPart<TS>, | ||
: false; | ||
type CountExtends<X, TS extends unknown[], Acc extends number = 0> = | ||
type CountExtends<X, TS extends unknown[]> = | ||
_CountExtends<X, TS> extends infer R extends number ? R : never; | ||
type _CountExtends<X, TS extends unknown[], Acc extends number = 0> = | ||
TS extends [] ? Acc | ||
: CountExtends< | ||
: _CountExtends< | ||
X, | ||
@@ -109,0 +115,0 @@ TailPart<TS>, |
import type { CompareStrLength, Reverse, StrToNum } from "./string"; | ||
export type Ordering = LT | EQ | GT; | ||
export type EQ = 0; | ||
@@ -137,5 +138,9 @@ export type GT = 1; | ||
export type Compare<N extends number, M extends number> = | ||
_CompareListLength<_ToChars<N>, _ToChars<M>> extends EQ ? | ||
_CompareDigits<_ToChars<N>, _ToChars<M>> | ||
: _CompareListLength<_ToChars<N>, _ToChars<M>>; | ||
( | ||
_CompareListLength<_ToChars<N>, _ToChars<M>> extends EQ ? | ||
_CompareDigits<_ToChars<N>, _ToChars<M>> | ||
: _CompareListLength<_ToChars<N>, _ToChars<M>> | ||
) extends infer R extends Ordering ? | ||
R | ||
: never; | ||
type _CompareListLength<NS extends unknown[], MS extends unknown[]> = | ||
@@ -142,0 +147,0 @@ NS extends [unknown, ...infer ATail extends unknown[]] ? |
Sorry, the diff of this file is not supported yet
3183
416
2156301