morphir-elm
Version:
Elm bindings for Morphir
84 lines (83 loc) • 2.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const immutable_1 = require("immutable");
const backed_structure_1 = __importDefault(require("./backed-structure"));
/**
* This module has a bunch of functions to help you work with morphir Lists!
*/
class List extends backed_structure_1.default {
/**
* Create an empty list.
*/
static empty() {
return new List();
}
/**
* Create a list from an iterable.
* @param iterable the iterable to create the list from.
* @returns a new list containing the elements from the iterable.
*/
static from(iterable) {
return new List(iterable);
}
constructor(iterable) {
super((0, immutable_1.List)(iterable));
}
/**
* Get the length of this list.
*/
get length() {
return this.struct.size;
}
/**
* Get an iterator for this list.
*/
get iterator() {
return this.struct.values();
}
/**
* Adds a value to the end of this list.
* @param value the value to append.
* @returns a new list with the value appended.
*/
append(value) {
return new List(this.struct.push(value));
}
/**
* Adds a value to the start of this list.
* @param value the value to prepend.
* @returns a new list with the value prepended.
*/
prepend(value) {
return new List(this.struct.unshift(value));
}
/**
* Removes the last value from this list.
* @returns a new list with the last value removed.
*/
concat(...list) {
return new List(list.reduce((acc, next) => acc.concat(next.struct), this.struct));
}
/**
* Reduce this list from the left.
* @param reducer reducer function
* @param initialValue starting value
* @returns the reduced value
*/
foldl(reducer, initialValue) {
return this.struct.reduce(reducer, initialValue);
}
equals(other) {
return this.struct.equals(other.struct);
}
hashCode() {
return this.struct.hashCode();
}
toString() {
return `List(${JSON.stringify(this.struct.toJS())})`;
}
}
exports.default = List;