UNPKG

2.56 kBJavaScriptView Raw
1var common = require('./common');
2var fs = require('fs');
3
4// add c spaces to the left of str
5function 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
13common.register('uniq', _uniq, {
14 canReceivePipe: true,
15 cmdOptions: {
16 'i': 'ignoreCase',
17 'c': 'count',
18 'd': 'duplicates',
19 },
20});
21
22//@
23//@ ### uniq([options,] [input, [output]])
24//@
25//@ Available options:
26//@
27//@ + `-i`: Ignore case while comparing
28//@ + `-c`: Prefix lines by the number of occurrences
29//@ + `-d`: Only print duplicate lines, one for each group of identical lines
30//@
31//@ Examples:
32//@
33//@ ```javascript
34//@ uniq('foo.txt');
35//@ uniq('-i', 'foo.txt');
36//@ uniq('-cd', 'foo.txt', 'bar.txt');
37//@ ```
38//@
39//@ Filter adjacent matching lines from `input`.
40function _uniq(options, input, output) {
41 // Check if this is coming from a pipe
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 // Perform uniq -c on the input
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 // Do we want only duplicated objects?
77 return options.duplicates ? obj.count > 1 : true;
78 }).map(function (obj) {
79 // Are we tracking the counts of each line?
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 // if uniq writes to output, nothing is passed to the next command in the pipeline (if any)
86 return '';
87 } else {
88 return uniqed;
89 }
90}
91
92module.exports = _uniq;