Add offset snippet

This commit is contained in:
Angelos Chalaris
2018-04-10 19:07:50 +03:00
parent 7cea814b6e
commit 6405c7f14d
6 changed files with 1104 additions and 1056 deletions

View File

@ -54,7 +54,7 @@ const snippetsPath = './snippets',
// Set variables for script
let snippets = {},
archivedSnippets = {},
beginnerSnippetNames = ['everyNth', 'filterNonUnique', 'last', 'maxN', 'minN', 'nthElement', 'sample', 'similarity', 'tail', 'currentURL', 'hasClass', 'getMeridiemSuffixOfInteger', 'factorial', 'fibonacci', 'gcd', 'isDivisible', 'isEven', 'isPrime', 'lcm', 'randomIntegerInRange', 'sum', 'reverseString', 'truncateString'],
beginnerSnippetNames = ['everyNth', 'filterNonUnique', 'last', 'maxN', 'minN', 'nthElement', 'offset', 'sample', 'similarity', 'tail', 'currentURL', 'hasClass', 'getMeridiemSuffixOfInteger', 'factorial', 'fibonacci', 'gcd', 'isDivisible', 'isEven', 'isPrime', 'lcm', 'randomIntegerInRange', 'sum', 'reverseString', 'truncateString'],
startPart = '',
endPart = '',
output = '',

16
snippets/offset.md Normal file
View File

@ -0,0 +1,16 @@
### offset
Moves the specified amount of elements to the end of the array.
Use `Array.slice()` twice to get the elements after the specified index and the elements before that.
Use the spread operator(`...`) to combine the two into one array.
If `offset` is negative, the elements will be moved from end to start.
```js
const offset = (arr, offset) => [...arr.slice(offset), ...arr.slice(0, offset)];
```
```js
offset([1, 2, 3, 4, 5], 2); // [3, 4, 5, 1, 2]
offset([1, 2, 3, 4, 5], -2); // [4, 5, 1, 2, 3]
```

View File

@ -171,6 +171,7 @@ objectFromPairs:object,array
objectToPairs:object,array
observeMutations:browser,event,advanced
off:browser,event
offset:array
omit:object,array
omitBy:object,array,function
on:browser,event

2
test/offset/offset.js Normal file
View File

@ -0,0 +1,2 @@
const offset = (arr, offset) => [...arr.slice(offset), ...arr.slice(0, offset)];
module.exports = offset;

View File

@ -0,0 +1,19 @@
const test = require('tape');
const offset = require('./offset.js');
test('Testing offset', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof offset === 'function', 'offset is a Function');
t.deepEqual(offset([1, 2, 3, 4, 5], 0), [1, 2, 3, 4, 5], 'Offset of 0 returns the same array.');
t.deepEqual(offset([1, 2, 3, 4, 5], 2), [3, 4, 5, 1, 2], 'Offset > 0 returns the offsetted array.');
t.deepEqual(offset([1, 2, 3, 4, 5], -2), [4, 5, 1, 2, 3], 'Offset < 0 returns the reverse offsetted array.');
t.deepEqual(offset([1, 2, 3, 4, 5], 6),[1, 2, 3, 4, 5], 'Offset greater than the length of the array returns the same array.');
t.deepEqual(offset([1, 2, 3, 4, 5], -6), [1, 2, 3, 4, 5], 'Offset less than the negative length of the array returns the same array.');
t.deepEqual(offset([], 3), [], 'Offsetting empty array returns an empty array.');
//t.deepEqual(offset(args..), 'Expected');
//t.equal(offset(args..), 'Expected');
//t.false(offset(args..), 'Expected');
//t.throws(offset(args..), 'Expected');
t.end();
});

File diff suppressed because it is too large Load Diff