Coder Social home page Coder Social logo

jonschlinkert / array-last Goto Github PK

View Code? Open in Web Editor NEW
35.0 4.0 14.0 2.48 MB

Return the last element in an array. Faster than `.slice`

Home Page: https://github.com/jonschlinkert

License: MIT License

JavaScript 100.00%
array last javascript nodejs slice element node

array-last's Introduction

array-last NPM version NPM monthly downloads NPM total downloads Linux Build Status

Get the last or last n elements in an array.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save array-last

Usage

var last = require('array-last');

last(['a', 'b', 'c', 'd', 'e', 'f']);
//=> 'f'

last(['a', 'b', 'c', 'd', 'e', 'f'], 1);
//=> 'f'

last(['a', 'b', 'c', 'd', 'e', 'f'], 3);
//=> ['d', 'e', 'f']

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

  • arr-union: Combines a list of arrays, returning a single array with unique values, using strict equality… more | homepage
  • array-unique: Remove duplicate values from an array. Fastest ES5 implementation. | homepage
  • array-xor: Returns the symmetric difference (exclusive-or) of an array of elements (elements that are present in… more | homepage

Contributors

Commits Contributor
19 jonschlinkert
3 SpyMaster356
2 bendrucker
2 phated

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on November 30, 2017.

array-last's People

Contributors

bendrucker avatar jonschlinkert avatar phated avatar zeragamba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

array-last's Issues

what if use Array.slice() directly?

//  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);
}

why don`t judge the length of the second parameters

When the length of the second parameters is greater than the length of the array,The undefined will be included in the result.

example:

 var last = require('array-last');

 var a = last(['a', 'b', 'c', 'd', 'e', 'f'],7);

result

[ undefined, 'a', 'b', 'c', 'd', 'e', 'f' ]

The second argument maybe less than 0

Whe the second argument less than 0, there will be an error.
Should we change the code like below to support negative number.

example:

 n = isNumber(n) ? Math.abs(+n) : 1;

Benchmarked is not work

  • Environment 1:
    • Arch linux/4.12.8-2-ARCH
    • node v8.5.0
    • npm 5.3.0
  • Environment 2:
    • macOS 10.12.6
    • node v8.3.0
    • npm 5.4.2
$ npm list --depth=0
[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

$ node benchmark
Benchmarking: (5 of 5)
 · 10
 · 1000
 · 100000
 · 1000000
 · 200

When I run node benchmark, I got 10, 1000, 100000, 1000000 and then nothing. benchmarked works well in is-number and kind-of, but not this repo. I have tried [email protected] & [email protected] on this, but it still the same, no errors & no more outputs.

what if second_params is greater than first_params.lenth?

you took independent action for scene -- n === 1, but another scene like n > arr.length , like arr.length == 1,
and its necessary to use another tool like is_number?
n = isNumber(n) ? +n :1; => n = typeof n == 'number' ? +n : 1;

add
if (n >= len || len == 1){ return arr }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.