A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/signumsoftware/framework/commit/91330aad5405df50cd7cc8fc42de36bbfc759b70 below:

add elementSelector to groupBy / groupToObject · signumsoftware/framework@91330aa · GitHub

File tree Expand file treeCollapse file tree 1 file changed

+6

-4

lines changed

Filter options

Expand file treeCollapse file tree 1 file changed

+6

-4

lines changed Original file line number Diff line number Diff line change

@@ -23,7 +23,9 @@ declare global {

23 23 24 24

interface Array<T> {

25 25

groupBy<K extends string | number>(this: Array<T>, keySelector: (element: T) => K): { key: K; elements: T[] }[];

26 +

groupBy<K extends string | number, E>(this: Array<T>, keySelector: (element: T) => K, elementSelector: (element: T) => E): { key: K; elements: E[] }[];

26 27

groupToObject(this: Array<T>, keySelector: (element: T) => string): { [key: string]: T[] };

28 +

groupToObject<E>(this: Array<T>, keySelector: (element: T) => string, elementSelector: (element: T) => E): { [key: string]: E[] };

27 29

groupWhen(this: Array<T>, condition: (element: T) => boolean, includeKeyInGroup?: boolean, initialGroup?: boolean): { key: T, elements: T[] }[];

28 30

groupWhenChange<K extends string | number>(this: Array<T>, keySelector: (element: T) => K): { key: K, elements: T[] }[];

29 31

@@ -137,25 +139,25 @@ Array.prototype.clear = function (): void {

137 139

this.length = 0;

138 140

};

139 141 140 -

Array.prototype.groupBy = function (this: any[], keySelector: (element: any) => string | number): { key: any /*string*/; elements: any[] }[] {

142 +

Array.prototype.groupBy = function (this: any[], keySelector: (element: any) => string | number, elementSelector?: (element: any) => unknown): { key: any /*string*/; elements: any[] }[] {

141 143

const result: { key: string | number; elements: any[] }[] = [];

142 -

const objectGrouped = this.groupToObject(keySelector as ((element: any) => string));

144 +

const objectGrouped = this.groupToObject(keySelector as ((element: any) => string), elementSelector!);

143 145

for (const prop in objectGrouped) {

144 146

if (objectGrouped.hasOwnProperty(prop))

145 147

result.push({ key: prop, elements: objectGrouped[prop] });

146 148

}

147 149

return result;

148 150

};

149 151 150 -

Array.prototype.groupToObject = function (this: any[], keySelector: (element: any) => string): { [key: string]: any[] } {

152 +

Array.prototype.groupToObject = function (this: any[], keySelector: (element: any) => string, elementSelector?: (element: any) => unknown): { [key: string]: any[] } {

151 153

const result: { [key: string]: any[] } = {};

152 154 153 155

for (let i = 0; i < this.length; i++) {

154 156

const element: any = this[i];

155 157

const key = keySelector(element);

156 158

if (!result[key])

157 159

result[key] = [];

158 -

result[key].push(element);

160 +

result[key].push(elementSelector ? elementSelector(element) : element);

159 161

}

160 162

return result;

161 163

};

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4