Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);Promise.resolve([1, 2, 3]) .then(call('map', x => 2 * x)) .then(console.log); //[ 2, 4, 6 ] @@ -1502,6 +1502,12 @@ Logs: { "id": 101 } */ +nthArg
Creates a function that gets the argument at index
n. Ifnis negative, the nth argument from the end is returned.Use
Array.slice()to get the desired argument at indexn.const nthArg = n => (...args) => args.slice(n)[0]; +const third = nthArg(2); +third(1, 2, 3); // 3 +third(1, 2); // undefined +const last = nthArg(-1); +last(1, 2, 3, 4, 5); // 5parseCookie
Parse an HTTP Cookie header string and return an object of all cookie name-value pairs.
Use
String.split(';')to separate key-value pairs from each other. UseArray.map()andString.split('=')to separate keys from values in each pair. UseArray.reduce()anddecodeURIComponent()to create an object with all key-value pairs.const parseCookie = str => str .split(';') diff --git a/snippets/nthArg.md b/snippets/nthArg.md index c94224326..159975b89 100644 --- a/snippets/nthArg.md +++ b/snippets/nthArg.md @@ -10,8 +10,8 @@ const nthArg = n => (...args) => args.slice(n)[0]; ```js const third = nthArg(2); -third(1,2,3); // 3 -third(1,2); // undefined +third(1, 2, 3); // 3 +third(1, 2); // undefined const last = nthArg(-1); -last(1,2,3,4,5); // 5 +last(1, 2, 3, 4, 5); // 5 ``` diff --git a/test/castArray/castArray.js b/test/castArray/castArray.js new file mode 100644 index 000000000..83816cde7 --- /dev/null +++ b/test/castArray/castArray.js @@ -0,0 +1,2 @@ +const castArray = val => (Array.isArray(val) ? val : [val]); + module.exports = castArray \ No newline at end of file diff --git a/test/castArray/castArray.test.js b/test/castArray/castArray.test.js new file mode 100644 index 000000000..59d5d7c28 --- /dev/null +++ b/test/castArray/castArray.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const castArray = require('./castArray.js'); + +test('Testing castArray', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof castArray === 'function', 'castArray is a Function'); + //t.deepEqual(castArray(args..), 'Expected'); + //t.equal(castArray(args..), 'Expected'); + //t.false(castArray(args..), 'Expected'); + //t.throws(castArray(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/deepClone/deepClone.js b/test/deepClone/deepClone.js new file mode 100644 index 000000000..dd6573dab --- /dev/null +++ b/test/deepClone/deepClone.js @@ -0,0 +1,8 @@ +const deepClone = obj => { +let clone = Object.assign({}, obj); +Object.keys(clone).forEach( +key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key]) +); +return clone; +}; + module.exports = deepClone \ No newline at end of file diff --git a/test/deepClone/deepClone.test.js b/test/deepClone/deepClone.test.js new file mode 100644 index 000000000..a94eabcda --- /dev/null +++ b/test/deepClone/deepClone.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const deepClone = require('./deepClone.js'); + +test('Testing deepClone', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof deepClone === 'function', 'deepClone is a Function'); + //t.deepEqual(deepClone(args..), 'Expected'); + //t.equal(deepClone(args..), 'Expected'); + //t.false(deepClone(args..), 'Expected'); + //t.throws(deepClone(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/findKey/findKey.js b/test/findKey/findKey.js new file mode 100644 index 000000000..b97dc0b7f --- /dev/null +++ b/test/findKey/findKey.js @@ -0,0 +1,2 @@ +const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj)); + module.exports = findKey \ No newline at end of file diff --git a/test/findKey/findKey.test.js b/test/findKey/findKey.test.js new file mode 100644 index 000000000..f70d57f32 --- /dev/null +++ b/test/findKey/findKey.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const findKey = require('./findKey.js'); + +test('Testing findKey', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof findKey === 'function', 'findKey is a Function'); + //t.deepEqual(findKey(args..), 'Expected'); + //t.equal(findKey(args..), 'Expected'); + //t.false(findKey(args..), 'Expected'); + //t.throws(findKey(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/findLastKey/findLastKey.js b/test/findLastKey/findLastKey.js new file mode 100644 index 000000000..0484b644c --- /dev/null +++ b/test/findLastKey/findLastKey.js @@ -0,0 +1,5 @@ +const findLastKey = (obj, fn) => +Object.keys(obj) +.reverse() +.find(key => fn(obj[key], key, obj)); + module.exports = findLastKey \ No newline at end of file diff --git a/test/findLastKey/findLastKey.test.js b/test/findLastKey/findLastKey.test.js new file mode 100644 index 000000000..b2fa57478 --- /dev/null +++ b/test/findLastKey/findLastKey.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const findLastKey = require('./findLastKey.js'); + +test('Testing findLastKey', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof findLastKey === 'function', 'findLastKey is a Function'); + //t.deepEqual(findLastKey(args..), 'Expected'); + //t.equal(findLastKey(args..), 'Expected'); + //t.false(findLastKey(args..), 'Expected'); + //t.throws(findLastKey(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/invertKeyValues/invertKeyValues.js b/test/invertKeyValues/invertKeyValues.js index bbb96740f..93ef7c7a2 100644 --- a/test/invertKeyValues/invertKeyValues.js +++ b/test/invertKeyValues/invertKeyValues.js @@ -1,6 +1,8 @@ -const invertKeyValues = obj => +const invertKeyValues = (obj, fn) => Object.keys(obj).reduce((acc, key) => { -acc[obj[key]] = key; +const val = fn ? fn(obj[key]) : obj[key]; +acc[val] = acc[val] || []; +acc[val].push(key); return acc; }, {}); module.exports = invertKeyValues \ No newline at end of file diff --git a/test/isEmpty/isEmpty.js b/test/isEmpty/isEmpty.js new file mode 100644 index 000000000..10e93f435 --- /dev/null +++ b/test/isEmpty/isEmpty.js @@ -0,0 +1,2 @@ +const isEmpty = val => val == null || !(Object.keys(val) || val).length; + module.exports = isEmpty \ No newline at end of file diff --git a/test/isEmpty/isEmpty.test.js b/test/isEmpty/isEmpty.test.js new file mode 100644 index 000000000..bc457c018 --- /dev/null +++ b/test/isEmpty/isEmpty.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const isEmpty = require('./isEmpty.js'); + +test('Testing isEmpty', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof isEmpty === 'function', 'isEmpty is a Function'); + //t.deepEqual(isEmpty(args..), 'Expected'); + //t.equal(isEmpty(args..), 'Expected'); + //t.false(isEmpty(args..), 'Expected'); + //t.throws(isEmpty(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/isObjectLike/isObjectLike.js b/test/isObjectLike/isObjectLike.js new file mode 100644 index 000000000..17073de4c --- /dev/null +++ b/test/isObjectLike/isObjectLike.js @@ -0,0 +1,2 @@ +const isObjectLike = val => val !== null && typeof val === 'object'; + module.exports = isObjectLike \ No newline at end of file diff --git a/test/isObjectLike/isObjectLike.test.js b/test/isObjectLike/isObjectLike.test.js new file mode 100644 index 000000000..48f3a5c72 --- /dev/null +++ b/test/isObjectLike/isObjectLike.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const isObjectLike = require('./isObjectLike.js'); + +test('Testing isObjectLike', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof isObjectLike === 'function', 'isObjectLike is a Function'); + //t.deepEqual(isObjectLike(args..), 'Expected'); + //t.equal(isObjectLike(args..), 'Expected'); + //t.false(isObjectLike(args..), 'Expected'); + //t.throws(isObjectLike(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/matches/matches.js b/test/matches/matches.js new file mode 100644 index 000000000..21412bb9b --- /dev/null +++ b/test/matches/matches.js @@ -0,0 +1,3 @@ +const matches = (obj, source) => +Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]); + module.exports = matches \ No newline at end of file diff --git a/test/matches/matches.test.js b/test/matches/matches.test.js new file mode 100644 index 000000000..8bf340a59 --- /dev/null +++ b/test/matches/matches.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const matches = require('./matches.js'); + +test('Testing matches', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof matches === 'function', 'matches is a Function'); + //t.deepEqual(matches(args..), 'Expected'); + //t.equal(matches(args..), 'Expected'); + //t.false(matches(args..), 'Expected'); + //t.throws(matches(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/matchesWith/matchesWith.js b/test/matchesWith/matchesWith.js new file mode 100644 index 000000000..09b61f457 --- /dev/null +++ b/test/matchesWith/matchesWith.js @@ -0,0 +1,8 @@ +const matchesWith = (obj, source, fn) => +Object.keys(source).every( +key => +obj.hasOwnProperty(key) && fn +? fn(obj[key], source[key], key, obj, source) +: obj[key] == source[key] +); + module.exports = matchesWith \ No newline at end of file diff --git a/test/matchesWith/matchesWith.test.js b/test/matchesWith/matchesWith.test.js new file mode 100644 index 000000000..0faa74a62 --- /dev/null +++ b/test/matchesWith/matchesWith.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const matchesWith = require('./matchesWith.js'); + +test('Testing matchesWith', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof matchesWith === 'function', 'matchesWith is a Function'); + //t.deepEqual(matchesWith(args..), 'Expected'); + //t.equal(matchesWith(args..), 'Expected'); + //t.false(matchesWith(args..), 'Expected'); + //t.throws(matchesWith(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/nthArg/nthArg.js b/test/nthArg/nthArg.js new file mode 100644 index 000000000..4a2ca5f0a --- /dev/null +++ b/test/nthArg/nthArg.js @@ -0,0 +1,2 @@ +const nthArg = n => (...args) => args.slice(n)[0]; + module.exports = nthArg \ No newline at end of file diff --git a/test/nthArg/nthArg.test.js b/test/nthArg/nthArg.test.js new file mode 100644 index 000000000..626789aa5 --- /dev/null +++ b/test/nthArg/nthArg.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const nthArg = require('./nthArg.js'); + +test('Testing nthArg', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof nthArg === 'function', 'nthArg is a Function'); + //t.deepEqual(nthArg(args..), 'Expected'); + //t.equal(nthArg(args..), 'Expected'); + //t.false(nthArg(args..), 'Expected'); + //t.throws(nthArg(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/over/over.js b/test/over/over.js new file mode 100644 index 000000000..557bbd8d3 --- /dev/null +++ b/test/over/over.js @@ -0,0 +1,2 @@ +const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args)); + module.exports = over \ No newline at end of file diff --git a/test/over/over.test.js b/test/over/over.test.js new file mode 100644 index 000000000..1f2ecada6 --- /dev/null +++ b/test/over/over.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const over = require('./over.js'); + +test('Testing over', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof over === 'function', 'over is a Function'); + //t.deepEqual(over(args..), 'Expected'); + //t.equal(over(args..), 'Expected'); + //t.false(over(args..), 'Expected'); + //t.throws(over(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/testlog b/test/testlog index 24e508045..a393f7a85 100644 --- a/test/testlog +++ b/test/testlog @@ -1,4 +1,4 @@ -Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) +Test log for: Tue Jan 23 2018 20:11:47 GMT+0000 (UTC) > 30-seconds-of-code@0.0.1 test /home/travis/build/Chalarangelo/30-seconds-of-code > tape test/**/*.test.js | tap-spec @@ -98,6 +98,10 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) ✔ capitalizeEveryWord is a Function ✔ Capitalizes the first letter of every word in a string + Testing castArray + + ✔ castArray is a Function + Testing chainAsync ✔ chainAsync is a Function @@ -200,6 +204,10 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) ✔ decapitalize is a Function + Testing deepClone + + ✔ deepClone is a Function + Testing deepFlatten ✔ deepFlatten is a Function @@ -320,10 +328,18 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) ✔ filterNonUnique is a Function ✔ Filters out the non-unique values in an array + Testing findKey + + ✔ findKey is a Function + Testing findLast ✔ findLast is a Function + Testing findLastKey + + ✔ findLastKey is a Function + Testing flatten ✔ flatten is a Function @@ -528,7 +544,17 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) Testing invertKeyValues ✔ invertKeyValues is a Function - ✔ Inverts the key-value pairs of an object + + ✖ Inverts the key-value pairs of an object + ------------------------------------------- + operator: deepEqual + expected: |- + { 20: 'age', John: 'name' } + actual: |- + { 20: [ 'age' ], John: [ 'name' ] } + at: Test.test (/home/travis/build/Chalarangelo/30-seconds-of-code/test/invertKeyValues/invertKeyValues.test.js:8:4) + stack: |- + Testing is @@ -570,6 +596,10 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) ✔ isDivisible is a Function ✔ The number 6 is divisible by 3 + Testing isEmpty + + ✔ isEmpty is a Function + Testing isEven ✔ isEven is a Function @@ -617,6 +647,10 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) ✔ isObject({ a:1 }) is a object ✔ isObject(true) is not a object + Testing isObjectLike + + ✔ isObjectLike is a Function + Testing isPlainObject ✔ isPlainObject is a Function @@ -769,6 +803,14 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) ✔ Replaces all but the last num of characters with the specified mask character ✔ Replaces all but the last num of characters with the specified mask character + Testing matches + + ✔ matches is a Function + + Testing matchesWith + + ✔ matchesWith is a Function + Testing maxBy ✔ maxBy is a Function @@ -808,6 +850,10 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) ✔ negate is a Function ✔ Negates a predicate function + Testing nthArg + + ✔ nthArg is a Function + Testing nthElement ✔ nthElement is a Function @@ -858,6 +904,10 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) ✔ Returns a sorted array of objects ordered by properties and orders. ✔ Returns a sorted array of objects ordered by properties and orders. + Testing over + + ✔ over is a Function + Testing palindrome ✔ palindrome is a Function @@ -1296,9 +1346,23 @@ Test log for: Mon Jan 22 2018 20:11:29 GMT+0000 (UTC) ✔ zipObject(string) throws an error ✔ zipObject(test, string) throws an error + Testing zipWith - total: 563 - passing: 563 - duration: 314ms + ✔ zipWith is a Function + + Failed Tests: There was 1 failure + + Testing invertKeyValues + + ✖ Inverts the key-value pairs of an object + + + total: 574 + passing: 573 + failing: 1 + duration: 440ms + + +undefined \ No newline at end of file diff --git a/test/tomorrow/tomorrow.js b/test/tomorrow/tomorrow.js index 5f70ab9af..4e3ee9776 100644 --- a/test/tomorrow/tomorrow.js +++ b/test/tomorrow/tomorrow.js @@ -1,2 +1,8 @@ -const tomorrow = () => new Date(new Date().getTime() + 86400000).toISOString().split('T')[0]; +const tomorrow = () => { +let t = new Date(); +t.setDate(t.getDate() + 1); +return `${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, '0')}-${String( +t.getDate() +).padStart(2, '0')}`; +}; module.exports = tomorrow \ No newline at end of file diff --git a/test/zipWith/zipWith.js b/test/zipWith/zipWith.js new file mode 100644 index 000000000..7c0336738 --- /dev/null +++ b/test/zipWith/zipWith.js @@ -0,0 +1,11 @@ +const zipWith = (...arrays) => { +const length = arrays.length; +let fn = length > 1 ? arrays[length - 1] : undefined; +fn = typeof fn == 'function' ? (arrays.pop(), fn) : undefined; +const maxLength = Math.max(...arrays.map(x => x.length)); +const result = Array.from({ length: maxLength }).map((_, i) => { +return Array.from({ length: arrays.length }, (_, k) => arrays[k][i]); +}); +return fn ? result.map(arr => fn(...arr)) : result; +}; + module.exports = zipWith \ No newline at end of file diff --git a/test/zipWith/zipWith.test.js b/test/zipWith/zipWith.test.js new file mode 100644 index 000000000..382c59776 --- /dev/null +++ b/test/zipWith/zipWith.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const zipWith = require('./zipWith.js'); + +test('Testing zipWith', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof zipWith === 'function', 'zipWith is a Function'); + //t.deepEqual(zipWith(args..), 'Expected'); + //t.equal(zipWith(args..), 'Expected'); + //t.false(zipWith(args..), 'Expected'); + //t.throws(zipWith(args..), 'Expected'); + t.end(); +}); \ No newline at end of file