Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kjirou/sort-array-for-drag-and-drop
Simple array sort logics to use when implements drag and drop
https://github.com/kjirou/sort-array-for-drag-and-drop
Last synced: 12 days ago
JSON representation
Simple array sort logics to use when implements drag and drop
- Host: GitHub
- URL: https://github.com/kjirou/sort-array-for-drag-and-drop
- Owner: kjirou
- License: mit
- Created: 2018-01-19T07:49:25.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-31T07:09:38.000Z (about 3 years ago)
- Last Synced: 2024-08-10T11:16:36.802Z (3 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/sort-array-for-drag-and-drop
- Size: 11.7 KB
- Stars: 1
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sort-array-for-drag-and-drop
[![npm version](https://badge.fury.io/js/sort-array-for-drag-and-drop.svg)](https://badge.fury.io/js/sort-array-for-drag-and-drop)
[![Build Status](https://travis-ci.org/kjirou/sort-array-for-drag-and-drop.svg?branch=master)](https://travis-ci.org/kjirou/sort-array-for-drag-and-drop)Simple array sort logics to use when implements drag and drop
## Installation
```bash
npm install sort-array-for-drag-and-drop
```## Usage
### `moveElement````js
const {moveElement} = require('sort-array-for-drag-and-drop');const array = [
'A',
'B',
'C',
];console.log(moveElement(array, 0, 2)); // -> ['B', 'A', 'C'] (Insert `array[0]` before `array[2]`)
console.log(moveElement(array, 0, 1)); // -> ['A', 'B', 'C'] (Insert `array[0]` before `array[1]`, not moved)
console.log(moveElement(array, 0, 0)); // -> ['A', 'B', 'C'] (Not moved)
console.log(moveElement(array, 0, 3)); // -> ['B', 'C', 'A'] (In the case of an index that is exceeded, it inserts to the end)
console.log(moveElement(array, 2, 0)); // -> ['C', 'A', 'B'] (Insert `array[2]` before `array[0]`)
console.log(moveElement(array, 2, 1)); // -> ['A', 'C', 'B'] (Insert `array[2]` before `array[1]`)
```### `moveElementByValue`
```js
const {moveElementByValue} = require('sort-array-for-drag-and-drop');// The values must be unique
const array = [
'A',
'B',
'C',
];console.log(moveElementByValue(array, 'A', 'C')); // -> ['B', 'A', 'C'] (Insert 'A' before 'C')
console.log(moveElementByValue(array, 'A', 'C', true)); // -> ['B', 'C', 'A'] (Insert 'A' after 'C')
console.log(moveElementByValue(array, 'D', 'C')); // -> Error! ('D' is not included)
```### `moveElementBy`
```js
const {moveElementBy} = require('sort-array-for-drag-and-drop');// The values must be unique
const a = {value: 'a'};
const b = {value: 'b'};
const c = {value: 'c'};
const array = [a, b, c];// return to compare with values
function getValue(elem) {
return elem.value;
}console.log(moveElementBy(array, getValue, 'A', 'C')); // -> [B, A, C] (Insert A before C)
console.log(moveElementBy(array, getValue, 'A', 'C', true)); // -> [B, C, A] (Insert A after C)
console.log(moveElementBy(array, getValue, 'D', 'C')); // -> Error! (D is not included)
```