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 cloneDeep with custom clone rules #367

Open
jacksonyu opened this issue Aug 10, 2024 · 1 comment
Open

Suggest to enhance cloneDeep with custom clone rules #367

jacksonyu opened this issue Aug 10, 2024 · 1 comment

Comments

@jacksonyu
Copy link

jacksonyu commented Aug 10, 2024

Hello, do you guys think it is a good idea to make cloneDeep to support custom colne rules ?

try this

import {cloneDeep} from 'es-toolkit';
import Decimal from 'decimal.js';

const a = cloneDeep({decimalVal: new Decimal(1)})
console.log(Decimal.isDecimal(a.decimalVal));

a.decimalVal is no longer a Decimal and will lost all functions.

so, if we can enhance cloneDeep to receive custom rules like this:

export function cloneDeep<T>(obj: T, options?:{
  customRules?:{
    check: (value: any) => boolean
    clone: (value: any) => any
  }[]
}): T {
  return cloneDeepImpl(obj, options);
}

then, we can solve the problem:

const a = cloneDeep({decimalVal: new Decimal(1)},{
  customRules:[{
    check:val=>Decimal.isDecimal(val),
    clone:val=> new Decimal(val)
  }]
})

Looking forward to your comments.

by the way , the isEqual function would be more powerful if it support custom rules too.

@raon0211
Copy link
Collaborator

I guess this is a functionality that we should support. I am not sure about how we support this. Let me think for a while which interface would be best.

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

2 participants