Skip to content

Commit

Permalink
feat: New Methods for Matreshka.Array: includes, find, findIndex
Browse files Browse the repository at this point in the history
finom committed Sep 30, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 44c044c commit a6057fd
Showing 6 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/array/_pseudonativemethods/createcopywithin.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ export default function createCopyWithin(hasOptions) {
return function copyWithin() {
const originalCopyWithin = Array.prototype.copyWithin;

if(!originalCopyWithin) {
if(typeof originalCopyWithin !== 'function') {
throw matreshkaError('array:nonexistent_method', { method: 'copyWithin' });
}
// +hasOptions is converted to 0 or 1 depending on its value (false/true)
2 changes: 1 addition & 1 deletion src/array/_pseudonativemethods/createfill.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ export default function createFill(hasOptions) {
return function fill(value) {
const originalFill = Array.prototype.fill;

if(!originalFill) {
if(typeof originalFill !== 'function') {
throw matreshkaError('array:nonexistent_method', { method: 'fill' });
}
// +hasOptions is converted to 0 or 1 depending on its value (false/true)
24 changes: 21 additions & 3 deletions src/array/_pseudonativemethods/createpseudonativemethod.js
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import createSplice from './createsplice';
import createCopyWithin from './createcopywithin';
import createFill from './createfill';
import apply from '../../_helpers/apply';
import matreshkaError from '../../_helpers/matreshkaerror';

const arrayPrototype = Array.prototype;

@@ -27,17 +28,34 @@ export default function createPseudoNativeMethod(name, hasOptions = false) {
};
case 'every':
case 'some':
case 'findIndex':
case 'find':
return function pseudoNativeMethod(callback, thisArg) {
return arrayPrototype[name].call(this, callback, thisArg);
const originalMethod = arrayPrototype[name];
if(typeof originalMethod !== 'function') {
throw matreshkaError('array:nonexistent_method', { method: name });
}
return originalMethod.call(this, callback, thisArg);
};
case 'join':
return function pseudoNativeMethod(separator = ',') {
return arrayPrototype[name].call(this, separator);
};
case 'indexOf':
case 'lastIndexOf':
return function pseudoNativeMethod(item) {
return arrayPrototype[name].call(this, item);
case 'includes':
return function pseudoNativeMethod(searchElement, fromIndex) {
const originalMethod = arrayPrototype[name];
if(typeof originalMethod !== 'function') {
throw matreshkaError('array:nonexistent_method', { method: name });
}

if(typeof fromIndex === 'undefined') {
return originalMethod.call(this, searchElement);
} else {
return originalMethod.call(this, searchElement, fromIndex);
}

};
case 'reduce':
case 'reduceRight':
6 changes: 2 additions & 4 deletions src/array/_pseudonativemethods/index.js
Original file line number Diff line number Diff line change
@@ -7,10 +7,8 @@ import entries from './entries';
const splitBySpaceReg = /\s+/;
const methods = { concat, keys, values, entries };

// TODO copyWithin, fill, find, findIndex, includes

`push pop unshift shift sort reverse splice map filter slice every
some reduce reduceRight forEach join indexOf lastIndexOf copyWithin fill`
`push pop unshift shift sort reverse splice map filter slice every some reduce reduceRight
forEach join indexOf lastIndexOf copyWithin fill includes find findIndex`
.split(splitBySpaceReg).forEach((name) => {
methods[name] = createPseudoNativeMethod(name);
});
5 changes: 5 additions & 0 deletions test/spec/matreshka_array/common_spec.js
Original file line number Diff line number Diff line change
@@ -33,6 +33,11 @@ describe('Matreshka.Array class', () => {
entries,
keys,
values,
copyWithin,
fill,
includes,
find,
findIndex,
push_,
pop_,
unshift_,
28 changes: 28 additions & 0 deletions test/spec/matreshka_array/native_methods_spec.js
Original file line number Diff line number Diff line change
@@ -147,4 +147,32 @@ describe('Matreshka.Array native methods', () => {
arr.entries()
).toEqual([[0, 'foo'], [1, 'bar'], [2, 'baz']]);
});

it('supports includes method', () => {
const arr = new MatreshkaArray('foo', 'bar', 'baz');

expect(
arr.includes('bar')
).toEqual(true);

expect(
arr.includes('qux')
).toEqual(false);
});

it('supports find method', () => {
const arr = new MatreshkaArray('foo', 'bar', 'baz');

expect(
arr.find(item => item[item.length - 1] === 'r')
).toEqual('bar');
});

it('supports findIndex method', () => {
const arr = new MatreshkaArray('foo', 'bar', 'baz');

expect(
arr.findIndex(item => item[item.length - 1] === 'r')
).toEqual(1);
});
});

0 comments on commit a6057fd

Please sign in to comment.