Skip to content

Commit ec18c0a

Browse files
Issue #69 Consider removing peer dependency usage directly in exported objects
Port lodash methods to private functions. Issue #69
2 parents 49bd9e1 + ac3410b commit ec18c0a

3 files changed

Lines changed: 382 additions & 84 deletions

File tree

src/utilities/collection-utils.ts

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import * as Immutable from "immutable";
22
import _ from "lodash";
3+
import {
4+
Comparator2,
5+
Dictionary,
6+
List,
7+
ListOfRecursiveArraysOrValues,
8+
NumericDictionary
9+
} from "lodash";
310

411
// -----------------------------------------------------------------------------------------
512
// #region Private Methods
@@ -16,7 +23,7 @@ import _ from "lodash";
1623
* @returns true if both arrays contain all the same elements of the other,
1724
* not considering order, false otherwise.
1825
*/
19-
const _equalsBy = function<T, V>(
26+
const equalsBy = function<T, V>(
2027
selector: (element: T) => V,
2128
array1: Array<T> | Immutable.List<any> | undefined,
2229
array2: Array<T> | Immutable.List<any> | undefined
@@ -29,7 +36,7 @@ const _equalsBy = function<T, V>(
2936
return false;
3037
}
3138

32-
if (_length(array1) !== _length(array2)) {
39+
if (length(array1) !== length(array2)) {
3340
return false;
3441
}
3542

@@ -52,9 +59,9 @@ const _equalsBy = function<T, V>(
5259
* @param values The arrays of values to exclude.
5360
* @return Returns the new array of filtered values.
5461
*/
55-
const _difference = <T>(
56-
array: Array<T> | null | undefined,
57-
...values: Array<Array<T>>
62+
const difference = <T>(
63+
array: List<T> | null | undefined,
64+
...values: Array<List<T>>
5865
): T[] => _.difference(array, ...values);
5966

6067
/**
@@ -63,7 +70,7 @@ const _difference = <T>(
6370
* @param array The array to recursively flatten.
6471
* @return Returns the new flattened array.
6572
*/
66-
const _flattenDeep = <T>(array: Array<T> | null | undefined): T[] =>
73+
const flattenDeep = <T>(array: ListOfRecursiveArraysOrValues<T> | null | undefined): T[] =>
6774
_.flattenDeep(array);
6875

6976
/**
@@ -77,13 +84,13 @@ const _flattenDeep = <T>(array: Array<T> | null | undefined): T[] =>
7784
* @returns {boolean} False if `collections` is null/undefined, or every element is also null/undefined,
7885
* or has no sub-elements. True if any element has sub-elements.
7986
*/
80-
const _hasValues = (
87+
const hasValues = (
8188
...collections: Array<any[] | Immutable.List<any> | undefined>
8289
): boolean => {
8390
let hasValues = false;
8491
collections.forEach(
8592
(collection: any[] | Immutable.List<any> | undefined) => {
86-
if (!_isEmpty(collection)) {
93+
if (!isEmpty(collection)) {
8794
hasValues = true;
8895
}
8996
}
@@ -102,7 +109,7 @@ const _hasValues = (
102109
* @returns {boolean} True if `collections` is null/undefined, or every element is also null/undefined,
103110
* or has no sub-elements. False if any element has sub-elements.
104111
*/
105-
const _isEmpty = (
112+
const isEmpty = (
106113
...collections: Array<any[] | Immutable.List<any> | undefined>
107114
): boolean => {
108115
let isEmpty = true;
@@ -140,10 +147,10 @@ const _isEmpty = (
140147
* @returns {boolean} False if `collections` is null/undefined, or every element is also null/undefined,
141148
* or has no sub-elements. True if any element has sub-elements.
142149
*/
143-
const _isNotEmpty = (
150+
const isNotEmpty = (
144151
...collections: Array<any[] | Immutable.List<any> | undefined>
145152
): boolean => {
146-
return !_isEmpty(...collections);
153+
return !isEmpty(...collections);
147154
};
148155

149156
/**
@@ -152,7 +159,7 @@ const _isNotEmpty = (
152159
* @param arr the collection
153160
* @returns number the length of the collection
154161
*/
155-
const _length = (arr: Array<any> | Immutable.List<any>): number => {
162+
const length = (arr: Array<any> | Immutable.List<any>): number => {
156163
if (arr == null) {
157164
return -1;
158165
}
@@ -172,7 +179,7 @@ const _length = (arr: Array<any> | Immutable.List<any>): number => {
172179
* @param array The array to query.
173180
* @return Returns the first element of array.
174181
*/
175-
const _head = <T>(array: Array<T> | null | undefined): T | undefined =>
182+
const first = <T>(array: List<T> | null | undefined): T | undefined =>
176183
_.head(array);
177184

178185
/**
@@ -182,7 +189,7 @@ const _head = <T>(array: Array<T> | null | undefined): T | undefined =>
182189
* @param arrays The arrays to inspect.
183190
* @return Returns the new array of shared values.
184191
*/
185-
const _intersection = <T>(...arrays: Array<Array<T>>) =>
192+
const intersection = <T>(...arrays: Array<List<T>>) =>
186193
_.intersection(...arrays);
187194

188195
/**
@@ -203,18 +210,18 @@ const _intersection = <T>(...arrays: Array<Array<T>>) =>
203210
* _.intersectionWith(objects, others, _.isEqual);
204211
* => [{ 'x': 1, 'y': 2 }]
205212
*/
206-
const _intersectionWith = <T1, T2>(
207-
array: Array<T1>,
208-
values: Array<T2>,
209-
comparator: (a: T1, b: T2) => boolean
213+
const intersectionWith = <T1, T2>(
214+
array: List<T1>,
215+
values: List<T2>,
216+
comparator: Comparator2<T1, T2>
210217
): T1[] => _.intersectionWith(array, values, comparator);
211218

212219
/**
213220
* Removes a supplied element by index
214221
* @param source original array
215222
* @param index array index to remove
216223
*/
217-
const _removeElementAt = <T>(source: Array<T>, index: number): Array<T> => {
224+
const removeElementAt = <T>(source: Array<T>, index: number): Array<T> => {
218225
if (index < 0 || index > source.length) {
219226
return source;
220227
}
@@ -234,7 +241,7 @@ const _removeElementAt = <T>(source: Array<T>, index: number): Array<T> => {
234241
* @param index
235242
* @param value
236243
*/
237-
const _replaceElementAt = <T>(
244+
const replaceElementAt = <T>(
238245
source: Array<T>,
239246
index: number,
240247
value: T
@@ -259,7 +266,7 @@ const _replaceElementAt = <T>(
259266
* @param collection The collection to sample.
260267
* @return Returns the random element.
261268
*/
262-
const _sample = <T>(collection: Array<T> | null | undefined): T | undefined =>
269+
const sample = <T>(collection: List<T> | Dictionary<T> | NumericDictionary<T> | null | undefined): T | undefined =>
263270
_.sample(collection);
264271

265272
/**
@@ -269,8 +276,8 @@ const _sample = <T>(collection: Array<T> | null | undefined): T | undefined =>
269276
* @param n The number of elements to sample.
270277
* @return Returns the random elements.
271278
*/
272-
const _sampleSize = <T>(
273-
collection: Array<T> | null | undefined,
279+
const sampleSize = <T>(
280+
collection: List<T> | Dictionary<T> | NumericDictionary<T> | null | undefined,
274281
n?: number
275282
): T[] => _.sampleSize(collection, n);
276283

@@ -280,7 +287,7 @@ const _sampleSize = <T>(
280287
* @param selector function returning property to sort by from item
281288
* @param caseSensitive whether to consider letter case when sorting
282289
*/
283-
const _sortByString = <T extends any>(
290+
const sortByString = <T extends any>(
284291
array: Array<T>,
285292
selector: (element: T) => string,
286293
caseSensitive: boolean = false
@@ -320,7 +327,7 @@ const _sortByString = <T extends any>(
320327
* @param n The number of elements to take.
321328
* @return Returns the slice of array.
322329
*/
323-
const _take = <T>(array: Array<T> | null | undefined, n?: number): T[] =>
330+
const take = <T>(array: List<T> | null | undefined, n?: number): T[] =>
324331
_.take(array, n);
325332

326333
// #endregion Private Methods
@@ -330,22 +337,22 @@ const _take = <T>(array: Array<T> | null | undefined, n?: number): T[] =>
330337
// -----------------------------------------------------------------------------------------
331338

332339
export const CollectionUtils = {
333-
difference: _difference,
334-
equalsBy: _equalsBy,
335-
first: _head,
336-
flattenDeep: _flattenDeep,
337-
hasValues: _hasValues,
338-
isEmpty: _isEmpty,
339-
isNotEmpty: _isNotEmpty,
340-
intersection: _intersection,
341-
intersectionWith: _intersectionWith,
342-
length: _length,
343-
removeElementAt: _removeElementAt,
344-
replaceElementAt: _replaceElementAt,
345-
sample: _sample,
346-
sampleSize: _sampleSize,
347-
sortByString: _sortByString,
348-
take: _take,
340+
difference,
341+
equalsBy,
342+
first,
343+
flattenDeep,
344+
hasValues,
345+
isEmpty,
346+
isNotEmpty,
347+
intersection,
348+
intersectionWith,
349+
length,
350+
removeElementAt,
351+
replaceElementAt,
352+
sample,
353+
sampleSize,
354+
sortByString,
355+
take,
349356
};
350357

351358
// #endregion Exports

0 commit comments

Comments
 (0)