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

Remove current behavior of Big(undefined) #220

Open
fisker opened this issue Feb 20, 2025 · 7 comments
Open

Remove current behavior of Big(undefined) #220

fisker opened this issue Feb 20, 2025 · 7 comments

Comments

@fisker
Copy link

fisker commented Feb 20, 2025

if the argument is undefined, then a new Big constructor will be returned

https://mikemcl.github.io/big.js/#big

It's too dangerous to return a new class instead of Big instance, users need check every input first.

if (foo !== undefined) {
   Big(foo)
}

of course, we still need to be careful to not pass undefined, but we can throw on Big(undefined), that's much easier to use and get noted.

To support creating new Big class, we can expose createBig() instead.

@MikeMcl
Copy link
Owner

MikeMcl commented Feb 20, 2025

It's too late, I think. That decision was made years ago.

It's unconventional but it's short and sweet and I'm okay with it.

Big.createBig() is okay but a bit of a mouthful in comparison. Big.create() was considered at the time also.

Anyway, using new Big() will throw an error, and even without new users would soon get an error when a method is called on what they think is an instance but is a constructor.

Thanks for the feedback.

@fisker
Copy link
Author

fisker commented Feb 20, 2025

Big.createBig() is okay but a bit of a mouthful in comparison.

I was thinking

import Big, {createBig} from 'big.js';

Big.create() sounds like shortcut for new Big()

users would soon get an error when a method is called on what they think is an instance but is a constructor.

Not always true, for example in this case,

function sum(array, initialValue) {
	return array.reduce((total, current) => total.plus(current), Big(initialValue))
}

console.log(sum([1,2,3], 0).toString()) // -> "6"
console.log(sum([], undefined).toString()) // -> The `Big` constructor 

@fisker
Copy link
Author

fisker commented Feb 20, 2025

In my project, I added a simple wrapper, to enforce new and prevent Big(undefined)

import Big from 'big.js';

class BigNumber {
	constrictor(value) {
		if (value === undefined) {
			throw new TypeError('...')
		}

		return new Big(value)
	}
}

export default BigNumber;

Didn't subclass since I'm not sure if it will work when I wrote it.

Just want to share how I use, and feel worth a try to propose a change to prevent such mistakes.

@fisker

This comment has been minimized.

@fisker
Copy link
Author

fisker commented Feb 20, 2025

Maybe we can check arguments.length === 0 instead of compare n with undefined. I don't think anyone want a new constructor passes explicit undefined.

So the change will be almost unnoticeable.

@MikeMcl
Copy link
Owner

MikeMcl commented Feb 20, 2025

Sorry, I'm not seeing any compelling arguments.

Your wrapper seems a bit pointless when new Big() throws anyway. Might as well just use:

Big.create = n => new Big(n);

That way, you wouldn't have to create instances using new and you would also have your necessarily instance-creating create function.

I just noticed that big.js, decimal.js-light, decimal.js, and bignumber.js all have the same behavior.

Which behaviour? None of the others create a new constructor by calling the default constructor without new.

@fisker
Copy link
Author

fisker commented Feb 21, 2025

That way, you wouldn't have to create instances using new

That's exactly why I use class I want constructors be called with new, I don't like Big("1") either, but that's just a personal preference.

None of the others create a new constructor by calling the default constructor without new.

Really sorry, I read them too quick, I saw instanceof check and assumed they are the same code as here. My bad.

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