-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pref(deps): remove lodash and implement lodash functions used in plug…
…in (#9) * pref(deps): remove lodash and implement lodash functions used in plugin pure js --------- Co-authored-by: Hossein Mirazimi <h.mirazimi@alibaba.ir>
- Loading branch information
1 parent
8a16f6f
commit 0901868
Showing
5 changed files
with
85 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function reduce(collection, item, initialVal) { | ||
if (!collection) return [] | ||
return Object.keys(collection).reduce( | ||
(carry, current, index, array) => | ||
item( | ||
carry, | ||
!Array.isArray(collection) ? collection[current] : current, | ||
!Array.isArray(collection) ? current : array, | ||
index | ||
Check failure on line 9 in src/util/lodash-fns.js
|
||
), | ||
initialVal | ||
) | ||
} | ||
|
||
function baseFlatten(array, depth) { | ||
const result = [] | ||
if (!array) { | ||
return result | ||
} | ||
|
||
for (const value of array) { | ||
if (depth && Array.isArray(value)) { | ||
if (depth > 1) { | ||
// Recursively flatten arrays (susceptible to call stack limits). | ||
baseFlatten(value, depth - 1) | ||
} else { | ||
result.push(...value) | ||
} | ||
} else { | ||
result[result.length] = value | ||
} | ||
} | ||
return result | ||
} | ||
|
||
function flatMap(arr, mapper) { | ||
if (!arr) return [] | ||
return baseFlatten( | ||
Object.keys(arr).map((value, index) => mapper(arr[value], !Array.isArray(arr) ? value : index)), | ||
1 | ||
) | ||
} | ||
|
||
function uniq(arr) { | ||
return [...new Set(arr)] | ||
} | ||
|
||
module.exports = { reduce, flatMap, uniq } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters