-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(limitTo): add support for array-likes #14694
Conversation
limitTo = $filter('limitTo'); | ||
})); | ||
|
||
function isInObject(obj) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
Generally looks good - I left some comments. |
26caa6f
to
12f0a49
Compare
Made adjustments as per comments. I do have a question though - I notice in the |
87212f9
to
50e46a3
Compare
}); | ||
|
||
it('should return an empty string when X = 0', function() { | ||
expect(limitTo(str, 0)).toEqual(""); | ||
expect(limitTo(str, '0')).toEqual(""); | ||
expect(limitTo({}, 1)).toEqual({}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this ?
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 😄 |
372d5f3
to
cb5f3fa
Compare
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. |
cb5f3fa
to
372d5f3
Compare
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. |
372d5f3
to
cb5f3fa
Compare
This should be up to date now. |
} | ||
} | ||
}; | ||
} | ||
|
||
function sliceFn(input, idx, length) { |
There was a problem hiding this comment.
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.
cb5f3fa
to
48c02f3
Compare
expect(limitTo(null, 1)).toEqual(null); | ||
expect(limitTo(undefined, 1)).toEqual(undefined); | ||
expect(limitTo({}, 1)).toEqual({}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why that ?
48c02f3
to
84308a4
Compare
|
||
expect(limitTo(argsObj, 2).length).toBe(2); | ||
expect(limitTo('abc', 1).length).toBe(1); | ||
expect(limitTo(nodeList, 3).length).toBe(3); |
There was a problem hiding this comment.
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.
84308a4
to
9b9d536
Compare
Now it's time for the "real stuff": We need to update the docs 😃 |
9b9d536
to
58e0e4e
Compare
Added documentation update, might need tweaking on wording (or maybe removing the bit about strings or numbers). |
@wesleycho, I don't see any docs 😟 |
- Add support for array-like objects
58e0e4e
to
8905747
Compare
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
I just realized, @googlebot didn't bother verifying the CLA fos us 😒 |
Not sure but @wesleycho is definitely a yes already :-) |
I'll add the label manually, because my PR-merging script doesn't proceed otherwise :) |
This implements #14657.