Bug fix: BaseArrayClass
MODIFY
www/BaseArrayClass.js
www/BaseArrayClass.js
@@ -46,6 +46,7 @@ BaseArrayClass.prototype.mapSeries = function(iteratee, callback) {
var results = [];
var _arrayLength = self[ARRAY_FIELD].length;
+ var currentCnt = 0;
if (_arrayLength === 0) {
if (typeof callback === 'function') {
callback.call(self, []);
@@ -58,7 +59,8 @@ BaseArrayClass.prototype.mapSeries = function(iteratee, callback) {
iteratee.call(self, self[ARRAY_FIELD][currentIdx], function(value) {
results[currentIdx] = value;
- if (_arrayLength === results.length) {
+ currentCnt++;
+ if (_arrayLength === currentCnt) {
resolve(results);
} else {
nextTick(function() {
@@ -326,14 +328,14 @@ BaseArrayClass.prototype.filterAsync = function(iteratee, callback) {
}
return (new Promise(function(resolve) {
var results = [];
- self[ARRAY_FIELD].forEach(function(item) {
+ self[ARRAY_FIELD].forEach(function(item, idx) {
iteratee.call(self, item, function(isOk) {
- if (isOk) {
- results.push(item);
- }
+ results[idx] = isOk;
finishCnt++;
if (finishCnt === _arrayLength) {
- resolve(results);
+ resolve(self[ARRAY_FIELD].filter(function(item, j) {
+ return results[j];
+ }));
}
});
});
@@ -587,7 +589,7 @@ BaseArrayClass.prototype.getLength = function() {
* @name reverse
*/
BaseArrayClass.prototype.reverse = function() {
- this[ARRAY_FIELD] = this[ARRAY_FIELD].reverse();
+ this[ARRAY_FIELD].reverse();
};
/**

MODIFY
www/__test__/BaseArrayClass.spec.js
www/__test__/BaseArrayClass.spec.js
@@ -4,7 +4,7 @@ const BaseArrayClass = require('../BaseArrayClass');
describe('[BaseArrayClass]', () => {
describe('getLength()', () => {
- it('() should return the same size with initial array', () => {
+ it('should return the same size with initial array', () => {
const initArray = [0, 1, 2];
const _ = new BaseArrayClass(initArray);
expect(_.getLength()).toBe(initArray.length);
@@ -19,7 +19,7 @@ describe('[BaseArrayClass]', () => {
expect(_.mapSeries((item, next) => {
setTimeout(() => {
next(item);
- }, Math.random(3));
+ }, 3 * Math.random());
})).resolves.toEqual(initArray);
});
});
@@ -31,7 +31,7 @@ describe('[BaseArrayClass]', () => {
expect(_.mapAsync((item, next) => {
setTimeout(() => {
next(item);
- }, Math.random(3));
+ }, 3 * Math.random());
})).resolves.toEqual(initArray);
});
});
@@ -43,7 +43,7 @@ describe('[BaseArrayClass]', () => {
_.map((item, next) => {
setTimeout(() => {
next(item);
- }, Math.random(3));
+ }, 3 * Math.random());
}, (results) => {
expect(results).toEqual(initArray);
done();
@@ -85,7 +85,7 @@ describe('[BaseArrayClass]', () => {
setTimeout(() => {
i++;
next();
- }, Math.random(3));
+ }, 3 * Math.random());
}, () => {
expect(i).toBe(3);
done();
@@ -97,8 +97,8 @@ describe('[BaseArrayClass]', () => {
const initArray = ['A', 'B', 'C'];
const _ = new BaseArrayClass(initArray);
_.forEach((item) => {
- i++
- })
+ i++;
+ });
expect(i).toBe(3);
});
});
@@ -110,7 +110,7 @@ describe('[BaseArrayClass]', () => {
_.filterAsync((item, next) => {
setTimeout(() => {
next(item % 2 === 0);
- }, Math.random(5));
+ }, 5 * Math.random());
}).then((results) => {
expect(results).toEqual([0, 2, 4]);
done();
@@ -126,7 +126,7 @@ describe('[BaseArrayClass]', () => {
_.filter((item, next) => {
setTimeout(() => {
next(item % 2 !== 0);
- }, Math.random(3));
+ }, 3 * Math.random());
}, (results) => {
expect(results).toEqual([1, 3, 5]);
done();
@@ -134,7 +134,6 @@ describe('[BaseArrayClass]', () => {
});
it('should work as `Array.filter()` if callback is omitted', () => {
- let i = 0;
const initArray = [
{condition: true},
{condition: false},
@@ -142,7 +141,7 @@ describe('[BaseArrayClass]', () => {
];
const _ = new BaseArrayClass(initArray);
expect(_.filter((item) => {
- return item.condition === true
+ return item.condition === true;
})).toHaveLength(2);
});
});
@@ -256,10 +255,10 @@ describe('[BaseArrayClass]', () => {
const initArray = ['c', 'C', 'A', 'B', 'b', 'a'];
const _ = new BaseArrayClass(initArray);
_.sort((a, b) => {
- let charCodeA = a.charCodeAt(0);
- let charCodeB = b.charCodeAt(0);
- let lowerA = charCodeA > 96 ? charCodeA - 32 : charCodeA;
- let lowerB = charCodeB > 96 ? charCodeB - 32 : charCodeB;
+ const charCodeA = a.charCodeAt(0);
+ const charCodeB = b.charCodeAt(0);
+ const lowerA = charCodeA > 96 ? charCodeA - 32 : charCodeA;
+ const lowerB = charCodeB > 96 ? charCodeB - 32 : charCodeB;
return lowerA === lowerB ? charCodeB - charCodeA : lowerA - lowerB;
});
expect(_.getArray()).toEqual(['a', 'A', 'b', 'B', 'c', 'C']);

Bug fix: BaseArrayClass