/* * Squidex Headless CMS * * @license * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. */ // tslint:disable: readonly-array interface ReadonlyArray { replaceBy(field: string, value: T): ReadonlyArray; removeBy(field: string, value: T): ReadonlyArray; removed(value?: T): ReadonlyArray; sorted(): ReadonlyArray; sortedByString(selector: (value: T) => string): ReadonlyArray; } interface Array { replaceBy(field: string, value: T): Array; removeBy(field: string, value: T): Array; removed(value?: T): Array; sorted(): Array; sortedByString(selector: (value: T) => string): Array; } Array.prototype.replaceBy = function(field: string, value: T) { if (!value) { return this; } return this.map((v: T) => v[field] === value[field] ? value : v); }; Array.prototype.removeBy = function(field: string, value: T) { if (!value) { return this; } return this.filter((v: T) => v[field] !== value[field]); }; Array.prototype.removed = function(value?: T) { if (!value) { return this; } return this.filter((v: T) => v !== value); }; Array.prototype.sorted = function() { const copy = [...this]; copy.sort(); return copy; }; Array.prototype.sortedByString = function(selector: (value: T) => string) { const copy = [...this]; copy.sort((a, b) => selector(a).localeCompare(selector(b), undefined, { sensitivity: 'base' })); return copy; };