-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
92 lines (77 loc) · 1.88 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
var parseCopyright = require('parse-copyright');
var utils = require('lazy-cache')(require);
var fn = require;
require = utils;
/**
* Lazily required module dependencies
*/
require('delete', 'del');
require('engine', 'Engine');
require('extend-shallow', 'extend');
require('is-valid-app', 'isValid');
require('parser-front-matter', 'parser');
require('through2', 'through');
require('update-copyright', 'copyright');
require('year');
require = fn;
utils.parse = function(str) {
var sections = ['MIT License', 'Permission is hereby granted'];
var lines = str.trim().split('\n');
var tok = { prefix: '', authors: [] };
var len = lines.length;
var idx = -1;
var first = lines[0];
if (contains(sections[0], first)) {
tok.prefix = lines.shift();
len--;
}
while (++idx < len) {
var line = lines[idx];
if (!line.trim()) continue;
if (contains(sections[1], line)) {
tok.license = lines.slice(idx).join('\n');
break;
}
tok.authors = tok.authors.concat(parseCopyright(line.trim()));
}
tok.authors = arrayify(tok.authors);
var latest = null;
var num = 0;
if (tok.authors.length > 1) {
tok.authors.sort(function(a, b) {
var anum = Number(a.latest);
var bnum = Number(b.latest);
if (anum > num) {
latest = a;
num = anum;
}
if (bnum > num) {
latest = b;
num = bnum;
}
if (anum < bnum) return 1;
if (anum > bnum) return -1;
return 0;
});
if (latest) {
tok.latest = latest;
}
} else {
tok.latest = tok.authors[0];
}
return tok;
};
function contains(a, b) {
return compact(b).indexOf(compact(a)) !== -1;
}
function compact(str) {
return str.replace(/\s/g, '').toLowerCase();
}
function arrayify(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
}
/**
* Expose `utils` modules
*/
module.exports = utils;