Skip to content

Commit f6f072f

Browse files
committed
utils: add filterUnique helper
- use it in groupby transform and findArrayAttributes
1 parent 7e0a382 commit f6f072f

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed

src/lib/coerce.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var isNumeric = require('fast-isnumeric');
1313
var tinycolor = require('tinycolor2');
1414
var nestedProperty = require('./nested_property');
1515
var isPlainObject = require('./is_plain_object');
16+
var filterUnique = require('./filter_unique');
1617

1718
var getColorscale = require('../components/colorscale/get_scale');
1819
var colorscaleNames = Object.keys(require('../components/colorscale/scales'));
@@ -466,9 +467,7 @@ exports.findArrayAttributes = function(trace) {
466467
if(trace._fullInput) {
467468
exports.crawl(trace._fullInput._module.attributes, callback);
468469

469-
arrayAttributes = arrayAttributes.filter(function(g, i, self) {
470-
return self.indexOf(g) === i;
471-
});
470+
arrayAttributes = filterUnique(arrayAttributes);
472471
}
473472

474473
return arrayAttributes;

src/lib/filter_unique.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
13+
/**
14+
* Return news array containing only the unique items
15+
* found in input array.
16+
*
17+
* IMPORTANT: Note that items are considered unique
18+
* if `String({})` is unique. For example;
19+
*
20+
* Lib.filterUnique([ { a: 1 }, { b: 2 } ])
21+
*
22+
* returns [{ a: 1 }]
23+
*
24+
* and
25+
*
26+
* Lib.filterUnique([ '1', 1 ])
27+
*
28+
* returns ['1']
29+
*
30+
*
31+
* @param {array} array base array
32+
* @return {array} new filtered array
33+
*/
34+
module.exports = function filterUnique(array) {
35+
var seen = {},
36+
out = [],
37+
j = 0;
38+
39+
for(var i = 0; i < array.length; i++) {
40+
var item = array[i];
41+
42+
if(seen[item] !== 1) {
43+
seen[item] = 1;
44+
out[j++] = item;
45+
}
46+
}
47+
48+
return out;
49+
};

src/lib/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ lib.error = loggersModule.error;
7575

7676
lib.notifier = require('./notifier');
7777

78+
lib.filterUnique = require('./filter_unique');
79+
7880
/**
7981
* swap x and y of the same attribute in container cont
8082
* specify attr with a ? in place of x/y

src/transforms/groupby.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,9 @@ function transformOne(trace, state) {
121121
return trace;
122122
}
123123

124-
var groupNames = groups.filter(function(g, i, self) {
125-
return self.indexOf(g) === i;
126-
});
127-
128-
var newData = new Array(groupNames.length);
129-
var len = groups.length;
124+
var groupNames = Lib.filterUnique(groups),
125+
newData = new Array(groupNames.length),
126+
len = groups.length;
130127

131128
var arrayAttrs = Lib.findArrayAttributes(trace);
132129

test/jasmine/tests/lib_test.js

+31
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,37 @@ describe('Test lib.js:', function() {
13671367
});
13681368
});
13691369

1370+
describe('filterUnique', function() {
1371+
1372+
it('should return array containing unique values', function() {
1373+
expect(
1374+
Lib.filterUnique(['a', 'a', 'b', 'b'])
1375+
)
1376+
.toEqual(['a', 'b']);
1377+
1378+
expect(
1379+
Lib.filterUnique(['1', ['1'], 1])
1380+
)
1381+
.toEqual(['1']);
1382+
1383+
expect(
1384+
Lib.filterUnique([1, '1', [1]])
1385+
)
1386+
.toEqual([1]);
1387+
1388+
expect(
1389+
Lib.filterUnique([ { a: 1 }, { b: 2 }])
1390+
)
1391+
.toEqual([{ a: 1 }]);
1392+
1393+
expect(
1394+
Lib.filterUnique([null, undefined, null, null, undefined])
1395+
)
1396+
.toEqual([null, undefined]);
1397+
});
1398+
1399+
});
1400+
13701401
describe('numSeparate', function() {
13711402

13721403
it('should work on numbers and strings', function() {

0 commit comments

Comments
 (0)