Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(limitTo): add support for array-likes #14694

Closed
wants to merge 1 commit into from

Conversation

wesleycho
Copy link
Contributor

  • Add support for array-like objects

This implements #14657.

limitTo = $filter('limitTo');
}));

function isInObject(obj) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never used.

}
}
};
}

function sliceFn(input, idx, length) {
if (isString(input)) return input.slice(idx, length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would check if input has a slice method (or property) so we support Array-like objects with custon slice methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This separation is specifically because String.prototype.slice returns a string, while Array.prototype.slice returns an array.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's stick with the current separation, so we are always returnign either a string or an array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One alternative implementation I can do is instead of the isString(input) check, I can do

return input.slice ? input.slice.call(input, idx, length) : slice.call(input, idx, length);

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, looks like this fails on falsy types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to keep things as is, so we don't break the guarantee that limitTo returns an Array or String.
(I don't see any real benefit in breaking that contract.)

@gkalpak
Copy link
Member

gkalpak commented May 31, 2016

Generally looks good - I left some comments.
I think it would be nice to test against a couple more Array-likes (e.g. a NodeList, a jqLite/jQuery collection).

@wesleycho
Copy link
Contributor Author

Made adjustments as per comments.

I do have a question though - I notice in the orderBy filter, it only checks against a similar object I have constructed here, and not against typical alternative array-likes. Does it make sense to test those here given that?

@wesleycho wesleycho force-pushed the feat/limit-arraylike branch 2 times, most recently from 87212f9 to 50e46a3 Compare May 31, 2016 11:54
});

it('should return an empty string when X = 0', function() {
expect(limitTo(str, 0)).toEqual("");
expect(limitTo(str, '0')).toEqual("");
expect(limitTo({}, 1)).toEqual({});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this ?

@gkalpak
Copy link
Member

gkalpak commented May 31, 2016

$filter has a couple of array-likes (see filterSpec.js#L428-L443), but I don't think consistency is important here.

I don't mean add all array-likes in all usecases, just add a simple testcase that tests basic limiting on 2-3 array-like types. But I don't feel strongly about it, so feel free to ignore 😄

@wesleycho wesleycho force-pushed the feat/limit-arraylike branch 2 times, most recently from 372d5f3 to cb5f3fa Compare May 31, 2016 20:59
@wesleycho
Copy link
Contributor Author

Updated as per comments (sloppiest PR I've done in a while...) - I also added a simple test case for basic array likes at the very end.

@wesleycho wesleycho force-pushed the feat/limit-arraylike branch from cb5f3fa to 372d5f3 Compare June 1, 2016 06:09
@wesleycho
Copy link
Contributor Author

Bah, I did a mistaken force push to try to restart the CI - will push again when I'm at work tomorrow, some changes got wiped out.

@wesleycho wesleycho force-pushed the feat/limit-arraylike branch from 372d5f3 to cb5f3fa Compare June 2, 2016 18:52
@wesleycho
Copy link
Contributor Author

This should be up to date now.

}
}
};
}

function sliceFn(input, idx, length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Renaming the arguments to input, begin, end sounds more intuitive.

@wesleycho wesleycho force-pushed the feat/limit-arraylike branch from cb5f3fa to 48c02f3 Compare June 7, 2016 20:32
expect(limitTo(null, 1)).toEqual(null);
expect(limitTo(undefined, 1)).toEqual(undefined);
expect(limitTo({}, 1)).toEqual({});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why that ?

@wesleycho wesleycho force-pushed the feat/limit-arraylike branch from 48c02f3 to 84308a4 Compare June 7, 2016 20:36

expect(limitTo(argsObj, 2).length).toBe(2);
expect(limitTo('abc', 1).length).toBe(1);
expect(limitTo(nodeList, 3).length).toBe(3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not testing anything. You need a limit that is less than the actual length.

@wesleycho wesleycho force-pushed the feat/limit-arraylike branch from 84308a4 to 9b9d536 Compare June 7, 2016 20:39
@gkalpak
Copy link
Member

gkalpak commented Jun 7, 2016

Now it's time for the "real stuff": We need to update the docs 😃

@wesleycho wesleycho force-pushed the feat/limit-arraylike branch from 9b9d536 to 58e0e4e Compare June 7, 2016 20:53
@wesleycho
Copy link
Contributor Author

Added documentation update, might need tweaking on wording (or maybe removing the bit about strings or numbers).

@gkalpak
Copy link
Member

gkalpak commented Jun 8, 2016

@wesleycho, I don't see any docs 😟

- Add support for array-like objects
@wesleycho wesleycho force-pushed the feat/limit-arraylike branch from 58e0e4e to 8905747 Compare June 8, 2016 21:56
@wesleycho
Copy link
Contributor Author

That's because I'm a moron :( - pushed

*
* @param {Array|string|number} input Source array, string or number to be limited.
* @param {Array} input Array or array-like to be limited.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the param type should be {Array|string|number|object} right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In orderBy, we use ArrayLike (which is totally made-up, but pretty descriptive).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. That works for me - does orderBy convert numbers to strings too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orderBy only accepts {Array|ArrayLike} (and calls Array.prototype.map on its input).

@gkalpak
Copy link
Member

gkalpak commented Jun 10, 2016

I just realized, @googlebot didn't bother verifying the CLA fos us 😒
Can we trigger it manually ?

@petebacondarwin
Copy link
Contributor

Not sure but @wesleycho is definitely a yes already :-)

@gkalpak
Copy link
Member

gkalpak commented Jun 10, 2016

I'll add the label manually, because my PR-merging script doesn't proceed otherwise :)

@gkalpak gkalpak closed this in f467dc3 Jun 10, 2016
gkalpak pushed a commit that referenced this pull request Jun 10, 2016
@wesleycho wesleycho deleted the feat/limit-arraylike branch June 10, 2016 18:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants