-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
221 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use strict"; | ||
|
||
const _ = require("lodash"); | ||
|
||
function joinScripts(acc) { | ||
if (acc.current) { | ||
acc.scripts.push( | ||
acc.src | ||
? acc.current | ||
: acc.current | ||
.map(x => { | ||
x = _.trim(x); | ||
return x.endsWith(";") ? x : `${x};`; | ||
}) | ||
.join("\n\n") | ||
); | ||
acc.current = undefined; | ||
} | ||
} | ||
|
||
module.exports = function groupScripts(data) { | ||
const output = data.filter(x => x).reduce((acc, x) => { | ||
const update = src => { | ||
if (acc.src !== src || !acc.current) { | ||
joinScripts(acc); | ||
acc.current = [x]; | ||
acc.src = src; | ||
} else { | ||
acc.current.push(x); | ||
} | ||
}; | ||
|
||
update(!!x.src); | ||
|
||
return acc; | ||
}, { src: false, scripts: [] }); | ||
|
||
joinScripts(output); | ||
|
||
return output; | ||
}; |
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
68 changes: 68 additions & 0 deletions
68
packages/electrode-react-webapp/test/spec/group-scripts.spec.js
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,68 @@ | ||
"use strict"; | ||
|
||
const data = [ | ||
"a", | ||
"b", | ||
"c", | ||
{ src: 1 }, | ||
{ src: 5, defer: 1 }, | ||
{ src: 2 }, | ||
"d", | ||
"e", | ||
"f", | ||
"g", | ||
{ src: 3 }, | ||
"h", | ||
"i", | ||
"j", | ||
{ | ||
src: 4, | ||
async: 1 | ||
}, | ||
{ src: 10 }, | ||
"k", | ||
"l", | ||
"m" | ||
]; | ||
|
||
const groupScripts = require("../../lib/group-scripts"); | ||
const chai = require("chai"); | ||
|
||
describe("group-scripts", function() { | ||
it("should group scripts", () => { | ||
const expected = [ | ||
"a;\n\nb;\n\nc;", | ||
[ | ||
{ | ||
src: 1 | ||
}, | ||
{ | ||
src: 5, | ||
defer: 1 | ||
}, | ||
{ | ||
src: 2 | ||
} | ||
], | ||
"d;\n\ne;\n\nf;\n\ng;", | ||
[ | ||
{ | ||
src: 3 | ||
} | ||
], | ||
"h;\n\ni;\n\nj;", | ||
[ | ||
{ | ||
src: 4, | ||
async: 1 | ||
}, | ||
{ | ||
src: 10 | ||
} | ||
], | ||
"k;\n\nl;\n\nm;" | ||
]; | ||
const result = groupScripts(data); | ||
chai.expect(result.scripts).to.deep.equal(expected); | ||
}); | ||
}); |
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