Coder Social home page Coder Social logo

array-move's Introduction

array-move

Move an array item to a different position

Install

$ npm install array-move

Usage

import {arrayMoveImmutable} from 'array-move';

const input = ['a', 'b', 'c'];

const array1 = arrayMoveImmutable(input, 1, 2);
console.log(array1);
//=> ['a', 'c', 'b']

const array2 = arrayMoveImmutable(input, -1, 0);
console.log(array2);
//=> ['c', 'a', 'b']

const array3 = arrayMoveImmutable(input, -2, -3);
console.log(array3);
//=> ['b', 'a', 'c']

API

arrayMoveImmutable(array, fromIndex, toIndex)

Clones the given array, moves the item to a new position in the new array, and then returns the new array. The given array is not mutated.

arrayMoveMutable(array, fromIndex, toIndex)

Moves the item to the new position in the array array. Useful for huge arrays where absolute performance is needed.

array

Type: Array

fromIndex

Type: number

The index of the item to move.

If negative, it will begin that many elements from the end.

toIndex

Type: number

The index of where to move the item.

If negative, it will begin that many elements from the end.

array-move's People

Contributors

arthurvr avatar bendingbender avatar juliang avatar mackhintoshi avatar mathewjoe avatar richienb avatar sindresorhus avatar szamanr avatar xcsrz 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

array-move's Issues

No "main" variable set in package.json

The package has no "main" set in its package.json file. This causes systemjs to try and load up the package's entire folder instead of its index.js file!

It needs to be set to:

"main": "index.js",

Incompatible with Jest

Hi there. First off thanks for making this library! It's really helpful. I'm having an issue when trying to test my code that uses this library via jest. I believe the issue that is that jest does not have full support yet for ESM. Therefore during my test, I'm getting:

SyntaxError: Unexpected token 'export'

Would it be possible to export a Common JS version of the library as well as an ESM version? Rollup can do this automatically.

https://jestjs.io/docs/ecmascript-modules

Leaking ES6 code to ES5 bundle

array-move is leaking ES6 code to ES5 bundle, which later fails on a strict ES5 check with the following error:

SyntaxError: The keyword 'const' is reserved (2:590396)

Not compatible with node 15.

Is there a problem with node 15 to be specific with this library ?

yarn add v1.22.11
[1/4] ๐Ÿ” Resolving packages...
[2/4] ๐Ÿšš Fetching packages...
error [email protected]: The engine "node" is incompatible with this module. Expected version "^12.20.0 || ^14.13.1 || >=16.0.0". Got "15.14.0"
error Found incompatible module.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

this is what i get when i try to install array move for node 15.

i think the "startIndex" may be applied to "from"

let arr = [1,2,3];

console.log(arrayMove(arr,-1000,0)); // [ undefined, 1, 2, 3 ]

when i change this line

const startIndex = from < 0 ? array.length + from : from;

the result is

console.log(arrayMove(arr,-1000,0)); // [ 1, 2, 3 ]

Failed to minify error

Hi,
Iam getting the following error while creating a build of my application which is using 'array-move' as a peer dependency for another package.
Failed to minify the code from this file:
./node_modules/array-move/index.js:3
Any help will be appreciated.

Default export to allow for ease of update

Quite a small and trivial idea, however, I was wondering why we wouldn't default export the original mutable function.

While it's an easy thing to change when updating from, say, 2.x -> 4.x, it seems worth it to simply default export the mutated array function, as that was the behavior prior to pure ESM changes, and that would alleviate the breaking change.

Node 18 SyntaxError: Cannot use import statement outside a module

Try to import this module to node project. Got this error

SyntaxError: Cannot use import statement outside a module

if i use require
const {arrayMoveImmutable} = require('array-move');

i got another error

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/goodzon/Projects/maptorium/node_modules/array-move/index.js from /home/goodzon/Projects/maptorium/routes/job.js not supported.
Instead change the require of index.js in /home/goodzon/Projects/maptorium/routes/job.js to a dynamic import() which is available in all CommonJS modules.

if i make
const {arrayMoveImmutable} = import('array-move');
i got
TypeError: arrayMoveImmutable is not a function

How properly make import of module to project???

Support browser

Currently this library requires Node.js 8. Please add legacy JS support (just broke our build updating a 10-line library).

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.