|
| 1 | +# Enhanced Array |
| 2 | + |
| 3 | +A collection of common utilities for the JavaScript array. |
| 4 | + |
| 5 | +> This library copies common utilities from other languages. For advanced utilities, please use a library like Lodash instead. |
| 6 | +
|
| 7 | +## Installation |
| 8 | + |
| 9 | +``` |
| 10 | +npm i enhanced-array |
| 11 | +``` |
| 12 | + |
| 13 | +Only need a few? Just copy the source code :) |
| 14 | + |
| 15 | +## API |
| 16 | + |
| 17 | +### Accessing Elements |
| 18 | + |
| 19 | +#### [getFirst](/src/getFirst.ts) |
| 20 | + |
| 21 | +Returns the first element of the array. |
| 22 | + |
| 23 | +```ts |
| 24 | +import { getFirst } from 'enhanced-array'; |
| 25 | + |
| 26 | +const result = getFirst([1, 2, 3, 4, 5]); |
| 27 | +console.log(result); // 1 |
| 28 | +``` |
| 29 | + |
| 30 | +> **Time complexity:** |
| 31 | +> |
| 32 | +> O(1) |
| 33 | +
|
| 34 | +#### [getLast](/src/getLast.ts) |
| 35 | + |
| 36 | +Returns the last element of the array. |
| 37 | + |
| 38 | +```ts |
| 39 | +import { getLast } from 'enhanced-array'; |
| 40 | + |
| 41 | +const result = getLast([1, 2, 3, 4, 5]); |
| 42 | +console.log(result); // 5 |
| 43 | +``` |
| 44 | + |
| 45 | +> **Time complexity:** |
| 46 | +> |
| 47 | +> O(1) |
| 48 | +
|
| 49 | +--- |
| 50 | + |
| 51 | +### Adding Elements |
| 52 | + |
| 53 | +#### [insertAt](/src/insertAt.ts) |
| 54 | + |
| 55 | +Inserts a new element at the specified position. |
| 56 | + |
| 57 | +```ts |
| 58 | +import { insertAt } from 'enhanced-array'; |
| 59 | + |
| 60 | +const result = insertAt([1, 2, 3, 4, 5], { index: 3, element: 9 }); |
| 61 | +console.log(result); // [1, 2, 3, 9, 4, 5] |
| 62 | +``` |
| 63 | + |
| 64 | +> **Time complexity:** |
| 65 | +> |
| 66 | +> O(n), where n is the length of the array. |
| 67 | +> |
| 68 | +> O(1), if `index` is the last index of the array. |
| 69 | +
|
| 70 | +--- |
| 71 | + |
| 72 | +### Inspecting Elements |
| 73 | + |
| 74 | +#### [isContainNil](/src/isContainNil.ts) |
| 75 | + |
| 76 | +Returns a boolean that indicates whether the array contains `undefined` or `null`. |
| 77 | + |
| 78 | +```ts |
| 79 | +import { isContainNil } from 'enhanced-array'; |
| 80 | + |
| 81 | +const result1 = isContainNil([1, 2, 3, 4, 5]); |
| 82 | +console.log(result1); // false |
| 83 | + |
| 84 | +const result2 = isContainNil([1, 2, undefined, 4, 5]); |
| 85 | +console.log(result2); // true |
| 86 | + |
| 87 | +const result3 = isContainNil([1, 2, null, 4, 5]); |
| 88 | +console.log(result3); // true |
| 89 | +``` |
| 90 | + |
| 91 | +> **Time complexity:** |
| 92 | +> |
| 93 | +> O(n), where n is the length of the array. |
| 94 | +> |
| 95 | +> O(1), if `index` is the last index of the array. |
| 96 | +
|
| 97 | +#### [isEmpty](/src/isEmpty.ts) |
| 98 | + |
| 99 | +Returns a boolean that indicates whether the array is empty. |
| 100 | + |
| 101 | +```ts |
| 102 | +import { isEmpty } from 'enhanced-array'; |
| 103 | + |
| 104 | +const result1 = isEmpty([1, 2, 3, 4, 5]); |
| 105 | +console.log(result1); // false |
| 106 | + |
| 107 | +const result2 = isEmpty([]); |
| 108 | +console.log(result2); // true |
| 109 | +``` |
| 110 | + |
| 111 | +> **Time complexity:** |
| 112 | +> |
| 113 | +> O(1) |
| 114 | +
|
| 115 | +--- |
| 116 | + |
| 117 | +### Removing Elements |
| 118 | + |
| 119 | +#### [removeAt](/src/removeAt.ts) |
| 120 | + |
| 121 | +Removes the element at the specified position. |
| 122 | + |
| 123 | +```ts |
| 124 | +import { removeAt } from 'enhanced-array'; |
| 125 | + |
| 126 | +const result = removeAt([1, 2, 3, 4, 5], { index: 3 }); |
| 127 | +console.log(result); // [1, 2, 3, 5] |
| 128 | +``` |
| 129 | + |
| 130 | +> **Time complexity:** |
| 131 | +> |
| 132 | +> O(n), where n is the length of the array. |
| 133 | +> |
| 134 | +> O(1), if `index` is the last index of the array. |
| 135 | +
|
| 136 | +--- |
| 137 | + |
| 138 | +### Reordering Elements |
| 139 | + |
| 140 | +#### [swap](/src/swap.ts) |
| 141 | + |
| 142 | +Exchanges the element at the specified indices of the array. |
| 143 | + |
| 144 | +```ts |
| 145 | +import { swap } from 'enhanced-array'; |
| 146 | + |
| 147 | +const result = swap([1, 2, 3, 4, 5], { i: 0, j: 4 }); |
| 148 | + |
| 149 | +console.log(result); // [5, 2, 3, 4, 1] |
| 150 | +``` |
| 151 | + |
| 152 | +> **Time complexity:** |
| 153 | +> |
| 154 | +> O(1) |
| 155 | +
|
| 156 | +## Stolen From |
| 157 | + |
| 158 | +- [Swift's Array](https://developer.apple.com/documentation/swift/array) |
0 commit comments