Opened 9 years ago

Closed 9 years ago

#12769 closed Bug (wontfix)

CKEDITOR.tools.indexOf should allow to find functions within arrays

Reported by: Marek Lewandowski Owned by:
Priority: Normal Milestone:
Component: General Version:
Keywords: Cc:

Description

CKEDITOR.tools.indexOf should allow to find functions within arrays

Currently if you'll pass a function to CKEDITOR.tools.indexOf, it will be treated as an evaluator.

It makes finding functions in an array impossible.

Proposal:

IMHO priority is to keep backward compatibility. I think we might fix it by adding a param like bool preventEvaluator.

If it's set to true then value won't be treated as an evaluator.

TC:

'test indexOf': function() {
	var stack = [],
		needle = function( foo ) {
			return 'bar';
		};

	// Push one dummy funciton.
	stack.push( function() {
	} );

	// Push needle.
	stack.push( needle );

	// Note that stack.indexOf( needle ) returns 1.
	assert.areSame( 1, CKEDITOR.tools.indexOf( stack, needle ), 'Return value' );
},

Expected result:
1

Current result:
0

Change History (5)

comment:1 Changed 9 years ago by Piotrek Koszuliński

Hmm... this is really unfortunate and a good lesson for the future. I'm not even sure how we can do that the next time - whether a function which is more or less a shim should be identical with the native one or it can have extensions. For instance, lodash's functions don't mimic the native ones, so such situations must have concerned it.

Anyway, for now adding a new argument "notEvaluator" seems to be the only solution.

Last edited 9 years ago by Piotrek Koszuliński (previous) (diff)

comment:2 Changed 9 years ago by Frederico Caldeira Knabben

It is possible to pass an evaluation function that makes the function check. So I'm thinking that this is on purpose and that there is no real issue.

@m.lewandowski: Do you feel that this shortcutting is really necessary? I mean, is it worth?

comment:3 Changed 9 years ago by Marek Lewandowski

@fredck: Nice trick, it does the job.

It passes:

'test indexOf': function() {
	var stack = [],
		needle = function( foo ) {
			return 'bar';
		},
		evaluator = function( val ) {
			return val === needle;
		};

	// Push one dummy funciton.
	stack.push( function() {
	} );

	// Push needle.
	stack.push( needle );

	// Note that stack.indexOf( needle ) returns 1.
	assert.areSame( 1, CKEDITOR.tools.indexOf( stack, evaluator ), 'Return value' );
},

Still, it makes it a little bit more complex.

Finding an array item is a trivial task, and it should be very simple to use without any special functions.

As for the problem I don't find it to be a super big issue, because it's limited to very particular case. Rarely it might surprise some developers blindly presuming standard interface.

comment:4 Changed 9 years ago by Frederico Caldeira Knabben

I agree. Still developers would have to figure out that the third parameter exists and by then they'll understand that the function passed as is is an evaluator.

I assume that dealing with functions inside arrays is quite uncommon and considering that in any case there is a way to make it work, I would vote to not change the API.

comment:5 Changed 9 years ago by Marek Lewandowski

Resolution: wontfix
Status: newclosed
Note: See TracTickets for help on using tickets.
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy