Created
July 10, 2019 23:25
-
-
Save brianswisher/2ce1ffe3ec08634f78aacd1b7baa31f9 to your computer and use it in GitHub Desktop.
Easy testing with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(()=>{ | |
const test = (result, expect) => | |
`${result === expect && '✅' || '🔴'} ${expect}:${result}` | |
function dynamicNestedObject (total, value) { | |
const result = {} | |
const keys = 'abcdefghijklmnopqrstuvwxyz'.substring(0, total).split('') | |
const lastIndex = keys.length - 1 | |
keys.reduce((o, k, i) => { | |
const isLastIndex = i === lastIndex | |
const val = isLastIndex && value || o[k] || {} | |
return o[k] = val | |
}, result) | |
return result | |
} | |
console.log(dynamicNestedObject(5, 'cool')) | |
console.log(test(dynamicNestedObject(3, 'hello').a.b.c, 'hello')) | |
console.log(test(dynamicNestedObject(4, 'world').a.b.c.d, 'world')) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://youtu.be/D6zLI8zrfVs