Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest to enhance clone() function. #196

Open
samchon opened this issue Jul 14, 2024 · 2 comments
Open

Suggest to enhance clone() function. #196

samchon opened this issue Jul 14, 2024 · 2 comments

Comments

@samchon
Copy link

samchon commented Jul 14, 2024

Current clone() function considers only those native classes.

  • Date
  • Set
  • Map
  • RegExp

However, there are much more native classes in the JavaScript, especially about binary handling.

So, I think that the clone() function should consider them.

  • Uint8Array
  • Uint8ClampedArray
  • Uint16Array
  • BigInt64Array
  • Int8Array
  • Int16Array
  • Int32Array
  • BigInt64Array
  • Float32Array
  • Float64Array
  • ArrayBuffer
  • SharedArrayBuffer
  • DataView
  • Blob
  • File

Also, current clone() function is returning the same T type with its parameter, but it is not correct.

The returned type must be casted, because non-native classes are converted to primitve object type.

To solve this problem, the clone() function needs to return Shallowed<T> type like below.

type Shallowed<T> = Equal<T, ShallowMain<T>> extends true ? T : ShallowMain<T>
type ShallowMain<T> = T extends [never]
  ? never
  : T extends object
  ? T extends 
    | Array<any> | Set<any> | Map<any, any> | Date | RegExp | Date
    | Uint8Array
    | Uint8ClampedArray
    | Uint16Array
    | Uint32Array
    | BigUint64Array
    | Int8Array
    | Int16Array
    | Int32Array
    | BigInt64Array
    | Float32Array
    | Float64Array
    | ArrayBuffer
    | SharedArrayBuffer
    | DataView
    | Blob
    | File
  ? T
  : {
    [P in keyof T]: T[P] extends Function ? never : T[P];
  }
  : T;
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;

Related PR: #155

samchon added a commit to samchon/es-toolkit that referenced this issue Jul 15, 2024
Current `clone()` function considers only those native classes.

- `Date`
- `Set`
- `Map`
- `RegExp`

However, there are much more native classes in the JavaScript, especially about binary handling.

So, I think that the `clone()` function should consider them.

- `Uint8Array`
- `Uint8ClampedArray`
- `Uint16Array`
- `BigInt64Array`
- `Int8Array`
- `Int16Array`
- `Int32Array`
- `BigInt64Array`
- `Float32Array`
- `Float64Array`
- `ArrayBuffer`
- `SharedArrayBuffer`
- `DataView`
- `Blob`
- `File`

Also, current `clone()` function is returning the same `T` type with its parameter, but it is not correct.

The returned type must be casted, because non-native classes are converted to primitve object type.

To solve this problem, the `clone()` function needs to return `Shallowed<T>` type like below.

```typescript
type Shallowed<T> = Equal<T, ShallowMain<T>> extends true ? T : ShallowMain<T>
type ShallowMain<T> = T extends [never]
  ? never
  : T extends object
  ? T extends
    | Array<any> | Set<any> | Map<any, any> | Date | RegExp | Date
    | Uint8Array
    | Uint8ClampedArray
    | Uint16Array
    | Uint32Array
    | BigUint64Array
    | Int8Array
    | Int16Array
    | Int32Array
    | BigInt64Array
    | Float32Array
    | Float64Array
    | ArrayBuffer
    | SharedArrayBuffer
    | DataView
    | Blob
    | File
  ? T
  : {
    [P in keyof T]: T[P] extends Function ? never : T[P];
  }
  : T;
type Equal<X, Y> = X extends Y ? (Y extends X ? true : false) : false;
```

- related issue: toss#196
- related PR: toss#155
@raon0211
Copy link
Collaborator

As you mentioned, our clone function should handle most JavaScript native objects. One thing that comes to my mind is that it will hurt bundle size, but accuracy is more important...

@ahaoboy
Copy link

ahaoboy commented Jul 20, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants