Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

array-move-with-pivot

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-move-with-pivot

This package moves items (objects) in circular format and around a selected property which acts a pivot.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

array-move-with-pivot

This is a simple javaScript utility function for moving objects in an array with a property as a rotation point called pivot.

This function is handy when dragging and rearrange objects on a table.

if array [1, 2, 3, 4] After dragging value 3 to position of value 1 We have [3, 1, 2, 4]

installation

npm i --save array-move-with-pivot

or 

yarn add array-move-with-pivot

Use Case

// I have an array of objects and I need to move objects
// in circular manner around a property of the objects.

type User = {
      id: number;
      name: string;
      position: number;
    }

const rawData: User[] = [
      { id: 11, name: "Ekene", position: 1},
      { id: 12, name: "Akunna", position: 2 },
      { id: 30, name: "Chidimna", position: 3 },
    ];

I want to move any user from one position to another without altering the position property

// for example moving user at index 2 to 0

// expected result

    const newData = [
      { id: 30, name: "Chidimna", position: 1},
      { id: 11, name: "Ekene", position: 2 },
      {  id: 12, name: "Akunna", position: 3 },
    ];

Usage

import moveArrayItemsWithPivot from 'array-move-with-pivot'

const rawData: User[] = [
      { id: 11, name: "Ekene", position: 1},
      { id: 12, name: "Akunna", position: 2 },
      { id: 30, name: "Chidimna", position: 3 },
    ];

const result = moveArrayItemsWithPivot(rawData,2,0,"position");

console.log(result) //  [{ id: 30, name: "Chidimna", position: 1}, { id: 11, name: "Ekene", position: 2 }, {  id: 12, name: "Akunna", position: 3 }];

Note

It is not a named export, you can import with your chosen name e.g arrayMove, moveArrayItems, etc.

Keywords

FAQs

Package last updated on 07 Jul 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc