1 | 'use strict';
|
2 |
|
3 | var _ = require('lodash');
|
4 |
|
5 |
|
6 | var headerTemplate = [
|
7 | '/**',
|
8 | ' * @license',
|
9 | ' * lodash lodash.com/license | Underscore.js <%= underscore.VERSION %> underscorejs.org/LICENSE',
|
10 | ' */'
|
11 | ].join('\n');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function postprocess(source, isMapped) {
|
23 |
|
24 | source = source.replace(/[\s;]*(\n\s*\/\/.*)?\s*$/, ';$1');
|
25 |
|
26 |
|
27 | if (isMapped) {
|
28 | return source;
|
29 | }
|
30 |
|
31 | var header = _.get(/^\/\**[\s\S]+?\*\/\n/.exec(source), 0, '');
|
32 | source = source.replace(header, '');
|
33 |
|
34 |
|
35 | source = _.template(headerTemplate)({
|
36 | 'underscore': { 'VERSION': _.get(/\bUnderscore\.js ([.$\w\-]+)/i.exec(header), 1, '') }
|
37 | }) + '\n;' + source;
|
38 |
|
39 |
|
40 | source = source.replace(/(.)(\![01])\b/g, function(match, chr, exp) {
|
41 | return chr + (/[$\w]/.test(chr) ? ' ' : '') + (exp == '!0');
|
42 | });
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | source = source.replace(/([$\w])?("[^"]+")\s*([!=]=)\s*(typeof(?:\s*\([^)]+\)|\s+[$\w]+(?:\s*\.\s*[$\w]+)*([[(])?))/g, function(match, left, type, equality, exp, right) {
|
48 | return right ? match : (left ? left + ' ' : '') + exp + equality + type;
|
49 | });
|
50 |
|
51 |
|
52 | source = source.replace(/(.)(define\()/, function(match, prelude, define) {
|
53 | return prelude + (/^\S/.test(prelude) ? ' ' : '') + define;
|
54 | });
|
55 |
|
56 |
|
57 | return source.replace(/\n{2,}/g, '\n');
|
58 | }
|
59 |
|
60 | module.exports = postprocess;
|