-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adaptation of @tsing80’s node flattening merge tree
- Loading branch information
1 parent
f588e5f
commit d474145
Showing
4 changed files
with
118 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
var ASSIMILATION_PROTOCOL_VERSION = '_1'; | ||
|
||
function canFlatten(node, options = { overwrite: false /* broccoli-merge-tree's default */ }) { | ||
const hasSameProtocol = node.ASSIMILATION_PROTOCOL_VERSION === ASSIMILATION_PROTOCOL_VERSION; | ||
const nodeOptions = node.options !== null && typeof node.options === 'object' ? node.options : { overwrite: false }; | ||
const hasSameOverwrite = nodeOptions.overwrite === options.overwrite; | ||
|
||
return hasSameProtocol && hasSameOverwrite; | ||
} | ||
|
||
function doFlatten(inputNodes, options) { | ||
var nodes = []; | ||
for (var i = 0 ; i < inputNodes.length ; i++ ) { | ||
var node = inputNodes[i]; | ||
|
||
if (canFlatten(node, options)) { | ||
nodes = nodes.concat(doFlatten(node._inputNodes, node.options)); | ||
} else { | ||
nodes.push(node); | ||
} | ||
} | ||
|
||
return nodes; | ||
} | ||
|
||
module.exports = function flattenNodes(inputNodes) { | ||
let seen = new Set(); | ||
let flattened = doFlatten(inputNodes); | ||
let result = []; | ||
|
||
for (let i = flattened.length - 1; i >= 0; i--) { | ||
let node = flattened[i]; | ||
|
||
if (seen.has(node) === false) { | ||
seen.add(node); | ||
result.push(node); | ||
} | ||
} | ||
|
||
return result.reverse(); | ||
}; | ||
|
||
module.exports.ASSIMILATION_PROTOCOL_VERSION = ASSIMILATION_PROTOCOL_VERSION; | ||
module.exports.canFlatten = canFlatten; |
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 |
---|---|---|
|
@@ -15,7 +15,8 @@ | |
"copy" | ||
], | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"flatten.js" | ||
], | ||
"engines": { | ||
"node": ">=6.0.0" | ||
|
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