mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-28 19:01:31 +02:00
Add util functions for set operations
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
|
||||
// http://2ality.com/2015/01/es6-set-operations.html
|
||||
|
||||
// Difference (a \ b): create a set that contains those elements of set a that are not in set b.
|
||||
// This operation is also sometimes called minus (-).
|
||||
// var a = [1,2,3];
|
||||
// var b = [4,3,2];
|
||||
// utilArrayDifference(a, b)
|
||||
// [1]
|
||||
// utilArrayDifference(b, a)
|
||||
// [4]
|
||||
export function utilArrayDifference(a, b) {
|
||||
var other = new Set(b);
|
||||
return Array.from(new Set(a))
|
||||
.filter(function(v) { return !other.has(v); });
|
||||
}
|
||||
|
||||
// Intersection (a ∩ b): create a set that contains those elements of set a that are also in set b.
|
||||
// var a = [1,2,3];
|
||||
// var b = [4,3,2];
|
||||
// utilArrayIntersection(a, b)
|
||||
// [2,3]
|
||||
export function utilArrayIntersection(a, b) {
|
||||
var other = new Set(b);
|
||||
return Array.from(new Set(a))
|
||||
.filter(function(v) { return other.has(v); });
|
||||
}
|
||||
|
||||
// Union (a ∪ b): create a set that contains the elements of both set a and set b.
|
||||
// var a = [1,2,3];
|
||||
// var b = [4,3,2];
|
||||
// utilArrayUnion(a, b)
|
||||
// [1,2,3,4]
|
||||
export function utilArrayUnion(a, b) {
|
||||
var result = new Set(a);
|
||||
b.forEach(function(v) { result.add(v); });
|
||||
return Array.from(result);
|
||||
}
|
||||
|
||||
// Returns an Array with all the duplicates removed
|
||||
// var a = [1,1,2,3,3];
|
||||
// utilArrayUniq(a)
|
||||
// [1,2,3]
|
||||
export function utilArrayUniq(a) {
|
||||
return Array.from(new Set(a));
|
||||
}
|
||||
@@ -1,3 +1,7 @@
|
||||
export { utilArrayDifference } from './array';
|
||||
export { utilArrayIntersection } from './array';
|
||||
export { utilArrayUnion } from './array';
|
||||
export { utilArrayUniq } from './array';
|
||||
export { utilAsyncMap } from './util';
|
||||
export { utilCallWhenIdle } from './call_when_idle';
|
||||
export { utilCleanTags } from './clean_tags';
|
||||
|
||||
@@ -143,8 +143,10 @@
|
||||
<script src='spec/ui/fields/localized.js'></script>
|
||||
<script src='spec/ui/fields/wikipedia.js'></script>
|
||||
|
||||
<script src='spec/util/array.js'></script>
|
||||
<script src='spec/util/clean_tags.js'></script>
|
||||
<script src='spec/util/keybinding.js'></script>
|
||||
<script src='spec/util/object.js'></script>
|
||||
<script src='spec/util/session_mutex.js'></script>
|
||||
<script src='spec/util/util.js'></script>
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
describe('iD.utilArrayDifference', function() {
|
||||
it('returns set difference', function() {
|
||||
var a = [1, 2, 3];
|
||||
var b = [4, 3, 2];
|
||||
expect(iD.utilArrayDifference([], [])).to.eql([]);
|
||||
expect(iD.utilArrayDifference([], a)).to.eql([]);
|
||||
expect(iD.utilArrayDifference(a, [])).to.have.members([1, 2, 3]);
|
||||
expect(iD.utilArrayDifference(a, b)).to.have.members([1]);
|
||||
expect(iD.utilArrayDifference(b, a)).to.have.members([4]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('iD.utilArrayIntersection', function() {
|
||||
it('returns set intersection', function() {
|
||||
var a = [1, 2, 3];
|
||||
var b = [4, 3, 2];
|
||||
expect(iD.utilArrayIntersection([], [])).to.eql([]);
|
||||
expect(iD.utilArrayIntersection([], a)).to.eql([]);
|
||||
expect(iD.utilArrayIntersection(a, [])).to.eql([]);
|
||||
expect(iD.utilArrayIntersection(a, b)).to.have.members([2, 3]);
|
||||
expect(iD.utilArrayIntersection(b, a)).to.have.members([2, 3]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('iD.utilArrayUnion', function() {
|
||||
it('returns set union', function() {
|
||||
var a = [1, 2, 3];
|
||||
var b = [4, 3, 2];
|
||||
expect(iD.utilArrayUnion([], [])).to.eql([]);
|
||||
expect(iD.utilArrayUnion([], a)).to.have.members([1, 2, 3]);
|
||||
expect(iD.utilArrayUnion(a, [])).to.have.members([1, 2, 3]);
|
||||
expect(iD.utilArrayUnion(a, b)).to.have.members([1, 2, 3, 4]);
|
||||
expect(iD.utilArrayUnion(b, a)).to.have.members([1, 2, 3, 4]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('iD.utilArrayUniq', function() {
|
||||
it('returns unique values', function() {
|
||||
var a = [1, 1, 2, 3, 3];
|
||||
expect(iD.utilArrayUniq([])).to.eql([]);
|
||||
expect(iD.utilArrayUniq(a)).to.have.members([1, 2, 3]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user