├── LICENSE ├── README.md ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # svelte-array-helper 2 | A dead simple helper for modifying arrays in Svelte, and making the code more readable. 3 | 4 | This library only contains the array methods that mutate the array. 5 | 6 | ## Installation 7 | ``` 8 | npm install svelte-array-helper 9 | ``` 10 | 11 | ## Usage 12 | ``` javascript 13 | // Import single static methods 14 | import { push, pop, splice } from 'svelte-array-helper' 15 | 16 | // Import class 17 | import ArrayHelper from 'svelte-array-helper' 18 | let arrayHelper = new ArrayHelper // <- Instaciate it 19 | ``` 20 | 21 | ## Example 22 | ``` javascript 23 | import { push } from 'svelte-array-helper' 24 | 25 | let list = [0,1,2,3] 26 | 27 | function addToList(itemToAdd) { 28 | list = push(list, itemToAdd) 29 | } 30 | ``` 31 | 32 | **Note** 33 | 34 | All methods will return the modified array 35 | 36 | ### push(array, value) 37 | ``` javascript 38 | push(array, value) 39 | ``` 40 | 41 | ### pop() 42 | ``` javascript 43 | push(array) 44 | ``` 45 | 46 | ### splice(array, ...args) 47 | ``` javascript 48 | splice(array, 0, 1, 'item1', 'item2' etc..) 49 | ``` 50 | 51 | ### copyWithin(array, ...args) 52 | ``` javascript 53 | copyWithin(array, ...arguments) 54 | ``` 55 | 56 | ### fill(array, ...args) 57 | ``` javascript 58 | fill(array, ...arguments) 59 | ``` 60 | 61 | ### reverse(array) 62 | ``` javascript 63 | reverse(array) 64 | ``` 65 | 66 | ### sort(array, ...args) 67 | ``` javascript 68 | sort(array, ...arguments) 69 | ``` 70 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Easy helper for modifying arrays in Svelte 3 | */ 4 | export default class ArrayHelper { 5 | /** 6 | * Push to the array 7 | * @param {Array} array 8 | * @param {*} value 9 | */ 10 | push (array, value) { 11 | array.push(value) 12 | return array 13 | } 14 | 15 | /** 16 | * Pop the last item in the array 17 | * @param {Array} array 18 | * @param {*} value 19 | */ 20 | pop (array) { 21 | array.pop() 22 | return array 23 | } 24 | 25 | /** 26 | * Remove the first item in the array 27 | * @param {Array} array 28 | */ 29 | shift (array) { 30 | array.shift() 31 | return array 32 | } 33 | 34 | /** 35 | * Replace the first element in the array 36 | * @param {Array} array 37 | * @param {*} value 38 | */ 39 | unshift (array, value) { 40 | array.unshift(value) 41 | return array 42 | } 43 | 44 | /** 45 | * Replace the first element in the array 46 | * @param {Array} array 47 | * @param {*} args 48 | */ 49 | splice (array, ...args) { 50 | array.splice(...args) 51 | return array 52 | } 53 | 54 | /** 55 | * Shallow copies part of an array to another location in the same array and returns it without modifying its length 56 | * @param {Array} array 57 | * @param {*} args 58 | */ 59 | copyWithin (array, ...args) { 60 | array.copyWithin(...args) 61 | return array 62 | } 63 | 64 | /** 65 | * Fills all the elements of an array from a start index (default zero) to an end index (default array length) with a static value. It returns the modified array. 66 | * @param {*} args 67 | */ 68 | fill (array, ...args) { 69 | array.fill(...args) 70 | return array 71 | } 72 | 73 | /** 74 | * Reverses the array 75 | * @param {*} args 76 | */ 77 | reverse (array) { 78 | array.reverse() 79 | return array 80 | } 81 | 82 | /** 83 | * Sorts the array 84 | * @param {*} args 85 | */ 86 | sort (array, ...args) { 87 | array.sort(...args) 88 | return array 89 | } 90 | } 91 | 92 | export const push = ArrayHelper.prototype.push 93 | export const pop = ArrayHelper.prototype.pop 94 | export const shift = ArrayHelper.prototype.shift 95 | export const unshift = ArrayHelper.prototype.unshift 96 | export const splice = ArrayHelper.prototype.splice 97 | export const copyWithin = ArrayHelper.prototype.copyWithin 98 | export const fill = ArrayHelper.prototype.fill 99 | export const reverse = ArrayHelper.prototype.reverse 100 | export const sort = ArrayHelper.prototype.sort -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svelte-array-helper", 3 | "version": "2.0.2", 4 | "description": "A dead simple helper for modifying arrays in Svelte", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/dasmikko/svelte-array-helper.git" 12 | }, 13 | "keywords": [ 14 | "array", 15 | "helper", 16 | "svelte", 17 | "utility" 18 | ], 19 | "author": "Mikkel Andersen", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/dasmikko/svelte-array-helper/issues" 23 | }, 24 | "homepage": "https://github.com/dasmikko/svelte-array-helper#readme" 25 | } 26 | --------------------------------------------------------------------------------