Coding style fixes and improvements
This commit is contained in:
@ -43,7 +43,7 @@ These snippets, while useful and interesting, didn\'t quite make it into the rep
|
|||||||
|
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
`
|
`;
|
||||||
for(const snippet of Object.entries(snippets))
|
for(const snippet of Object.entries(snippets))
|
||||||
output += `* [\`${snippet[0].slice(0,-3)}\`](#${snippet[0].toLowerCase().slice(0,-3)})\n`;
|
output += `* [\`${snippet[0].slice(0,-3)}\`](#${snippet[0].toLowerCase().slice(0,-3)})\n`;
|
||||||
output += '\n---\n';
|
output += '\n---\n';
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
### binarySearch
|
### binarySearch
|
||||||
|
|
||||||
Use recursion. Similar to `Array.indexOf()` that finds the index of a value within an array.
|
Use recursion. Similar to `Array.indexOf()` that finds the index of a value within an array.
|
||||||
The difference being this operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search or `Array.indexOf()`.
|
The difference being this operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search or `Array.indexOf()`.
|
||||||
|
|
||||||
Search a sorted array by repeatedly dividing the search interval in half.
|
Search a sorted array by repeatedly dividing the search interval in half.
|
||||||
Begin with an interval covering the whole array.
|
Begin with an interval covering the whole array.
|
||||||
If the value of the search is less than the item in the middle of the interval, recurse into the lower half. Otherwise recurse into the upper half.
|
If the value of the search is less than the item in the middle of the interval, recurse into the lower half. Otherwise recurse into the upper half.
|
||||||
Repeatedly recurse until the value is found which is the mid or you've recursed to a point that is greater than the length which means the value doesn't exist and return `-1`.
|
Repeatedly recurse until the value is found which is the mid or you've recursed to a point that is greater than the length which means the value doesn't exist and return `-1`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -15,8 +15,8 @@ const binarySearch = (arr, val, start = 0, end = arr.length - 1) => {
|
|||||||
if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1);
|
if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1);
|
||||||
if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);
|
if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);
|
||||||
return mid;
|
return mid;
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2
|
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2
|
||||||
|
|||||||
@ -1,4 +0,0 @@
|
|||||||
module.exports = README = e = arr => {
|
|
||||||
const dt = new Date(parseInt(arr.toString().substr(6)));
|
|
||||||
return `${dt.getDate()}/${dt.getMonth() + 1}/${dt.getFullYear()}`;
|
|
||||||
};
|
|
||||||
@ -1,13 +0,0 @@
|
|||||||
const test = require('tape');
|
|
||||||
const README = require('./README.js');
|
|
||||||
|
|
||||||
test('Testing README', (t) => {
|
|
||||||
//For more information on all the methods supported by tape
|
|
||||||
//Please go to https://github.com/substack/tape
|
|
||||||
t.true(typeof README === 'function', 'README is a Function');
|
|
||||||
//t.deepEqual(README(args..), 'Expected');
|
|
||||||
//t.equal(README(args..), 'Expected');
|
|
||||||
//t.false(README(args..), 'Expected');
|
|
||||||
//t.throws(README(args..), 'Expected');
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
@ -6,7 +6,7 @@ test('Testing ary', (t) => {
|
|||||||
//Please go to https://github.com/substack/tape
|
//Please go to https://github.com/substack/tape
|
||||||
t.true(typeof ary === 'function', 'ary is a Function');
|
t.true(typeof ary === 'function', 'ary is a Function');
|
||||||
const firstTwoMax = ary(Math.max, 2);
|
const firstTwoMax = ary(Math.max, 2);
|
||||||
t.deepEquals([[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)), [6, 8, 10], 'Discards arguments with index >=n')
|
t.deepEquals([[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)), [6, 8, 10], 'Discards arguments with index >=n');
|
||||||
//t.deepEqual(ary(args..), 'Expected');
|
//t.deepEqual(ary(args..), 'Expected');
|
||||||
//t.equal(ary(args..), 'Expected');
|
//t.equal(ary(args..), 'Expected');
|
||||||
//t.false(ary(args..), 'Expected');
|
//t.false(ary(args..), 'Expected');
|
||||||
|
|||||||
@ -6,7 +6,7 @@ test('Testing attempt', (t) => {
|
|||||||
//Please go to https://github.com/substack/tape
|
//Please go to https://github.com/substack/tape
|
||||||
t.true(typeof attempt === 'function', 'attempt is a Function');
|
t.true(typeof attempt === 'function', 'attempt is a Function');
|
||||||
t.equals(attempt(() => 0), 0, 'Returns a value');
|
t.equals(attempt(() => 0), 0, 'Returns a value');
|
||||||
t.true(attempt(() => {throw new Error()}) instanceof Error, 'Returns an error');
|
t.true(attempt(() => {throw new Error();}) instanceof Error, 'Returns an error');
|
||||||
//t.deepEqual(attempt(args..), 'Expected');
|
//t.deepEqual(attempt(args..), 'Expected');
|
||||||
//t.equal(attempt(args..), 'Expected');
|
//t.equal(attempt(args..), 'Expected');
|
||||||
//t.false(attempt(args..), 'Expected');
|
//t.false(attempt(args..), 'Expected');
|
||||||
|
|||||||
@ -4,5 +4,5 @@ const mid = Math.floor((start + end) / 2);
|
|||||||
if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1);
|
if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1);
|
||||||
if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);
|
if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);
|
||||||
return mid;
|
return mid;
|
||||||
}
|
};
|
||||||
module.exports = binarySearch;
|
module.exports = binarySearch;
|
||||||
|
|||||||
@ -10,7 +10,7 @@ test('Testing bindAll', (t) => {
|
|||||||
'click': function() {
|
'click': function() {
|
||||||
return 'clicked ' + this.label;
|
return 'clicked ' + this.label;
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
bindAll(view, 'click');
|
bindAll(view, 'click');
|
||||||
t.equal(view.click(), 'clicked docs', 'Binds to an object context')
|
t.equal(view.click(), 'clicked docs', 'Binds to an object context')
|
||||||
//t.deepEqual(bindAll(args..), 'Expected');
|
//t.deepEqual(bindAll(args..), 'Expected');
|
||||||
|
|||||||
@ -9,7 +9,7 @@ test('Testing equals', (t) => {
|
|||||||
t.true(equals([1, 2, 3], [1, 2, 3]), '[1,2,3] is equal to [1,2,3]');
|
t.true(equals([1, 2, 3], [1, 2, 3]), '[1,2,3] is equal to [1,2,3]');
|
||||||
t.false(equals({ a: [2, 3], b: [4] }, { a: [2, 3], b: [6] }), '{ a: [2, 3], b: [4] } is not equal to { a: [2, 3], b: [6] }');
|
t.false(equals({ a: [2, 3], b: [4] }, { a: [2, 3], b: [6] }), '{ a: [2, 3], b: [4] } is not equal to { a: [2, 3], b: [6] }');
|
||||||
t.false(equals([1, 2, 3], [1, 2, 4]), '[1,2,3] is not equal to [1,2,4]');
|
t.false(equals([1, 2, 3], [1, 2, 4]), '[1,2,3] is not equal to [1,2,4]');
|
||||||
t.true(equals([1, 2, 3], { 0: 1, 1: 2, 2: 3 }), '[1, 2, 3] should be equal to { 0: 1, 1: 2, 2: 3 }) - type is different, but their enumerable properties match.')
|
t.true(equals([1, 2, 3], { 0: 1, 1: 2, 2: 3 }), '[1, 2, 3] should be equal to { 0: 1, 1: 2, 2: 3 }) - type is different, but their enumerable properties match.');
|
||||||
//t.deepEqual(equals(args..), 'Expected');
|
//t.deepEqual(equals(args..), 'Expected');
|
||||||
//t.equal(equals(args..), 'Expected');
|
//t.equal(equals(args..), 'Expected');
|
||||||
//t.false(equals(args..), 'Expected');
|
//t.false(equals(args..), 'Expected');
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
const isArray = val => Array.isArray(val);
|
const isArray = val => Array.isArray(val);
|
||||||
module.exports = isArray
|
module.exports = isArray;
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
const isMap = val => val instanceof Map;
|
const isMap = val => val instanceof Map;
|
||||||
module.exports = isMap
|
module.exports = isMap;
|
||||||
|
|||||||
@ -8,8 +8,8 @@ test('Testing isSorted', (t) => {
|
|||||||
//t.deepEqual(isSorted(args..), 'Expected');
|
//t.deepEqual(isSorted(args..), 'Expected');
|
||||||
t.equal(isSorted([0, 1, 2, 2]), 1, 'Array is sorted in ascending order');
|
t.equal(isSorted([0, 1, 2, 2]), 1, 'Array is sorted in ascending order');
|
||||||
t.equal(isSorted([4, 3, 2]), -1, 'Array is sorted in descending order');
|
t.equal(isSorted([4, 3, 2]), -1, 'Array is sorted in descending order');
|
||||||
t.equal(isSorted([4, 3, 5]), 0, 'Array is not sorted, direction changed in array')
|
t.equal(isSorted([4, 3, 5]), 0, 'Array is not sorted, direction changed in array');
|
||||||
//t.false(isSorted(args..), 'Expected');
|
//t.false(isSorted(args..), 'Expected');
|
||||||
//t.throws(isSorted(args..), 'Expected');
|
//t.throws(isSorted(args..), 'Expected');
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
const isTypedArray = val => val instanceof TypedArray;
|
const isTypedArray = val => val instanceof TypedArray;
|
||||||
module.exports = isTypedArray
|
module.exports = isTypedArray;
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
const isWeakMap = val => val instanceof WeakMap;
|
const isWeakMap = val => val instanceof WeakMap;
|
||||||
module.exports = isWeakMap
|
module.exports = isWeakMap;
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
const isWeakSet = val => val instanceof WeakSet;
|
const isWeakSet = val => val instanceof WeakSet;
|
||||||
module.exports = isWeakSet
|
module.exports = isWeakSet;
|
||||||
|
|||||||
@ -17,6 +17,6 @@ test('Testing quickSort', (t) => {
|
|||||||
quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]);
|
quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]);
|
||||||
let end = new Date().getTime();
|
let end = new Date().getTime();
|
||||||
t.true((end - start) < 2000, 'quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]) takes less than 2s to run');
|
t.true((end - start) < 2000, 'quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]) takes less than 2s to run');
|
||||||
|
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,11 +5,11 @@ test('Testing take', (t) => {
|
|||||||
//For more information on all the methods supported by tape
|
//For more information on all the methods supported by tape
|
||||||
//Please go to https://github.com/substack/tape
|
//Please go to https://github.com/substack/tape
|
||||||
t.true(typeof take === 'function', 'take is a Function');
|
t.true(typeof take === 'function', 'take is a Function');
|
||||||
t.deepEqual(take([1, 2, 3], 5), [1, 2, 3], "Returns an array with n elements removed from the beginning.")
|
t.deepEqual(take([1, 2, 3], 5), [1, 2, 3], "Returns an array with n elements removed from the beginning.");
|
||||||
t.deepEqual(take([1, 2, 3], 0), [], "Returns an array with n elements removed from the beginning.")
|
t.deepEqual(take([1, 2, 3], 0), [], "Returns an array with n elements removed from the beginning.");
|
||||||
//t.deepEqual(take(args..), 'Expected');
|
//t.deepEqual(take(args..), 'Expected');
|
||||||
//t.equal(take(args..), 'Expected');
|
//t.equal(take(args..), 'Expected');
|
||||||
//t.false(take(args..), 'Expected');
|
//t.false(take(args..), 'Expected');
|
||||||
//t.throws(take(args..), 'Expected');
|
//t.throws(take(args..), 'Expected');
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|||||||
12
test/testlog
12
test/testlog
@ -1,4 +1,4 @@
|
|||||||
Test log for: Sun Feb 04 2018 17:47:43 GMT+0200 (GTB Standard Time)
|
Test log for: Sun Feb 04 2018 17:54:05 GMT+0200 (GTB Standard Time)
|
||||||
|
|
||||||
> 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code
|
> 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code
|
||||||
> tape test/**/*.test.js | tap-spec
|
> tape test/**/*.test.js | tap-spec
|
||||||
@ -1177,10 +1177,6 @@ Test log for: Sun Feb 04 2018 17:47:43 GMT+0200 (GTB Standard Time)
|
|||||||
|
|
||||||
√ readFileLines is a Function
|
√ readFileLines is a Function
|
||||||
|
|
||||||
Testing README
|
|
||||||
|
|
||||||
√ README is a Function
|
|
||||||
|
|
||||||
Testing rearg
|
Testing rearg
|
||||||
|
|
||||||
√ rearg is a Function
|
√ rearg is a Function
|
||||||
@ -1709,8 +1705,8 @@ Test log for: Sun Feb 04 2018 17:47:43 GMT+0200 (GTB Standard Time)
|
|||||||
√ Works with multiple promises
|
√ Works with multiple promises
|
||||||
|
|
||||||
|
|
||||||
total: 813
|
total: 812
|
||||||
passing: 813
|
passing: 812
|
||||||
duration: 2.4s
|
duration: 2.5s
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -18,7 +18,7 @@ test('Testing uniqueElements', (t) => {
|
|||||||
t.throws(() => uniqueElements(false), 'uniqueElements(false) throws an error');
|
t.throws(() => uniqueElements(false), 'uniqueElements(false) throws an error');
|
||||||
|
|
||||||
let start = new Date().getTime();
|
let start = new Date().getTime();
|
||||||
uniqueElements([true, 0, 1, false, false, undefined, null, ''])
|
uniqueElements([true, 0, 1, false, false, undefined, null, '']);
|
||||||
let end = new Date().getTime();
|
let end = new Date().getTime();
|
||||||
t.true((end - start) < 2000, 'uniqueElements([true, 0, 1, false, false, undefined, null]) takes less than 2s to run');
|
t.true((end - start) < 2000, 'uniqueElements([true, 0, 1, false, false, undefined, null]) takes less than 2s to run');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user