Last active
September 10, 2018 14:15
-
-
Save ryanjohnston/90b56fe658affbff5dd37945d02e2d64 to your computer and use it in GitHub Desktop.
[๐ JavaScript - Snippets] Snippets, Gists and Workflows about general JavaScript functionality #dev
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
import { List } from 'immutable'; | |
const pets = List(['cat', 'dog']); | |
const finalPets = pets.push('goldfish').push('tortoise'); | |
console.log(pets.toJS()); // prints ['cat', 'dog']; | |
console.log(finalPets.toJS()); // prints ['cat', 'dog', 'goldfish', 'tortoise']; |
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
import { Map } from 'immutable'; | |
const immutablePerson = Map({ name: 'Will' }); | |
const person = immutablePerson.toJS(); | |
console.log(person); // prints { name: 'Will' }; |
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
import { Map, List, fromJS } from 'immutable'; | |
// Normal Javascript | |
const person = { | |
name: 'Will', | |
pets: ['cat', 'dog'] | |
}; | |
// To create the equivalent in Immutable: | |
const immutablePerson = Map({ | |
name: 'Will', | |
pets: List(['cat', 'dog']) | |
}); | |
// Or ... | |
const immutablePerson = fromJS(person); |
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
// Custom Header | |
const request = new Request('./file.json', { | |
headers: new Headers({ 'Content-Type': 'application/json' }) | |
}); | |
fetch(request); |
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 options = { | |
method: 'post', | |
headers: { | |
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, | |
body: 'foo=bar&test=1' | |
} | |
fetch(url, options) | |
.catch((err) => { | |
console.error('Request failed', err) | |
}) |
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 fruits = ['apple', 'banana', 'grapes', 'mango', 'orange']; | |
/** | |
* Array filters items based on search criteria (query) | |
*/ | |
const filterItems = (query) => { | |
return fruits.filter((el) => | |
el.toLowerCase().indexOf(query.toLowerCase()) > -1 | |
); | |
} | |
console.log(filterItems('ap')); // ['apple', 'grapes'] |
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
import { fromJS } from 'immutable'; | |
const data = fromJS({ my: { nested: { name: 'Will' } } }); | |
const goodName = data.getIn(['my', 'nested', 'name']); | |
console.log(goodName); // prints Will | |
const badName = data.getIn(['my', 'lovely', 'name']); | |
console.log(badName); // prints undefined - no error thrown |
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
import { fromJS } from 'immutable'; | |
const data = fromJS({ name: 'Will' }); | |
const newNameData = data.set('name', 'Susie'); | |
console.log(data.get('name')); // prints 'Will' | |
console.log(newNameData.get('name')); // prints 'Susie' |
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 kvArray = [{key: 1, value: 10}, | |
{key: 2, value: 20}, | |
{key: 3, value: 30}]; | |
var reformattedArray = kvArray.map(obj =>{ | |
var rObj = {}; | |
rObj[obj.key] = obj.value; | |
return rObj; | |
}) |
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
// friends - an array of objects | |
// where object field "books" - list of favorite books | |
var friends = [{ | |
name: 'Anna', | |
books: ['Bible', 'Harry Potter'], | |
age: 21 | |
}, { | |
name: 'Bob', | |
books: ['War and peace', 'Romeo and Juliet'], | |
age: 26 | |
}, { | |
name: 'Alice', | |
books: ['The Lord of the Rings', 'The Shining'], | |
age: 18 | |
}]; | |
// allbooks - list which will contain all friends' books + | |
// additional list contained in initialValue | |
var allbooks = friends.reduce(function(accumulator, currentValue) { | |
return [...accumulator, ...currentValue.books]; | |
}, ['Alphabet']); | |
// allbooks = [ | |
// 'Alphabet', 'Bible', 'Harry Potter', 'War and peace', | |
// 'Romeo and Juliet', 'The Lord of the Rings', | |
// 'The Shining' | |
// ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment