Skip to content

Commit

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

Verified

This commit was signed with the committer’s verified signature.
re-taro Rintaro Itokawa
1 parent ce031ae commit 44c044c
Showing 6 changed files with 149 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/_helpers/matreshkaerror.js
Original file line number Diff line number Diff line change
@@ -54,6 +54,9 @@ const errors = {
+ ` ${length > 0 ? 'To fix this wrap these nodes by single node.' : ''}`,
'array:renderer_node_missing': ({ selector }) =>
`${arrayErrorPrefix} renderer node is missing (given selector is "${selector}")`,
'array:nonexistent_method': ({ method }) =>
`${arrayErrorPrefix} Array.prototype.${method} doesn't exist.`
+ ' You need to include a polyfill for it (e. g. babel-node)',

'pull:to_remove_type': ({ toRemove }) =>
`Error in pull: ${getTypeError(toRemove, 'toRemove', 'number')}`,
41 changes: 41 additions & 0 deletions src/array/_pseudonativemethods/createcopywithin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import apply from '../../_helpers/apply';
import reportModified from '../_reportmodified';
import matreshkaError from '../../_helpers/matreshkaerror';

export default function createCopyWithin(hasOptions) {
return function copyWithin() {
const originalCopyWithin = Array.prototype.copyWithin;

if(!originalCopyWithin) {
throw matreshkaError('array:nonexistent_method', { method: 'copyWithin' });
}
// +hasOptions is converted to 0 or 1 depending on its value (false/true)
const argsLength = arguments.length - +hasOptions;
const args = Array(argsLength);
const givenEventOptions = hasOptions ? arguments[arguments.length - 1] : null;

for (let i = 0; i < argsLength; i++) {
args[i] = arguments[i];
}

apply(originalCopyWithin, this, args);

const eventOptions = {
method: 'copyWithin',
self: this,
added: [],
removed: []
};

// extend event options by custom event options if they are given
if (hasOptions) {
if (givenEventOptions && typeof givenEventOptions === 'object') {
nofn.assign(eventOptions, givenEventOptions);
}
}

reportModified(this, eventOptions);

return this;
}
}
41 changes: 41 additions & 0 deletions src/array/_pseudonativemethods/createfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import apply from '../../_helpers/apply';
import reportModified from '../_reportmodified';
import matreshkaError from '../../_helpers/matreshkaerror';

export default function createFill(hasOptions) {
return function fill(value) {
const originalFill = Array.prototype.fill;

if(!originalFill) {
throw matreshkaError('array:nonexistent_method', { method: 'fill' });
}
// +hasOptions is converted to 0 or 1 depending on its value (false/true)
const argsLength = arguments.length - +hasOptions;
const args = Array(argsLength);
const givenEventOptions = hasOptions ? arguments[arguments.length - 1] : null;

for (let i = 0; i < argsLength; i++) {
args[i] = arguments[i];
}

apply(originalFill, this, args);

const eventOptions = {
method: 'fill',
self: this,
added: [value],
removed: []
};

// extend event options by custom event options if they are given
if (hasOptions) {
if (givenEventOptions && typeof givenEventOptions === 'object') {
nofn.assign(eventOptions, givenEventOptions);
}
}

reportModified(this, eventOptions);

return this;
}
}
6 changes: 6 additions & 0 deletions src/array/_pseudonativemethods/createpseudonativemethod.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ import createSortingMethod from './createsortingmethod';
import createRemovingMethod from './createremovingmethod';
import createAddingMethod from './createaddingmethod';
import createSplice from './createsplice';
import createCopyWithin from './createcopywithin';
import createFill from './createfill';
import apply from '../../_helpers/apply';

const arrayPrototype = Array.prototype;
@@ -53,6 +55,10 @@ export default function createPseudoNativeMethod(name, hasOptions = false) {
return createAddingMethod(name, hasOptions);
case 'splice':
return createSplice(hasOptions);
case 'copyWithin':
return createCopyWithin(hasOptions);
case 'fill':
return createFill(hasOptions);
default:
return undefined;
}
4 changes: 2 additions & 2 deletions src/array/_pseudonativemethods/index.js
Original file line number Diff line number Diff line change
@@ -10,12 +10,12 @@ 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`
some reduce reduceRight forEach join indexOf lastIndexOf copyWithin fill`
.split(splitBySpaceReg).forEach((name) => {
methods[name] = createPseudoNativeMethod(name);
});

'push pop unshift shift sort reverse splice'
'push pop unshift shift sort reverse splice copyWithin fill'
.split(splitBySpaceReg).forEach((name) => {
methods[`${name}_`] = createPseudoNativeMethod(name, true);
});
56 changes: 56 additions & 0 deletions test/spec/matreshka_array/native_modifying_methods_spec.js
Original file line number Diff line number Diff line change
@@ -245,4 +245,60 @@ describe('Matreshka.Array native modifying methods (including ones that ending b
expect(result).toEqual(arr);
expect(testFlagHandler).toHaveBeenCalledTimes(2);
});

it('supports copyWithin method', () => {
const arr = new MatreshkaArray();
arr.push(1, 2, 3, 4, 5);
arr.on('copyWithin', simpleHandler);
arr.on('modify', simpleHandler);
const result = arr.copyWithin(0, 3);
expect(arr.length).toEqual(5);
expect(
arr.toJSON(false)
).toEqual([4, 5, 3, 4, 5]);
expect(result).toEqual(arr);
expect(simpleHandler).toHaveBeenCalledTimes(2);
});

it('supports copyWithin_ method', () => {
const arr = new MatreshkaArray();
arr.push(1, 2, 3, 4, 5);
arr.on('copyWithin', testFlagHandler);
arr.on('modify', testFlagHandler);
const result = arr.copyWithin_(0, 3, testFlag);
expect(arr.length).toEqual(5);
expect(
arr.toJSON(false)
).toEqual([4, 5, 3, 4, 5]);
expect(result).toEqual(arr);
expect(testFlagHandler).toHaveBeenCalledTimes(2);
});

it('supports fill method', () => {
const arr = new MatreshkaArray();
arr.push(1, 2, 3);
arr.on('fill', simpleHandler);
arr.on('modify', simpleHandler);
const result = arr.fill(4, -3, -2)
expect(arr.length).toEqual(3);
expect(
arr.toJSON(false)
).toEqual([4, 2, 3]);
expect(result).toEqual(arr);
expect(simpleHandler).toHaveBeenCalledTimes(2);
});

it('supports fill_ method', () => {
const arr = new MatreshkaArray();
arr.push(1, 2, 3);
arr.on('fill', testFlagHandler);
arr.on('modify', testFlagHandler);
const result = arr.fill_(4, 1, 2, testFlag)
expect(arr.length).toEqual(3);
expect(
arr.toJSON(false)
).toEqual([1, 4, 3]);
expect(result).toEqual(arr);
expect(testFlagHandler).toHaveBeenCalledTimes(2);
});
});

0 comments on commit 44c044c

Please sign in to comment.