1 | var common = require('./common');
|
2 | var fs = require('fs');
|
3 |
|
4 |
|
5 | function lpad(c, str) {
|
6 | var res = '' + str;
|
7 | if (res.length < c) {
|
8 | res = Array((c - res.length) + 1).join(' ') + res;
|
9 | }
|
10 | return res;
|
11 | }
|
12 |
|
13 | common.register('uniq', _uniq, {
|
14 | canReceivePipe: true,
|
15 | cmdOptions: {
|
16 | 'i': 'ignoreCase',
|
17 | 'c': 'count',
|
18 | 'd': 'duplicates',
|
19 | },
|
20 | });
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function _uniq(options, input, output) {
|
40 |
|
41 | var pipe = common.readFromPipe();
|
42 |
|
43 | if (!pipe) {
|
44 | if (!input) common.error('no input given');
|
45 |
|
46 | if (!fs.existsSync(input)) {
|
47 | common.error(input + ': No such file or directory');
|
48 | } else if (common.statFollowLinks(input).isDirectory()) {
|
49 | common.error("error reading '" + input + "'");
|
50 | }
|
51 | }
|
52 | if (output && fs.existsSync(output) && common.statFollowLinks(output).isDirectory()) {
|
53 | common.error(output + ': Is a directory');
|
54 | }
|
55 |
|
56 | var lines = (input ? fs.readFileSync(input, 'utf8') : pipe).
|
57 | trimRight().
|
58 | split('\n');
|
59 |
|
60 | var compare = function (a, b) {
|
61 | return options.ignoreCase ?
|
62 | a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase()) :
|
63 | a.localeCompare(b);
|
64 | };
|
65 | var uniqed = lines.reduceRight(function (res, e) {
|
66 |
|
67 | if (res.length === 0) {
|
68 | return [{ count: 1, ln: e }];
|
69 | } else if (compare(res[0].ln, e) === 0) {
|
70 | return [{ count: res[0].count + 1, ln: e }].concat(res.slice(1));
|
71 | } else {
|
72 | return [{ count: 1, ln: e }].concat(res);
|
73 | }
|
74 | }, []).filter(function (obj) {
|
75 |
|
76 | return options.duplicates ? obj.count > 1 : true;
|
77 | }).map(function (obj) {
|
78 |
|
79 | return (options.count ? (lpad(7, obj.count) + ' ') : '') + obj.ln;
|
80 | }).join('\n') + '\n';
|
81 |
|
82 | if (output) {
|
83 | (new common.ShellString(uniqed)).to(output);
|
84 |
|
85 | return '';
|
86 | } else {
|
87 | return uniqed;
|
88 | }
|
89 | }
|
90 |
|
91 | module.exports = _uniq;
|