Skip to content

Instantly share code, notes, and snippets.

View nattatorn-dev's full-sized avatar

Mark nattatorn-dev

  • LedgerX Co., Ltd.
  • Bangkok, Thailand
View GitHub Profile
Head & Tail
Destructuring syntax allows us to get the head and tail of a list without utility functions.
_.head([1, 2, 3]);
// 1
_.tail([1, 2, 3]);
// [2, 3]
// becomes
@nattatorn-dev
nattatorn-dev / Pure javascript immutable arrays
Created April 17, 2017 03:52
Pure javascript immutable arrays
/** all pure func avoid push, pop, shift, unshift, sort, reverse, splice and delete */
/**
* @param {[array]} array | ['USD', 'JPY', 'GBP'] |
* @param {[number]} index | 0 |
* @return {[array]} array | ['JPY', 'GBP'] | delete element of array by index and return new array
*/
const deleteByIndex = ( array, index ) =>
array.slice( 0, index ).concat( array.slice( index + 1 ) )
// fetch api
{
posts: [
{
id: 88,
content: 'North Korea News',
author: {
id: 41,
name: 'Kim Jong-un',
},
var arr = [0,1,2,3,4];
var g = arr.join(', ').replace(/,(?!.*,)/gmi, ' and');
// result 0, 1, 2, 3 and 4
@nattatorn-dev
nattatorn-dev / es7-async-await.js
Created June 13, 2017 18:06 — forked from msmfsd/es7-async-await.js
Javascript fetch JSON with ES7 Async Await
// Async/Await requirements: Latest Chrome/FF browser or Babel: https://babeljs.io/docs/plugins/transform-async-to-generator/
// Fetch requirements: Latest Chrome/FF browser or Github fetch polyfill: https://github.com/github/fetch
// async function
async function fetchAsync () {
// await response of fetch call
let response = await fetch('https://api.github.com');
// only proceed once promise is resolved
let data = await response.json();
// only proceed once second promise is resolved
// ...
lifecycle({
componentWillMount() {
const { subscribeToMessages } = this.props;
return subscribeToMessages();
},
})
)(ChatMessages);
const indexByKey = key => pipe(
indexBy(prop(key)),
map(omit([key])) // eslint-disable-line
);
const indexByKey = key => pipe(
indexBy(prop(key)),
map(omit([key])) // eslint-disable-line
);
import { is } from "immutable";
function CompareFn(objA, objB) {
if (objA === objB || is(objA, objB)) {
return true;
}
if (
typeof objA !== "object" ||
objA === null ||
typeof objB !== "object" ||
static propTypes = {
data: PropTypes.array.isRequired,
title: PropTypes.string.isRequired,
len: PropTypes.number,
keyName: PropTypes.string.isRequired,
showLess: PropTypes.bool.isRequired,
containerStlye: PropTypes.object,
children: PropTypes.node,
}
static defaultProps = {