Skip to content

Commit

Permalink
Update test case for list.
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Sep 14, 2014
1 parent 98d4334 commit 8575518
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/js/core/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ define(['summernote/core/func'], function (func) {
var list = (function () {
/**
* returns the first item of an array.
*
* @param {Array} array
*/
var head = function (array) {
Expand All @@ -13,6 +14,7 @@ define(['summernote/core/func'], function (func) {

/**
* returns the last item of an array.
*
* @param {Array} array
*/
var last = function (array) {
Expand All @@ -21,6 +23,7 @@ define(['summernote/core/func'], function (func) {

/**
* returns everything but the last entry of the array.
*
* @param {Array} array
*/
var initial = function (array) {
Expand All @@ -29,6 +32,7 @@ define(['summernote/core/func'], function (func) {

/**
* returns the rest of the items in an array.
*
* @param {Array} array
*/
var tail = function (array) {
Expand All @@ -48,27 +52,8 @@ define(['summernote/core/func'], function (func) {
};

/**
* returns next item.
* @param {Array} array
*/
var next = function (array, item) {
var idx = array.indexOf(item);
if (idx === -1) { return null; }

return array[idx + 1];
};

/**
* returns prev item.
* @param {Array} array
* returns true if all of the values in the array pass the predicate truth test.
*/
var prev = function (array, item) {
var idx = array.indexOf(item);
if (idx === -1) { return null; }

return array[idx - 1];
};

var all = function (array, pred) {
for (var idx = 0, len = array.length; idx < len; idx ++) {
if (!pred(array[idx])) {
Expand All @@ -78,12 +63,16 @@ define(['summernote/core/func'], function (func) {
return true;
};

/**
* returns true if the value is present in the list.
*/
var contains = function (array, item) {
return array.indexOf(item) !== -1;
};

/**
* get sum from a list
*
* @param {Array} array - array
* @param {Function} fn - iterator
*/
Expand All @@ -108,6 +97,7 @@ define(['summernote/core/func'], function (func) {

/**
* cluster elements by predicate function.
*
* @param {Array} array - array
* @param {Function} fn - predicate function for cluster rule
* @param {Array[]}
Expand All @@ -128,6 +118,7 @@ define(['summernote/core/func'], function (func) {

/**
* returns a copy of the array with all falsy values removed
*
* @param {Array} array - array
* @param {Function} fn - predicate function for cluster rule
*/
Expand All @@ -139,6 +130,11 @@ define(['summernote/core/func'], function (func) {
return aResult;
};

/**
* produces a duplicate-free version of the array
*
* @param {Array} array
*/
var unique = function (array) {
var results = [];

Expand All @@ -152,7 +148,7 @@ define(['summernote/core/func'], function (func) {
};

return { head: head, last: last, initial: initial, tail: tail,
prev: prev, next: next, find: find, contains: contains,
find: find, contains: contains,
all: all, sum: sum, from: from,
clusterBy: clusterBy, compact: compact, unique: unique };
})();
Expand Down
22 changes: 22 additions & 0 deletions test/unit/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ define(['jquery', 'summernote/core/list'], function ($, list) {
deepEqual(list.tail([1, 2, 3]), [2, 3], 'should exclude first element');
});

var isEven = function (num) {
return num % 2 === 0;
};

test('list.find', function () {
deepEqual(list.find([1, 2, 3], isEven), 2, 'should returns first matched element');
});

test('list.all', function () {
deepEqual(list.all([1, 2, 3], isEven), false, 'should returns false');
deepEqual(list.all([2, 4], isEven), true, 'should returns true');
});

test('list.sum', function () {
deepEqual(list.contains([1, 2, 3], 4), false, 'should returns false');
deepEqual(list.contains([1, 2, 4], 4), true, 'should returns true');
});

test('list.sum', function () {
deepEqual(list.sum([1, 2, 3]), 6, 'should return 6');
deepEqual(list.sum([1, 2, 3], function (v) { return v * 2; }), 12, 'should return 12');
Expand Down Expand Up @@ -53,5 +71,9 @@ define(['jquery', 'summernote/core/list'], function ($, list) {
test('list.compact', function () {
deepEqual(list.compact([0, 1, false, 2, '', 3]), [1, 2, 3], 'falsey values of `array` removed');
});

test('list.unique', function () {
deepEqual(list.unique([1, 2, 3, 3, 2, 1]), [1, 2, 3], 'returns duplicate-free version of array');
});
};
});

0 comments on commit 8575518

Please sign in to comment.