npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

move-by-property

v1.0.3

Published

A simple package (with minimal dependencies) for moving elements within an array

Downloads

6

Readme

Move by Property

A lightweight package (with minimal dependencies) for moving an element within an array, including movement based on a key/property (even deeply nested). moveElementByProperty() and moveElementByPropertyImmutable() achieve property-based movement by updating the origin element and all other elements in the list to maintain the desired order. Note: this is not swapping elements. It involves moving one element from an origin position to a destination position and adjusting the other elements above/below that point of destination accordingly. For a simpler operation, moveElement() can carry out direct movement and uses Array.splice(). See below for examples.

The term "property-based movement" refers to the concept of relying on a specific property within your objects to determine the order, rather than the actual positions of the elements in the array.

For instance, imagine you are working with an array of data where the meaningful order is not determined by the actual array order, but instead depends on a property value of the data returned (e.g., [{ position: 2, data: { ... } }, { position: 1, data: { ... } }]). If you need to "move" an element within that data, you may need to adjust the properties while keeping the other elements' corresponding properties updated. If so, this package may be helpful.

Installation

Using npm or yarn:

npm install move-by-property
yarn add move-by-property

Usage

Calling moveElement() will simply move the element using Array.splice() and the array is adjusted accordingly.

import { moveElement } from "move-by-property";

const list = ["A", "B", "C", "D"];
moveElement(list, 0, 2);
console.log(list); // ["B", "C", "A", "D"] <- "A" is moved to index 2

Calling moveElementImmutable() will still replace the element using splice(), but does not mutate the data and immediately creates a deep copy (using _.cloneDeep).

import { moveElementImmutable } from "move-by-property";

const list = ["A", "B", "C", "D"];
const newList = moveElementImmutable(list, 0, 2);
console.log(list); // ["A", "B", "C", "D"] <- stays the same
console.log(newList); // ["B", "C", "A", "D"] <- "A" is moved to index 2

Calling moveElementByProperty() allows you to adjust the elements based on a property (including nested properties). It assumes a 0-indexed values for the order numbers, but you can pass in an optional flag to use 1-indexed values. Mutates the original array and cannot be sorted (...just yet).

import { moveElementByProperty } from "move-by-property";

const list = [
  {
    data: { position: 0 },
    otherData:
      "Originally position 0, but will change the position property to 2",
  },
  {
    data: { position: 1 },
    otherData:
      "Originally position 1, but will change the position property to 0 when position 0 changes the position property to 2",
  },
  {
    data: { position: 2 },
    otherData:
      "Originally position 2, but will change the position property to 1 when position 0 changes the position property to 2",
  },
];

const nestedPropertyPath = ["data", "position"];
const isZeroIndexed = true;
moveElementByProperty(list, 0, 2, nestedPropertyPath, isZeroIndexed);
console.log(list)
/**
  {
    data: { position: 2 },
    otherData:
      "Originally position 0, but will change the position property to 2",
  },
  {
    data: { position: 0 },
    otherData:
      "Originally position 1, but will change the position property to 0 when position 0 changes the position property to 2",
  },
  {
    data: { position: 1 },
    otherData:
      "Originally position 2, but will change the position property to 1 when position 0 changes the position property to 2",
  },
 * /

Calling moveElementByPropertyImmutable() is similar, but it does not mutate the data and immediately creates a deep copy (using _.cloneDeep). You can optionally pass in a boolean flag to sort the array, if desired.

import { moveElementByPropertyImmutable } from "move-by-property";

const list = [
  {
    data: { position: 0 },
    otherData:
      "Originally position 0, but will change the position property to 2",
  },
  {
    data: { position: 1 },
    otherData:
      "Originally position 1, but will change the position property to 0 when position 0 changes the position property to 2",
  },
  {
    data: { position: 2 },
    otherData:
      "Originally position 2, but will change the position property to 1 when position 0 changes the position property to 2",
  },
];

const nestedPropertyPath = ["data", "position"];
const isZeroIndexed = true;
const shouldSort = true;
const result = moveElementByPropertyImmutable(list, 0, 2, nestedPropertyPath, isZeroIndexed, shouldSort);
/**
  {
    data: { position: 0 },
    otherData:
      "Originally position 1, but will change the position property to 0 when position 0 changes the position property to 2",
  },
  {
    data: { position: 1 },
    otherData:
      "Originally position 2, but will change the position property to 1 when position 0 changes the position property to 2",
  },
  {
    data: { position: 2 },
    otherData:
      "Originally position 0, but will change the position property to 2",
  },
 * /

Additional Details

Method: moveElement()

  • Input:
    /** The list of elements holding the element to be moved */
    elemList: any[],
    /** The index of the element to be moved */
    originIndex: number,
    /** The destination index of the element to be moved */
    destinationIndex: number
  • Output: N/A - It mutates the original array and nothing is returned.

Method: moveElementImmutable()

  • Inputs: Same as moveElement()
  • Output: It returns a modified deep clone of the original array and the original array remains intact.

Method: moveElementByProperty()

  • Input:
    /** The list of elements holding the element to be moved */
    elemList: any[],
    /** The index of the element to be moved */
    originIndex: number,
    /** The destination index of the element to be moved */
    destinationIndex: number,
    /** A reference or references to the properties/keys needed to access the position value */
    indexKey: string | number | (string | number)[],
    /** Optional, defaults to true. An indication as to whether the index is 0-indexed or 1-indexed */
    zeroIndexed: boolean = true
  • Output: N/A - It mutates the original array and nothing is returned.

Method: moveElementByPropertyImmutable()

  • Inputs:
    /** The list of elements holding the element to be moved */
    elemList: any[],
    /** The index of the element to be moved */
    originIndex: number,
    /** The destination index of the element to be moved */
    destinationIndex: number,
    /** A reference or references to the properties/keys needed to access the position value */
    indexKey: string | number | (string | number)[],
    /** Optional, defaults to true. An indication as to whether the index is 0-indexed or 1-indexed */
    zeroIndexed: boolean = true,
    /** Optional, defaults to false. A flag that will sort the results based on the ultimate position values */
    sort: boolean = false
  • Output: It returns a modified deep clone of the original array and the original array remains intact.