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 |
|
40 | function _uniq(options, input, output) {
|
41 |
|
42 | var pipe = common.readFromPipe();
|
43 |
|
44 | if (!pipe) {
|
45 | if (!input) common.error('no input given');
|
46 |
|
47 | if (!fs.existsSync(input)) {
|
48 | common.error(input + ': No such file or directory');
|
49 | } else if (common.statFollowLinks(input).isDirectory()) {
|
50 | common.error("error reading '" + input + "'");
|
51 | }
|
52 | }
|
53 | if (output && fs.existsSync(output) && common.statFollowLinks(output).isDirectory()) {
|
54 | common.error(output + ': Is a directory');
|
55 | }
|
56 |
|
57 | var lines = (input ? fs.readFileSync(input, 'utf8') : pipe).
|
58 | trimRight().
|
59 | split('\n');
|
60 |
|
61 | var compare = function (a, b) {
|
62 | return options.ignoreCase ?
|
63 | a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase()) :
|
64 | a.localeCompare(b);
|
65 | };
|
66 | var uniqed = lines.reduceRight(function (res, e) {
|
67 |
|
68 | if (res.length === 0) {
|
69 | return [{ count: 1, ln: e }];
|
70 | } else if (compare(res[0].ln, e) === 0) {
|
71 | return [{ count: res[0].count + 1, ln: e }].concat(res.slice(1));
|
72 | } else {
|
73 | return [{ count: 1, ln: e }].concat(res);
|
74 | }
|
75 | }, []).filter(function (obj) {
|
76 |
|
77 | return options.duplicates ? obj.count > 1 : true;
|
78 | }).map(function (obj) {
|
79 |
|
80 | return (options.count ? (lpad(7, obj.count) + ' ') : '') + obj.ln;
|
81 | }).join('\n') + '\n';
|
82 |
|
83 | if (output) {
|
84 | (new common.ShellString(uniqed)).to(output);
|
85 |
|
86 | return '';
|
87 | } else {
|
88 | return uniqed;
|
89 | }
|
90 | }
|
91 |
|
92 | module.exports = _uniq;
|