2016-11-03 01:33:53 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var filter = require('through2-filter').obj;
|
|
|
|
var stringify = require("json-stable-stringify");
|
|
|
|
|
|
|
|
var ES6Set;
|
|
|
|
if (typeof global.Set === 'function') {
|
|
|
|
ES6Set = global.Set;
|
|
|
|
} else {
|
|
|
|
ES6Set = function() {
|
|
|
|
this.keys = [];
|
|
|
|
this.has = function(val) {
|
|
|
|
return this.keys.indexOf(val) !== -1;
|
|
|
|
},
|
|
|
|
this.add = function(val) {
|
|
|
|
this.keys.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
|
|
|
|
function prop(propName) {
|
|
|
|
return function (data) {
|
|
|
|
return data[propName];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = unique;
|
2016-11-03 01:33:53 +01:00
|
|
|
function unique(propName, keyStore) {
|
|
|
|
keyStore = keyStore || new ES6Set();
|
|
|
|
|
|
|
|
var keyfn = stringify;
|
2016-10-10 03:43:44 +02:00
|
|
|
if (typeof propName === 'string') {
|
|
|
|
keyfn = prop(propName);
|
|
|
|
} else if (typeof propName === 'function') {
|
|
|
|
keyfn = propName;
|
|
|
|
}
|
|
|
|
|
2016-11-03 01:33:53 +01:00
|
|
|
return filter(function (data) {
|
2016-10-10 03:43:44 +02:00
|
|
|
var key = keyfn(data);
|
|
|
|
|
2016-11-03 01:33:53 +01:00
|
|
|
if (keyStore.has(key)) {
|
|
|
|
return false;
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
|
2016-11-03 01:33:53 +01:00
|
|
|
keyStore.add(key);
|
|
|
|
return true;
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
|
|
|
}
|