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
Yup.object().shape({ | |
email: Yup.string().email('Invalid email address').required('Email is required!'), | |
username: Yup.string().required('This man needs a username').when('email', (email, schema) => { | |
if (email === 'foobar@example.com') { return schema.min(10); } | |
return schema; | |
}), | |
}); |
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
var allSongNames = songs.map(function (song) { | |
return song.name; | |
}); | |
// ES6 | |
const allSongNames = songs.map(song => { | |
return song.name; | |
}); | |
console.log(allSongNames); // ["Curl of the Burl","Oblivion","Flying Whales","L'Enfant Sauvage"]; |
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
var strings = ["hello", "Matt", "Mastodon", "Cat", "Dog"]; | |
var filtered = strings.filter(function (str) { | |
return str.includes("at"); | |
}); | |
// ES6 | |
const filtered = strings.filter(str => { | |
return str.includes("at"); |
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
// Let's reduce the array of arrays into a single one | |
const songNames = allSongs.reduce((acc, currValue) => { | |
return acc.concat(currValue); | |
}, []) | |
// Let's map it out with the seconds turned into minutes | |
.map(song => { | |
return { ...song, duration: Math.floor(song.duration / 60) }; | |
}) | |
// Let's filter the ones under 3 minutes | |
.filter(song => { |
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 mult = [songs, [{id: 112, name: "Chop Suey", artist: "System of a Down" }]]; | |
var flatMult = mult.reduce(function (acc, currValue) { | |
return acc.concat(currValue); | |
}, []); | |
// ES6 | |
const flatMult = mult.reduce((acc, currValue) => { | |
return acc.concat(currValue); | |
}, []); |
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
var obj = songs.reduce(function (acc, currValue) { | |
var artist = currValue.artist; | |
var artistCount = acc[artist] ? acc[artist] + 1 : 1; | |
var newObj = {}; | |
newObj[artist] = artistCount; | |
return Object.assign(acc, newObj); | |
}, {}); |
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 spotifySongs = [ | |
{ id: 1, name: "Curl of the Burl", artist: "Mastodon", duration: 204 }, | |
{ id: 2, name: "Oblivion", artist: "Mastodon", duration: 306 }, | |
{ id: 3, name: "Flying Whales", artist: "Gojira", duration: 444 }, | |
{ id: 4, name: "L'Enfant Sauvage", artist: "Gojira", duration: 246 } | |
]; | |
const lastFmSongs = [ | |
{ id: 5, name: "Chop Suey", artist: "System of a Down", duration: 192 }, | |
{ id: 6, name: "Throne", artist: "Bring me the Horizon", duration: 186 }, |
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 lowerCaseSongs = songs.map(mySongFunc); | |
var mySongFunc = function(song) { | |
return song.name.toLowerCase(); | |
}; | |
// ES6 | |
const mySongFunc = song => { | |
return song.name.toLowerCase(); | |
}; |
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 songs = [ | |
{ | |
id: 1, | |
name: "Curl of the Burl", | |
artist: "Mastodon", | |
}, | |
{ | |
id: 2, | |
name: "Oblivion", | |
artist: "Mastodon", |
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
var mapped = songs.map(function (song) { | |
// using _.omit we remove the artist property | |
// don't use delete because it mutates the song object | |
// while _.omit creates a new one | |
var newSong = _.omit(song, "artist"); | |
return Object.assign(newSong, { | |
scrobbleCount: 0, | |
spotifyUrl: "let's just imagine these properties make sense for now", | |
}); |
NewerOlder