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

what if use Array.slice() directly? #13

Open
jxxl998 opened this issue Dec 7, 2022 · 0 comments
Open

what if use Array.slice() directly? #13

jxxl998 opened this issue Dec 7, 2022 · 0 comments

Comments

@jxxl998
Copy link

jxxl998 commented Dec 7, 2022

//  Get the last or last n elements in an array.

export default function (array, num = 1) {
  if (!Array.isArray)
    throw new Error('array-first expects an array as the first element');

  if (typeof num !== 'number') throw new Error('num expects a number');

  if (array.length === 0) return null;

  return num === 1 ? array.pop() : array.slice(-num);
}
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

1 participant