Skip to content

Commit

Permalink
Update docs - misc, encode and random
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Oct 21, 2023
1 parent 4545c08 commit 15a836f
Show file tree
Hide file tree
Showing 6 changed files with 704 additions and 52 deletions.
8 changes: 4 additions & 4 deletions website/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"prettier": "^2.8.8",
"rimraf": "^5.0.0",
"ts-node": "^10.9.1",
"typia": "^5.2.2"
"typia": "^5.2.3"
}
}
190 changes: 179 additions & 11 deletions website/pages/docs/misc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import AlertTitle from '@mui/material/AlertTitle';
<Tabs items={[
<code>typia</code>,
<code>TypeGuardError.ts</code>,
<code>IValidation.ts</code>
<code>IValidation.ts</code>,
<code>Resolved.ts</code>
]}>
<Tab>
```typescript copy
export namespace misc {
export function clone<T>(input: T): T;
export function isClone<T>(input: unknown): T | null;
export function assertClone<T>(input: unknown): T;
export function validateClone<T>(input: unknown): IValidation<T>;

export function createClone<T>(): (input: T) => T;
export function createIsClone<T>(): (input: unknown) => T | null;
export function createAssertClone<T>(): (input: unknown) => T;
export function createValidateClone<T>(): (input: unknown) => IValidation<T>;
export function isClone<T>(input: unknown): Resolved<T> | null;
export function assertClone<T>(input: unknown): Resolved<T>;
export function validateClone<T>(input: unknown): IValidation<Resolved<T>>;

export function createClone<T>(): (input: T) => Resolved<T>;
export function createIsClone<T>(): (input: unknown) => Resolved<T> | null;
export function createAssertClone<T>(): (input: unknown) => Resolved<T>;
export function createValidateClone<T>(): (
input: unknown
) => IValidation<Resolved<T>>;
}
```
</Tab>
Expand Down Expand Up @@ -53,6 +56,126 @@ export namespace IValidation {
value: any;
}
}
```
</Tab>
<Tab>
```typescript copy filename="typia/Resolved.ts"
/**
* Resolved type erased every methods.
*
* `Resolved` is a type of TMP (Type Meta Programming) type which converts
* its argument as a resolved type that erased every method properties.
*
* If the target argument is a built-in class which returns its origin primitive type
* through the `valueOf()` method like the `String` or `Number`, its return type would
* be the `string` or `number`. Otherwise, the built-in class does not have the
* `valueOf()` method, the return type would be same with the target argument.
*
* Otherwise, the target argument is a type of custom class, all of its custom methods
* would be erased and its prototype would be changed to the primitive `object`.
* Therefore, return type of the TMP type finally be the resolved object.
*
* Before | After
* ------------------------|----------------------------------------
* `Boolean` | `boolean`
* `Number` | `number`
* `BigInt` | `bigint`
* `String` | `string`
* `Class` | `interface`
* Native Class or Others | No change
*
* @template Instance Target argument type.
* @author Jeongho Nam - https://github.com/samchon
* @author Kyungsu Kang - https://github.com/kakasoo
*/
export type Resolved<T> = Equal<T, ResolvedMain<T>> extends true
? T
: ResolvedMain<T>;

type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;

type ResolvedMain<Instance> = Instance extends [never]
? never // (special trick for jsonable | null) type
: ValueOf<Instance> extends boolean | number | bigint | string
? ValueOf<Instance>
: Instance extends Function
? never
: Instance extends object
? ResolvedObject<Instance>
: ValueOf<Instance>;

type ResolvedObject<Instance extends object> = Instance extends Array<infer T>
? IsTuple<Instance> extends true
? ResolvedTuple<Instance>
: ResolvedMain<T>[]
: Instance extends Set<infer U>
? Set<ResolvedMain<U>>
: Instance extends Map<infer K, infer V>
? Map<ResolvedMain<K>, ResolvedMain<V>>
: Instance extends WeakSet<any> | WeakMap<any, any>
? never
: Instance extends
| Date
| Uint8Array
| Uint8ClampedArray
| Uint16Array
| Uint32Array
| BigUint64Array
| Int8Array
| Int16Array
| Int32Array
| BigInt64Array
| Float32Array
| Float64Array
| ArrayBuffer
| SharedArrayBuffer
| DataView
? Instance
: {
[P in keyof Instance]: ResolvedMain<Instance[P]>;
};

type ResolvedTuple<T extends readonly any[]> = T extends []
? []
: T extends [infer F]
? [ResolvedMain<F>]
: T extends [infer F, ...infer Rest extends readonly any[]]
? [ResolvedMain<F>, ...ResolvedTuple<Rest>]
: T extends [(infer F)?]
? [ResolvedMain<F>?]
: T extends [(infer F)?, ...infer Rest extends readonly any[]]
? [ResolvedMain<F>?, ...ResolvedTuple<Rest>]
: [];

type ValueOf<Instance> = IsValueOf<Instance, Boolean> extends true
? boolean
: IsValueOf<Instance, Number> extends true
? number
: IsValueOf<Instance, String> extends true
? string
: Instance;

type IsTuple<T extends readonly any[] | { length: number }> = [T] extends [
never,
]
? false
: T extends readonly any[]
? number extends T["length"]
? false
: true
: false;

type IsValueOf<Instance, Object extends IValueOf<any>> = Instance extends Object
? Object extends IValueOf<infer Primitive>
? Instance extends Primitive
? false
: true // not Primitive, but Object
: false // cannot be
: false;

interface IValueOf<T> {
valueOf(): T;
}
```
</Tab>
</Tabs>
Expand Down Expand Up @@ -293,12 +416,12 @@ console.log(cloned);
]}>
<Tab>
```typescript copy
export function prune<T>(input: T): T;
export function prune<T>(input: T): void;
export function isPrune<T>(input: unknown): T | null;
export function assertPrune<T>(input: unknown): T;
export function validatePrune<T>(input: unknown): IValidation<T>;

export function createPrune<T>(): (input: T) => T;
export function createPrune<T>(): (input: T) => void;
export function createIsPrune<T>(): (input: unknown) => T | null;
export function createAssertPrune<T>(): (input: unknown) => T;
export function createValidatePrune<T>(): (input: unknown) => IValidation<T>;
Expand Down Expand Up @@ -567,6 +690,51 @@ const pruned = (input => { const assert = input => {
$po0(input);
}; assert(input); prune(input); return input; })(department);
console.log(pruned);
```
</Tab>
</Tabs>




## `literals()` function
```typescript filename="typia"
export namespace misc {
export function literals<T extends Atomic.Type | null>(): T[];
}

export namespace Atomic {
export type Type = boolean | number | string | bigint;
}
```

Union literal type to array.

When you call `typia.misc.literals<T>()` function with union literal type, it returns an array of literal values listed in the generic `T` argument. This `typia.misc.literals<T>` function is useful when you are developing test program, especially handling some discriminated union types.

<Tabs items={['TypeScript Source Code', 'Compiled JavaScript File']}>
<Tab>
```typescript showLineNumbers
import typia from "typia";

typia.misc.literals<"A" | "B" | "C" | 1 | 2n>();
```
</Tab>
<Tab>
```javascript showLineNumbers
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const typia_1 = __importDefault(require("typia"));
[
"A",
"B",
"C",
1,
BigInt(2)
];
```
</Tab>
</Tabs>
Loading

0 comments on commit 15a836f

Please sign in to comment.