Created
April 25, 2018 22:35
-
-
Save stukennedy/fbd94827a5ab46a2d3bd22489ac3b96e to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/tuxubuk
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// jshint esnext: true | |
'use strict'; | |
var curry = function curry(f) { | |
return function () { | |
for (var _len = arguments.length, a = Array(_len), _key = 0; _key < _len; _key++) { | |
a[_key] = arguments[_key]; | |
} | |
return a.length < f.length ? curry(f.bind.apply(f, [undefined].concat(a))) : f.apply(undefined, a); | |
}; | |
}; | |
var clone = function clone(obj) { | |
return Object.assign({}, obj); | |
}; | |
var chain = function chain() { | |
for (var _len2 = arguments.length, fs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | |
fs[_key2] = arguments[_key2]; | |
} | |
return function (a) { | |
return fs.reduce(function (o, f) { | |
return f(o); | |
}, a); | |
}; | |
}; | |
var isEmpty = curry(function (a) { | |
return !a || a === null || a === '' || Object.values(a).length === 0; | |
}); | |
var isString = curry(function (a) { | |
return typeof a === 'string'; | |
}); | |
var isArray = curry(function (a) { | |
return Array.isArray(a); | |
}); | |
var range = curry(function (size) { | |
return Array(size).fill(undefined).map(function (x, i) { | |
return i; | |
}); | |
}); | |
var keys = curry(function (o) { | |
return Object.keys(o); | |
}); | |
var values = curry(function (o) { | |
return Object.values(o); | |
}); | |
var fromPairs = function fromPairs(arr) { | |
return arr.reduce(function (o, el) { | |
o[el[0]] = el[1];return o; | |
}, {}); | |
}; | |
var mapValues = curry(function (fn, a) { | |
return Object.keys(a).reduce(function (o, k) { | |
o[k] = fn(a[k], k);return o; | |
}, {}); | |
}); | |
var pickBy = curry(function (fn, a) { | |
return Object.keys(a).reduce(function (o, k) { | |
if (fn(a[k], k)) o[k] = a[k];return o; | |
}, {}); | |
}); | |
var reduce = curry(function (fn, k, a) { | |
return a.length ? a.reduce(fn, k) : Object.keys(a).reduce(function (o, i) { | |
return fn(o, a[i], i); | |
}, k); | |
}); | |
var filter = curry(function (fn, args) { | |
return args.length ? args.filter(fn) : pickBy(fn, args); | |
}); | |
var map = curry(function (fn, args) { | |
return args.length ? args.map(fn) : mapValues(fn, args); | |
}); | |
var fromPath = curry(function (path) { | |
return path.replace('[', '.').replace(']', '').split('.'); | |
}); | |
var get = curry(function (p, a) { | |
return (isArray(p) ? p : fromPath(p)).reduce(function (acc, o) { | |
return acc ? acc[o] : acc; | |
}, a); | |
}); | |
var set = curry(function (p, value, obj) { | |
var i = undefined; | |
var objClone = clone(obj); | |
var point = objClone; | |
var path = isArray(p) ? p : fromPath(p); | |
for (i = 0; i < path.length - 1; i++) { | |
point[path[i]] = point[path[i]] || (isString(path[i + 1]) ? {} : []); | |
point = point[path[i]]; | |
} | |
point[path[i]] = value; | |
return objClone; | |
}); | |
console.log( | |
// fromPath('the.many[0].ways'), | |
chain(set('the.many[5].ways', 'YO'), get(['the', 'many', 5, 'ways']) | |
// get('the.many[5].ways') | |
)({})); | |
// Example | |
// const addAll = curry((a, b) => a + b) | |
// const square = curry((n) => n * n) | |
// const a = chain( | |
// addAll(2), | |
// square | |
// )(2) | |
// console.log(a) | |
// const output = chain( | |
// map((x, i) => i), | |
// filter(x => x % 2) | |
// )(range(100)) | |
// console.log(output) | |
// const obj = { a: 'foo', b: 'bar' } | |
// const arr = [ 'foo', 'bar' ] | |
// console.log( | |
// map(x => x + '-', obj), | |
// map(x => x + '-', arr) | |
// ); | |
// console.log( | |
// chain( | |
// range, | |
// map((x) => ['_' + x + '_', 100 + x]), | |
// fromPairs, | |
// map((x, i) => i + 'what'), | |
// filter((x, i) => i !== '_5_'), | |
// reduce((acc, x, k) => acc + k, '') | |
// )(10) | |
// ) | |
// console.log(isEmpty([0])) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// jshint esnext: true | |
const curry = (f) => (...a) => (a.length < f.length) ? curry(f.bind(this, ...a)) : f(...a) | |
const clone = (obj) => Object.assign({}, obj) | |
const chain = (...fs) => (a) => fs.reduce((o, f) => f(o), a) | |
const isEmpty = curry((a) => !a || a === null || a === '' || Object.values(a).length === 0) | |
const isString = curry((a) => (typeof a === 'string')) | |
const isArray = curry((a) => Array.isArray(a)) | |
const range = curry((size) => Array(size).fill(undefined).map((x, i) => i)) | |
const keys = curry((o) => Object.keys(o)) | |
const values = curry((o) => Object.values(o)) | |
const fromPairs = (arr) => arr.reduce((o, el) => { o[el[0]] = el[1]; return o }, {}) | |
const mapValues = curry((fn, a) => Object.keys(a).reduce((o, k) => { o[k] = fn(a[k], k); return o }, {})) | |
const pickBy = curry((fn, a) => Object.keys(a).reduce((o, k) => { if (fn(a[k], k)) o[k] = a[k]; return o }, {})) | |
const reduce = curry((fn, k, a) => a.length ? a.reduce(fn, k) : Object.keys(a).reduce((o, i) => fn(o, a[i], i), k)) | |
const filter = curry((fn, args) => args.length ? args.filter(fn) : pickBy(fn, args)) | |
const map = curry((fn, args) => args.length ? args.map(fn) : mapValues(fn, args)) | |
const fromPath = curry((path) => path.replace('[', '.').replace(']', '').split('.')) | |
const get = curry((p, a) => (isArray(p) ? p : fromPath(p)).reduce((acc, o) => acc ? acc[o] : acc, a)) | |
const set = curry((p, value, obj) => { | |
let i | |
let objClone = clone(obj) | |
let point = objClone | |
const path = isArray(p) ? p : fromPath(p) | |
for (i = 0; i < path.length - 1; i++) { | |
point[path[i]] = point[path[i]] || (isString(path[i + 1]) ? {} : []) | |
point = point[path[i]] | |
} | |
point[path[i]] = value | |
return objClone | |
}) | |
console.log( | |
// fromPath('the.many[0].ways'), | |
chain( | |
set('the.many[5].ways', 'YO'), | |
get(['the', 'many', 5, 'ways']) | |
// get('the.many[5].ways') | |
)({}) | |
) | |
// Example | |
// const addAll = curry((a, b) => a + b) | |
// const square = curry((n) => n * n) | |
// const a = chain( | |
// addAll(2), | |
// square | |
// )(2) | |
// console.log(a) | |
// const output = chain( | |
// map((x, i) => i), | |
// filter(x => x % 2) | |
// )(range(100)) | |
// console.log(output) | |
// const obj = { a: 'foo', b: 'bar' } | |
// const arr = [ 'foo', 'bar' ] | |
// console.log( | |
// map(x => x + '-', obj), | |
// map(x => x + '-', arr) | |
// ); | |
// console.log( | |
// chain( | |
// range, | |
// map((x) => ['_' + x + '_', 100 + x]), | |
// fromPairs, | |
// map((x, i) => i + 'what'), | |
// filter((x, i) => i !== '_5_'), | |
// reduce((acc, x, k) => acc + k, '') | |
// )(10) | |
// ) | |
// console.log(isEmpty([0]))</script></body> | |
</html> |
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
// jshint esnext: true | |
'use strict'; | |
var curry = function curry(f) { | |
return function () { | |
for (var _len = arguments.length, a = Array(_len), _key = 0; _key < _len; _key++) { | |
a[_key] = arguments[_key]; | |
} | |
return a.length < f.length ? curry(f.bind.apply(f, [undefined].concat(a))) : f.apply(undefined, a); | |
}; | |
}; | |
var clone = function clone(obj) { | |
return Object.assign({}, obj); | |
}; | |
var chain = function chain() { | |
for (var _len2 = arguments.length, fs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | |
fs[_key2] = arguments[_key2]; | |
} | |
return function (a) { | |
return fs.reduce(function (o, f) { | |
return f(o); | |
}, a); | |
}; | |
}; | |
var isEmpty = curry(function (a) { | |
return !a || a === null || a === '' || Object.values(a).length === 0; | |
}); | |
var isString = curry(function (a) { | |
return typeof a === 'string'; | |
}); | |
var isArray = curry(function (a) { | |
return Array.isArray(a); | |
}); | |
var range = curry(function (size) { | |
return Array(size).fill(undefined).map(function (x, i) { | |
return i; | |
}); | |
}); | |
var keys = curry(function (o) { | |
return Object.keys(o); | |
}); | |
var values = curry(function (o) { | |
return Object.values(o); | |
}); | |
var fromPairs = function fromPairs(arr) { | |
return arr.reduce(function (o, el) { | |
o[el[0]] = el[1];return o; | |
}, {}); | |
}; | |
var mapValues = curry(function (fn, a) { | |
return Object.keys(a).reduce(function (o, k) { | |
o[k] = fn(a[k], k);return o; | |
}, {}); | |
}); | |
var pickBy = curry(function (fn, a) { | |
return Object.keys(a).reduce(function (o, k) { | |
if (fn(a[k], k)) o[k] = a[k];return o; | |
}, {}); | |
}); | |
var reduce = curry(function (fn, k, a) { | |
return a.length ? a.reduce(fn, k) : Object.keys(a).reduce(function (o, i) { | |
return fn(o, a[i], i); | |
}, k); | |
}); | |
var filter = curry(function (fn, args) { | |
return args.length ? args.filter(fn) : pickBy(fn, args); | |
}); | |
var map = curry(function (fn, args) { | |
return args.length ? args.map(fn) : mapValues(fn, args); | |
}); | |
var fromPath = curry(function (path) { | |
return path.replace('[', '.').replace(']', '').split('.'); | |
}); | |
var get = curry(function (p, a) { | |
return (isArray(p) ? p : fromPath(p)).reduce(function (acc, o) { | |
return acc ? acc[o] : acc; | |
}, a); | |
}); | |
var set = curry(function (p, value, obj) { | |
var i = undefined; | |
var objClone = clone(obj); | |
var point = objClone; | |
var path = isArray(p) ? p : fromPath(p); | |
for (i = 0; i < path.length - 1; i++) { | |
point[path[i]] = point[path[i]] || (isString(path[i + 1]) ? {} : []); | |
point = point[path[i]]; | |
} | |
point[path[i]] = value; | |
return objClone; | |
}); | |
console.log( | |
// fromPath('the.many[0].ways'), | |
chain(set('the.many[5].ways', 'YO'), get(['the', 'many', 5, 'ways']) | |
// get('the.many[5].ways') | |
)({})); | |
// Example | |
// const addAll = curry((a, b) => a + b) | |
// const square = curry((n) => n * n) | |
// const a = chain( | |
// addAll(2), | |
// square | |
// )(2) | |
// console.log(a) | |
// const output = chain( | |
// map((x, i) => i), | |
// filter(x => x % 2) | |
// )(range(100)) | |
// console.log(output) | |
// const obj = { a: 'foo', b: 'bar' } | |
// const arr = [ 'foo', 'bar' ] | |
// console.log( | |
// map(x => x + '-', obj), | |
// map(x => x + '-', arr) | |
// ); | |
// console.log( | |
// chain( | |
// range, | |
// map((x) => ['_' + x + '_', 100 + x]), | |
// fromPairs, | |
// map((x, i) => i + 'what'), | |
// filter((x, i) => i !== '_5_'), | |
// reduce((acc, x, k) => acc + k, '') | |
// )(10) | |
// ) | |
// console.log(isEmpty([0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment