Skip to content

Commit

Permalink
added listunion
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Tsongas committed Jun 8, 2013
1 parent 9a3441f commit b026e1e
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 20 deletions.
41 changes: 40 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Replaces the contents of a list element.
- **[listSort](#listSort)**
Sorts list elements according to a sort type and sort order.
- **[listUnion](#listUnion)**
Gets the elements that two lists have in common.
Combines the elements from two different lists.
- **[listValueCount](#listValueCount)**
Counts the instances of a specified value in a list. The search is case-sensitive.
- **[listValueCountNoCase](#listValueCountNoCase)**
Expand Down Expand Up @@ -1262,6 +1262,45 @@ The following table shows examples of *listSort* processing:

---

<a name="listUnion"></a>
##listUnion

**Description**
Combines the elements from two different lists.

**Availability**
v1.6

**Function syntax**
listUnion(list1, list2 [, delimiter ])

**Returns**
A new list with duplicates removed containing all elements from both *list1* and *list2*.

**Parameters**

| Parameter | Description |
| :--------- | :---------- |
| list1 | A list or a variable that contains one. |
| list2 | A list or a variable that contains one. |
| delimiter | A string or a variable that contains one. The character that separates list elements. The default value is comma. |

**Usage**

The following table shows examples of *listUnion* processing:

| Statement | Output |
| :-- | :-- |
| jList.listUnion('elem1,elem2', 'elem2,elem3' ) | 'elem1,elem2,elem3' |
| jList.listUnion('','elem1,elem2') | 'elem1,elem2' |
| jList.listUnion('elem1,elem2','') | 'elem1,elem2' |
| jList.listUnion('cat-dog-mouse','cat-rabbit','-') | 'cat-dog-mouse-rabbit' |

**jsFiddle**

[http://jsfiddle.net/christsongas/](http://jsfiddle.net/christsongas/)

---

<a name="listValueCount"></a>
##listValueCount
Expand Down
99 changes: 80 additions & 19 deletions test/spec/jlist-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe( 'jList library v1.5.0', function () {
'listReverse',
'listSetAt',
'listSort',
// 'listUnion',
'listUnion',
'listValueCount',
'listValueCountNoCase'
];
Expand Down Expand Up @@ -1110,47 +1110,47 @@ describe( 'jList library v1.5.0', function () {
expect( jList.listReplace('', '', '') ).toEqual('');
});

it ('04. Replaces an empty element in an empty list with a non-empty element', function () {
it ('05. Replaces an empty element in an empty list with a non-empty element', function () {
expect( jList.listReplace('', '', 'cat') ).toEqual('cat');
});

it ('04. Replaces a non-empty element in a single-element list with an empty element', function () {
it ('06. Replaces a non-empty element in a single-element list with an empty element', function () {
expect( jList.listReplace('cat', 'cat', '') ).toEqual('');
});

it ('05. Replaces a non-empty element in a single-element list with a different non-empty element', function () {
it ('07. Replaces a non-empty element in a single-element list with a different non-empty element', function () {
expect( jList.listReplace('cat', 'cat', 'dog') ).toEqual('dog');
});

it ('05. Replaces a non-empty element in a multiple-element list with a different non-empty element', function () {
it ('08. Replaces a non-empty element in a multiple-element list with a different non-empty element', function () {
expect( jList.listReplace('cat,dog', 'cat', 'mouse') ).toEqual('mouse,dog');
});

it ('05. Replaces non-empty elements in a multiple-element list with a different non-empty element', function () {
it ('09. Replaces non-empty elements in a multiple-element list with a different non-empty element', function () {
expect( jList.listReplace('cat,dog,cat,mouse', 'cat', 'squirrel') ).toEqual('squirrel,dog,squirrel,mouse');
});

it ('06. Replaces nothing in a list with one element', function () {
it ('10. Replaces nothing in a list with one element', function () {
expect( jList.listReplace('cat', 'mouse', 'squirrel') ).toEqual('cat');
});

it ('06. Replaces nothing in a list with multiple elements', function () {
it ('11. Replaces nothing in a list with multiple elements', function () {
expect( jList.listReplace('cat,dog,flea', 'mouse', 'squirrel') ).toEqual('cat,dog,flea');
});

it ('12. Replaces one element in a list with a custom delimiter', function () {
expect( jList.listReplace('cat~dog', 'dog', 'flea', '~') ).toEqual('cat~flea');
});

it ('12. Replaces nothing in a list with a custom delimiter', function () {
it ('13. Replaces nothing in a list with a custom delimiter', function () {
expect( jList.listReplace('cat~dog', 'Dog', 'flea', '~') ).toEqual('cat~dog');
});

it ('13. Replaces an empty element in a list with a non-empty element', function () {
it ('14. Replaces an empty element in a list with a non-empty element', function () {
expect( jList.listReplace('cat~', '', 'dog', '~') ).toEqual('cat~dog');
});

it ('13. Replaces a non-empty element in a list with an empty element', function () {
it ('15. Replaces a non-empty element in a list with an empty element', function () {
expect( jList.listReplace('cat~dog', 'dog', '', '~') ).toEqual('cat~');
});

Expand All @@ -1175,31 +1175,31 @@ describe( 'jList library v1.5.0', function () {
expect( jList.listReplaceNoCase('', '', '') ).toEqual('');
});

it ('04. Replaces an empty element in an empty list with a non-empty element', function () {
it ('05. Replaces an empty element in an empty list with a non-empty element', function () {
expect( jList.listReplaceNoCase('', '', 'cat') ).toEqual('cat');
});

it ('04. Replaces a non-empty element in a single-element list with an empty element', function () {
it ('06. Replaces a non-empty element in a single-element list with an empty element', function () {
expect( jList.listReplaceNoCase('cat', 'cat', '') ).toEqual('');
});

it ('05. Replaces a non-empty element in a single-element list with a different non-empty element', function () {
it ('07. Replaces a non-empty element in a single-element list with a different non-empty element', function () {
expect( jList.listReplaceNoCase('cat', 'cat', 'dog') ).toEqual('dog');
});

it ('05. Replaces a non-empty element in a multiple-element list with a different non-empty element', function () {
it ('08. Replaces a non-empty element in a multiple-element list with a different non-empty element', function () {
expect( jList.listReplaceNoCase('cat,dog', 'cat', 'mouse') ).toEqual('mouse,dog');
});

it ('05. Replaces non-empty elements in a multiple-element list with a different non-empty element', function () {
it ('09. Replaces non-empty elements in a multiple-element list with a different non-empty element', function () {
expect( jList.listReplaceNoCase('cat,dog,cat,mouse', 'cat', 'squirrel') ).toEqual('squirrel,dog,squirrel,mouse');
});

it ('06. Replaces nothing in a list with one element', function () {
it ('10. Replaces nothing in a list with one element', function () {
expect( jList.listReplaceNoCase('cat', 'mouse', 'squirrel') ).toEqual('cat');
});

it ('06. Replaces nothing in a list with multiple elements', function () {
it ('11. Replaces nothing in a list with multiple elements', function () {
expect( jList.listReplaceNoCase('cat,dog,flea', 'mouse', 'squirrel') ).toEqual('cat,dog,flea');
});

Expand All @@ -1211,7 +1211,7 @@ describe( 'jList library v1.5.0', function () {
expect( jList.listReplaceNoCase('cat~', '', 'dog', '~') ).toEqual('cat~dog');
});

it ('13. Replaces a non-empty element in a list with an empty element', function () {
it ('14. Replaces a non-empty element in a list with an empty element', function () {
expect( jList.listReplaceNoCase('cat~dog', 'dog', '', '~') ).toEqual('cat~');
});

Expand Down Expand Up @@ -1464,6 +1464,67 @@ describe( 'jList library v1.5.0', function () {
});


describe( 'listUnion: Combines the elements from two different lists.', function () {

it ('01. Throws an error when no parameters are passed in', function () {
expect( function(){ jList.listUnion(); } ).toThrow('Missing parameter: list1 and list2 must be provided');
});

it ('02. Throws an error when only 1 parameter is passed in', function () {
expect( function(){ jList.listUnion('cat,dog'); } ).toThrow('Missing parameter: list1 and list2 must be provided');
});

it ('03. Gets the union of two empty lists', function () {
expect( jList.listUnion('', '') ).toEqual('');
});

it ('04. Gets the union of a list with multiple elements that uses the default delimiter and an empty list', function () {
expect( jList.listUnion('cat,dog', '') ).toEqual('cat,dog');
});

it ('05. Gets the union of an empty list and a list with multiple elements that uses the default delimiter', function () {
expect( jList.listUnion('', 'cat,dog') ).toEqual('cat,dog');
});

it ('06. Gets the union of two lists that have a single common element', function () {
expect( jList.listUnion('cat', 'cat') ).toEqual('cat');
});

it ('07. Gets the union of two lists that have a single unique element', function () {
expect( jList.listUnion('cat', 'rabbit') ).toEqual('cat,rabbit');
});

it ('08. Gets the union of a list with one common element and a list that uses the default delimiter and has multiple elements', function () {
expect( jList.listUnion('cat', 'cat,dog,rabbit' ) ).toEqual('cat,dog,rabbit');
});

it ('09. Gets the union of a list that uses the default delimiter and has multiple elements and a list with one common element', function () {
expect( jList.listUnion('cat,dog,rabbit', 'rabbit' ) ).toEqual('cat,dog,rabbit');
});

it ('10. Gets the union of a list with two elements and a list that uses the default delimiter and has two common elements', function () {
expect( jList.listUnion('cat,dog', 'cat,dog,rabbit,squirrel' ) ).toEqual('cat,dog,rabbit,squirrel');
});

it ('11. Gets the union of a list that uses the default delimiter and has two common elements and a list with two elements', function () {
expect( jList.listUnion('cat,dog,rabbit,squirrel', 'rabbit,squirrel' ) ).toEqual('cat,dog,rabbit,squirrel');
});

it ('12. Gets the union of a list that uses a custom delimiter and an empty list', function () {
expect( jList.listUnion('', 'cat~dog', '~') ).toEqual('cat~dog');
});

it ('13. Gets the union of two lists populated with a single element using a custom delimiter', function () {
expect( jList.listUnion('cat', 'cat', '~') ).toEqual('cat');
});

it ('14. Gets the union of two lists with multiple repeating common elements that use a custom delimiter', function () {
expect( jList.listUnion('cat~dog~rabbit~dog', 'rabbit~cat~dog~rabbit~rabbit', '~') ).toEqual('cat~dog~rabbit');
});

});


describe( 'listValueCount: Counts the instances of a specified value in a list. The search is case-sensitive.', function () {

it ('01. Throws an error when no parameters are passed in', function () {
Expand Down

0 comments on commit b026e1e

Please sign in to comment.