Skip to content

Instantly share code, notes, and snippets.

View JoaoCnh's full-sized avatar
🎯
Focusing

João Cunha JoaoCnh

🎯
Focusing
View GitHub Profile
@JoaoCnh
JoaoCnh / schema.js
Last active September 30, 2023 17:59
Yup conditional validation
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;
}),
});
@JoaoCnh
JoaoCnh / map.js
Last active February 1, 2020 21:45
js map example
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"];
@JoaoCnh
JoaoCnh / filter2.js
Created February 14, 2018 11:06
Filter example 2
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");
@JoaoCnh
JoaoCnh / triforce.js
Created February 14, 2018 15:18
Solving the problem
// 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 => {
@JoaoCnh
JoaoCnh / flatarray.js
Created February 14, 2018 14:55
Array of Arrays flatten with reduce
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);
}, []);
@JoaoCnh
JoaoCnh / obj.js
Last active October 14, 2018 09:27
Reduce object from array
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);
}, {});
@JoaoCnh
JoaoCnh / songs.js
Last active October 14, 2018 09:27
complex songs
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 },
@JoaoCnh
JoaoCnh / map2.js
Created February 14, 2018 09:29
javascript map 2nd example
const lowerCaseSongs = songs.map(mySongFunc);
var mySongFunc = function(song) {
return song.name.toLowerCase();
};
// ES6
const mySongFunc = song => {
return song.name.toLowerCase();
};
@JoaoCnh
JoaoCnh / songs.js
Created February 14, 2018 09:22
js song object
const songs = [
{
id: 1,
name: "Curl of the Burl",
artist: "Mastodon",
},
{
id: 2,
name: "Oblivion",
artist: "Mastodon",
@JoaoCnh
JoaoCnh / map3.js
Created February 14, 2018 10:03
javascript map 3rd example
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",
});