Skip to content

Instantly share code, notes, and snippets.

@JohannesHoppe
Last active June 19, 2019 13:05
Show Gist options
  • Save JohannesHoppe/b4f26e82114e9997b8fee926877c9e53 to your computer and use it in GitHub Desktop.
Save JohannesHoppe/b4f26e82114e9997b8fee926877c9e53 to your computer and use it in GitHub Desktop.
8 immutable arrays operations you should know
import { ImmutableArray } from './immutable-array';
describe('ImmutableArray', function() {
let abc;
beforeEach(() => abc = ['A', 'B', 'C']);
it('clone() should create a shallow copy of the array', function() {
const result = new ImmutableArray(abc).clone();
expect(result).toEqual(['A', 'B', 'C']);
});
it('push() should add one element to the end of the array', function() {
const result = new ImmutableArray(abc).push('D');
expect(result).toEqual(['A', 'B', 'C', 'D']);
});
it('pop() should remove the last element from the array', function() {
const result = new ImmutableArray(abc).pop();
expect(result).toEqual(['A', 'B']);
});
it('unshift() should add one element to the front of the array', function() {
const result = new ImmutableArray(abc).unshift('D');
expect(result).toEqual(['D', 'A', 'B', 'C']);
});
it('shift() should remove the first element from the array', function() {
const result = new ImmutableArray(abc).shift();
expect(result).toEqual(['B', 'C']);
});
it('sort() should sort the elements of an array', function() {
const result = new ImmutableArray(abc).sort((a, b) => b.localeCompare(a));
expect(result).toEqual(['C', 'B', 'A']);
});
it('delete() should remove an element by index position', function() {
const result = new ImmutableArray(abc).delete(1);
expect(result).toEqual(['A', 'C']);
});
it('splice() remove 0 elements from index 2, and insert "Z"', function() {
const result = new ImmutableArray(abc).splice(2, 0, 'Z');
expect(result).toEqual(['A', 'B', 'Z', 'C']);
});
it('splice() remove 2 elements from index 1', function() {
const result = new ImmutableArray(abc).splice(1, 2);
expect(result).toEqual(['A']);
});
});
/**
* Immutable array manipulations.
* These functions don't mutate the orginal array but return a new one.
*
* inspired by https://twitter.com/lukejacksonn/status/928244319760220160
*/
export class ImmutableArray {
constructor(private arr: any[]) { }
/**
* Creates a shallow copy of the array
*/
clone = () => [...this.arr];
/**
* Adds one element to the end of the array
*/
push = newElement => [...this.arr, newElement];
/**
* Removes the last element from the array
*/
pop = () => this.arr.slice(0, -1);
/**
* Adds one elements to the front of the array
*/
unshift = (newElement) => [newElement, ...this.arr];
/**
* Removes the first element from the array
*/
shift = () => this.arr.slice(1);
/**
* Sorts the elements of an array
*/
sort = compareFn => this.clone().sort(compareFn);
/**
* Removes an element by index position
*/
delete = index => [...this.arr.slice(0, index), ...this.arr.slice(index + 1)];
/**
* Removes existing elements and/or adds new elements
*
* @param start Index at which to start changing the array
* @param deleteCount An integer indicating the number of old array elements to remove.
* @param elements The elements to add to the array, beginning at the start index.
* If you don't specify any elements, splice() will only remove elements from the array.
*/
splice = (start, deleteCount, ...elements) => [
...this.arr.slice(0, start),
...elements,
...this.arr.slice(start + deleteCount)
]
}
@JohannesHoppe
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment