Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: putout: ignore: add ability to merge negated (kaelzhang/node…
- Loading branch information
1 parent
7a4f7e5
commit 4a83bb8
Showing
2 changed files
with
65 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,39 @@ | ||
'use strict'; | ||
|
||
const deepmerge = require('deepmerge'); | ||
const arrayUnion = (...a) => [...new Set(a.flat())]; | ||
|
||
const isString = (a) => typeof a === 'string'; | ||
const arrayUnion = (...a) => { | ||
const flatten = a.flat(); | ||
return mergeIgnore(Array.from(new Set(flatten))); | ||
}; | ||
|
||
const arrayMerge = (a, b) => arrayUnion(b, a); | ||
|
||
module.exports = (...args) => { | ||
return deepmerge.all(args, { | ||
arrayMerge, | ||
}); | ||
}; | ||
|
||
function mergeIgnore(list) { | ||
const negatives = []; | ||
|
||
for (const current of list) { | ||
if (isString(current) && current.startsWith('!')) | ||
negatives.push(current.slice(1)); | ||
} | ||
|
||
for (const current of negatives) { | ||
const index = list.indexOf(current); | ||
|
||
if (index >= 0) { | ||
list.splice(index, 1); | ||
|
||
const negIndex = list.indexOf(`!${current}`); | ||
list.splice(negIndex, 1); | ||
} | ||
} | ||
|
||
return list; | ||
} |
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