|
1 | | -module.exports = file => { |
2 | | - const ignoreList = file.content.split('\n') |
3 | | - const projectIgnoreList = file.targetFile.content.split('\n') |
| 1 | +const defaultBucketName = Symbol() |
| 2 | +const classifyIgnore = list => { |
| 3 | + let last = { name: defaultBucketName, values: [] } |
| 4 | + const bucket = [last] |
4 | 5 |
|
| 6 | + list.forEach(item => { |
| 7 | + if (/^#/.test(item)) { |
| 8 | + const name = item.substring(1) |
| 9 | + /** comment adjacent comments */ |
| 10 | + if (!last.values.length && last.name !== defaultBucketName) { |
| 11 | + last.name = `${last.name}\n${name}` |
| 12 | + return |
| 13 | + } |
5 | 14 |
|
6 | | - let mergedList = [] |
7 | | - let lastIgnoreListIndex = 0 |
8 | | - let lastProjectIgnoreListIndex = 0 |
9 | | - |
10 | | - ignoreList.forEach((item, index) => { |
11 | | - let i = projectIgnoreList |
12 | | - .slice(lastProjectIgnoreListIndex) |
13 | | - .findIndex(value => value === item) |
14 | | - i = i === -1 ? -1 : i + lastProjectIgnoreListIndex |
15 | | - |
16 | | - if (i !== -1 && lastProjectIgnoreListIndex < i) { |
17 | | - mergedList.push(...ignoreList.slice(lastIgnoreListIndex, index)) |
18 | | - |
19 | | - const lastProjectListFragment = projectIgnoreList |
20 | | - .slice(lastProjectIgnoreListIndex, i) |
21 | | - .filter(item => !ignoreList.includes(item) && item.length) |
22 | | - mergedList.push(...lastProjectListFragment) |
23 | | - |
24 | | - lastIgnoreListIndex = index |
25 | | - lastProjectIgnoreListIndex = i |
26 | | - } else if (index === ignoreList.length - 1) { |
27 | | - mergedList.push(...ignoreList.slice(lastIgnoreListIndex)) |
28 | | - const lastProjectListFragment = projectIgnoreList |
29 | | - .slice(lastProjectIgnoreListIndex) |
30 | | - .filter(item => !ignoreList.includes(item) && item.length) |
31 | | - mergedList.push(...lastProjectListFragment) |
| 15 | + const pair = bucket.find(item => item.name === name) |
| 16 | + if (pair) { |
| 17 | + last = pair |
| 18 | + } else { |
| 19 | + last = { name, values: [] } |
| 20 | + bucket.push(last) |
| 21 | + } |
| 22 | + } else if (item) { |
| 23 | + if (!last.values.includes(item)) last.values.push(item) |
32 | 24 | } |
33 | 25 | }) |
34 | 26 |
|
35 | | - if (lastIgnoreListIndex < projectIgnoreList.length) { |
36 | | - const lastProjectListFragment = projectIgnoreList |
37 | | - .slice(lastProjectIgnoreListIndex, projectIgnoreList.length) |
38 | | - .filter(item => !ignoreList.includes(item) && item.length) |
39 | | - mergedList.push(...lastProjectListFragment) |
40 | | - } |
| 27 | + return bucket |
| 28 | +} |
| 29 | + |
| 30 | +const mergeBucket = (b1, b2) => { |
| 31 | + const bucket = b1.map(item => ({ ...item })) |
| 32 | + |
| 33 | + b2.forEach(pair => { |
| 34 | + const sameOne = bucket.find(item => item.name === pair.name) |
| 35 | + if (!sameOne) { |
| 36 | + bucket.push(pair) |
| 37 | + } else { |
| 38 | + pair.values.forEach(value => { |
| 39 | + if (!(sameOne.values.includes(value))) sameOne.values = sameOne.values.concat(value) |
| 40 | + }) |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + return bucket |
| 45 | +} |
| 46 | + |
| 47 | +const uniqBucket = (b1, b2) => { |
| 48 | + const allValues = [].concat(...b2.map(item => item.values)) |
| 49 | + return b1.map(pair => { |
| 50 | + const values = pair.values.filter(item => !allValues.includes(item)) |
| 51 | + return { name: pair.name, values } |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +const renderBucket = bucket => bucket |
| 56 | + .filter(pair => (pair.name !== defaultBucketName || pair.values.length)) |
| 57 | + .map(({ name, values }) => { |
| 58 | + let str = '' |
| 59 | + if (typeof name === 'string') str += name.replace(/^(.*)(\n|$)/mg, '#$1\n') |
| 60 | + str += values.join('\n') |
| 61 | + |
| 62 | + return str |
| 63 | + }) |
| 64 | + .join('\n\n') |
| 65 | + |
| 66 | +module.exports = file => { |
| 67 | + const templateIgnoreList = file.content.split('\n') |
| 68 | + const projectIgnoreList = file.targetFile.content.split('\n') |
| 69 | + |
| 70 | + const templateIgnoreBucket = classifyIgnore(templateIgnoreList) |
| 71 | + const projectIgnoreBucket = classifyIgnore(projectIgnoreList) |
| 72 | + |
| 73 | + const uniquedBucket = uniqBucket(projectIgnoreBucket, templateIgnoreBucket) |
41 | 74 |
|
42 | | - mergedList = mergedList.reduce((list, item) => { |
43 | | - if (!item.length || !list.includes(item)) list.push(item) |
44 | | - return list |
45 | | - }, []) |
| 75 | + const bucket = mergeBucket(templateIgnoreBucket, uniquedBucket) |
| 76 | + const result = renderBucket(bucket) |
46 | 77 |
|
| 78 | + const beginBlank = file.content.match(/^\s*/g)[0] |
| 79 | + const endBlank = file.content.match(/\s*$/g)[0] |
47 | 80 |
|
48 | 81 | return { |
49 | 82 | ...file, |
50 | | - content: mergedList.join('\n'), |
| 83 | + content: `${beginBlank}${result}${endBlank}`, |
51 | 84 | } |
52 | 85 | } |
0 commit comments