Socket
Socket
Sign inDemoInstall

get-reverse-iterating-array

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    get-reverse-iterating-array

A lightweight proxy function reversing an array’s default iteration direction


Version published
Weekly downloads
3
Maintainers
1
Install size
6.71 kB
Created
Weekly downloads
 

Changelog

Source

2.0.0 (2022-05-01)

BREAKING CHANGES

  • dist: Changes the default module format of this package to ES module. Import it using import getReverseIteratingArray from 'get-reverse-iterating-array'.
  • dist: Removes the get-reverse-iterating-array/dist/cjs/get-reverse-iterating-array.js and get-reverse-iterating-array/dist/esm/get-reverse-iterating-array.mjs module specifiers.

Readme

Source

get-reverse-iterating-array

A function returning an array whose iteration direction is reversed without affecting the original array or affecting the iteration direction of any other arrays.

By default, the iteration protocols are defined to iterate on an iterable object’s elements according to their insertion order.

The function getReverseIteratingArray returns a new Proxy object with the array being its target. The array’s [Symbol.iterator] property is trapped in order to define custom iteration behaviour. This has the advantage of not changing the implementation of Array.prototype[Symbol.iterator] directly which would cause the iteration direction of all arrays to be reversed. Using a proxy also avoids unnecessarily copying an array for reverse iteration like Array.prototype.reverse() would.

If you require to iterate over an array in both forwards and backwards direction, have a look at ReverseIterableArray.

Links:

See also:

Table of contents

Installation

npm install get-reverse-iterating-array

Usage

import getReverseIteratingArray from 'get-reverse-iterating-array';

const array = getReverseIteratingArray([1, 2, 3]);

Examples

For some live usage examples, clone the repository and run the following:

npm install
npm run build
npm start

Then, open localhost:8080/examples in a browser.

Tests

In order to run the tests, clone the repository and run the following:

npm install
npm test

Documentation

Create a reverse-iterating array by passing any array to getReverseIteratingArray.

const reverseIteratingArray = getReverseIteratingArray([5, 4, 1, 2, 3]);

The returned object will behave like a regular built-in array with the exception of all cases involving the iteration protocols. The behaviour of the following concepts will be changed when used together with a reverse-iterating array:

  • for..of statement

    for (const element of reverseIteratingArray) {
      console.log(element)
    }
    //> 3
    //> 2
    //> 1
    //> 4
    //> 5
    
  • spread syntax

    [...reverseIteratingArray];
    //> [ 3, 2, 1, 4, 5 ]
    
  • Array.from() method

    Array.from(reverseIteratingArray);
    //> [ 3, 2, 1, 4, 5 ]
    
  • destructuring assignment

    const [a, b, c, d, e] = reverseIteratingArray
    //> a = 3, b = 2, c = 1, d = 4, e = 5
    

Keywords

FAQs

Last updated on 01 May 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc